@@ -149,17 +149,81 @@ func TestParseFlakeRef(t *testing.T) {
149
149
for ref , want := range cases {
150
150
t .Run (ref , func (t * testing.T ) {
151
151
got , err := ParseFlakeRef (ref )
152
- if diff := cmp .Diff (want , got , cmpopts . IgnoreUnexported ( FlakeRef {}) ); diff != "" {
152
+ if diff := cmp .Diff (want , got ); diff != "" {
153
153
if err != nil {
154
154
t .Errorf ("got error: %s" , err )
155
155
}
156
156
t .Errorf ("wrong flakeref (-want +got):\n %s" , diff )
157
157
}
158
- if err != nil {
159
- return
160
- }
161
- if ref != got .String () {
162
- t .Errorf ("got.String() = %q != %q" , got , ref )
158
+ })
159
+ }
160
+ }
161
+
162
+ func TestFlakeRefString (t * testing.T ) {
163
+ cases := map [FlakeRef ]string {
164
+ {}: "" ,
165
+
166
+ // Path references.
167
+ {Type : "path" , Path : "." }: "path:." ,
168
+ {Type : "path" , Path : "./" }: "path:." ,
169
+ {Type : "path" , Path : "./flake" }: "path:flake" ,
170
+ {Type : "path" , Path : "./relative/flake" }: "path:relative/flake" ,
171
+ {Type : "path" , Path : "/" }: "path:/" ,
172
+ {Type : "path" , Path : "/flake" }: "path:/flake" ,
173
+ {Type : "path" , Path : "/absolute/flake" }: "path:/absolute/flake" ,
174
+
175
+ // Path references with escapes.
176
+ {Type : "path" , Path : "%" }: "path:%25" ,
177
+ {Type : "path" , Path : "/%2F" }: "path:/%252F" ,
178
+ {Type : "path" , Path : "./Ûñî©ôδ€/flake\n " }: "path:%C3%9B%C3%B1%C3%AE%C2%A9%C3%B4%CE%B4%E2%82%AC/flake%0A" ,
179
+ {Type : "path" , Path : "/Ûñî©ôδ€/flake\n " }: "path:/%C3%9B%C3%B1%C3%AE%C2%A9%C3%B4%CE%B4%E2%82%AC/flake%0A" ,
180
+
181
+ // Indirect references.
182
+ {Type : "indirect" , ID : "indirect" }: "flake:indirect" ,
183
+ {Type : "indirect" , ID : "indirect" , Dir : "sub/dir" }: "flake:indirect?dir=sub%2Fdir" ,
184
+ {Type : "indirect" , ID : "indirect" , Ref : "ref" }: "flake:indirect/ref" ,
185
+ {Type : "indirect" , ID : "indirect" , Ref : "my/ref" }: "flake:indirect/my%2Fref" ,
186
+ {Type : "indirect" , ID : "indirect" , Rev : "5233fd2ba76a3accb5aaa999c00509a11fd0793c" }: "flake:indirect/5233fd2ba76a3accb5aaa999c00509a11fd0793c" ,
187
+ {Type : "indirect" , ID : "indirect" , Ref : "ref" , Rev : "5233fd2ba76a3accb5aaa999c00509a11fd0793c" }: "flake:indirect/ref/5233fd2ba76a3accb5aaa999c00509a11fd0793c" ,
188
+
189
+ // GitHub references.
190
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" }: "github:NixOS/nix" ,
191
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" , Ref : "v1.2.3" }: "github:NixOS/nix/v1.2.3" ,
192
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" , Ref : "my/ref" }: "github:NixOS/nix/my%2Fref" ,
193
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" , Ref : "5233fd2ba76a3accb5aaa999c00509a11fd0793c" }: "github:NixOS/nix/5233fd2ba76a3accb5aaa999c00509a11fd0793c" ,
194
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" , Ref : "5233fd2bb76a3accb5aaa999c00509a11fd0793z" }: "github:NixOS/nix/5233fd2bb76a3accb5aaa999c00509a11fd0793z" ,
195
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" , Dir : "sub/dir" }: "github:NixOS/nix?dir=sub%2Fdir" ,
196
+ {Type : "github" , Owner : "NixOS" , Repo : "nix" , Dir : "sub/dir" , Host : "example.com" }: "github:NixOS/nix?dir=sub%2Fdir&host=example.com" ,
197
+
198
+ // Git references.
199
+ {Type : "git" , URL : "git://example.com/repo/flake" }: "git://example.com/repo/flake" ,
200
+ {Type : "git" , URL : "https://example.com/repo/flake" }: "git+https://example.com/repo/flake" ,
201
+ {
Type :
"git" ,
URL :
"ssh://[email protected] /repo/flake" }:
"git+ssh://[email protected] /repo/flake" ,
202
+ {Type : "git" , URL : "git:/repo/flake" }: "git:/repo/flake" ,
203
+ {Type : "git" , URL : "file:///repo/flake" }: "git+file:///repo/flake" ,
204
+ {
Type :
"git" ,
URL :
"ssh://[email protected] /repo/flake" ,
Ref :
"my/ref" ,
Rev :
"e486d8d40e626a20e06d792db8cc5ac5aba9a5b4" }:
"git+ssh://[email protected] /repo/flake?ref=my%2Fref&rev=e486d8d40e626a20e06d792db8cc5ac5aba9a5b4" ,
205
+ {
Type :
"git" ,
URL :
"ssh://[email protected] /repo/flake?dir=sub%2Fdir" ,
Ref :
"my/ref" ,
Rev :
"e486d8d40e626a20e06d792db8cc5ac5aba9a5b4" ,
Dir :
"sub/dir" }:
"git+ssh://[email protected] /repo/flake?dir=sub%2Fdir&ref=my%2Fref&rev=e486d8d40e626a20e06d792db8cc5ac5aba9a5b4" ,
206
+ {Type : "git" , URL : "git:repo/flake?dir=sub%2Fdir" , Ref : "my/ref" , Rev : "e486d8d40e626a20e06d792db8cc5ac5aba9a5b4" , Dir : "sub/dir" }: "git:repo/flake?dir=sub%2Fdir&ref=my%2Fref&rev=e486d8d40e626a20e06d792db8cc5ac5aba9a5b4" ,
207
+
208
+ // Tarball references.
209
+ {Type : "tarball" , URL : "http://example.com/flake" }: "tarball+http://example.com/flake" ,
210
+ {Type : "tarball" , URL : "https://example.com/flake" }: "tarball+https://example.com/flake" ,
211
+ {Type : "tarball" , URL : "https://example.com/flake" , Dir : "sub/dir" }: "tarball+https://example.com/flake?dir=sub%2Fdir" ,
212
+ {Type : "tarball" , URL : "file:///home/flake" }: "tarball+file:///home/flake" ,
213
+
214
+ // File URL references.
215
+ {Type : "file" , URL : "file:///flake" }: "file+file:///flake" ,
216
+ {Type : "file" , URL : "http://example.com/flake" }: "file+http://example.com/flake" ,
217
+ {Type : "file" , URL : "http://example.com/flake.git" }: "file+http://example.com/flake.git" ,
218
+ {Type : "file" , URL : "http://example.com/flake.tar?dir=sub%2Fdir" , Dir : "sub/dir" }: "file+http://example.com/flake.tar?dir=sub%2Fdir" ,
219
+ }
220
+
221
+ for ref , want := range cases {
222
+ t .Run (want , func (t * testing.T ) {
223
+ t .Logf ("input = %#v" , ref )
224
+ got := ref .String ()
225
+ if got != want {
226
+ t .Errorf ("got %#q, want %#q" , got , want )
163
227
}
164
228
})
165
229
}
@@ -253,3 +317,19 @@ func TestFlakeInstallableAllOutputs(t *testing.T) {
253
317
t .Errorf ("AllOutputs() = true for empty outputs slice, want false" )
254
318
}
255
319
}
320
+
321
+ func TestBuildQueryString (t * testing.T ) {
322
+ defer func () {
323
+ if r := recover (); r == nil {
324
+ t .Error ("wanted panic for odd-number of key-value parameters" )
325
+ }
326
+ }()
327
+
328
+ // staticcheck impressively catches buildQueryString calls that have an
329
+ // odd number of parameters. Build the slice in a convoluted way to
330
+ // throw it off and suppress the warning (gopls doesn't have nolint
331
+ // directives).
332
+ var elems []string
333
+ elems = append (elems , "1" )
334
+ buildQueryString (elems ... )
335
+ }
0 commit comments