-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_test.go
More file actions
119 lines (109 loc) · 3.13 KB
/
Copy pathadd_test.go
File metadata and controls
119 lines (109 loc) · 3.13 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
package pin
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
func TestAdd_EndToEnd(t *testing.T) {
srv := fakeNPM(t, "newpkg", "1.2.3", map[string]string{"dist/x.js": "x"})
dir := t.TempDir()
writeManifest(t, dir, `out: "v"
assets:
- name: "existing"
version: "1.0.0"
files: ["x.js"]
`)
// Seed lockfile with an unrelated entry so Add's call to Sync has
// something to start from. We won't actually fetch existing — give
// it a registry that 404s and the manifest already has the locked
// version. Easier path: just add to an empty manifest.
writeManifest(t, dir, `out: "v"
assets: []
`)
res, err := Add(context.Background(),
"newpkg@1.2.3",
[]string{"dist/x.js"},
AddOptions{Dir: dir, RegistryURL: srv.URL})
if err != nil {
t.Fatalf("Add: %v", err)
}
if res.Resolved != "1.2.3" {
t.Errorf("Resolved = %q, want 1.2.3", res.Resolved)
}
if res.SyncResult == nil || len(res.SyncResult.Lock.Assets) != 1 {
t.Errorf("SyncResult missing or empty: %+v", res.SyncResult)
}
manifestBytes, err := os.ReadFile(filepath.Join(dir, DefaultManifest))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(manifestBytes), "newpkg") {
t.Errorf("manifest does not contain newpkg after Add:\n%s", manifestBytes)
}
}
func TestAdd_DryRun(t *testing.T) {
srv := fakeNPM(t, "newpkg", "1.0.0", map[string]string{"index.js": "x"})
dir := t.TempDir()
writeManifest(t, dir, `out: "v"
assets: []
`)
original, _ := os.ReadFile(filepath.Join(dir, DefaultManifest))
res, err := Add(context.Background(),
"newpkg@1.0.0",
[]string{"index.js"},
AddOptions{Dir: dir, RegistryURL: srv.URL, DryRun: true})
if err != nil {
t.Fatalf("Add: %v", err)
}
if res.Resolved != "1.0.0" {
t.Errorf("Resolved = %q", res.Resolved)
}
after, _ := os.ReadFile(filepath.Join(dir, DefaultManifest))
if string(original) != string(after) {
t.Error("--dry-run modified the manifest:\nbefore:\n" + string(original) + "\nafter:\n" + string(after))
}
}
func TestAdd_EmptyNameRejected(t *testing.T) {
dir := t.TempDir()
writeManifest(t, dir, "out: v\nassets: []\n")
_, err := Add(context.Background(), "", nil, AddOptions{Dir: dir})
if err == nil {
t.Error("Add with empty spec should error")
}
}
func TestParseSpec(t *testing.T) {
cases := []struct {
in string
name, constraint string
}{
{"htmx.org", "htmx.org", ""},
{"htmx.org@2.0.6", "htmx.org", "2.0.6"},
{"htmx.org@^2.0", "htmx.org", "^2.0"},
{"htmx.org@latest", "htmx.org", "latest"},
{"@scope/pkg", "@scope/pkg", ""},
{"@scope/pkg@1.0.0", "@scope/pkg", "1.0.0"},
{"@scope/pkg@^1.0", "@scope/pkg", "^1.0"},
}
for _, tc := range cases {
name, c := parseSpec(tc.in)
if name != tc.name || c != tc.constraint {
t.Errorf("parseSpec(%q) = %q, %q; want %q, %q", tc.in, name, c, tc.name, tc.constraint)
}
}
}
func TestCaretMajorMinor(t *testing.T) {
cases := map[string]string{
"2.0.6": "^2.0",
"1.5.0": "^1.5",
"0.3.11": "^0.3",
"4.0.0-beta.1": "^4.0",
"1": "^1",
}
for in, want := range cases {
if got := caretMajorMinor(in); got != want {
t.Errorf("caretMajorMinor(%q) = %q, want %q", in, got, want)
}
}
}