1515 */
1616package com .google .copybara .regenerate ;
1717
18+ import static com .google .common .base .Strings .nullToEmpty ;
1819import static com .google .common .truth .Truth .assertThat ;
1920import static com .google .copybara .testing .FileSubjects .assertThatPath ;
2021import static org .junit .Assert .assertThrows ;
@@ -113,18 +114,19 @@ public DestinationReader getDestinationReader(
113114 return new DestinationReader () {
114115 @ Override
115116 public String readFile (String path ) throws RepoException {
116- throw new RepoException ( "not implemented" );
117- }
118-
119- @ Override
120- public void copyDestinationFiles ( Object glob , Object path )
121- throws RepoException , ValidationException {
122- throw new RepoException ( "not implemented" );
117+ String destinationSubdir = nullToEmpty ( baseline );
118+ try {
119+ return Files . readString (
120+ destinationRoot . resolve ( destinationSubdir ). resolve ( path ));
121+ } catch ( IOException e ) {
122+ throw new RepoException ( "Error reading file in test" , e );
123+ }
123124 }
124125
125126 @ Override
126127 public void copyDestinationFilesToDirectory (Glob glob , Path directory ) {
127- Path sourceDirectory = destinationRoot .resolve (baseline );
128+ String destinationSubdir = nullToEmpty (baseline );
129+ Path sourceDirectory = destinationRoot .resolve (destinationSubdir );
128130 try {
129131 FileUtil .copyFilesRecursively (
130132 sourceDirectory ,
@@ -138,7 +140,15 @@ public void copyDestinationFilesToDirectory(Glob glob, Path directory) {
138140
139141 @ Override
140142 public boolean exists (String path ) {
141- return Files .exists (destinationRoot .resolve (baseline ).resolve (path ));
143+ // In this test, 'baseline' can be the destination subdir (e.g., "target")
144+ String destinationSubdir = nullToEmpty (baseline );
145+ return Files .exists (destinationRoot .resolve (destinationSubdir ).resolve (path ));
146+ }
147+
148+ @ Override
149+ public void copyDestinationFiles (Object glob , Object path )
150+ throws RepoException , ValidationException {
151+ throw new RepoException ("copyDestinationFiles(Object, Object) not implemented" );
142152 }
143153 };
144154 }
@@ -936,4 +946,47 @@ private void writeDestination(String name, String filepath, byte[] bytes) throws
936946 private Path destinationPath (String name ) {
937947 return destinationRoot .resolve (name );
938948 }
949+
950+ @ Test
951+ public void testRegenerate_nonMergeImport_preservesConsistencyFile () throws Exception {
952+ setupFooOriginImport (); // This creates "foo.txt" in origin
953+ when (patchRegenerator .inferImportBaseline (any (), any ()))
954+ .thenReturn (Optional .of (origin .getLatestChange ().asString ()));
955+ String destRef = "target" ;
956+ setupTarget (destRef );
957+ // Initial state with a consistency file
958+ String consistencyContent = "===BASELINE===" ;
959+ // Path relative to destination root for this test
960+ String consistencyFilePath = CONSISTENCY_FILE_PATH ;
961+ writeDestination (destRef , consistencyFilePath , consistencyContent );
962+ // Simulate foo.txt being in the destination from a previous import
963+ writeDestination (destRef , "foo.txt" , "foo" );
964+ String config =
965+ """
966+ core.workflow(
967+ name = 'default',
968+ origin = testing.origin(),
969+ origin_files = glob(['**']),
970+ destination = testing.destination(),
971+ mode = 'SQUASH',
972+ authoring = authoring.pass_thru('example <example@example.com>'),
973+ consistency_file_path = '%s'
974+ )
975+ """
976+ .formatted (consistencyFilePath );
977+ RegenerateCmd cmd = getCmd (config );
978+ CommandEnv commandEnv =
979+ prepAndGetCommandEnv (ImmutableList .of (testRoot .resolve ("copy.bara.sky" ).toString ()), cmd );
980+
981+ ExitCode exitCode = cmd .run (commandEnv );
982+
983+ assertThat (exitCode ).isEqualTo (ExitCode .SUCCESS );
984+ // Verify the consistency file was copied into the workdir passed to updateChange
985+ ArgumentCaptor <Path > pathArg = ArgumentCaptor .forClass (Path .class );
986+ verify (patchRegenerator ).updateChange (any (), pathArg .capture (), any (), eq (destRef ));
987+ // Assert content in the workdir
988+ assertThat (Files .readString (pathArg .getValue ().resolve (consistencyFilePath )))
989+ .isEqualTo (consistencyContent );
990+ assertThat (Files .readString (pathArg .getValue ().resolve ("foo.txt" ))).isEqualTo ("foo" );
991+ }
939992}
0 commit comments