Skip to content

Commit c430893

Browse files
ssuuyyuuaciidgh
authored andcommitted
[Workspace] Fix invalid source code generated by InitPackage. (#1846)
* [Workspace] Fix invalid source code generated by InitPackage. The generated source code is invalid because there is an unwanted comma at the end of Package arguments. This patch fixes this issue. [SR-8947] swift package init generates invalid source code * Add tests for validating generated empty/system-module packages.
1 parent d1e12f4 commit c430893

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,46 +88,48 @@ public final class InitPackage {
8888
import PackageDescription
8989
9090
let package = Package(
91-
name: "\(pkgname)",
9291
9392
"""
9493

95-
if packageType == .library {
96-
stream <<< """
97-
products: [
98-
// Products define the executables and libraries produced by a package, and make them visible to other packages.
99-
.library(
100-
name: "\(pkgname)",
101-
targets: ["\(pkgname)"]),
102-
],
94+
var pkgParams = [String]()
95+
pkgParams.append("""
96+
name: "\(pkgname)"
97+
""")
10398

104-
"""
99+
if packageType == .library {
100+
pkgParams.append("""
101+
products: [
102+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
103+
.library(
104+
name: "\(pkgname)",
105+
targets: ["\(pkgname)"]),
106+
]
107+
""")
105108
}
106109

107-
stream <<< """
110+
pkgParams.append("""
108111
dependencies: [
109112
// Dependencies declare other packages that this package depends on.
110113
// .package(url: /* package url */, from: "1.0.0"),
111-
],
112-
113-
"""
114+
]
115+
""")
114116

115117
if packageType == .library || packageType == .executable {
116-
stream <<< """
117-
targets: [
118-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
119-
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
120-
.target(
121-
name: "\(pkgname)",
122-
dependencies: []),
123-
.testTarget(
124-
name: "\(pkgname)Tests",
125-
dependencies: ["\(pkgname)"]),
126-
]
127-
128-
"""
118+
pkgParams.append("""
119+
targets: [
120+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
121+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
122+
.target(
123+
name: "\(pkgname)",
124+
dependencies: []),
125+
.testTarget(
126+
name: "\(pkgname)Tests",
127+
dependencies: ["\(pkgname)"]),
128+
]
129+
""")
129130
}
130-
stream <<< ")\n"
131+
132+
stream <<< pkgParams.joined(separator: ",\n") <<< "\n)\n"
131133
}
132134

133135
// Create a tools version with current version but with patch set to zero.

Tests/WorkspaceTests/InitTests.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ class InitTests: XCTestCase {
3838
XCTAssert(progressMessages.count > 0)
3939

4040
// Verify basic file system content that we expect in the package
41-
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
41+
let manifest = path.appending(component: "Package.swift")
42+
XCTAssertTrue(fs.exists(manifest))
43+
let manifestContents = try localFileSystem.readFileContents(manifest).asString!
44+
let version = "\(InitPackage.newPackageToolsVersion.major).\(InitPackage.newPackageToolsVersion.minor)"
45+
XCTAssertTrue(manifestContents.hasPrefix("// swift-tools-version:\(version)\n"))
46+
XCTAssertTrue(manifestContents.contains(packageWithNameAndDependencies(with: name)))
4247
XCTAssert(fs.exists(path.appending(component: "README.md")))
4348
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), [])
4449
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Tests")), [])
@@ -149,7 +154,12 @@ class InitTests: XCTestCase {
149154
XCTAssert(progressMessages.count > 0)
150155

151156
// Verify basic file system content that we expect in the package
152-
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
157+
let manifest = path.appending(component: "Package.swift")
158+
XCTAssertTrue(fs.exists(manifest))
159+
let manifestContents = try localFileSystem.readFileContents(manifest).asString!
160+
let version = "\(InitPackage.newPackageToolsVersion.major).\(InitPackage.newPackageToolsVersion.minor)"
161+
XCTAssertTrue(manifestContents.hasPrefix("// swift-tools-version:\(version)\n"))
162+
XCTAssertTrue(manifestContents.contains(packageWithNameAndDependencies(with: name)))
153163
XCTAssert(fs.exists(path.appending(component: "README.md")))
154164
XCTAssert(fs.exists(path.appending(component: "module.modulemap")))
155165
}
@@ -197,4 +207,16 @@ class InitTests: XCTestCase {
197207
#endif
198208

199209
}
210+
211+
private func packageWithNameAndDependencies(with name: String) -> String {
212+
return """
213+
let package = Package(
214+
name: "\(name)",
215+
dependencies: [
216+
// Dependencies declare other packages that this package depends on.
217+
// .package(url: /* package url */, from: "1.0.0"),
218+
]
219+
)
220+
"""
221+
}
200222
}

0 commit comments

Comments
 (0)