Search in sources :

Example 1 with SimpleFileFinder

use of com.synopsys.integration.common.util.finder.SimpleFileFinder in project synopsys-detect by blackducksoftware.

the class SimpleFileFinderTest method findFiles.

private List<File> findFiles(boolean followSymLinks) throws IOException {
    // Create a subDir with a symlink that points to isolated directory
    File initialDirectory = initialDirectoryPath.toFile();
    File subDir = new File(initialDirectory, "sub");
    subDir.mkdirs();
    File link = new File(subDir, "linkToInitial");
    Path linkPath = link.toPath();
    Files.createSymbolicLink(linkPath, initialDirectoryPath);
    File regularDir = new File(subDir, "regularDir");
    regularDir.mkdir();
    File regularFile = new File(subDir, "regularFile");
    regularFile.createNewFile();
    SimpleFileFinder finder = new SimpleFileFinder();
    List<String> filenamePatterns = Arrays.asList("sub", "linkToInitial", "regularDir", "regularFile");
    return finder.findFiles(initialDirectoryPath.toFile(), filenamePatterns, followSymLinks, 10);
}
Also used : Path(java.nio.file.Path) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) File(java.io.File)

Example 2 with SimpleFileFinder

use of com.synopsys.integration.common.util.finder.SimpleFileFinder in project synopsys-detect by blackducksoftware.

the class ExclusionPatternCreatorTest method testDoesNotCreateRedundantExclusions.

@Test
public void testDoesNotCreateRedundantExclusions() throws IOException {
    File root = new File("root");
    root.mkdir();
    File foo = new File("root", "foo");
    foo.mkdir();
    File bar = new File(foo, "bar");
    bar.mkdir();
    List<String> toExclude = Arrays.asList("foo", "bar");
    DetectExcludedDirectoryFilter filter = new DetectExcludedDirectoryFilter(toExclude);
    ExclusionPatternCreator exclusionPatternCreator = new ExclusionPatternCreator(new SimpleFileFinder(), file -> filter.isExcluded(file), root);
    Set<String> exlusionPatterns = exclusionPatternCreator.determineExclusionPatterns(false, 2, toExclude);
    Assertions.assertEquals(1, exlusionPatterns.size());
    Assertions.assertTrue(exlusionPatterns.contains("/foo/"));
    Assertions.assertFalse(exlusionPatterns.contains("/foo/bar/"));
    FileUtils.deleteDirectory(root);
}
Also used : DetectExcludedDirectoryFilter(com.synopsys.integration.detect.util.finder.DetectExcludedDirectoryFilter) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) File(java.io.File) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with SimpleFileFinder

use of com.synopsys.integration.common.util.finder.SimpleFileFinder in project synopsys-detect by blackducksoftware.

the class DetectorFinderTest method testSimple.

@Test
// TODO: See if we can fix on windows.
@DisabledOnOs(WINDOWS)
public void testSimple() {
    Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS);
    File initialDirectory = initialDirectoryPath.toFile();
    File subDir = new File(initialDirectory, "testSimple");
    subDir.mkdirs();
    File subSubDir1 = new File(subDir, "subSubDir1");
    subSubDir1.mkdir();
    File subSubDir2 = new File(subDir, "subSubDir2");
    subSubDir2.mkdir();
    DetectorRuleSet detectorRuleSet = new DetectorRuleSet(new ArrayList<>(0), new HashMap<>(0));
    Predicate<File> fileFilter = f -> true;
    int maximumDepth = 10;
    DetectorFinderOptions options = new DetectorFinderOptions(fileFilter, maximumDepth, false);
    DetectorFinder finder = new DetectorFinder();
    Optional<DetectorEvaluationTree> tree = finder.findDetectors(initialDirectory, detectorRuleSet, options, new SimpleFileFinder());
    // make sure both dirs were found
    Set<DetectorEvaluationTree> testDirs = tree.get().getChildren();
    DetectorEvaluationTree simpleTestDir = null;
    for (DetectorEvaluationTree testDir : testDirs) {
        if (testDir.getDirectory().getName().equals("testSimple")) {
            simpleTestDir = testDir;
            break;
        }
    }
    Set<DetectorEvaluationTree> subDirResults = simpleTestDir.getChildren();
    assertEquals(2, subDirResults.size());
    String subDirContentsName = subDirResults.iterator().next().getDirectory().getName();
    assertTrue(subDirContentsName.startsWith("subSubDir"));
}
Also used : Files(java.nio.file.Files) Predicate(java.util.function.Predicate) SystemUtils(org.apache.commons.lang3.SystemUtils) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) FileUtils(org.apache.commons.io.FileUtils) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) File(java.io.File) ArrayList(java.util.ArrayList) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) Assumptions(org.junit.jupiter.api.Assumptions) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) NotNull(org.jetbrains.annotations.NotNull) Path(java.nio.file.Path) WINDOWS(org.junit.jupiter.api.condition.OS.WINDOWS) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) File(java.io.File) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Test(org.junit.jupiter.api.Test)

