blob: 9150a3c279013884d4ad8e46142e431090294ee6 [file] [log] [blame]
// 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 org.mockito.Mockito.mock;
import com.google.gerrit.server.config.DownloadConfig;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.googlesource.gerrit.plugins.download.DownloadCommandTest;
import org.eclipse.jgit.lib.Config;
import org.junit.Test;
public class BranchCommandTest extends DownloadCommandTest {
private static final String TEST_URL = "unit.test/";
private static final String TEST_REF = "origin/main";
private static final String TEST_ID = "1234";
private static final String CHECKOUT_COMMAND_PREFIX =
"git fetch " + TEST_URL + " " + TEST_REF + " && ";
@Test
public void buildBranchCommand() {
BranchCommand branchCommand = newBranchCommand(defaultGerritConfig());
String actual = branchCommand.getCommand(TEST_URL, TEST_REF, TEST_ID);
assertThat(actual)
.isEqualTo(CHECKOUT_COMMAND_PREFIX + "git checkout -b change-1234 FETCH_HEAD");
}
@Test
public void buildBranchCommandWithRecurseSubmodules() {
Config cfg = defaultGerritConfig();
cfg.setBoolean("download", null, "recurseSubmodules", true);
BranchCommand branchCommand = newBranchCommand(cfg);
String actual = branchCommand.getCommand(TEST_URL, TEST_REF, TEST_ID);
assertThat(actual)
.isEqualTo(
CHECKOUT_COMMAND_PREFIX
+ "git checkout --recurse-submodules -b change-1234 FETCH_HEAD");
}
private BranchCommand newBranchCommand(Config cfg) {
GitRepositoryManager gitRepositoryManagerMock = mock(GitRepositoryManager.class);
return new BranchCommand(cfg, new DownloadConfig(cfg), gitRepositoryManagerMock);
}
private Config defaultGerritConfig() {
Config cfg = new Config();
cfg.setString("download", null, "command", "branch");
return cfg;
}
}