Skip to content

Commit

Permalink
[PackageEditing] Fix dependency insertion when the existing deps list…
Browse files Browse the repository at this point in the history
… doesn't have a trailing comma
  • Loading branch information
owenv committed Nov 15, 2020
1 parent 07948f6 commit 81ff1f7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/SPMPackageEditor/ManifestRewriter.swift
Expand Up @@ -344,7 +344,12 @@ final class PackageDependencyWriter: SyntaxRewriter {
let rightBrace = SyntaxFactory.makeRightSquareBracketToken(
leadingTrivia: [.newlines(1), .spaces(4)])

return ExprSyntax(node.addElement(newDependencyElement)
let newElements = SyntaxFactory.makeArrayElementList(
node.elements.dropLast() +
[node.elements.last?.withTrailingComma(SyntaxFactory.makeCommaToken()),
newDependencyElement].compactMap {$0})

return ExprSyntax(node.withElements(newElements)
.withRightSquare(rightBrace))
}
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/SPMPackageEditorTests/AddPackageDependencyTests.swift
Expand Up @@ -233,4 +233,38 @@ final class AddPackageDependencyTests: XCTestCase {
)
""")
}

func testAddPackageDependency7() throws {
let manifest = """
let package = Package(
name: "exec",
dependencies: [
.package(url: "https://github.com/foo/bar", from: "1.0.3")
],
targets: [
.target(name: "exec")
]
)
"""


let editor = try ManifestRewriter(manifest)
try editor.addPackageDependency(
url: "https://github.com/foo/goo",
requirement: .upToNextMajor("1.0.1")
)

XCTAssertEqual(editor.editedManifest, """
let package = Package(
name: "exec",
dependencies: [
.package(url: "https://github.com/foo/bar", from: "1.0.3"),
.package(url: "https://github.com/foo/goo", from: "1.0.1"),
],
targets: [
.target(name: "exec")
]
)
""")
}
}

0 comments on commit 81ff1f7

Please sign in to comment.