Skip to content

[5.5] Fix a problem in the logic to generate manifest source from existing parsed manifest structs #3422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Sources/PackageModel/ManifestSourceGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ extension Manifest {
public var generatedManifestFileContents: String {
/// Only write out the major and minor (not patch) versions of the
/// tools version, since the patch version doesn't change semantics.
/// We leave out the spacer if the tools version doesn't support it.
return """
// swift-tools-version: \(toolsVersion.major).\(toolsVersion.minor)
// swift-tools-version:\(toolsVersion < .v5_4 ? "" : " ")\(toolsVersion.major).\(toolsVersion.minor)
import PackageDescription

let package = \(SourceCodeFragment(from: self).generateSourceCode())
Expand Down Expand Up @@ -129,9 +130,9 @@ fileprivate extension SourceCodeFragment {
params.append(SourceCodeFragment(key: "url", string: data.location))
switch data.requirement {
case .exact(let version):
params.append(SourceCodeFragment(enum: "exact", string: version.description))
params.append(SourceCodeFragment(enum: "exact", string: "\(version)"))
case .range(let range):
params.append(SourceCodeFragment(enum: "range", string: range.description))
params.append(SourceCodeFragment("\"\(range.lowerBound)\"..<\"\(range.upperBound)\""))
case .revision(let revision):
params.append(SourceCodeFragment(enum: "revision", string: revision))
case .branch(let branch):
Expand Down
36 changes: 35 additions & 1 deletion Tests/WorkspaceTests/ManifestSourceGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ class ManifestSourceGenerationTests: XCTestCase {
completion: $0)
}

// Generate source code for the loaded manifest, write it out to replace the manifest file contents, and load it again.
// Generate source code for the loaded manifest,
let newContents = manifest.generatedManifestFileContents

// Check that the tools version was serialized properly.
let versionSpacing = (toolsVersion >= .v5_4) ? " " : ""
XCTAssertTrue(newContents.hasPrefix("// swift-tools-version:\(versionSpacing)\(toolsVersion.major).\(toolsVersion.minor)"), newContents)

// Write out the generated manifest to replace the old manifest file contents, and load it again.
try fs.writeFileContents(packageDir.appending(component: Manifest.filename), bytes: ByteString(encodingAsUTF8: newContents))
let newManifest = try tsc_await {
manifestLoader.load(at: packageDir,
Expand Down Expand Up @@ -159,6 +165,34 @@ class ManifestSourceGenerationTests: XCTestCase {
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
}

func testPackageDependencyVariations() throws {
let manifestContents = """
// swift-tools-version:5.4
import PackageDescription

let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "/foo1", from: "1.0.0"),
.package(url: "/foo2", .revision("58e9de4e7b79e67c72a46e164158e3542e570ab6")),
.package(path: "../foo3"),
.package(path: "/path/to/foo4"),
.package(url: "/foo5", .exact("1.2.3")),
.package(url: "/foo6", "1.2.3"..<"2.0.0"),
.package(url: "/foo7", .branch("master")),
.package(url: "/foo8", .upToNextMinor(from: "1.3.4")),
.package(url: "/foo9", .upToNextMajor(from: "1.3.4")),
.package(path: "~/path/to/foo10"),
.package(path: "~foo11"),
.package(path: "~/path/to/~/foo12"),
.package(path: "~"),
.package(path: "file:///path/to/foo13"),
]
)
"""
try testManifestWritingRoundTrip(manifestContents: manifestContents, toolsVersion: .v5_3)
}

func testResources() throws {
let manifestContents = """
// swift-tools-version:5.3
Expand Down