Skip to content

Commit 01f2b33

Browse files
committed
First submodule test working
1 parent d75441b commit 01f2b33

File tree

4 files changed

+148
-52
lines changed

4 files changed

+148
-52
lines changed

tests/integration/java/com/github/bazel_contrib/target_determinator/integration/Commits.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,6 @@ class Commits {
6161
public static final String REDUCE_DEPENDENCY_VISIBILITY = "reduce_dependency_visibility";
6262
public static final String ONE_TEST_WITH_GITIGNORE = "one_test_with_gitignore";
6363
public static final String TWO_TESTS_WITH_GITIGNORE = "two_tests_with_gitignore";
64-
public static final String SUBMODULE_ADD_TRIVIAL_SUBMODULE =
65-
"b88ddcfe3da63c8308ce6d3274dd424d2c7b211a";
66-
public static final String SUBMODULE_ADD_DEPENDENT_ON_SIMPLE_JAVA_LIBRARY =
67-
"4e9b396b3a8030925d7b544cda3f1edbc199810f";
68-
public static final String SUBMODULE_CHANGE_DIRECTORY =
69-
"1cef87480c2c1dac74cc9de7470504fbd2b80265";
70-
public static final String SUBMODULE_DELETE_SUBMODULE =
71-
"d1b1d8f07f2e99429bafda134282b97588c69b3d";
7264
public static final String TWO_TESTS_BRANCH =
7365
"two-tests-branch"; // Local only (created by the test case).
7466
public static final String ONE_SH_TEST = "one_sh_test";

tests/integration/java/com/github/bazel_contrib/target_determinator/integration/TargetDeterminatorSpecificFlagsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void failForUncleanSubmoduleWithEnforceClean() throws Exception {
207207

208208
String beforeCommit = repo.commit("Add dependent target in submodule", "demo-submodule");
209209

210-
repo.move("demo-submodule", "demo-submodule-2");
210+
repo.moveSubmodule("demo-submodule", "demo-submodule-2");
211211
repo.commit("Move demo-submodule to demo-submodule-2");
212212

213213
Files.createFile(repo.getDir().resolve("demo-submodule-2").resolve("untracked-file"));

tests/integration/java/com/github/bazel_contrib/target_determinator/integration/TestRepo.java

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.nio.file.SimpleFileVisitor;
2222
import java.nio.file.attribute.BasicFileAttributes;
2323
import java.util.Comparator;
24+
import java.util.List;
2425
import java.util.Objects;
2526
import java.util.Set;
2627
import java.util.function.Predicate;
@@ -200,11 +201,35 @@ public void pull() {
200201
}
201202
}
202203

203-
public void move(String from, String to) {
204+
public void moveSubmodule(String from, String to) {
204205
try {
205206
Files.move(dir.resolve(from), dir.resolve(to));
206-
} catch (IOException e) {
207-
throw new UncheckedIOException(e);
207+
208+
// TODO: Using `String.replace` the way we are is asking for trouble. Do this nicely.
209+
210+
// Check the .gitmodules file if necessary
211+
Path gitModules = dir.resolve(".gitmodules");
212+
String contents = Files.readString(gitModules);
213+
contents = contents.replace("path = " + from, "path = " + to);
214+
Files.writeString(gitModules, contents);
215+
216+
gitRepo.add().addFilepattern(to).call();
217+
gitRepo.rm().addFilepattern(from).setCached(true).call();
218+
219+
Path newGitSubmoduleDir = dir.resolve(".git").resolve("modules").resolve(to);
220+
Files.move(dir.resolve(".git").resolve("modules").resolve(from), newGitSubmoduleDir);
221+
222+
Path submoduleConfig = newGitSubmoduleDir.resolve("config");
223+
String configContents = Files.readString(submoduleConfig);
224+
configContents = configContents.replace("/" + from, "/" + to);
225+
Files.writeString(submoduleConfig, configContents);
226+
227+
Path gitFile = dir.resolve(to).resolve(".git");
228+
String gitFileContents = Files.readString(gitFile);
229+
gitFileContents = gitFileContents.replace("/" + from, "/" + to);
230+
Files.writeString(gitFile, gitFileContents);
231+
} catch (IOException | GitAPIException e) {
232+
throw new RuntimeException(e);
208233
}
209234
}
210235

@@ -225,4 +250,43 @@ public String getBranch() {
225250
throw new UncheckedIOException(e);
226251
}
227252
}
253+
254+
public void removeSubmodule(String submodulePath) {
255+
try {
256+
FileBasedConfig config = (FileBasedConfig) gitRepo.getRepository().getConfig();
257+
config.unsetSection("submodule", submodulePath);
258+
config.save();
259+
260+
// Remove the submodule from the `.gitmodules` file
261+
deleteFromGitsubmodulesFile(submodulePath);
262+
263+
gitRepo.rm().addFilepattern(submodulePath).setCached(true).call();
264+
} catch (GitAPIException | IOException e) {
265+
throw new RuntimeException(e);
266+
}
267+
}
268+
269+
private void deleteFromGitsubmodulesFile(String submodulePath) throws IOException {
270+
Path gitModules = dir.resolve(".gitmodules");
271+
List<String> contents = Files.readAllLines(gitModules);
272+
StringBuilder newContents = new StringBuilder();
273+
boolean inSection = false;
274+
for (String line : contents) {
275+
// New sections start at column 0, with something like
276+
// `[submodule`. Look for the starter character.
277+
if (inSection && !line.isEmpty() && line.charAt(0) == '[') {
278+
inSection = false;
279+
}
280+
// However, the section we want to delete starts with a very
281+
// particular pattern, so look for that next.
282+
if (!inSection && line.startsWith("[submodule \"" + submodulePath + "\"]")) {
283+
inSection = true;
284+
}
285+
// If we're not deleting things, copy it into the new contents
286+
if (!inSection) {
287+
newContents.append(line).append("\n");
288+
}
289+
}
290+
Files.writeString(gitModules, newContents.toString());
291+
}
228292
}

