22
33import static junit .framework .TestCase .fail ;
44import static org .hamcrest .MatcherAssert .assertThat ;
5+ import static org .hamcrest .Matchers .not ;
56import static org .junit .Assert .assertEquals ;
67import static org .junit .Assume .assumeFalse ;
78
3132 */
3233public 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