Skip to content

Commit

Permalink
Tests for includes/excludes override and supports check
Browse files Browse the repository at this point in the history
  • Loading branch information
andrecsilva committed Aug 16, 2024
1 parent 4bedc5d commit 5900270
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions framework/codemodder-base/src/test/java/io/codemodder/CLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class CLITest {
private Path fooJavaFile;
private Path barJavaFile;
private Path testFile;
private Path notJavaFile;
private List<SourceDirectory> sourceDirectories;

@BeforeEach
Expand All @@ -49,11 +50,13 @@ void setup(final @TempDir Path tmpDir) throws IOException {
fooJavaFile = module1JavaDir.resolve("Foo.java");
barJavaFile = module2JavaDir.resolve("Bar.java");
testFile = module2JavaDir.resolve("MyTest.java");
notJavaFile = module2JavaDir.resolve("MyTest.txt");
Files.write(
fooJavaFile,
"import com.acme.util.Bar; class Foo {private var bar = new Bar();}".getBytes());
Files.write(barJavaFile, "public class Bar {}".getBytes());
Files.write(testFile, "public class MyTest {}".getBytes());
Files.write(notJavaFile, "ground0".getBytes());

/*
* Only add module2 to the discovered source directories. This will help prove that the module1 files can still be seen and changed, even if we couldn't locate it as a "source directory".
Expand Down Expand Up @@ -82,6 +85,22 @@ void it_doesnt_change_test_file() throws IOException {
assertThat(Files.readString(testFile)).doesNotContain("cloud9");
}

@Test
void global_overrides_codemod_includes_excludes() throws IOException {
String[] args =
new String[] {"--dont-exit", "--path-include=**/*Test.java", workingRepoDir.toString()};
Runner.run(List.of(Cloud9Changer.class), args);
assertThat(Files.readString(testFile)).contains("cloud9");
}

@Test
void it_must_respect_codemod_supports() throws IOException {
String[] args =
new String[] {"--dont-exit", "--path-include=**/*Test.txt", workingRepoDir.toString()};
Runner.run(List.of(Cloud9Changer.class), args);
assertThat(Files.readString(notJavaFile)).doesNotContain("cloud9");
}

@Test
void it_works_without_output_file() throws IOException {
String[] args = new String[] {"--dont-exit", workingRepoDir.toString()};
Expand Down Expand Up @@ -214,7 +233,7 @@ void file_finder_works() throws IOException {

IncludesExcludes all = IncludesExcludes.any();
List<Path> files = finder.findFiles(workingRepoDir, all);
assertThat(files).containsExactly(fooJavaFile, barJavaFile, testFile);
assertThat(files).containsExactly(fooJavaFile, barJavaFile, testFile, notJavaFile);

IncludesExcludes onlyFoo =
IncludesExcludes.withSettings(workingRepoDir.toFile(), List.of("**/Foo.java"), List.of());
Expand All @@ -236,12 +255,9 @@ private Cloud9Changer() {
public CodemodFileScanningResult visitFile(final CodemodInvocationContext context)
throws IOException {
Path path = context.path();
if (path.toString().endsWith(".java")) {
Files.writeString(path, "cloud9");
List<CodemodChange> changes = List.of(CodemodChange.from(1));
return CodemodFileScanningResult.withOnlyChanges(changes);
}
return CodemodFileScanningResult.none();
Files.writeString(path, "cloud9");
List<CodemodChange> changes = List.of(CodemodChange.from(1));
return CodemodFileScanningResult.withOnlyChanges(changes);
}

@Override
Expand All @@ -250,8 +266,8 @@ public IncludesExcludesPattern getIncludesExcludesPattern() {
}

@Override
public boolean supports(Path file) {
return true;
public boolean supports(final Path file) {
return file.toString().endsWith(".java");
}
}

Expand Down

0 comments on commit 5900270

Please sign in to comment.