Example 4 with SimpleFileFinder

use of com.synopsys.integration.common.util.finder.SimpleFileFinder in project synopsys-detect by blackducksoftware.

the class DetectorToolTest method executeToolTest.

private DetectorToolResult executeToolTest(Extraction extraction, DetectableResult extractionResult, String projectBomTool) throws DetectableException, ExecutableFailedException {
    ExtractionEnvironmentProvider extractionEnvironmentProvider = Mockito.mock(ExtractionEnvironmentProvider.class);
    DetectorFinder detectorFinder = Mockito.mock(DetectorFinder.class);
    EventSystem eventSystem = Mockito.mock(EventSystem.class);
    CodeLocationConverter codeLocationConverter = Mockito.mock(CodeLocationConverter.class);
    DetectorIssuePublisher detectorIssuePublisher = Mockito.mock(DetectorIssuePublisher.class);
    StatusEventPublisher statusEventPublisher = Mockito.mock(StatusEventPublisher.class);
    ExitCodePublisher exitCodePublisher = Mockito.mock(ExitCodePublisher.class);
    DetectorEventPublisher detectorEventPublisher = Mockito.mock(DetectorEventPublisher.class);
    DetectorTool tool = new DetectorTool(detectorFinder, extractionEnvironmentProvider, eventSystem, codeLocationConverter, detectorIssuePublisher, statusEventPublisher, exitCodePublisher, detectorEventPublisher);
    File directory = new File(".");
    GoModCliDetectable detectable = createDetectable(extraction, extractionResult);
    DetectorRule<GoModCliDetectable> rule = createRule(detectable);
    DetectorRuleSet detectorRuleSet = createRuleSet(rule);
    DetectorFinderOptions detectorFinderOptions = createFinderOptions();
    DetectorEvaluationOptions evaluationOptions = createEvaluationOptions();
    DetectorEvaluationTree evaluationTree = createEvaluationTree(extraction, extractionResult, directory, rule, detectorRuleSet);
    Mockito.when(detectorFinder.findDetectors(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Optional.of(evaluationTree));
    return tool.performDetectors(directory, detectorRuleSet, detectorFinderOptions, evaluationOptions, projectBomTool, new ArrayList<>(), new SimpleFileFinder());
}
Also used : DetectorEvaluationOptions(com.synopsys.integration.detector.evaluation.DetectorEvaluationOptions) GoModCliDetectable(com.synopsys.integration.detectable.detectables.go.gomod.GoModCliDetectable) DetectorFinderOptions(com.synopsys.integration.detector.finder.DetectorFinderOptions) ExtractionEnvironmentProvider(com.synopsys.integration.detect.tool.detector.extraction.ExtractionEnvironmentProvider) ExitCodePublisher(com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher) DetectorRuleSet(com.synopsys.integration.detector.rule.DetectorRuleSet) DetectorEvaluationTree(com.synopsys.integration.detector.base.DetectorEvaluationTree) SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) DetectorFinder(com.synopsys.integration.detector.finder.DetectorFinder) StatusEventPublisher(com.synopsys.integration.detect.workflow.status.StatusEventPublisher) EventSystem(com.synopsys.integration.detect.workflow.event.EventSystem) File(java.io.File)

