-
Notifications
You must be signed in to change notification settings - Fork 303
/
TestHelpers.scala
182 lines (155 loc) · 5.36 KB
/
TestHelpers.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package coursier
import java.lang.{Boolean => JBoolean}
import coursier.params.ResolutionParams
import scala.async.Async.{async, await}
import scala.concurrent.{ExecutionContext, Future}
object TestHelpers extends PlatformTestHelpers {
implicit def ec: ExecutionContext =
cache.ec
private def validate(
name: String,
res: Resolution,
params: ResolutionParams,
extraKeyPart: String = ""
)(
result: => Seq[String]
): Future[Unit] = async {
assert(res.rootDependencies.nonEmpty)
val rootDep = res.rootDependencies.head
val isSimple = res.rootDependencies == Seq(Dependency(rootDep.module, rootDep.version, configuration = rootDep.configuration))
val attrPathPart =
if (rootDep.module.attributes.isEmpty)
""
else
"/" + rootDep.module.attributes.toVector.sorted.map {
case (k, v) => k + "_" + v
}.mkString("_")
val hashPart =
if (isSimple) ""
else {
val repr =
if (res.rootDependencies.lengthCompare(1) == 0) res.rootDependencies.head.toString
else res.rootDependencies.toString
s"_dep" + sha1(repr)
}
val paramsPart =
if (params == ResolutionParams())
""
else {
import coursier.core.Configuration
// hack not to have to edit / review lots of test fixtures
val params0 =
if (params.defaultConfiguration == Configuration.defaultCompile)
params.withDefaultConfiguration(Configuration.compile)
else if (params.defaultConfiguration == Configuration.compile)
params.withDefaultConfiguration(Configuration("really-compile"))
else
params
// This avoids some sha1 changes
def normalize(s: String): String = {
val noComma = s.replaceAllLiterally(", ", "||")
noComma
.replaceAllLiterally("|None|", "")
.replaceAllLiterally("|List()|", "")
.replaceAllLiterally("|Map()|", "")
.replaceAllLiterally("HashSet", "Set")
.replaceAllLiterally("|Set()|", "")
}
val n = normalize(params0.toString)
"_params" + sha1(n)
}
val path = Seq(
s"modules/tests/shared/src/test/resources/$name",
rootDep.module.organization.value,
rootDep.module.name.value,
attrPathPart,
rootDep.version + (
if (rootDep.configuration.isEmpty)
""
else
"_" + rootDep.configuration.value.replace('(', '_').replace(')', '_')
) + hashPart + paramsPart + extraKeyPart
).filter(_.nonEmpty).mkString("/")
def tryRead = textResource(path)
val result0 = result.map { r =>
r.replace(handmadeMetadataBase, "file:///handmade-metadata/")
}
val expected =
await(
tryRead.recoverWith {
case _: Exception if writeMockData =>
maybeWriteTextResource(path, result0.mkString("\n"))
tryRead
}
).split('\n').toSeq.filter(_.nonEmpty)
for (((e, r), idx) <- expected.zip(result0).zipWithIndex if e != r)
println(s"Line ${idx + 1}:\n expected: $e\n got: $r")
assert(result0 == expected)
}
def dependenciesWithRetainedVersion(res: Resolution): Seq[Dependency] =
res.orderedDependencies.map { dep =>
val version = res.projectCache
.get(dep.moduleVersion)
.map(_._2.actualVersion)
.orElse {
res.reconciledVersions.get(dep.module)
}
.getOrElse {
System.err.println(s"Project not found for ${dep.module.repr}:${dep.version}")
for ((mod, v) <- res.projectCache.keys.toVector.sortBy(_.toString()))
System.err.println(s" ${mod.repr}:$v")
dep.version
}
dep.withVersion(version)
}
def validateDependencies(res: Resolution, params: ResolutionParams = ResolutionParams()): Future[Unit] =
validate("resolutions", res, params) {
val elems = dependenciesWithRetainedVersion(res)
.map { dep =>
(dep.module.organization.value, dep.module.nameWithAttributes, dep.version, dep.configuration.value)
}
elems
.map {
case (org, name, ver, cfg) =>
Seq(org, name, ver, cfg).mkString(":")
}
}
def versionOf(res: Resolution, mod: Module): Option[String] =
res
.minDependencies
.collectFirst {
case dep if dep.module == mod =>
res
.projectCache
.get(dep.moduleVersion)
.fold(dep.version)(_._2.actualVersion)
}
def validateArtifacts(
res: Resolution,
artifacts: Seq[Artifact],
params: ResolutionParams = ResolutionParams(),
classifiers: Set[Classifier] = Set(),
mainArtifacts: JBoolean = null,
artifactTypes: Set[Type] = Set(),
extraKeyPart: String = ""
): Future[Unit] = {
val classifiersPart =
if (classifiers.isEmpty)
""
else
"_classifiers_" + sha1(classifiers.toVector.sorted.mkString("|"))
val mainArtifactsPart =
Option(mainArtifacts) match {
case None => ""
case Some(b) => s"_main_$b"
}
val artifactTypesPart =
if (artifactTypes.isEmpty)
""
else
"_types_" + sha1(artifactTypes.toVector.sorted.mkString("|"))
validate("artifacts", res, params, mainArtifactsPart + classifiersPart + artifactTypesPart + extraKeyPart) {
artifacts.map(_.url)
}
}
}