Skip to content

Commit d8887d0

Browse files
recognise any number of newline characters (U+000A) to be backward compatible with Swift ≤ 5.3
1 parent 5b50d3f commit d8887d0

File tree

2 files changed

+19
-42
lines changed

2 files changed

+19
-42
lines changed

Sources/PackageLoading/ToolsVersionLoader.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public class ToolsVersionLoader: ToolsVersionLoaderProtocol {
131131
///
132132
/// A backward-incompatibility is not necessarily a malformation.
133133
public enum BackwardIncompatibilityPre5_3_1 {
134-
/// The line terminators at the start of the manifest is not either empty or a single `U+000A`.
134+
/// The line terminators at the start of the manifest are not all `U+000A`.
135135
case leadingLineTerminators(_ lineTerminators: Substring)
136136
/// The horizontal spacing between "//" and "swift-tools-version" either is empty or uses whitespace characters unsupported by Swift ≤ 5.3.
137137
case spacingAfterCommentMarker(_ spacing: Substring)
@@ -205,7 +205,7 @@ public class ToolsVersionLoader: ToolsVersionLoaderProtocol {
205205
case let .backwardIncompatiblePre5_3_1(incompatibility, specifiedVersion):
206206
switch incompatibility {
207207
case let .leadingLineTerminators(lineTerminators):
208-
return "leading line terminator sequence \(unicodeCodePointsPrefixedByUPlus(of: lineTerminators)) in manifest is supported by only Swift > 5.3; for the specified version \(specifiedVersion), only zero or one newline (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
208+
return "leading line terminator sequence \(unicodeCodePointsPrefixedByUPlus(of: lineTerminators)) in manifest is supported by only Swift > 5.3; for the specified version \(specifiedVersion), only newline characters (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
209209
case let .spacingAfterCommentMarker(spacing):
210210
return "\(spacing.isEmpty ? "zero spacing" : "horizontal whitespace sequence \(unicodeCodePointsPrefixedByUPlus(of: spacing))") between '//' and 'swift-tools-version' is supported by only Swift > 5.3; consider using a single space (U+0020) for Swift \(specifiedVersion)"
211211
}
@@ -224,7 +224,7 @@ public class ToolsVersionLoader: ToolsVersionLoaderProtocol {
224224
public let contentsAfterToolsVersionSpecification: Substring
225225
/// A Boolean value indicating whether the manifest represented in its constituent parts is backward-compatible with Swift ≤ 5.3.
226226
public var isCompatibleWithPreSwift5_3_1: Bool {
227-
(leadingLineTerminators.isEmpty || leadingLineTerminators == "\n") && toolsVersionSpecificationComponents.isCompatibleWithPreSwift5_3_1
227+
leadingLineTerminators.allSatisfy { $0 == "\n" } && toolsVersionSpecificationComponents.isCompatibleWithPreSwift5_3_1
228228
}
229229
}
230230

@@ -351,7 +351,7 @@ public class ToolsVersionLoader: ToolsVersionLoaderProtocol {
351351

352352
guard version > .v5_3 || manifestComponents.isCompatibleWithPreSwift5_3_1 else {
353353
let manifestLeadingLineTerminators = manifestComponents.leadingLineTerminators
354-
if !manifestLeadingLineTerminators.isEmpty && manifestLeadingLineTerminators != "\n" {
354+
if !manifestLeadingLineTerminators.allSatisfy({ $0 == "\n" }) {
355355
throw Error.backwardIncompatiblePre5_3_1(.leadingLineTerminators(manifestLeadingLineTerminators), specifiedVersion: version)
356356
}
357357

Tests/PackageLoadingTests/ToolsVersionLoaderTests.swift

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ class ToolsVersionLoaderTests: XCTestCase {
5555
"// swift-tools-version:3.1.2;x;x;x;x;x;" : (3, 1, 2, "3.1.2"),
5656
"// swift-toolS-version:3.5.2;hello" : (3, 5, 2, "3.5.2"),
5757
"// sWiFt-tOoLs-vErSiOn:3.5.2\nkkk\n" : (3, 5, 2, "3.5.2"),
58+
// leading newline characters (U+000A), and 1 space (U+0020) between "//" and "swift-tools-version":
59+
"\n// swift-tools-version:3.1" : (3, 1, 0, "3.1.0"),
60+
"\n\n// swift-tools-version:3.1-dev" : (3, 1, 0, "3.1.0"),
61+
"\n\n\n// swift-tools-version:5.8.0" : (5, 8, 0, "5.8.0"),
62+
"\n\n\n\n// swift-tools-version:5.8.0-dev.al+sha.x" : (5, 8, 0, "5.8.0"),
63+
"\n\n\n\n\n// swift-tools-version:3.1.2" : (3, 1, 2, "3.1.2"),
64+
"\n\n\n\n\n\n// swift-tools-version:3.1.2;" : (3, 1, 2, "3.1.2"),
65+
"\n\n\n\n\n\n\n// swift-tools-vErsion:3.1.2;;;;;" : (3, 1, 2, "3.1.2"),
66+
"\n\n\n\n\n\n\n\n// swift-tools-version:3.1.2;x;x;x;x;x;" : (3, 1, 2, "3.1.2"),
67+
"\n\n\n\n\n\n\n\n\n// swift-toolS-version:3.5.2;hello" : (3, 5, 2, "3.5.2"),
68+
"\n\n\n\n\n\n\n\n\n\n// sWiFt-tOoLs-vErSiOn:3.5.2\nkkk\n" : (3, 5, 2, "3.5.2"),
5869
// 1 character tabulation (U+0009) between "//" and "swift-tools-version" for Swift > 5.3:
5970
"//\tswift-tools-version:5.4" : (5, 4, 0, "5.4.0"),
6071
"//\tswift-tools-version:5.4-dev" : (5, 4, 0, "5.4.0"),
@@ -407,40 +418,6 @@ class ToolsVersionLoaderTests: XCTestCase {
407418
// 2. Test that backward-incompatible leading line terminators are diagnosed before backward-incompatible spacings after the comment marker.
408419
// 3. Test spacings after the comment marker.
409420

410-
// MARK: 2 leading line feed (U+000A)
411-
412-
let manifestSnippetWith2LeadingLineFeeds = [
413-
"\u{A}\u{A}//swift-tools-version:3.1" : "3.1.0",
414-
"\u{A}\u{A}//swift-tools-version:3.1-dev" : "3.1.0",
415-
"\u{A}\u{A}//swift-tools-version:5.3" : "5.3.0",
416-
"\u{A}\u{A}//swift-tools-version:5.3.0" : "5.3.0",
417-
"\u{A}\u{A}//swift-tools-version:5.3-dev" : "5.3.0",
418-
"\u{A}\u{A}//swift-tools-version:4.8.0" : "4.8.0",
419-
"\u{A}\u{A}//swift-tools-version:4.8.0-dev.al+sha.x" : "4.8.0",
420-
"\u{A}\u{A}//swift-tools-version:3.1.2" : "3.1.2",
421-
"\u{A}\u{A}//swift-tools-version:3.1.2;" : "3.1.2",
422-
"\u{A}\u{A}//swift-tools-vErsion:3.1.2;;;;;" : "3.1.2",
423-
"\u{A}\u{A}//swift-tools-version:3.1.2;x;x;x;x;x;" : "3.1.2",
424-
"\u{A}\u{A}//swift-toolS-version:3.5.2;hello" : "3.5.2",
425-
"\u{A}\u{A}//sWiFt-tOoLs-vErSiOn:3.5.2\nkkk\n" : "3.5.2",
426-
]
427-
428-
for (specification, toolsVersionString) in manifestSnippetWith2LeadingLineFeeds {
429-
XCTAssertThrowsError(
430-
try load(ByteString(encodingAsUTF8: specification)),
431-
"a 'ToolsVersionLoader.Error' should've been thrown, because the manifest starts with more than 1 line terminator, and the specified version \(toolsVersionString) (≤ 5.3) supports at most 1 leading U+000A."
432-
) { error in
433-
guard let error = error as? ToolsVersionLoader.Error, case .backwardIncompatiblePre5_3_1(.leadingLineTerminators, _) = error else {
434-
XCTFail("'ToolsVersionLoader.Error.backwardIncompatiblePre5_3_1(.leadingLineTerminators, _)' should've been thrown, but a different error is thrown.")
435-
return
436-
}
437-
XCTAssertEqual(
438-
error.description,
439-
"leading line terminator sequence [U+000A, U+000A] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only zero or one newline (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
440-
)
441-
}
442-
}
443-
444421
// MARK: 1 leading u+000D
445422

446423
let manifestSnippetWith1LeadingCarriageReturn = [
@@ -470,7 +447,7 @@ class ToolsVersionLoaderTests: XCTestCase {
470447
}
471448
XCTAssertEqual(
472449
error.description,
473-
"leading line terminator sequence [U+000D] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only zero or one newline (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
450+
"leading line terminator sequence [U+000D] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only newline characters (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
474451
)
475452
}
476453
}
@@ -504,7 +481,7 @@ class ToolsVersionLoaderTests: XCTestCase {
504481
}
505482
XCTAssertEqual(
506483
error.description,
507-
"leading line terminator sequence [U+000D, U+000A] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only zero or one newline (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
484+
"leading line terminator sequence [U+000D, U+000A] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only newline characters (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
508485
)
509486
}
510487
}
@@ -538,7 +515,7 @@ class ToolsVersionLoaderTests: XCTestCase {
538515
}
539516
XCTAssertEqual(
540517
error.description,
541-
"leading line terminator sequence [U+000A, U+000B, U+000C, U+000D, U+000D, U+000A, U+0085, U+2028, U+2029] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only zero or one newline (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
518+
"leading line terminator sequence [U+000A, U+000B, U+000C, U+000D, U+000D, U+000A, U+0085, U+2028, U+2029] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only newline characters (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
542519
)
543520
}
544521
}
@@ -574,7 +551,7 @@ class ToolsVersionLoaderTests: XCTestCase {
574551
}
575552
XCTAssertEqual(
576553
error.description,
577-
"leading line terminator sequence [U+000A, U+000B, U+000C, U+000D, U+000D, U+000A, U+0085, U+2028, U+2029] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only zero or one newline (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
554+
"leading line terminator sequence [U+000A, U+000B, U+000C, U+000D, U+000D, U+000A, U+0085, U+2028, U+2029] in manifest is supported by only Swift > 5.3; for the specified version \(toolsVersionString), only newline characters (U+000A) at the beginning of the manifest is supported; consider moving the Swift tools version specification to the first line of the manifest"
578555
)
579556
}
580557
}

0 commit comments

Comments
 (0)