Example 5 with SimpleFileFinder

use of com.synopsys.integration.common.util.finder.SimpleFileFinder in project synopsys-detect by blackducksoftware.

the class BinaryUploadOperationTest method testDirExclusion.

@Test
public void testDirExclusion() throws DetectUserFriendlyException, IOException, IntegrationException {
    SimpleFileFinder fileFinder = new SimpleFileFinder();
    DirectoryManager directoryManager = Mockito.mock(DirectoryManager.class);
    File rootDirectory = Files.createTempDirectory("BinaryScannerTest").toFile();
    createDirWithFiles(rootDirectory, "includedDir");
    createDirWithFiles(rootDirectory, "excludedDir");
    ArrayList<String> targetPaths = new ArrayList<>();
    targetPaths.add("*.txt");
    Mockito.when(directoryManager.getSourceDirectory()).thenReturn(rootDirectory);
    Mockito.when(directoryManager.getBinaryOutputDirectory()).thenReturn(rootDirectory);
    BinaryScanFindMultipleTargetsOperation multipleTargets = new BinaryScanFindMultipleTargetsOperation(fileFinder, directoryManager);
    DetectDirectoryFileFilter fileFilter = new DetectDirectoryFileFilter(Arrays.asList("excludedDir"), targetPaths);
    Optional<File> zip = multipleTargets.searchForMultipleTargets(fileFilter, false, 3);
    Assertions.assertTrue(zip.isPresent());
    List<String> entries = getZipEntries(zip);
    Assertions.assertTrue(entries.contains("includedDir/binaryTestFile_1.txt"));
    Assertions.assertFalse(entries.contains("includedDir/binaryTestFile_2.text"));
    Assertions.assertFalse(entries.contains("excludedDir/binaryTestFile_1.txt"));
    Assertions.assertFalse(entries.contains("excludedDir/binaryTestFile_2.text"));
    FileUtils.deleteDirectory(rootDirectory);
}
Also used : SimpleFileFinder(com.synopsys.integration.common.util.finder.SimpleFileFinder) DetectDirectoryFileFilter(com.synopsys.integration.detect.util.finder.DetectDirectoryFileFilter) DirectoryManager(com.synopsys.integration.detect.workflow.file.DirectoryManager) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleFileFinder (com.synopsys.integration.common.util.finder.SimpleFileFinder)11 File (java.io.File)10 Test (org.junit.jupiter.api.Test)6 DetectorRuleSet (com.synopsys.integration.detector.rule.DetectorRuleSet)4 EventSystem (com.synopsys.integration.detect.workflow.event.EventSystem)3 DetectorEvaluationTree (com.synopsys.integration.detector.base.DetectorEvaluationTree)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 ExitCodePublisher (com.synopsys.integration.detect.lifecycle.shutdown.ExitCodePublisher)2 ExtractionEnvironmentProvider (com.synopsys.integration.detect.tool.detector.extraction.ExtractionEnvironmentProvider)2 DetectDirectoryFileFilter (com.synopsys.integration.detect.util.finder.DetectDirectoryFileFilter)2 DetectExcludedDirectoryFilter (com.synopsys.integration.detect.util.finder.DetectExcludedDirectoryFilter)2 DirectoryManager (com.synopsys.integration.detect.workflow.file.DirectoryManager)2 StatusEventPublisher (com.synopsys.integration.detect.workflow.status.StatusEventPublisher)2 DetectorEvaluationOptions (com.synopsys.integration.detector.evaluation.DetectorEvaluationOptions)2 DetectorFinder (com.synopsys.integration.detector.finder.DetectorFinder)2 DetectorFinderOptions (com.synopsys.integration.detector.finder.DetectorFinderOptions)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Predicate (java.util.function.Predicate)2