Skip to content

Commit c023700

Browse files
committed
feat(Gradle): Use the correctly initialized DependencyGraphBuilder
Obtain the DependencyGraphBuilder via the function provided by PackageManager to make sure that the exclusions are correctly initialized. Add a fun test to verify this. Signed-off-by: Oliver Heger <[email protected]>
1 parent 09a15a5 commit c023700

File tree

3 files changed

+211
-4
lines changed

3 files changed

+211
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
project:
3+
id: "Gradle:org.ossreviewtoolkit.gradle.example:app:1.0.0"
4+
definition_file_path: "analyzer/src/funTest/assets/projects/synthetic/gradle/app/build.gradle"
5+
declared_licenses: []
6+
declared_licenses_processed: {}
7+
vcs:
8+
type: ""
9+
url: ""
10+
revision: ""
11+
path: ""
12+
vcs_processed:
13+
type: "Git"
14+
url: "<REPLACE_URL>"
15+
revision: "<REPLACE_REVISION>"
16+
path: "analyzer/src/funTest/assets/projects/synthetic/gradle/app"
17+
homepage_url: ""
18+
scopes:
19+
- name: "compileClasspath"
20+
dependencies:
21+
- id: "Gradle:org.ossreviewtoolkit.gradle.example:lib:1.0.0"
22+
linkage: "PROJECT_DYNAMIC"
23+
- name: "runtimeClasspath"
24+
dependencies:
25+
- id: "Gradle:org.ossreviewtoolkit.gradle.example:lib:1.0.0"
26+
linkage: "PROJECT_DYNAMIC"
27+
dependencies:
28+
- id: "Maven:org.apache.commons:commons-text:1.1"
29+
dependencies:
30+
- id: "Maven:org.apache.commons:commons-lang3:3.5"
31+
- id: "Maven:org.apache.struts:struts2-assembly:2.5.14.1"
32+
packages:
33+
- id: "Maven:org.apache.commons:commons-lang3:3.5"
34+
purl: "pkg:maven/org.apache.commons/[email protected]"
35+
authors:
36+
- "Benedikt Ritter"
37+
- "Carman Consulting, Inc."
38+
- "CollabNet, Inc."
39+
- "Duncan Jones"
40+
- "Fredrik Westermarck"
41+
- "Gary D. Gregory"
42+
- "Henri Yandell"
43+
- "Joerg Schaible"
44+
- "Loic Guibert"
45+
- "Matt Benson"
46+
- "Niall Pemberton"
47+
- "Oliver Heger"
48+
- "Paul Benedict"
49+
- "Rob Tompkins"
50+
- "Robert Burrell Donkin"
51+
- "SITA ATS Ltd"
52+
- "Steven Caswell"
53+
- "The Apache Software Foundation"
54+
declared_licenses:
55+
- "Apache License, Version 2.0"
56+
declared_licenses_processed:
57+
spdx_expression: "Apache-2.0"
58+
mapped:
59+
Apache License, Version 2.0: "Apache-2.0"
60+
description: "Apache Commons Lang, a package of Java utility classes for the\n \
61+
\ classes that are in java.lang's hierarchy, or are considered to be so\n standard\
62+
\ as to justify existence in java.lang."
63+
homepage_url: "http://commons.apache.org/proper/commons-lang/"
64+
binary_artifact:
65+
url: "https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar"
66+
hash:
67+
value: "6c6c702c89bfff3cd9e80b04d668c5e190d588c6"
68+
algorithm: "SHA-1"
69+
source_artifact:
70+
url: "https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5-sources.jar"
71+
hash:
72+
value: "f7d878153e86a1cdddf6b37850e00a9f8bff726f"
73+
algorithm: "SHA-1"
74+
vcs:
75+
type: "Git"
76+
url: "http://git-wip-us.apache.org/repos/asf/commons-lang.git"
77+
revision: "LANG_3_5"
78+
path: ""
79+
vcs_processed:
80+
type: "Git"
81+
url: "http://git-wip-us.apache.org/repos/asf/commons-lang.git"
82+
revision: "LANG_3_5"
83+
path: ""
84+
- id: "Maven:org.apache.commons:commons-text:1.1"
85+
purl: "pkg:maven/org.apache.commons/[email protected]"
86+
authors:
87+
- "Benedikt Ritter"
88+
- "Bruno P. Kinoshita"
89+
- "Gary Gregory"
90+
- "Rob Tompkins"
91+
- "The Apache Software Foundation"
92+
declared_licenses:
93+
- "Apache License, Version 2.0"
94+
declared_licenses_processed:
95+
spdx_expression: "Apache-2.0"
96+
mapped:
97+
Apache License, Version 2.0: "Apache-2.0"
98+
description: "Apache Commons Text is a library focused on algorithms working on\
99+
\ strings."
100+
homepage_url: "http://commons.apache.org/proper/commons-text/"
101+
binary_artifact:
102+
url: "https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.1/commons-text-1.1.jar"
103+
hash:
104+
value: "c336bf600f44b88af356c8a85eef4af822b06a4d"
105+
algorithm: "SHA-1"
106+
source_artifact:
107+
url: "https://repo.maven.apache.org/maven2/org/apache/commons/commons-text/1.1/commons-text-1.1-sources.jar"
108+
hash:
109+
value: "f0770f7f0472bf120ada47beecadce4056fbd20a"
110+
algorithm: "SHA-1"
111+
vcs:
112+
type: "Git"
113+
url: "http://git-wip-us.apache.org/repos/asf/commons-text.git"
114+
revision: ""
115+
path: ""
116+
vcs_processed:
117+
type: "Git"
118+
url: "http://git-wip-us.apache.org/repos/asf/commons-text.git"
119+
revision: ""
120+
path: ""
121+
- id: "Maven:org.apache.struts:struts2-assembly:2.5.14.1"
122+
purl: "pkg:maven/org.apache.struts/[email protected]"
123+
authors:
124+
- "Aleksandr Mashchenko"
125+
- "Alexandru Popescu"
126+
- "Apache Software Foundation"
127+
- "Bob Lee"
128+
- "Bruce A. Phillips"
129+
- "Cedric Dumoulin"
130+
- "Christian Grobmeier"
131+
- "Christoph Nenning"
132+
- "Dave Newton"
133+
- "David H. DeWolf"
134+
- "Don Brown"
135+
- "Greg Huber"
136+
- "James Holmes"
137+
- "James Mitchell"
138+
- "Jeromy Evans"
139+
- "Johannes Geppert"
140+
- "John Lindal"
141+
- "Laurie Harper"
142+
- "Lukasz Lenart"
143+
- "Martin Cooper"
144+
- "Mathias Bogaert"
145+
- "Matt Raible"
146+
- "Maurizio Cucchiara"
147+
- "Michael Jouravlev"
148+
- "Niall Pemberton"
149+
- "Nils-Helge Garli Hegvik"
150+
- "Paul Benedict"
151+
- "Rainer Hermanns"
152+
- "Rene Gielen"
153+
- "Ted Husted"
154+
- "Toby Jee"
155+
- "Wendy Smoak"
156+
- "Wes Wannemacher"
157+
declared_licenses:
158+
- "The Apache Software License, Version 2.0"
159+
declared_licenses_processed:
160+
spdx_expression: "Apache-2.0"
161+
mapped:
162+
The Apache Software License, Version 2.0: "Apache-2.0"
163+
description: "Apache Struts 2"
164+
homepage_url: "http://struts.apache.org/struts2-assembly/"
165+
binary_artifact:
166+
url: "https://repo.maven.apache.org/maven2/org/apache/struts/struts2-assembly/2.5.14.1/struts2-assembly-2.5.14.1-min-lib.zip"
167+
hash:
168+
value: "8e75a38e3b8ceb01e007c5899d8d29e7a075cb7d"
169+
algorithm: "SHA-1"
170+
source_artifact:
171+
url: ""
172+
hash:
173+
value: ""
174+
algorithm: ""
175+
vcs:
176+
type: "Git"
177+
url: "https://gitbox.apache.org/repos/asf/struts.git"
178+
revision: "STRUTS_2_5_14_1"
179+
path: ""
180+
vcs_processed:
181+
type: "Git"
182+
url: "https://gitbox.apache.org/repos/asf/struts.git"
183+
revision: "STRUTS_2_5_14_1"
184+
path: ""
185+
is_metadata_only: true

