Skip to content

Commit

Permalink
Dataprovider update to fix Pre commit hook config
Browse files Browse the repository at this point in the history
Part of SAP#730
  • Loading branch information
sourabhsparkala committed Sep 6, 2022
1 parent e37f9b0 commit 337f3d8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,18 +432,16 @@ public static class Visitor extends AbstractGitHubVisitor {
@Override
public void visitPreCommitHook(LocalRepository repository,
Map<String, Predicate<String>> matchers, Set<Location> locations) throws IOException {
Optional<Path> preCommitConfigPath = repository.path(PRE_COMMIT_HOOK_CONFIG);
if (!preCommitConfigPath.isPresent()) {
Optional<InputStream> content = repository.fileStream(PRE_COMMIT_HOOK_CONFIG);
if (!content.isPresent()) {
return;
}

try (InputStream content = Files.newInputStream(preCommitConfigPath.get())) {
Map<String, Object> config = Yaml.readMap(content);
if (hasPreCommitHook(config, matchers)) {
runCheck = true;
usesCheck = true;
}
Map<String, Object> config = Yaml.readMap(content.get());
if (hasPreCommitHook(config, matchers)) {
runCheck = true;
usesCheck = true;
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,37 @@ public Optional<String> file(Path file) throws IOException {
return Optional.of(IOUtils.toString(is, UTF_8));
}
}

/**
* Returns a content of a file if it exists.
*
* @param file The file name.
* @return A inputstream of the file.
* @throws IOException If something went wrong.
*/
public Optional<InputStream> fileStream(String file) throws IOException {
Objects.requireNonNull(file, "On no! File name is null!");
return fileStream(Paths.get(file));
}

/**
* Returns a content of a file if it exists.
*
* @param file The file name.
* @return A inputstream of the file.
* @throws IOException If something went wrong.
*/
public Optional<InputStream> fileStream(Path file) throws IOException {
Objects.requireNonNull(file, "On no! File name is null!");
Path path = info.path().resolve(file);
if (!Files.isRegularFile(path)) {
return Optional.empty();
}

try (InputStream is = Files.newInputStream(path)) {
return Optional.ofNullable(is);
}
}

/**
* Checks if the repository has a specified directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testWithNoPylintRunsButInstallsPylintAndUsesPylint() throws IOExcept
public void testWithPylintInRepo() throws IOException {
try (InputStream content =
getClass().getResourceAsStream("pylint-analysis-with-pylint-in-repo.yml")) {
testPylintPathCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
testPylintFileStreamCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
RUNS_PYLINT_SCANS.value(true),
USES_PYLINT_SCAN_CHECKS.value(true));
}
Expand All @@ -120,7 +120,7 @@ public void testWithPylintInRepo() throws IOException {
public void testWithPylintInEntry() throws IOException {
try (InputStream content =
getClass().getResourceAsStream("pylint-analysis-with-pylint-in-entry.yml")) {
testPylintPathCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
testPylintFileStreamCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
RUNS_PYLINT_SCANS.value(true),
USES_PYLINT_SCAN_CHECKS.value(true));
}
Expand All @@ -130,7 +130,7 @@ public void testWithPylintInEntry() throws IOException {
public void testWithPylintInRev() throws IOException {
try (InputStream content =
getClass().getResourceAsStream("pylint-analysis-with-pylint-in-rev.yml")) {
testPylintPathCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
testPylintFileStreamCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
RUNS_PYLINT_SCANS.value(true),
USES_PYLINT_SCAN_CHECKS.value(true));
}
Expand All @@ -140,7 +140,7 @@ public void testWithPylintInRev() throws IOException {
public void testWithNoPylintAsPreCommitHookConfig() throws IOException {
try (InputStream content =
getClass().getResourceAsStream("pylint-analysis-no-pylint-hook.yml")) {
testPylintPathCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
testPylintFileStreamCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
RUNS_PYLINT_SCANS.value(false),
USES_PYLINT_SCAN_CHECKS.value(false));
}
Expand All @@ -150,7 +150,7 @@ public void testWithNoPylintAsPreCommitHookConfig() throws IOException {
public void testWithPylintProspector() throws IOException {
try (InputStream content =
getClass().getResourceAsStream("pylint-analysis-with-prospector.yml")) {
testPylintPathCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
testPylintFileStreamCheck(GITHUB_PRE_COMMIT_HOOK_CONFIG_FILENAME, content,
RUNS_PYLINT_SCANS.value(true),
USES_PYLINT_SCAN_CHECKS.value(true));
}
Expand Down Expand Up @@ -193,13 +193,14 @@ private void testPylintFilesCheck(String filename, InputStream content,
}
}

private void testPylintPathCheck(String filename, InputStream content,
private void testPylintFileStreamCheck(String filename, InputStream content,
Value<?>... expectedValues) throws IOException {
Path file = repositoryDirectory.resolve(filename);
Files.createDirectories(file.getParent());
when(localRepository.hasDirectory(any(Path.class))).thenReturn(true);
IOUtils.copy(content, Files.newOutputStream(file));
when(localRepository.path(any())).thenReturn(Optional.of(file));
when(localRepository.fileStream(any(String.class)))
.thenReturn(Optional.of(Files.newInputStream(file)));

PylintDataProvider provider = new PylintDataProvider(fetcher);
ValueSet values = provider.fetchValuesFor(PROJECT);
Expand Down

0 comments on commit 337f3d8

Please sign in to comment.