Skip to content

Commit

Permalink
Merge pull request #338 from SimonErm/bugfix/alternative-package-id
Browse files Browse the repository at this point in the history
Bugfix/alternative package-id
  • Loading branch information
dmikusa authored Jan 22, 2025
2 parents d102750 + 5d3cc10 commit 41c2007
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
17 changes: 12 additions & 5 deletions runner/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,12 @@ func (c CargoRunner) WorkspaceMembers(srcDir string, destLayer libcnb.Layer) ([]

// parseWorkspaceMember parses a workspace member which can be in a couple of different formats
//
// pre-1.77: `package-name package-version (url)`, like `function 0.1.0 (path+file:///Users/dmikusa/Downloads/fn-rs)`
// 1.77+: `url#package-name@package-version` like `path+file:///Users/dmikusa/Downloads/fn-rs#[email protected]`
// pre-1.77: `package-name package-version (url)`, like `function 0.1.0 (path+file:///Users/dmikusa/Downloads/fn-rs)`
// 1.77+:
// - `url#package-name@package-version` like `path+file:///Users/dmikusa/Downloads/fn-rs#[email protected]`
// - `url#version` for local packages where the workspace member name is equal to the directory name like `path+file:///Users/jondoe/.../services/example-transform#0.1.0`
//
// The final directory is assumed to be the package name with the local package format.
// returns the package name, version, URL, and optional error in that order
func ParseWorkspaceMember(workspaceMember string) (string, string, string, error) {
if strings.HasPrefix(workspaceMember, "path+file://") {
Expand All @@ -253,11 +256,15 @@ func ParseWorkspaceMember(workspaceMember string) (string, string, string, error
}

otherHalf := strings.SplitN(half[1], "@", 2)
if len(otherHalf) != 2 {
return "", "", "", fmt.Errorf("unable to parse workspace member [%s], missing `@`", workspaceMember)
if len(otherHalf) == 2 {
return strings.TrimSpace(otherHalf[0]), strings.TrimSpace(otherHalf[1]), strings.TrimSpace(half[0]), nil
} else {
splitIndex := strings.LastIndex(half[0], "/")
path := half[0][:splitIndex]
pkgName := half[0][splitIndex+1:]
return strings.TrimSpace(pkgName), strings.TrimSpace(half[1]), strings.TrimSpace(path), nil
}

return strings.TrimSpace(otherHalf[0]), strings.TrimSpace(otherHalf[1]), strings.TrimSpace(half[0]), nil
} else {
// This is OK because the workspace member format is `package-name package-version (url)` and
// none of name, version or URL may contain a space & be valid
Expand Down
20 changes: 13 additions & 7 deletions runner/runners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func testRunners(t *testing.T, context spec.G, it spec.S) {
"path+file:///workspace/todo#[email protected]",
"path+file:///workspace/routes#[email protected]",
"path+file:///workspace/jokes#[email protected]",
"path+file:///workspace/other-format/other-format#1.0.0",
})

executor.On("Execute", mock.MatchedBy(func(ex effect.Execution) bool {
Expand All @@ -490,7 +491,7 @@ func testRunners(t *testing.T, context spec.G, it spec.S) {
urls, err := runner.WorkspaceMembers(workingDir, destLayer)
Expect(err).ToNot(HaveOccurred())

Expect(urls).To(HaveLen(4))
Expect(urls).To(HaveLen(5))

url, err := url.Parse("path+file:///workspace/basics")
Expect(err).ToNot(HaveOccurred())
Expand All @@ -507,6 +508,10 @@ func testRunners(t *testing.T, context spec.G, it spec.S) {
url, err = url.Parse("path+file:///workspace/jokes")
Expect(err).ToNot(HaveOccurred())
Expect(urls[3]).To(Equal(*url))

url, err = url.Parse("path+file:///workspace/other-format")
Expect(err).ToNot(HaveOccurred())
Expect(urls[4]).To(Equal(*url))
})
})

Expand Down Expand Up @@ -830,16 +835,17 @@ func testRunners(t *testing.T, context spec.G, it spec.S) {
Expect(version).To(Equal("2.0.0"))
Expect(url).To(Equal("path+file:///workspace/basics"))
})

it("parses alternative package-id", func() {
pkgName, version, url, err := runner.ParseWorkspaceMember("path+file:///workspace/basics/basics#2.0.0")
Expect(err).ToNot(HaveOccurred())
Expect(pkgName).To(Equal("basics"))
Expect(version).To(Equal("2.0.0"))
Expect(url).To(Equal("path+file:///workspace/basics"))
})
it("fails to parse because there is no hash sign", func() {
_, _, _, err := runner.ParseWorkspaceMember("path+file:///workspace/basics")
Expect(err).To(MatchError("unable to parse workspace member [path+file:///workspace/basics], missing `#`"))
})

it("fails to parse because there is no at sign", func() {
_, _, _, err := runner.ParseWorkspaceMember("path+file:///workspace/basics#foo")
Expect(err).To(MatchError("unable to parse workspace member [path+file:///workspace/basics#foo], missing `@`"))
})
})
})
}
Expand Down

0 comments on commit 41c2007

Please sign in to comment.