analyzer/src/funTest/kotlin/managers/GradleFunTest.kt

+25-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import java.io.File
3232
import org.ossreviewtoolkit.downloader.VersionControlSystem
3333
import org.ossreviewtoolkit.downloader.vcs.Git
3434
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
35+
import org.ossreviewtoolkit.model.config.Excludes
3536
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
37+
import org.ossreviewtoolkit.model.config.ScopeExclude
38+
import org.ossreviewtoolkit.model.config.ScopeExcludeReason
3639
import org.ossreviewtoolkit.utils.common.Os
3740
import org.ossreviewtoolkit.utils.common.ProcessCapture
3841
import org.ossreviewtoolkit.utils.ort.normalizeVcsUrl
@@ -107,6 +110,24 @@ class GradleFunTest : StringSpec() {
107110
patchActualResult(result.toYaml()) shouldBe expectedResult
108111
}
109112

113+
"Scopes are correctly excluded from the dependency graph" {
114+
val definitionFile = projectDir.resolve("app/build.gradle")
115+
val expectedResult = patchExpectedResult(
116+
projectDir.resolveSibling("gradle-expected-output-scopes-excludes.yml"),
117+
url = normalizeVcsUrl(vcsUrl),
118+
revision = vcsRevision
119+
)
120+
121+
val analyzerConfig = AnalyzerConfiguration(skipExcludedScopesInDependencyGraph = true)
122+
val scopeExclude = ScopeExclude("test.*", ScopeExcludeReason.TEST_DEPENDENCY_OF)
123+
val repoConfig = RepositoryConfiguration(excludes = Excludes(scopes = listOf(scopeExclude)))
124+
125+
val result = createGradle(analyzerConfig, repoConfig)
126+
.resolveSingleProject(definitionFile, resolveScopes = true)
127+
128+
result.toYaml() shouldBe expectedResult
129+
}
130+
110131
// Disabled because despite following the example at [1] Gradle says there is "No service of type
111132
// ToolingModelBuilderRegistry available in GradleScopeServices".
112133
//
@@ -199,6 +220,8 @@ class GradleFunTest : StringSpec() {
199220
.requireSuccess()
200221
}
201222

202-
private fun createGradle() =
203-
Gradle("Gradle", USER_DIR, AnalyzerConfiguration(), RepositoryConfiguration())
223+
private fun createGradle(
224+
analyzerConfig: AnalyzerConfiguration = AnalyzerConfiguration(),
225+
repoConfig: RepositoryConfiguration = RepositoryConfiguration()
226+
) = Gradle("Gradle", USER_DIR, analyzerConfig, repoConfig)
204227
}

analyzer/src/main/kotlin/managers/Gradle.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import org.ossreviewtoolkit.model.VcsInfo
5454
import org.ossreviewtoolkit.model.config.AnalyzerConfiguration
5555
import org.ossreviewtoolkit.model.config.RepositoryConfiguration
5656
import org.ossreviewtoolkit.model.createAndLogIssue
57-
import org.ossreviewtoolkit.model.utils.DependencyGraphBuilder
5857
import org.ossreviewtoolkit.utils.common.Os
5958
import org.ossreviewtoolkit.utils.common.splitOnWhitespace
6059
import org.ossreviewtoolkit.utils.common.temporaryProperties
@@ -135,7 +134,7 @@ class Gradle(
135134

136135
private val maven = MavenSupport(GradleCacheReader())
137136
private val dependencyHandler = GradleDependencyHandler(managerName, maven)
138-
private val graphBuilder = DependencyGraphBuilder(dependencyHandler)
137+
private val graphBuilder = createDependencyGraphBuilder(dependencyHandler)
139138

140139
// The path to the root project. In a single-project, just points to the project path.
141140
private lateinit var rootProjectDir: File

0 commit comments

Comments
 (0)