Skip to content

Commit 3040ea4

Browse files
authored
Add Nuget Exclude Dependency Type Option (#1032)
* Add New Nuget Exclude Dependency Property in Detect * Uncomment integration tag * Refactored Dockerfile name and version * Add assert examples in test
1 parent 92ef07b commit 3040ea4

File tree

9 files changed

+99
-4
lines changed

9 files changed

+99
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.synopsys.integration.detectable.detectables.nuget;
2+
3+
public enum NugetDependencyType {
4+
DEV
5+
}

detectable/src/main/java/com/synopsys/integration/detectable/detectables/nuget/NugetInspectorArguments.java

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.Arrays;
77
import java.util.List;
8+
import java.util.Set;
89

910
import org.apache.commons.lang3.StringUtils;
1011
import org.slf4j.Logger;
@@ -37,6 +38,10 @@ public static List<String> fromInspectorOptions(NugetInspectorOptions nugetInspe
3738
nugetInspectorOptions.getNugetConfigPath()
3839
.ifPresent(arg -> options.add("--nuget_config_path=" + arg.toString()));
3940

41+
if(!nugetInspectorOptions.getNugetExcludedDependencyTypes().isEmpty()){
42+
options.add("--excluded_dependency_types=" + toCommaSeparatedStringSet(nugetInspectorOptions.getNugetExcludedDependencyTypes()));
43+
}
44+
4045
if (logger.isTraceEnabled()) {
4146
options.add("-v");
4247
}
@@ -47,4 +52,8 @@ public static List<String> fromInspectorOptions(NugetInspectorOptions nugetInspe
4752
private static String toCommaSeparatedString(List<String> list) {
4853
return StringUtils.joinWith(",", list.toArray());
4954
}
55+
56+
private static String toCommaSeparatedStringSet(Set<NugetDependencyType> list) {
57+
return StringUtils.joinWith(",", list.toArray());
58+
}
5059
}

detectable/src/main/java/com/synopsys/integration/detectable/detectables/nuget/NugetInspectorOptions.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.nio.file.Path;
44
import java.util.List;
55
import java.util.Optional;
6+
import java.util.Set;
67

78
import org.jetbrains.annotations.Nullable;
89

@@ -12,13 +13,15 @@ public class NugetInspectorOptions {
1213
private final List<String> includedModules;
1314
private final List<String> packagesRepoUrl;
1415
private final Path nugetConfigPath;
16+
private final Set<NugetDependencyType> nugetExcludedDependencyTypes;
1517

16-
public NugetInspectorOptions(boolean ignoreFailures, List<String> excludedModules, List<String> includedModules, List<String> packagesRepoUrl, @Nullable Path nugetConfigPath) {
18+
public NugetInspectorOptions(boolean ignoreFailures, List<String> excludedModules, List<String> includedModules, List<String> packagesRepoUrl, @Nullable Path nugetConfigPath, Set<NugetDependencyType> nugetExcludedDependencyTypes) {
1719
this.ignoreFailures = ignoreFailures;
1820
this.excludedModules = excludedModules;
1921
this.includedModules = includedModules;
2022
this.packagesRepoUrl = packagesRepoUrl;
2123
this.nugetConfigPath = nugetConfigPath;
24+
this.nugetExcludedDependencyTypes = nugetExcludedDependencyTypes;
2225
}
2326

2427
public boolean isIgnoreFailures() {
@@ -40,4 +43,6 @@ public List<String> getPackagesRepoUrl() {
4043
public Optional<Path> getNugetConfigPath() {
4144
return Optional.ofNullable(nugetConfigPath);
4245
}
46+
47+
public Set<NugetDependencyType> getNugetExcludedDependencyTypes() { return nugetExcludedDependencyTypes; }
4348
}

src/main/java/com/synopsys/integration/detect/configuration/DetectProperties.java

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.synopsys.integration.detectable.detectables.gradle.inspection.GradleConfigurationType;
5959
import com.synopsys.integration.detectable.detectables.lerna.LernaPackageType;
6060
import com.synopsys.integration.detectable.detectables.npm.NpmDependencyType;
61+
import com.synopsys.integration.detectable.detectables.nuget.NugetDependencyType;
6162
import com.synopsys.integration.detectable.detectables.packagist.PackagistDependencyType;
6263
import com.synopsys.integration.detectable.detectables.pear.PearDependencyType;
6364
import com.synopsys.integration.detectable.detectables.pipenv.parse.PipenvDependencyType;
@@ -1156,6 +1157,14 @@ private DetectProperties() {
11561157
.setGroups(DetectGroup.NUGET, DetectGroup.GLOBAL)
11571158
.build();
11581159

1160+
public static final NoneEnumListProperty<NugetDependencyType> DETECT_NUGET_DEPENDENCY_TYPES_EXCLUDED =
1161+
NoneEnumListProperty.newBuilder("detect.nuget.dependency.types.excluded", NoneEnum.NONE, NugetDependencyType.class)
1162+
.setInfo("Nuget Dependency Types Excluded", DetectPropertyFromVersion.VERSION_9_4_0)
1163+
.setHelp(createTypeFilterHelpText("Nuget dependency types"))
1164+
.setExample(String.format("%s", NugetDependencyType.DEV.name()))
1165+
.setGroups(DetectGroup.NUGET, DetectGroup.GLOBAL, DetectGroup.SOURCE_SCAN)
1166+
.build();
1167+
11591168
public static final NullablePathProperty DETECT_OUTPUT_PATH =
11601169
NullablePathProperty.newBuilder("detect.output.path")
11611170
.setInfo("Detect Output Path", DetectPropertyFromVersion.VERSION_3_0_0)

src/main/java/com/synopsys/integration/detect/configuration/DetectPropertyFromVersion.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public enum DetectPropertyFromVersion implements PropertyVersion {
4444
VERSION_8_5_0("8.5.0"),
4545
VERSION_8_8_0("8.8.0"),
4646
VERSION_8_11_0("8.11.0"),
47-
VERSION_9_1_0("9.1.0");
47+
VERSION_9_1_0("9.1.0"),
48+
VERSION_9_4_0("9.4.0");
4849

4950
private final String version;
5051

src/main/java/com/synopsys/integration/detect/configuration/DetectableOptionFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Map;
66
import java.util.Set;
77

8+
import com.synopsys.integration.detectable.detectables.nuget.NugetDependencyType;
89
import org.jetbrains.annotations.Nullable;
910

1011
import com.synopsys.integration.detect.workflow.ArtifactoryConstants;
@@ -287,7 +288,8 @@ public NugetInspectorOptions createNugetInspectorOptions() {
287288
List<String> includedModules = detectConfiguration.getValue(DetectProperties.DETECT_NUGET_INCLUDED_MODULES);
288289
List<String> packagesRepoUrl = detectConfiguration.getValue(DetectProperties.DETECT_NUGET_PACKAGES_REPO_URL);
289290
Path nugetConfigPath = detectConfiguration.getPathOrNull(DetectProperties.DETECT_NUGET_CONFIG_PATH);
290-
return new NugetInspectorOptions(ignoreFailures, excludedModules, includedModules, packagesRepoUrl, nugetConfigPath);
291+
Set<NugetDependencyType> nugetExcludedDependencyTypes = detectConfiguration.getValue(DetectProperties.DETECT_NUGET_DEPENDENCY_TYPES_EXCLUDED).representedValueSet();
292+
return new NugetInspectorOptions(ignoreFailures, excludedModules, includedModules, packagesRepoUrl, nugetConfigPath, nugetExcludedDependencyTypes);
291293
}
292294

293295
private boolean getFollowSymLinks() {

src/test/java/com/synopsys/integration/detect/battery/docker/NugetInspectorTests.java

+30
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,34 @@ void nugetMultiplePropsFileTest() throws IOException, IntegrationException {
8080
blackduckAssertions.hasComponents("Microsoft.UI.Xaml");
8181
}
8282
}
83+
84+
@Test
85+
void nugetExcludeDevDependencyTest() throws IOException, IntegrationException {
86+
try(DetectDockerTestRunner test = new DetectDockerTestRunner("detect-nuget-inspector-exclude-dependency","detect-dotnet-seven:1.0.3")) {
87+
test.withImageProvider(BuildDockerImageProvider.forDockerfilResourceNamed("Nuget_ExcludeDevDependency.dockerfile"));
88+
89+
String projectVersion = PROJECT_NAME + "-exclude_dev_dependency";
90+
BlackDuckTestConnection blackDuckTestConnection = BlackDuckTestConnection.fromEnvironment();
91+
BlackDuckAssertions blackduckAssertions = blackDuckTestConnection.projectVersionAssertions(PROJECT_NAME, projectVersion);
92+
blackduckAssertions.emptyOnBlackDuck();
93+
94+
DetectCommandBuilder commandBuilder = new DetectCommandBuilder().defaults().defaultDirectories(test);
95+
commandBuilder.connectToBlackDuck(blackDuckTestConnection);
96+
commandBuilder.projectNameVersion(blackduckAssertions);
97+
commandBuilder.waitForResults();
98+
99+
commandBuilder.property(DetectProperties.DETECT_TOOLS, "DETECTOR");
100+
commandBuilder.property(DetectProperties.DETECT_INCLUDED_DETECTOR_TYPES, DetectorType.NUGET.toString());
101+
commandBuilder.property(DetectProperties.DETECT_NUGET_DEPENDENCY_TYPES_EXCLUDED,"DEV");
102+
DockerAssertions dockerAssertions = test.run(commandBuilder);
103+
104+
dockerAssertions.logContains("NuGet Solution Native Inspector: SUCCESS");
105+
dockerAssertions.atLeastOneBdioFile();
106+
107+
blackduckAssertions.doesNotHaveComponents("Microsoft.CodeAnalysis.NetAnalyzers");
108+
blackduckAssertions.doesNotHaveComponents("Microsoft.Windows.CsWin32");
109+
blackduckAssertions.hasComponents("CommunityToolkit.WinUI.Animations");
110+
blackduckAssertions.hasComponents("Microsoft.Extensions.Logging");
111+
}
112+
}
83113
}

src/test/java/com/synopsys/integration/detect/battery/docker/integration/BlackDuckAssertions.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
66
import java.util.List;
77
import java.util.Optional;
88
import java.util.Set;
@@ -113,6 +113,20 @@ public void hasComponents(Set<String> componentNames) throws IntegrationExceptio
113113
});
114114
}
115115

116+
public void doesNotHaveComponents(String... componentNames) throws IntegrationException {
117+
doesNotHaveComponents(Bds.of(componentNames).toSet());
118+
}
119+
120+
public void doesNotHaveComponents(Set<String> componentNames) throws IntegrationException {
121+
List<ProjectVersionComponentVersionView> bomComponents = projectBomService.getComponentsForProjectVersion(retrieveProjectVersionWrapper().getProjectVersionView());
122+
componentNames.forEach(componentName -> {
123+
Optional<ProjectVersionComponentVersionView> blackDuckCommonComponent = bomComponents.stream()
124+
.filter(ProjectVersionComponentView -> componentName.equals(ProjectVersionComponentView.getComponentName()))
125+
.findFirst();
126+
assertFalse(blackDuckCommonComponent.isPresent());
127+
});
128+
}
129+
116130
public NameVersion getProjectNameVersion() {
117131
return projectNameVersion;
118132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM mcr.microsoft.com/dotnet/sdk:7.0
2+
3+
ENV SRC_DIR=/opt/project/src
4+
5+
RUN apt-get update
6+
7+
# Install java
8+
RUN mkdir -p /usr/share/man/man1/
9+
# Above due to: https://github.com/geerlingguy/ansible-role-java/issues/64
10+
RUN apt-get install -y openjdk-11-jre
11+
12+
# Install git
13+
RUN apt-get install -y git
14+
15+
RUN mkdir -p air-gap-docker
16+
17+
# Set up the test project
18+
RUN mkdir -p ${SRC_DIR}
19+
WORKDIR ${SRC_DIR}
20+
RUN git clone --depth 1 https://github.com/microsoft/PowerToys.git ${SRC_DIR}

0 commit comments

Comments
 (0)