-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file searching ability to
CodeDirectory
(#363)
Analyzing binary result from multiple tools shows we'll need this functionality in multiple places, so we're adding it to the generally available `CodeDirectory` type.
- Loading branch information
Showing
18 changed files
with
161 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
framework/codemodder-base/src/main/java/io/codemodder/CodeDirectory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,22 @@ | ||
package io.codemodder; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
/** Holds a code directory (e.g., a repository root). */ | ||
public interface CodeDirectory { | ||
|
||
/** The filesystem directory path we are running against. */ | ||
Path asPath(); | ||
|
||
/** | ||
* Find a file with the given trailing path. This is useful for situations in which you only know | ||
* the last part of the path for a file within the project. | ||
*/ | ||
Optional<Path> findFilesWithTrailingPath(final String path) throws IOException; | ||
|
||
static CodeDirectory from(final Path projectDir) { | ||
return new DefaultCodeDirectory(projectDir); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
framework/codemodder-base/src/main/java/io/codemodder/RuleSarifFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
package io.codemodder; | ||
|
||
import com.contrastsecurity.sarif.SarifSchema210; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
/** Builds {@link RuleSarif}s. */ | ||
public interface RuleSarifFactory { | ||
|
||
/** Builds {@link RuleSarif}s if it supports {@code toolName}. */ | ||
Optional<RuleSarif> build( | ||
String toolName, String rule, SarifSchema210 sarif, Path repositoryRoot); | ||
String toolName, String rule, SarifSchema210 sarif, CodeDirectory codeDirectory); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
framework/codemodder-base/src/test/java/io/codemodder/DefaultCodeDirectoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.codemodder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
final class DefaultCodeDirectoryTest { | ||
|
||
private DefaultCodeDirectory codeDirectory; | ||
private Path repoDir; | ||
|
||
@BeforeEach | ||
void setup(@TempDir final Path repoDir) throws IOException { | ||
Path testFile1 = repoDir.resolve("my/other/test/file1.java"); | ||
Path srcFile1 = repoDir.resolve("src/main/file1.java"); | ||
Path srcFile2 = repoDir.resolve("src/main/file2.java"); | ||
|
||
Files.createDirectories(testFile1.getParent()); | ||
Files.createFile(testFile1); | ||
|
||
Files.createDirectories(srcFile1.getParent()); | ||
Files.createFile(srcFile1); | ||
|
||
Files.createDirectories(srcFile2.getParent()); | ||
Files.createFile(srcFile2); | ||
|
||
Files.writeString(testFile1, "test file 1"); | ||
Files.writeString(srcFile1, "src file 1"); | ||
Files.writeString(srcFile2, "src file 2"); | ||
|
||
this.repoDir = repoDir; | ||
codeDirectory = new DefaultCodeDirectory(repoDir); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("fileTests") | ||
void it_finds_files(final String givenPath, final String expectedPath) throws IOException { | ||
Optional<Path> filesWithTrailingPath = codeDirectory.findFilesWithTrailingPath(givenPath); | ||
|
||
if (expectedPath == null) { | ||
assertThat(filesWithTrailingPath).isEmpty(); | ||
} else { | ||
assertThat(filesWithTrailingPath).isPresent(); | ||
Path expected = repoDir.resolve(expectedPath); | ||
assertThat(filesWithTrailingPath.get()).isEqualTo(expected); | ||
} | ||
} | ||
|
||
private static Stream<Arguments> fileTests() { | ||
return Stream.of( | ||
Arguments.of("file1.java", "my/other/test/file1.java"), | ||
Arguments.of("main/file1.java", "src/main/file1.java"), | ||
Arguments.of("main//file1.java", "src/main/file1.java"), | ||
Arguments.of("main\\file1.java", "src/main/file1.java"), | ||
Arguments.of("src\\\\main\\file1.java", "src/main/file1.java"), | ||
Arguments.of("file2.java", "src/main/file2.java"), | ||
Arguments.of("file3.java", null)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
...-appscan/src/main/java/io/codemodder/providers/sarif/appscan/AppScanRuleSarifFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.