forked from apple/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDLoadingTests.swift
242 lines (218 loc) · 8.23 KB
/
PDLoadingTests.swift
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Basics
import PackageLoading
import PackageModel
import SPMTestSupport
import TSCBasic
import TSCUtility
import XCTest
class PackageDescriptionLoadingTests: XCTestCase, ManifestLoaderDelegate {
lazy var manifestLoader = ManifestLoader(toolchain: ToolchainConfiguration.default, delegate: self)
var parsedManifest : AbsolutePath?
public func willLoad(manifest: AbsolutePath) {
}
public func willParse(manifest: AbsolutePath) {
parsedManifest = manifest
}
var toolsVersion: ToolsVersion {
fatalError("implement in subclass")
}
func loadManifestThrowing(
_ contents: String,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
file: StaticString = #file,
line: UInt = #line,
body: (Manifest) -> Void
) throws {
try self.loadManifestThrowing(ByteString(encodingAsUTF8: contents),
toolsVersion: toolsVersion,
packageKind: packageKind,
file: file,
line: line,
body: body)
}
// TODO: deprecate in favor of String version
func loadManifestThrowing(
_ contents: ByteString,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
file: StaticString = #file,
line: UInt = #line,
body: (Manifest) -> Void
) throws {
let packageKind = packageKind ?? .fileSystem(.root)
let packagePath: AbsolutePath
switch packageKind {
case .root(let path):
packagePath = path
case .fileSystem(let path):
packagePath = path
case .localSourceControl(let path):
packagePath = path
case .remoteSourceControl, .registry:
throw InternalError("invalid package kind \(packageKind)")
}
let toolsVersion = toolsVersion ?? self.toolsVersion
let fs = InMemoryFileSystem()
let manifestPath = packagePath.appending(component: Manifest.filename)
try fs.writeFileContents(manifestPath, bytes: contents)
let m = try manifestLoader.load(
at: packagePath,
packageKind: packageKind,
toolsVersion: toolsVersion,
fileSystem: fs)
guard m.toolsVersion == toolsVersion else {
return XCTFail("Invalid manifest version", file: file, line: line)
}
body(m)
}
func loadManifest(
_ contents: String,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
line: UInt = #line,
body: (Manifest) -> Void
) {
self.loadManifest(ByteString(encodingAsUTF8: contents),
toolsVersion: toolsVersion,
packageKind: packageKind,
line: line,
body: body)
}
// TODO: deprecate in favor of String version
func loadManifest(
_ contents: ByteString,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
line: UInt = #line,
body: (Manifest) -> Void
) {
do {
let toolsVersion = toolsVersion ?? self.toolsVersion
try loadManifestThrowing(
contents,
toolsVersion: toolsVersion,
packageKind: packageKind,
line: line,
body: body
)
} catch ManifestParseError.invalidManifestFormat(let error, _) {
print(error)
XCTFail(file: #file, line: line)
} catch {
XCTFail("Unexpected error: \(error)", file: #file, line: line)
}
}
func XCTAssertManifestLoadNoThrows(
_ contents: String,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
file: StaticString = #file,
line: UInt = #line,
onSuccess: ((Manifest, DiagnosticsTestResult) -> Void)? = nil
) {
let observability = ObservabilitySystem.makeForTesting()
do {
let manifest = try loadManifest(
contents,
toolsVersion: toolsVersion ?? self.toolsVersion,
packageKind: packageKind,
diagnostics: observability.topScope.makeDiagnosticsEngine(),
file: file,
line: line)
if let onSuccess = onSuccess {
testDiagnostics(observability.diagnostics) { result in
onSuccess(manifest, result)
}
}
} catch {
XCTFail("Unexpected error: \(error)", file: file, line: line)
}
}
func XCTAssertManifestLoadThrows(
_ contents: String,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
file: StaticString = #file,
line: UInt = #line,
onCatch: ((Error, DiagnosticsTestResult) -> Void)? = nil
) {
let observability = ObservabilitySystem.makeForTesting()
do {
let manifest = try loadManifest(
contents,
toolsVersion: toolsVersion ?? self.toolsVersion,
packageKind: packageKind,
diagnostics: observability.topScope.makeDiagnosticsEngine(),
file: file,
line: line)
XCTFail("Unexpected success: \(manifest)", file: file, line: line)
} catch {
if let onCatch = onCatch {
testDiagnostics(observability.diagnostics, file: file, line: line) { result in
onCatch(error, result)
}
}
}
}
func XCTAssertManifestLoadThrows<E: Error & Equatable>(
_ expectedError: E,
_ contents: String,
toolsVersion: ToolsVersion? = nil,
packageKind: PackageReference.Kind? = nil,
file: StaticString = #file,
line: UInt = #line,
onCatch: ((DiagnosticsTestResult) -> Void)? = nil
) {
XCTAssertManifestLoadThrows(contents, toolsVersion: toolsVersion, file: file, line: line) { error, result in
if let typedError = error as? E, typedError == expectedError {
// Everything okay
} else {
XCTFail("Unexpected error: \(error)", file: file, line: line)
}
onCatch?(result)
}
}
func loadManifest(
_ contents: String,
toolsVersion: ToolsVersion?,
packageKind: PackageReference.Kind? = nil,
diagnostics: DiagnosticsEngine?,
file: StaticString = #file,
line: UInt = #line
) throws -> Manifest {
let packageKind = packageKind ?? .fileSystem(.root)
let packagePath: AbsolutePath
switch packageKind {
case .root(let path):
packagePath = path
case .fileSystem(let path):
packagePath = path
case .localSourceControl(let path):
packagePath = path
case .remoteSourceControl, .registry:
throw InternalError("invalid package kind \(packageKind)")
}
let toolsVersion = toolsVersion ?? self.toolsVersion
let fileSystem = InMemoryFileSystem()
let manifestPath = packagePath.appending(component: Manifest.filename)
try fileSystem.writeFileContents(manifestPath, bytes: ByteString(encodingAsUTF8: contents))
let manifest = try manifestLoader.load(
at: packagePath,
packageKind: packageKind,
toolsVersion: toolsVersion,
fileSystem: fileSystem,
diagnostics: diagnostics)
if manifest.toolsVersion != toolsVersion {
XCTFail("Invalid manifest version", file: file, line: line)
}
return manifest
}
}