Add test for CloneWithCommitMsgHook
The other CloneCommands are basically just factories whereas the
CloneWithCommitMsgHook has logic for when parts of the command
should or should not be added, and which combination of
sub-commands are valid.
To be able to confidently change the behavior of CloneWithCommitMsgHook
a test that covers this logic is valuable.
Change-Id: I22185f73e5eb9afebb602464fe9ca6073b1377e5
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
index 61e696a..135d9b2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
@@ -14,6 +14,7 @@
package com.googlesource.gerrit.plugins.download.command;
+import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.config.DownloadScheme;
import com.google.gerrit.server.CurrentUser;
@@ -27,6 +28,9 @@
import org.eclipse.jgit.lib.Config;
public class CloneWithCommitMsgHook extends CloneCommand {
+ @VisibleForTesting protected static final String HOOK_COMMAND_KEY = "installCommitMsgHookCommand";
+ @VisibleForTesting protected static final String EXTRA_COMMAND_KEY = "installCommitExtraCommand";
+
private static final String HOOK = "hooks/commit-msg";
private static final String TARGET = " `git rev-parse --git-dir`/";
@@ -38,8 +42,8 @@
@Inject
CloneWithCommitMsgHook(
@GerritServerConfig Config config, SshScheme sshScheme, Provider<CurrentUser> userProvider) {
- this.configCommand = config.getString("gerrit", null, "installCommitMsgHookCommand");
- this.extraCommand = config.getString("gerrit", null, "installCommitExtraCommand");
+ this.configCommand = config.getString("gerrit", null, HOOK_COMMAND_KEY);
+ this.extraCommand = config.getString("gerrit", null, EXTRA_COMMAND_KEY);
this.sshScheme = sshScheme;
this.userProvider = userProvider;
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
index 3f5987b..f825745 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
@@ -16,6 +16,7 @@
import static com.google.gerrit.entities.CoreDownloadSchemes.SSH;
+import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.config.DownloadScheme;
import com.google.gerrit.server.CurrentUser;
@@ -40,7 +41,8 @@
private final boolean schemeAllowed;
@Inject
- SshScheme(
+ @VisibleForTesting
+ public SshScheme(
@SshAdvertisedAddresses List<String> sshAddresses,
@CanonicalWebUrl @Nullable Provider<String> urlProvider,
Provider<CurrentUser> userProvider,
diff --git a/src/test/java/com/googlesource/gerrit/plugins/download/DownloadCommandTest.java b/src/test/java/com/googlesource/gerrit/plugins/download/DownloadCommandTest.java
new file mode 100644
index 0000000..bf7a49d
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/download/DownloadCommandTest.java
@@ -0,0 +1,105 @@
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.plugins.download;
+
+import com.google.common.collect.ImmutableList;
+import com.google.gerrit.entities.Account;
+import com.google.gerrit.server.CurrentUser;
+import com.google.gerrit.server.account.GroupMembership;
+import com.google.gerrit.server.config.DownloadConfig;
+import com.google.inject.Provider;
+import com.google.inject.util.Providers;
+import com.googlesource.gerrit.plugins.download.scheme.HttpScheme;
+import com.googlesource.gerrit.plugins.download.scheme.SshScheme;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+import org.eclipse.jgit.lib.Config;
+import org.junit.Before;
+import org.junit.Ignore;
+
+@Ignore
+public class DownloadCommandTest {
+
+ static CurrentUser fakeUser() {
+ return new CurrentUser() {
+ @Override
+ public Optional<String> getUserName() {
+ return Optional.of(ENV.userName);
+ }
+
+ @Override
+ public GroupMembership getEffectiveGroups() {
+ throw new UnsupportedOperationException("not implemented");
+ }
+
+ @Override
+ public Object getCacheKey() {
+ return new Object();
+ }
+
+ @Override
+ public boolean isIdentifiedUser() {
+ return true;
+ }
+
+ @Override
+ public Account.Id getAccountId() {
+ return Account.id(1);
+ }
+ };
+ }
+
+ public static class TestEnvironment {
+ public final String fqdn = "gerrit.company.com";
+ public final String projectName = "my/project";
+ public final String userName = "john-doe@company.com";
+ public final int sshPort = 29418;
+
+ public String urlEncodedUserName() throws UnsupportedEncodingException {
+ return URLEncoder.encode(userName, StandardCharsets.UTF_8.name());
+ }
+
+ public String canonicalUrl() {
+ return "https://" + fqdn + "/";
+ }
+ }
+
+ protected static TestEnvironment ENV = new TestEnvironment();
+
+ protected HttpScheme httpScheme;
+ protected SshScheme sshScheme;
+ protected Provider<String> urlProvider;
+ protected Provider<CurrentUser> userProvider;
+
+ public DownloadCommandTest() {
+ super();
+ }
+
+ @Before
+ public void setUp() {
+ Config cfg = new Config();
+ urlProvider = Providers.of(ENV.canonicalUrl());
+ DownloadConfig downloadConfig = new DownloadConfig(cfg);
+ userProvider = Providers.of(fakeUser());
+ httpScheme = new HttpScheme(cfg, urlProvider, userProvider, downloadConfig);
+ sshScheme =
+ new SshScheme(
+ ImmutableList.of(String.format("%s:%d", ENV.fqdn, ENV.sshPort)),
+ urlProvider,
+ userProvider,
+ downloadConfig);
+ }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java b/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java
new file mode 100644
index 0000000..03c1ba0
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHookTest.java
@@ -0,0 +1,122 @@
+// Copyright (C) 2023 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.googlesource.gerrit.plugins.download.command;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.googlesource.gerrit.plugins.download.command.CloneWithCommitMsgHook.EXTRA_COMMAND_KEY;
+import static com.googlesource.gerrit.plugins.download.command.CloneWithCommitMsgHook.HOOK_COMMAND_KEY;
+
+import com.googlesource.gerrit.plugins.download.DownloadCommandTest;
+import org.eclipse.jgit.lib.Config;
+import org.junit.Test;
+
+public class CloneWithCommitMsgHookTest extends DownloadCommandTest {
+
+ @Test
+ public void testSshNoConfiguredCommands() throws Exception {
+ String command = getCloneCommand(null, null).getCommand(sshScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && scp -p -P %d %s@%s:hooks/commit-msg %s/.git/hooks/",
+ sshScheme.getUrl(ENV.projectName),
+ ENV.sshPort,
+ ENV.userName,
+ ENV.fqdn,
+ baseName(ENV.projectName)));
+ }
+
+ @Test
+ public void testSshConfiguredHookCommand() throws Exception {
+ String hookCommand = "my hook command";
+ String command = getCloneCommand(hookCommand, null).getCommand(sshScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && (cd %s && %s)",
+ sshScheme.getUrl(ENV.projectName), baseName(ENV.projectName), hookCommand));
+ }
+
+ @Test
+ public void testSshConfiguredExtraCommand() throws Exception {
+ String extraCommand = "my extra command";
+ String command = getCloneCommand(extraCommand, null).getCommand(sshScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && (cd %s && %s)",
+ sshScheme.getUrl(ENV.projectName), baseName(ENV.projectName), extraCommand));
+ }
+
+ @Test
+ public void testSshConfiguredHookAndExtraCommand() throws Exception {
+ String hookCommand = "my hook command";
+ String extraCommand = "my extra command";
+ String command =
+ getCloneCommand(hookCommand, extraCommand).getCommand(sshScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && (cd %s && %s)",
+ sshScheme.getUrl(ENV.projectName), baseName(ENV.projectName), hookCommand));
+ }
+
+ @Test
+ public void testHttpNoConfiguredCommands() throws Exception {
+ String command = getCloneCommand(null, null).getCommand(httpScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && (cd %s && mkdir -p .git/hooks && curl -Lo `git rev-parse --git-dir`/hooks/commit-msg https://%s@%s/tools/hooks/commit-msg; chmod +x `git rev-parse --git-dir`/hooks/commit-msg)",
+ httpScheme.getUrl(ENV.projectName),
+ baseName(ENV.projectName),
+ ENV.urlEncodedUserName(),
+ ENV.fqdn));
+ }
+
+ @Test
+ public void testHttpConfiguredExtraCommand() throws Exception {
+ String extraCommand = "my extra command";
+ String command = getCloneCommand(extraCommand, null).getCommand(httpScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && (cd %s && %s)",
+ httpScheme.getUrl(ENV.projectName), baseName(ENV.projectName), extraCommand));
+ }
+
+ @Test
+ public void testHttpConfiguredHookAndExtraCommand() throws Exception {
+ String hookCommand = "my hook command";
+ String extraCommand = "my extra command";
+ String command =
+ getCloneCommand(hookCommand, extraCommand).getCommand(httpScheme, ENV.projectName);
+ assertThat(command)
+ .isEqualTo(
+ String.format(
+ "git clone \"%s\" && (cd %s && %s)",
+ httpScheme.getUrl(ENV.projectName), baseName(ENV.projectName), hookCommand));
+ }
+
+ private String baseName(String projectName) {
+ return projectName.substring(projectName.lastIndexOf('/') + 1);
+ }
+
+ private CloneCommand getCloneCommand(String hookCommand, String extraCommand) {
+ Config cfg = new Config();
+ cfg.setString("gerrit", null, HOOK_COMMAND_KEY, hookCommand);
+ cfg.setString("gerrit", null, EXTRA_COMMAND_KEY, extraCommand);
+ return new CloneWithCommitMsgHook(cfg, sshScheme, userProvider);
+ }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/download/scheme/SchemeTest.java b/src/test/java/com/googlesource/gerrit/plugins/download/scheme/SchemeTest.java
index 309107e..eaae634 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/download/scheme/SchemeTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/download/scheme/SchemeTest.java
@@ -16,75 +16,24 @@
import static com.google.common.truth.Truth.assertThat;
-import com.google.common.collect.ImmutableList;
-import com.google.gerrit.entities.Account;
-import com.google.gerrit.server.CurrentUser;
-import com.google.gerrit.server.account.GroupMembership;
-import com.google.gerrit.server.config.DownloadConfig;
-import com.google.inject.Provider;
-import com.google.inject.util.Providers;
-import java.util.Optional;
-import org.eclipse.jgit.lib.Config;
-import org.junit.Before;
+import com.googlesource.gerrit.plugins.download.DownloadCommandTest;
import org.junit.Test;
-public class SchemeTest {
- private HttpScheme httpScheme;
- private SshScheme sshScheme;
-
- @Before
- public void setUp() {
- Config cfg = new Config();
- Provider<String> urlProvider = Providers.of("https://gerrit.company.com/");
- Provider<CurrentUser> userProvider = Providers.of(fakeUser());
- DownloadConfig downloadConfig = new DownloadConfig(cfg);
- httpScheme = new HttpScheme(cfg, urlProvider, userProvider, downloadConfig);
- sshScheme =
- new SshScheme(
- ImmutableList.of("gerrit.company.com:29418"),
- urlProvider,
- userProvider,
- downloadConfig);
+public class SchemeTest extends DownloadCommandTest {
+ @Test
+ public void ensureHttpSchemeEncodedInUrl() throws Exception {
+ assertThat(httpScheme.getUrl(ENV.projectName))
+ .isEqualTo(
+ String.format(
+ "https://%s@%s/a/%s", ENV.urlEncodedUserName(), ENV.fqdn, ENV.projectName));
}
@Test
- public void ensureHttpSchemeEncodedInUrl() {
- assertThat(httpScheme.getUrl("foo"))
- .isEqualTo("https://john-doe%40company.com@gerrit.company.com/a/foo");
- }
-
- @Test
- public void ensureSshSchemeEncodedInUrl() {
- assertThat(sshScheme.getUrl("foo"))
- .isEqualTo("ssh://john-doe%40company.com@gerrit.company.com:29418/foo");
- }
-
- private static CurrentUser fakeUser() {
- return new CurrentUser() {
- @Override
- public Optional<String> getUserName() {
- return Optional.of("john-doe@company.com");
- }
-
- @Override
- public GroupMembership getEffectiveGroups() {
- throw new UnsupportedOperationException("not implemented");
- }
-
- @Override
- public Object getCacheKey() {
- return new Object();
- }
-
- @Override
- public boolean isIdentifiedUser() {
- return true;
- }
-
- @Override
- public Account.Id getAccountId() {
- return Account.id(1);
- }
- };
+ public void ensureSshSchemeEncodedInUrl() throws Exception {
+ assertThat(sshScheme.getUrl(ENV.projectName))
+ .isEqualTo(
+ String.format(
+ "ssh://%s@%s:%d/%s",
+ ENV.urlEncodedUserName(), ENV.fqdn, ENV.sshPort, ENV.projectName));
}
}