Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/main/java/org/pqca/scanning/go/GoScannerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
Expand All @@ -38,7 +39,6 @@
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.impl.utils.DefaultTempFolder;
import org.sonar.api.rule.RuleKey;
import org.sonar.go.converter.GoConverter;
import org.sonar.go.plugin.GoChecks;
Expand All @@ -65,10 +65,8 @@ public GoScannerService(
int numberOfScannedLines = 0;
int numberOfScannedFiles = 0;

File goTempFolder = new DefaultTempFolder(this.projectDirectory).newDir();

try {
GoConverter goConverter = new GoConverter(goTempFolder);
try (GoTempFolder goTempFolder = GoTempFolder.create()) {
GoConverter goConverter = new GoConverter(goTempFolder.file());
GoCheck visitor = new GoDetectionCollectionRule(this);
GoChecks checks = new GoRuleChecks(visitor);
final SensorContextTester sensorContext = SensorContextTester.create(projectDirectory);
Expand Down Expand Up @@ -97,12 +95,8 @@ public GoScannerService(
counter += 1;
}
LOGGER.info("Scanned {} go projects", index.size());
} finally {
try {
FileUtils.deleteDirectory(goTempFolder);
} catch (IOException e) {
LOGGER.error("Failed to delete temp dir {}", goTempFolder);
}
} catch (IOException e) {
LOGGER.error("Failed to create temp dir for go scanner", e);
}

return new ScanResultDTO(
Expand All @@ -113,6 +107,31 @@ public GoScannerService(
this.getBOM().map(CBOM::new).orElse(null));
}

private static final class GoTempFolder implements AutoCloseable {
private final File directory;

private GoTempFolder(File directory) {
this.directory = directory;
}

private static GoTempFolder create() throws IOException {
return new GoTempFolder(Files.createTempDirectory("cbomkit-go-").toFile());
}

private File file() {
return this.directory;
}

@Override
public void close() {
try {
FileUtils.deleteDirectory(this.directory);
} catch (IOException e) {
LOGGER.error("Failed to delete temp dir {}", this.directory, e);
}
}
}

private class GoRuleChecks extends GoChecks {
private final List<GoCheck> checks = new ArrayList<>();

Expand Down
21 changes: 20 additions & 1 deletion src/test/java/org/pqca/scanning/GoScannerServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.pqca.errors.ClientDisconnected;
import org.pqca.indexing.ProjectModule;
Expand All @@ -33,9 +39,11 @@
class GoScannerServiceTest {

@Test
void test() throws ClientDisconnected {
void test() throws ClientDisconnected, IOException {
// indexing
final File projectDirectory = new File("src/test/testdata/go/gocrypto");
final Set<String> projectDirectoryEntriesBeforeScan =
listRelativePaths(projectDirectory.toPath());
final GoIndexService goIndexService = new GoIndexService(projectDirectory);
final List<ProjectModule> goModules = goIndexService.index(null);
assertThat(goModules).hasSize(1);
Expand Down Expand Up @@ -84,5 +92,16 @@ void test() throws ClientDisconnected {
"src/test/testdata/go/gocrypto/GoCryptoPBKDF2TestFile.go",
15))
.isTrue();

assertThat(listRelativePaths(projectDirectory.toPath()))
.isEqualTo(projectDirectoryEntriesBeforeScan);
}

private static Set<String> listRelativePaths(Path projectDirectory) throws IOException {
try (Stream<Path> paths = Files.walk(projectDirectory)) {
return paths.map(projectDirectory::relativize)
.map(Path::toString)
.collect(Collectors.toSet());
}
}
}