|
4 | 4 | "testing"
|
5 | 5 |
|
6 | 6 | "github.com/google/go-cmp/cmp"
|
7 |
| - "github.com/google/go-cmp/cmp/cmpopts" |
8 | 7 | )
|
9 | 8 |
|
10 | 9 | func TestParseFlakeRef(t *testing.T) {
|
@@ -264,17 +263,80 @@ func TestParseFlakeInstallable(t *testing.T) {
|
264 | 263 | for installable, want := range cases {
|
265 | 264 | t.Run(installable, func(t *testing.T) {
|
266 | 265 | got, err := ParseFlakeInstallable(installable)
|
267 |
| - if diff := cmp.Diff(want, got, cmpopts.IgnoreUnexported(FlakeRef{}, FlakeInstallable{})); diff != "" { |
| 266 | + if diff := cmp.Diff(want, got); diff != "" { |
268 | 267 | if err != nil {
|
269 | 268 | t.Errorf("got error: %s", err)
|
270 | 269 | }
|
271 | 270 | t.Errorf("wrong installable (-want +got):\n%s", diff)
|
272 | 271 | }
|
273 |
| - if err != nil { |
274 |
| - return |
275 |
| - } |
276 |
| - if installable != got.String() { |
277 |
| - t.Errorf("got.String() = %q != %q", got, installable) |
| 272 | + }) |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +func TestFlakeInstallableString(t *testing.T) { |
| 277 | + cases := map[FlakeInstallable]string{ |
| 278 | + {}: "", |
| 279 | + |
| 280 | + // No attribute or outputs. |
| 281 | + {Ref: FlakeRef{Type: "path", Path: "."}}: "path:.", |
| 282 | + {Ref: FlakeRef{Type: "path", Path: "./flake"}}: "path:flake", |
| 283 | + {Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake", |
| 284 | + {Ref: FlakeRef{Type: "indirect", ID: "indirect"}}: "flake:indirect", |
| 285 | + |
| 286 | + // Attribute without outputs. |
| 287 | + {AttrPath: "app", Ref: FlakeRef{Type: "path", Path: "."}}: "path:.#app", |
| 288 | + {AttrPath: "my#app", Ref: FlakeRef{Type: "path", Path: "."}}: "path:.#my%23app", |
| 289 | + {AttrPath: "app", Ref: FlakeRef{Type: "path", Path: "./flake"}}: "path:flake#app", |
| 290 | + {AttrPath: "my#app", Ref: FlakeRef{Type: "path", Path: "./flake"}}: "path:flake#my%23app", |
| 291 | + {AttrPath: "app", Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake#app", |
| 292 | + {AttrPath: "my#app", Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake#my%23app", |
| 293 | + {AttrPath: "app", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app", |
| 294 | + {AttrPath: "my#app", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#my%23app", |
| 295 | + |
| 296 | + // Attribute with single output. |
| 297 | + {AttrPath: "app", Outputs: "out", Ref: FlakeRef{Type: "path", Path: "."}}: "path:.#app^out", |
| 298 | + {AttrPath: "app", Outputs: "out", Ref: FlakeRef{Type: "path", Path: "./flake"}}: "path:flake#app^out", |
| 299 | + {AttrPath: "app", Outputs: "out", Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake#app^out", |
| 300 | + {AttrPath: "app", Outputs: "out", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^out", |
| 301 | + |
| 302 | + // Attribute with multiple outputs. |
| 303 | + {AttrPath: "app", Outputs: "out,lib", Ref: FlakeRef{Type: "path", Path: "."}}: "path:.#app^lib,out", |
| 304 | + {AttrPath: "app", Outputs: "out,lib", Ref: FlakeRef{Type: "path", Path: "./flake"}}: "path:flake#app^lib,out", |
| 305 | + {AttrPath: "app", Outputs: "out,lib", Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake#app^lib,out", |
| 306 | + {AttrPath: "app", Outputs: "out,lib", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^lib,out", |
| 307 | + |
| 308 | + // Outputs are cleaned and sorted. |
| 309 | + {AttrPath: "app", Outputs: "out,lib", Ref: FlakeRef{Type: "path", Path: "."}}: "path:.#app^lib,out", |
| 310 | + {AttrPath: "app", Outputs: "lib,out", Ref: FlakeRef{Type: "path", Path: "./flake"}}: "path:flake#app^lib,out", |
| 311 | + {AttrPath: "app", Outputs: "out,,", Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake#app^out", |
| 312 | + {AttrPath: "app", Outputs: ",lib,out", Ref: FlakeRef{Type: "path", Path: "/flake"}}: "path:/flake#app^lib,out", |
| 313 | + {AttrPath: "app", Outputs: ",", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app", |
| 314 | + |
| 315 | + // Wildcard replaces other outputs. |
| 316 | + {AttrPath: "app", Outputs: "*", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^*", |
| 317 | + {AttrPath: "app", Outputs: "out,*", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^*", |
| 318 | + {AttrPath: "app", Outputs: ",*", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^*", |
| 319 | + |
| 320 | + // Outputs are not percent-encoded. |
| 321 | + {AttrPath: "app", Outputs: "%", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^%", |
| 322 | + {AttrPath: "app", Outputs: "/", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^/", |
| 323 | + {AttrPath: "app", Outputs: "%2F", Ref: FlakeRef{Type: "indirect", ID: "nixpkgs"}}: "flake:nixpkgs#app^%2F", |
| 324 | + |
| 325 | + // Missing or invalid fields. |
| 326 | + {AttrPath: "app", Ref: FlakeRef{Type: "file", URL: ""}}: "", |
| 327 | + {AttrPath: "app", Ref: FlakeRef{Type: "git", URL: ""}}: "", |
| 328 | + {AttrPath: "app", Ref: FlakeRef{Type: "github", Owner: ""}}: "", |
| 329 | + {AttrPath: "app", Ref: FlakeRef{Type: "indirect", ID: ""}}: "", |
| 330 | + {AttrPath: "app", Ref: FlakeRef{Type: "path", Path: ""}}: "", |
| 331 | + {AttrPath: "app", Ref: FlakeRef{Type: "tarball", URL: ""}}: "", |
| 332 | + } |
| 333 | + |
| 334 | + for installable, want := range cases { |
| 335 | + t.Run(want, func(t *testing.T) { |
| 336 | + t.Logf("input = %#v", installable) |
| 337 | + got := installable.String() |
| 338 | + if got != want { |
| 339 | + t.Errorf("got %#q, want %#q", got, want) |
278 | 340 | }
|
279 | 341 | })
|
280 | 342 | }
|
|
0 commit comments