Skip to content

Commit de29f55

Browse files
authored
Merge pull request #2624 from aciidb0mb3r/relative-path
[PackageLoading] Validate paths in target description
2 parents 6d47bda + 93c2bfb commit de29f55

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

Sources/PackageLoading/PackageDescription4Loader.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@ extension ManifestBuilder {
121121
.getArray("dependencies")
122122
.map(TargetDescription.Dependency.init(v4:))
123123

124+
let sources: [String]? = try? json.get("sources")
125+
try sources?.forEach{ _ = try RelativePath(validating: $0) }
126+
127+
let exclude: [String] = try json.get("exclude")
128+
try exclude.forEach{ _ = try RelativePath(validating: $0) }
129+
124130
return TargetDescription(
125131
name: try json.get("name"),
126132
dependencies: dependencies,
127133
path: json.get("path"),
128134
url: json.get("url"),
129-
exclude: try json.get("exclude"),
130-
sources: try? json.get("sources"),
135+
exclude: exclude,
136+
sources: sources,
131137
resources: try parseResources(json),
132138
publicHeadersPath: json.get("publicHeadersPath"),
133139
type: try .init(v4: json.get("type")),
@@ -147,10 +153,10 @@ extension ManifestBuilder {
147153
return try resourcesJSON.map { json in
148154
let rawRule = try json.get(String.self, forKey: "rule")
149155
let rule = TargetDescription.Resource.Rule(rawValue: rawRule)!
150-
let path = try json.get(String.self, forKey: "path")
156+
let path = try RelativePath(validating: json.get(String.self, forKey: "path"))
151157
let localizationString = try? json.get(String.self, forKey: "localization")
152158
let localization = localizationString.map({ TargetDescription.Resource.Localization(rawValue: $0)! })
153-
return .init(rule: rule, path: path, localization: localization)
159+
return .init(rule: rule, path: path.pathString, localization: localization)
154160
}
155161
}
156162

Tests/PackageLoadingTests/PDNextLoadingTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,37 @@ class PackageDescriptionNextLoadingTests: PackageDescriptionLoadingTests {
352352
XCTAssertEqual(manifest.defaultLocalization, "fr")
353353
}
354354
}
355+
356+
func testTargetPathsValidation() throws {
357+
let manifestItemToDiagnosticMap = [
358+
"sources: [\"/foo.swift\"]": "invalid relative path '/foo.swift",
359+
"resources: [.copy(\"/foo.txt\")]": "invalid relative path '/foo.txt'",
360+
"exclude: [\"/foo.md\"]": "invalid relative path '/foo.md",
361+
]
362+
363+
for (manifestItem, expectedDiag) in manifestItemToDiagnosticMap {
364+
let stream = BufferedOutputByteStream()
365+
stream <<< """
366+
import PackageDescription
367+
let package = Package(
368+
name: "Foo",
369+
targets: [
370+
.target(
371+
name: "Foo",
372+
\(manifestItem)
373+
),
374+
]
375+
)
376+
"""
377+
378+
XCTAssertManifestLoadThrows(stream.bytes) { error, _ in
379+
switch error {
380+
case let pathError as PathValidationError:
381+
XCTAssertMatch(pathError.description, .contains(expectedDiag))
382+
default:
383+
XCTFail("\(error)")
384+
}
385+
}
386+
}
387+
}
355388
}

0 commit comments

Comments
 (0)