Skip to content
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

Use both key and version when sorting lockfile entries #4541

Merged
merged 2 commits into from Apr 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/internal/lockfile/lockfile.go
Expand Up @@ -56,7 +56,7 @@ func (p ByKey) Swap(i, j int) {
}

func (p ByKey) Less(i, j int) bool {
return p[i].Key < p[j].Key
return p[i].Key+p[i].Version < p[j].Key+p[j].Version
}

var _ (sort.Interface) = (*ByKey)(nil)
Expand Down
29 changes: 29 additions & 0 deletions cli/internal/lockfile/pnpm_lockfile_test.go
Expand Up @@ -3,6 +3,7 @@ package lockfile
import (
"bytes"
"os"
"sort"
"testing"

"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -375,3 +376,31 @@ func Test_DepPathParsing(t *testing.T) {
assert.Equal(t, parseDepPath(tc.input), tc.dp, tc.input)
}
}

func Test_MixedVersioning(t *testing.T) {
contents, err := getFixture(t, "pnpm-absolute.yaml")
assert.NilError(t, err)

lockfile, err := DecodePnpmLockfile(contents)
assert.NilError(t, err)

closure, err := transitiveClosure("packages/a", map[string]string{"@scope/parent": "^1.0.0", "another": "^1.0.0", "special": "npm:Special@1.2.3"}, lockfile)
assert.NilError(t, err)

deps := []Package{}

for _, v := range closure.ToSlice() {
dep := v.(Package)
deps = append(deps, dep)
}
sort.Sort(ByKey(deps))

assert.DeepEqual(t, deps, []Package{
{"/@scope/child/1.0.0", "1.0.0", true},
{"/@scope/parent/1.0.0", "1.0.0", true},
{"/Special/1.2.3", "/Special/1.2.3", true},
{"/Special/1.2.3", "1.2.3", true},
{"/another/1.0.0", "1.0.0", true},
{"/foo/1.0.0", "1.0.0", true},
})
}
23 changes: 22 additions & 1 deletion cli/internal/lockfile/testdata/pnpm-absolute.yaml
Expand Up @@ -2,9 +2,14 @@ lockfileVersion: 5.4
importers:
packages/a:
specifiers:
another: ^1.0.0
"@scope/parent": ^1.0.0
special: npm:Special@1.2.3
dependencies:
somepackage: 1.0.0
another: 1.0.0
"@scope/parent": 1.0.0
special: /Special/1.2.3

packages:
/@scope/parent/1.0.0:
resolution: { integrity: junk }
Expand All @@ -15,3 +20,19 @@ packages:
/@scope/child/1.0.0:
resolution: { integrity: junk }
dev: false

/another/1.0.0:
resolution: { integrity: junk }
dev: false
dependencies:
foo: 1.0.0

/foo/1.0.0:
resolution: { integrity: junk }
dev: false
dependencies:
Special: 1.2.3

/Special/1.2.3:
resolution: { integrity: junk }
dev: false