Serge Bazanski | a03b60b | 2023-04-01 14:47:44 +0000 | [diff] [blame] | 1 | package workspace |
| 2 | |
| 3 | import ( |
| 4 | "context" |
Serge Bazanski | 54183ba | 2023-09-01 19:12:14 +0200 | [diff] [blame] | 5 | "os" |
Serge Bazanski | a03b60b | 2023-04-01 14:47:44 +0000 | [diff] [blame] | 6 | "testing" |
| 7 | |
| 8 | "github.com/google/go-cmp/cmp" |
| 9 | ) |
| 10 | |
| 11 | func TestCheckNixPath(t *testing.T) { |
| 12 | for _, te := range []struct { |
| 13 | in string |
| 14 | okay bool |
| 15 | }{ |
| 16 | {"foo", true}, |
| 17 | {"foo.bar.baz", true}, |
| 18 | {"foo.bar.baz.", false}, |
| 19 | {"foo.bar.baz..", false}, |
| 20 | {".foo.bar.baz", false}, |
| 21 | {"..foo.bar.baz", false}, |
| 22 | {"foo..bar.baz", false}, |
| 23 | {".", false}, |
| 24 | {"", false}, |
| 25 | |
| 26 | {"ops.machines.\"test.example.com\".config", true}, |
| 27 | {"ops.machines.\"test.example.com.config", false}, |
| 28 | {"ops.machines.\"test.example.com\"bar.config", false}, |
| 29 | {"ops.machines.bar\"test.example.com\".config", false}, |
| 30 | {"\"test.example.com\"bar", false}, |
| 31 | {"test.example.com\"", false}, |
| 32 | |
| 33 | {"foo--.__bar.b-a-z---", true}, |
| 34 | {"foo.0bar", false}, |
| 35 | {"foo.-bar", false}, |
| 36 | |
| 37 | {"test\\.test", false}, |
| 38 | {"test test", false}, |
| 39 | } { |
| 40 | err := checkNixPath(te.in) |
| 41 | if te.okay && err != nil { |
| 42 | t.Errorf("%q: expected okay, got %v", te.in, err) |
| 43 | } |
| 44 | if !te.okay && err == nil { |
| 45 | t.Errorf("%q: expected error", te.in) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // TestEvalHscloud nix exercises EvalHscloudNix against |
| 51 | // //go/workspace/exports.nix. |
| 52 | func TestEvalHscloudNix(t *testing.T) { |
Serge Bazanski | 54183ba | 2023-09-01 19:12:14 +0200 | [diff] [blame] | 53 | if _, err := os.Stat("/nix/store"); err != nil { |
| 54 | t.Skip("no /nix/store") |
| 55 | } |
Serge Bazanski | a03b60b | 2023-04-01 14:47:44 +0000 | [diff] [blame] | 56 | ctx, ctxC := context.WithCancel(context.Background()) |
| 57 | defer ctxC() |
| 58 | |
| 59 | { |
| 60 | var got []string |
| 61 | if err := EvalHscloudNix(ctx, &got, "go.workspace.exports.someArray"); err != nil { |
| 62 | t.Errorf("Accessing someArray failed: %v", err) |
| 63 | } else { |
| 64 | want := []string{"hello", "there"} |
| 65 | if diff := cmp.Diff(want, got); diff != "" { |
| 66 | t.Errorf("someArray diff:\n%s", diff) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | { |
| 72 | type barS struct { |
| 73 | Baz int64 `json:"baz"` |
| 74 | } |
| 75 | type gotS struct { |
| 76 | Foo string `json:"foo"` |
| 77 | Bar barS `json:"bar"` |
| 78 | } |
| 79 | var got gotS |
| 80 | if err := EvalHscloudNix(ctx, &got, "go.workspace.exports.someAttrset"); err != nil { |
| 81 | t.Errorf("Accessing someAttrset failed: %v", err) |
| 82 | } else { |
| 83 | want := gotS{ |
| 84 | Foo: "foo", |
| 85 | Bar: barS{ |
| 86 | Baz: 42, |
| 87 | }, |
| 88 | } |
| 89 | if diff := cmp.Diff(want, got); diff != "" { |
| 90 | t.Errorf("someAttrset diff:\n%s", diff) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |