Skip to content

Commit bb9f295

Browse files
committed
feat(ort-utils): Support merging non-conflicting Environments
Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 4749254 commit bb9f295

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

model/src/main/kotlin/ScannerRun.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ data class ScannerRun(
298298
/**
299299
* Merge this [ScannerRun] with the given [other] [ScannerRun].
300300
*
301-
* Both [ScannerRun]s must have the same [environment] and [config], otherwise an [IllegalArgumentException] is
302-
* thrown.
301+
* Both [ScannerRun]s must have the same [config] and non-conflicting [environment], otherwise an
302+
* [IllegalArgumentException] is thrown.
303303
*
304304
* files and scanResults are merged by their [KnownProvenance]s, while issues and scanners are merged by their
305305
* names.
@@ -324,11 +324,7 @@ data class ScannerRun(
324324
return ScannerRun(
325325
startTime = minOf(startTime, other.startTime),
326326
endTime = maxOf(endTime, other.endTime),
327-
environment = environment.also {
328-
require(it == other.environment) {
329-
"Cannot merge ScannerRuns with different environments: $it != ${other.environment}."
330-
}
331-
},
327+
environment = environment + other.environment,
332328
config = config.also {
333329
require(it == other.config) {
334330
"Cannot merge ScannerRuns with different configurations: $it != ${other.config}."

model/src/test/kotlin/ScannerRunTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class ScannerRunTest : WordSpec({
318318
}.message shouldBe "Cannot merge ScannerRuns with different configurations: $config1 != $config2."
319319
}
320320

321-
"not merge runs with different environments" {
321+
"not merge runs with conflicting environments" {
322322
val env1 = Environment("1.0.0", "x86_64")
323323
val env2 = Environment("2.0.0", "x86_64")
324324

@@ -332,7 +332,8 @@ class ScannerRunTest : WordSpec({
332332

333333
shouldThrow<IllegalArgumentException> {
334334
run1 + run2
335-
}.message shouldBe "Cannot merge ScannerRuns with different environments: $env1 != $env2."
335+
}.message shouldBe
336+
"Cannot merge Environments with different ORT versions: '${env1.ortVersion}' != '${env2.ortVersion}'."
336337
}
337338

338339
"combine start and end time" {

utils/ort/src/main/kotlin/Environment.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,64 @@ data class Environment(
112112
"GOPATH"
113113
)
114114
}
115+
116+
/**
117+
* Merge this [Environment] with the given [other] [Environment].
118+
*
119+
* Both [Environment]s must have the same properties except for [variables] and [toolVersions], otherwise an
120+
* [IllegalArgumentException] is thrown.
121+
*/
122+
operator fun plus(other: Environment) =
123+
Environment(
124+
ortVersion = ortVersion.also {
125+
require(it == other.ortVersion) {
126+
"Cannot merge Environments with different ORT versions: '$ortVersion' != '${other.ortVersion}'."
127+
}
128+
},
129+
buildJdk = buildJdk.also {
130+
require(it == other.buildJdk) {
131+
"Cannot merge Environments with different build JDKs: '$buildJdk' != '${other.buildJdk}'."
132+
}
133+
},
134+
javaVersion = javaVersion.also {
135+
require(it == other.javaVersion) {
136+
"Cannot merge Environments with different Java versions: '$javaVersion' != '${other.javaVersion}'."
137+
}
138+
},
139+
os = os.also {
140+
require(it == other.os) {
141+
"Cannot merge Environments with different operating systems: '$os' != '${other.os}'."
142+
}
143+
},
144+
processors = processors.also {
145+
require(it == other.processors) {
146+
"Cannot merge Environments with different processor counts: '$processors' != '${other.processors}'."
147+
}
148+
},
149+
maxMemory = maxMemory.also {
150+
require(it == other.maxMemory) {
151+
"Cannot merge Environments with different memory amounts: '$maxMemory' != '${other.maxMemory}'."
152+
}
153+
},
154+
variables = run {
155+
val keyIntersection by lazy { variables.keys.intersect(other.variables.keys) }
156+
157+
require(variables == other.variables || keyIntersection.isEmpty()) {
158+
"Cannot merge Environments with conflicting variable keys: $keyIntersection"
159+
}
160+
161+
variables + other.variables
162+
},
163+
toolVersions = run {
164+
val keyIntersection by lazy { toolVersions.keys.intersect(other.toolVersions.keys) }
165+
166+
require(toolVersions == other.toolVersions || keyIntersection.isEmpty()) {
167+
"Cannot merge Environments with conflicting tool versions keys: $keyIntersection"
168+
}
169+
170+
toolVersions + other.toolVersions
171+
}
172+
)
115173
}
116174

117175
/**

0 commit comments

Comments
 (0)