tests/integration/java/com/github/bazel_contrib/target_determinator/integration/Tests.java

Lines changed: 80 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static junit.framework.TestCase.fail;
44
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.hamcrest.Matchers.not;
56
import static org.junit.Assert.assertEquals;
67
import static org.junit.Assume.assumeFalse;
78

@@ -31,8 +32,15 @@
3132
*/
3233
public abstract class Tests {
3334

34-
@Rule
35-
public TemporaryFolder tempDir = new TemporaryFolder();
35+
@Rule public TestName name = new TestName();
36+
@Rule public TemporaryFolder tempDir = new TemporaryFolder();
37+
38+
protected Path testDir;
39+
40+
protected static final String ignoredDirectoryName = "ignored-directory";
41+
protected static final String ignoredFileName = "some-file";
42+
43+
private boolean allowOverBuilds = false;
3644

3745
/**
3846
* Get the targets affected by the diff between two commits.
@@ -46,15 +54,6 @@ public abstract class Tests {
4654
abstract Set<Label> getTargets(Path workspace, String commitBefore, String commitAfter)
4755
throws TargetComputationErrorException;
4856

49-
protected Path testDir;
50-
51-
protected static final String ignoredDirectoryName = "ignored-directory";
52-
protected static final String ignoredFileName = "some-file";
53-
54-
private boolean allowOverBuilds = false;
55-
56-
@Rule public TestName name = new TestName();
57-
5857
private Set<Label> getTargets(String commitBefore, String commitAfter)
5958
throws TargetComputationErrorException {
6059
return getTargets(testDir, commitBefore, commitAfter);
@@ -480,7 +479,7 @@ public void succeedForUncleanSubmodule() throws Exception {
480479
submoduleWithinRepo.pull();
481480
String beforeCommit = repo.commit("Add dependent targets in submodule", "demo-submodule");
482481

483-
repo.move("demo-submodule", "demo-submodule-2");
482+
repo.moveSubmodule("demo-submodule", "demo-submodule-2");
484483
String afterCommit = repo.commit("Move demo-submodule to demo-submodule-2");
485484

486485
Files.createFile(repo.getDir().resolve("demo-submodule-2").resolve("untracked-file"));
@@ -542,32 +541,71 @@ public void addDependentTargetInSubmodule() throws Exception {
542541
Set.of("//demo-submodule:submodule_simple"),
543542
Set.of());
544543
}
545-
//
546-
// @Test
547-
// public void changeSubmodulePath() throws Exception {
548-
// doTest(
549-
// Commits.SUBMODULE_ADD_DEPENDENT_ON_SIMPLE_JAVA_LIBRARY,
550-
// Commits.SUBMODULE_CHANGE_DIRECTORY,
551-
// Set.of("//demo-submodule-2:submodule_simple"));
552-
//
553-
// assertThat(
554-
// "The old submodule directory should not exist anymore",
555-
// not(Files.exists(testDir.resolve("demo-submodule"))));
556-
//
557-
// assertThat(
558-
// "The moved submodule should now be present with its README.md but isn't",
559-
// Files.exists(testDir.resolve("demo-submodule-2").resolve("README.md")));
560-
// }
561-
//
562-
// @Test
563-
// public void deleteSubmodule() throws Exception {
564-
// doTest(Commits.SUBMODULE_CHANGE_DIRECTORY, Commits.SUBMODULE_DELETE_SUBMODULE, Set.of());
565-
//
566-
// assertThat(
567-
// "The old submodule directory should not exist anymore",
568-
// not(Files.exists(testDir.resolve("demo-submodule-2"))));
569-
// }
570-
//
544+
545+
@Test
546+
public void targetDeterminationShouldLeaveTheMovedSubmoduleBehind() throws Exception {
547+
TestRepo repo = TestRepo.create(testDir);
548+
549+
repo.replaceWithContentsFrom(Commits.SIMPLE_JAVA_LIBRARY_TARGETS);
550+
repo.commit("Initial commit");
551+
552+
TestRepo submodule = TestRepo.create(tempDir.newFolder("submodule").toPath());
553+
submodule.replaceWithContentsFrom(Commits.EMPTY_SUBMODULE);
554+
submodule.commit("Create empty submodule");
555+
556+
TestRepo submoduleWithinRepo = repo.addSubModule(submodule, "demo-submodule");
557+
558+
// Update the submodule
559+
submodule.replaceWithContentsFrom(Commits.ADD_DEPENDENT_ON_SIMPLE_JAVA_LIBRARY);
560+
submodule.commit("Update submodule");
561+
562+
// Now update the reference in the main repo
563+
submoduleWithinRepo.pull();
564+
String beforeCommit = repo.commit("Add dependent targets in submodule", "demo-submodule");
565+
566+
repo.moveSubmodule("demo-submodule", "demo-submodule-2");
567+
String afterCommit = repo.commit("Move demo-submodule to demo-submodule-2");
568+
569+
assertTargetDeterminatorRun(
570+
beforeCommit,
571+
afterCommit,
572+
Set.of("//demo-submodule-2:submodule_simple"),
573+
Set.of());
574+
575+
assertThat(
576+
"The old submodule directory should not exist anymore",
577+
not(Files.exists(testDir.resolve("demo-submodule"))));
578+
579+
assertThat(
580+
"The moved submodule should now be present with its README.md but isn't",
581+
Files.exists(testDir.resolve("demo-submodule-2").resolve("README.md")));
582+
}
583+
584+
@Test
585+
public void targetDeterminationDoesNotLeaveDeletedSubmoduleDirectoriesBehind() throws Exception {
586+
TestRepo repo = TestRepo.create(testDir);
587+
588+
repo.replaceWithContentsFrom(Commits.SIMPLE_JAVA_LIBRARY_TARGETS);
589+
repo.commit("Initial commit");
590+
591+
TestRepo submodule = TestRepo.create(tempDir.newFolder("submodule").toPath());
592+
submodule.replaceWithContentsFrom(Commits.EMPTY_SUBMODULE);
593+
submodule.commit("Create empty submodule");
594+
595+
repo.addSubModule(submodule, "demo-submodule");
596+
String before = repo.commit("Add a submodule");
597+
598+
// Now delete the submodule
599+
repo.removeSubmodule("demo-submodule");
600+
String after = repo.commit("Delete submodule");
601+
602+
assertTargetDeterminatorRun(before, after, Set.of(), Set.of());
603+
604+
assertThat(
605+
"The old submodule directory should not exist anymore",
606+
not(Files.exists(testDir.resolve("demo-submodule-2"))));
607+
}
608+
571609
@Test
572610
public void testRelativeRevisions() throws Exception {
573611
TestRepo repo = TestRepo.create(testDir);
@@ -727,8 +765,10 @@ private void assertTargetDeterminatorRun(
727765
if (supportsIgnoredUnstagedFiles()) {
728766
try {
729767
Path ignoredDirectory = testDir.resolve(ignoredDirectoryName);
730-
Files.createDirectory(ignoredDirectory);
731-
Files.createFile(ignoredDirectory.resolve(ignoredFileName));
768+
if (!Files.exists(ignoredDirectory)) {
769+
Files.createDirectory(ignoredDirectory);
770+
Files.createFile(ignoredDirectory.resolve(ignoredFileName));
771+
}
732772
} catch (IOException e) {
733773
throw new UncheckedIOException(e);
734774
}

0 commit comments

Comments
 (0)