-
Notifications
You must be signed in to change notification settings - Fork 356
Expand file tree
/
Copy pathnix_test.go
More file actions
225 lines (212 loc) · 7.09 KB
/
Copy pathnix_test.go
File metadata and controls
225 lines (212 loc) · 7.09 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
//nolint:dupl
package nix
import (
"slices"
"testing"
)
func TestParseVersionInfo(t *testing.T) {
raw := `nix (Nix) 2.21.2
System type: aarch64-darwin
Additional system types: x86_64-darwin
Features: gc, signed-caches
System configuration file: /etc/nix/nix.conf
User configuration files: /Users/nobody/.config/nix/nix.conf:/etc/xdg/nix/nix.conf
Store directory: /nix/store
State directory: /nix/var/nix
Data directory: /nix/store/m0ns07v8by0458yp6k30rfq1rs3kaz6g-nix-2.21.2/share
`
info, err := parseInfo([]byte(raw))
if err != nil {
t.Error("got parse error:", err)
}
if got, want := info.Name, "nix"; got != want {
t.Errorf("got Name = %q, want %q", got, want)
}
if got, want := info.Version, "2.21.2"; got != want {
t.Errorf("got Version = %q, want %q", got, want)
}
if got, want := info.System, "aarch64-darwin"; got != want {
t.Errorf("got System = %q, want %q", got, want)
}
if got, want := info.ExtraSystems, []string{"x86_64-darwin"}; !slices.Equal(got, want) {
t.Errorf("got ExtraSystems = %q, want %q", got, want)
}
if got, want := info.Features, []string{"gc", "signed-caches"}; !slices.Equal(got, want) {
t.Errorf("got Features = %q, want %q", got, want)
}
if got, want := info.SystemConfig, "/etc/nix/nix.conf"; got != want {
t.Errorf("got SystemConfig = %q, want %q", got, want)
}
if got, want := info.UserConfigs, []string{"/Users/nobody/.config/nix/nix.conf", "/etc/xdg/nix/nix.conf"}; !slices.Equal(got, want) {
t.Errorf("got UserConfigs = %q, want %q", got, want)
}
if got, want := info.StoreDir, "/nix/store"; got != want {
t.Errorf("got StoreDir = %q, want %q", got, want)
}
if got, want := info.StateDir, "/nix/var/nix"; got != want {
t.Errorf("got StateDir = %q, want %q", got, want)
}
if got, want := info.DataDir, "/nix/store/m0ns07v8by0458yp6k30rfq1rs3kaz6g-nix-2.21.2/share"; got != want {
t.Errorf("got DataDir = %q, want %q", got, want)
}
}
func TestParseLixVersionInfo(t *testing.T) {
raw := `nix (Lix, like Nix) 2.90.0-beta.1
System type: aarch64-darwin
Additional system types: x86_64-darwin
Features: gc, signed-caches
System configuration file: /etc/nix/nix.conf
User configuration files: /Users/nobody/.config/nix/nix.conf:/etc/xdg/nix/nix.conf
Store directory: /nix/store
State directory: /nix/var/nix
Data directory: /nix/store/12asl5a17ffj78njcy2fj31v59rdmanx-lix-2.90-beta.1/share
`
info, err := parseInfo([]byte(raw))
if err != nil {
t.Error("got parse error:", err)
}
if got, want := info.Name, "nix"; got != want {
t.Errorf("got Name = %q, want %q", got, want)
}
if got, want := info.Version, "2.90.0-beta.1"; got != want {
t.Errorf("got Version = %q, want %q", got, want)
}
if got, want := info.System, "aarch64-darwin"; got != want {
t.Errorf("got System = %q, want %q", got, want)
}
if got, want := info.ExtraSystems, []string{"x86_64-darwin"}; !slices.Equal(got, want) {
t.Errorf("got ExtraSystems = %q, want %q", got, want)
}
if got, want := info.Features, []string{"gc", "signed-caches"}; !slices.Equal(got, want) {
t.Errorf("got Features = %q, want %q", got, want)
}
if got, want := info.SystemConfig, "/etc/nix/nix.conf"; got != want {
t.Errorf("got SystemConfig = %q, want %q", got, want)
}
if got, want := info.UserConfigs, []string{"/Users/nobody/.config/nix/nix.conf", "/etc/xdg/nix/nix.conf"}; !slices.Equal(got, want) {
t.Errorf("got UserConfigs = %q, want %q", got, want)
}
if got, want := info.StoreDir, "/nix/store"; got != want {
t.Errorf("got StoreDir = %q, want %q", got, want)
}
if got, want := info.StateDir, "/nix/var/nix"; got != want {
t.Errorf("got StateDir = %q, want %q", got, want)
}
if got, want := info.DataDir, "/nix/store/12asl5a17ffj78njcy2fj31v59rdmanx-lix-2.90-beta.1/share"; got != want {
t.Errorf("got DataDir = %q, want %q", got, want)
}
}
func TestParseVersionInfoShort(t *testing.T) {
cases := []struct {
in string
name string
version string
}{
{"nix (Nix) 2.21.2", "nix", "2.21.2"},
{"nix (Nix) 2.23.0pre20240526_7de033d6", "nix", "2.23.0pre20240526_7de033d6"},
{"command (Nix) name (Nix) 2.21.2", "command (Nix) name", "2.21.2"},
{"nix (Lix, like Nix) 2.90.0-beta.1", "nix", "2.90.0-beta.1"},
// Nix builds that omit the patch component get it inserted so the
// version stays comparable as a semver. See
// https://github.com/jetify-com/devbox/issues/2766.
{"nix (Nix) 2.33pre20251107_479b6b73", "nix", "2.33.0pre20251107_479b6b73"},
{"nix (Nix) 2.33", "nix", "2.33.0"},
}
for _, tt := range cases {
t.Run(tt.in, func(t *testing.T) {
got, err := parseInfo([]byte(tt.in))
if err != nil {
t.Error("got parse error:", err)
}
if got.Name != tt.name {
t.Errorf("got Name = %q, want %q", got.Name, tt.name)
}
if got.Version != tt.version {
t.Errorf("got Version = %q, want %q", got.Version, tt.version)
}
})
}
}
func TestParseVersionInfoError(t *testing.T) {
t.Run("NilOutput", func(t *testing.T) {
_, err := parseInfo(nil)
if err == nil {
t.Error("want non-nil error")
}
})
t.Run("EmptyOutput", func(t *testing.T) {
_, err := parseInfo([]byte{})
if err == nil {
t.Error("want non-nil error")
}
})
t.Run("MissingVersionOutput", func(t *testing.T) {
_, err := parseInfo([]byte("nix output without a version"))
if err == nil {
t.Error("want non-nil error")
}
})
}
func TestVersionInfoAtLeast(t *testing.T) {
info := Info{}
if info.AtLeast(Version2_12) {
t.Errorf("got empty current version >= %s", Version2_12)
}
info.Version = Version2_13
if !info.AtLeast(Version2_12) {
t.Errorf("got %s < %s", info.Version, Version2_12)
}
if !info.AtLeast(Version2_13) {
t.Errorf("got %s < %s", info.Version, Version2_13)
}
if info.AtLeast(Version2_14) {
t.Errorf("got %s >= %s", info.Version, Version2_14)
}
// https://github.com/jetify-com/devbox/issues/2128
info.Version = "2.23.0pre20240526_7de033d6"
if !info.AtLeast(Version2_12) {
t.Errorf("got %s < %s", info.Version, Version2_12)
}
if info.AtLeast("2.23.0") {
t.Errorf("got %s > %s", info.Version, "2.23.0")
}
if info.AtLeast("2.24.0") {
t.Errorf("got %s > %s", info.Version, "2.24.0")
}
if info.AtLeast("2.23.0-pre.99999999") {
t.Errorf("got %s > %s", info.Version, "2.23.0-pre.99999999")
}
if !info.AtLeast("2.23.0-pre.1") {
t.Errorf("got %s < %s", info.Version, "2.23.0-pre.1")
}
// https://github.com/jetify-com/devbox/issues/2766: a prerelease that
// omits the patch component is normalized to "2.33.0pre..." by parseInfo,
// which AtLeast must then treat as newer than the minimum version.
info.Version = "2.33.0pre20251107_479b6b73"
if !info.AtLeast(Version2_12) {
t.Errorf("got %s < %s", info.Version, Version2_12)
}
if !info.AtLeast(MinVersion) {
t.Errorf("got %s < %s", info.Version, MinVersion)
}
if info.AtLeast("2.34.0") {
t.Errorf("got %s >= %s", info.Version, "2.34.0")
}
t.Run("ArgEmptyPanic", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("want panic for empty version")
}
}()
info.AtLeast("")
})
t.Run("ArgInvalidPanic", func(t *testing.T) {
v := "notasemver"
defer func() {
if r := recover(); r == nil {
t.Errorf("want panic for invalid version %q", v)
}
}()
info.AtLeast(v)
})
}