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