-
Notifications
You must be signed in to change notification settings - Fork 310
Expand file tree
/
Copy pathpackage_test.go
More file actions
240 lines (220 loc) · 6.38 KB
/
package_test.go
File metadata and controls
240 lines (220 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2024 Jetify Inc. and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package devpkg
import (
"fmt"
"path/filepath"
"strings"
"testing"
"github.com/samber/lo"
"go.jetify.com/devbox/internal/lock"
"go.jetify.com/devbox/internal/nix"
"go.jetify.com/devbox/nix/flake"
)
const nixCommitHash = "hsdafkhsdafhas"
type inputTestCase struct {
pkg string
isFlake bool
name string
urlWithoutFragment string
urlForInput string
}
func TestInput(t *testing.T) {
projectDir := "/tmp/my-project"
cases := []inputTestCase{
{
pkg: "path:path/to/my-flake#my-package",
isFlake: true,
name: "my-flake-9a897d",
urlWithoutFragment: "path:" + filepath.Join(projectDir, "path/to/my-flake"),
urlForInput: "path:" + filepath.Join(projectDir, "path/to/my-flake"),
},
{
pkg: "path:.#my-package",
isFlake: true,
name: "my-project-45b022",
urlWithoutFragment: "path:" + projectDir,
urlForInput: "path:" + projectDir,
},
{
pkg: "path:/tmp/my-project/path/to/my-flake#my-package",
isFlake: true,
name: "my-flake-9a897d",
urlWithoutFragment: "path:" + filepath.Join(projectDir, "path/to/my-flake"),
urlForInput: "path:" + filepath.Join(projectDir, "path/to/my-flake"),
},
{
pkg: "path:/tmp/my-project/path/to/my-flake",
isFlake: true,
name: "my-flake-7d03be",
urlWithoutFragment: "path:" + filepath.Join(projectDir, "path/to/my-flake"),
urlForInput: "path:" + filepath.Join(projectDir, "path/to/my-flake"),
},
{
pkg: "hello",
isFlake: false,
name: "nixpkgs-hsdafk",
urlWithoutFragment: "hello",
urlForInput: fmt.Sprintf("github:NixOS/nixpkgs/%s", nixCommitHash),
},
{
pkg: "hello@123",
isFlake: false,
name: "nixpkgs-hsdafk",
urlWithoutFragment: "hello@123",
urlForInput: fmt.Sprintf("github:NixOS/nixpkgs/%s", nixCommitHash),
},
{
pkg: "github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c#hello",
isFlake: true,
name: "gh-nixos-nixpkgs-5233fd2ba76a3accb5aaa999c00509a11fd0793c",
urlWithoutFragment: "github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c",
urlForInput: "github:nixos/nixpkgs/5233fd2ba76a3accb5aaa999c00509a11fd0793c",
},
{
pkg: "github:F1bonacc1/process-compose",
isFlake: true,
name: "gh-F1bonacc1-process-compose",
urlWithoutFragment: "github:F1bonacc1/process-compose",
urlForInput: "github:F1bonacc1/process-compose",
},
}
for _, testCase := range cases {
i := testInputFromString(testCase.pkg, projectDir)
if name := i.FlakeInputName(); testCase.name != name {
t.Errorf("Name() = %v, want %v", name, testCase.name)
}
if urlForInput := i.URLForFlakeInput(); testCase.urlForInput != urlForInput {
t.Errorf("URLForFlakeInput() = %v, want %v", urlForInput, testCase.urlForInput)
}
}
}
type testInput struct {
*Package
}
type lockfile struct {
projectDir string
}
func (l *lockfile) ProjectDir() string {
return l.projectDir
}
func (l *lockfile) Stdenv() flake.Ref {
return flake.Ref{
Type: flake.TypeGitHub,
Owner: "NixOS",
Repo: "nixpkgs",
Rev: nixCommitHash,
}
}
func (l *lockfile) Get(pkg string) *lock.Package {
return nil
}
func (l *lockfile) Resolve(pkg string) (*lock.Package, error) {
switch {
case strings.Contains(pkg, "path:"):
return &lock.Package{Resolved: pkg}, nil
case strings.Contains(pkg, "github:"):
return &lock.Package{Resolved: pkg}, nil
default:
return &lock.Package{
Resolved: flake.Installable{
Ref: l.Stdenv(),
AttrPath: pkg,
}.String(),
}, nil
}
}
func testInputFromString(s, projectDir string) *testInput {
return lo.ToPtr(testInput{Package: PackageFromStringWithDefaults(s, &lockfile{projectDir})})
}
func TestHashFromNixPkgsURL(t *testing.T) {
tests := []struct {
url string
expected string
}{
{
url: "github:NixOS/nixpkgs/12345",
expected: "12345",
},
{
url: "github:NixOS/nixpkgs/abcdef#hello",
expected: "abcdef",
},
{
url: "github:NixOS/nixpkgs/",
expected: "",
},
{
url: "github:NixOS/nixpkgs",
expected: "",
},
{
url: "github:NixOS/other-repo/12345",
expected: "",
},
{
url: "",
expected: "",
},
}
for _, test := range tests {
result := nix.HashFromNixPkgsURL(test.url)
if result != test.expected {
t.Errorf(
"Expected hash '%s' for URL '%s', but got '%s'",
test.expected,
test.url,
result,
)
}
}
}
func TestCanonicalName(t *testing.T) {
tests := []struct {
pkgName string
expectedName string
}{
{"go", "go"},
{"go@latest", "go"},
{"go@1.21", "go"},
{"runx:golangci/golangci-lint@latest", "runx:golangci/golangci-lint"},
{"runx:golangci/golangci-lint@v0.0.2", "runx:golangci/golangci-lint"},
{"runx:golangci/golangci-lint", "runx:golangci/golangci-lint"},
{"github:NixOS/nixpkgs/12345", ""},
{"path:/to/my/file", ""},
}
for _, tt := range tests {
t.Run(tt.pkgName, func(t *testing.T) {
pkg := PackageFromStringWithDefaults(tt.pkgName, &lockfile{})
got := pkg.CanonicalName()
if got != tt.expectedName {
t.Errorf("Expected canonical name %q, but got %q", tt.expectedName, got)
}
})
}
}
func TestScopedPackageTransformation(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"@angular/cli", "@angular/cli"}, // No longer transformed without npm: prefix
{"@angular/cli@1.0", "@angular/cli@1.0"},
{"@github/copilot", "@github/copilot"},
{"npm:@angular/cli", `nodePackages."@angular/cli"`},
{"npm:@angular/cli@1.0", `nodePackages."@angular/cli"@1.0`},
{"npm:lodash", `nodePackages."lodash"`},
{"npm:lodash@4.17.21", `nodePackages."lodash"@4.17.21`},
{"regular", "regular"},
{"@notscoped", "@notscoped"}, // no slash, so not transformed
{"go@1.20", "go@1.20"}, // not npm
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
pkg := PackageFromStringWithDefaults(tt.input, &lockfile{})
if pkg.Raw != tt.expected {
t.Errorf("Expected Raw %q, but got %q", tt.expected, pkg.Raw)
}
})
}
}