|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/robertkrimen/otto" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "github.com/Southclaws/wadsworth/service/task" |
| 10 | +) |
| 11 | + |
| 12 | +func Test_applyFileTargets(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + script string |
| 16 | + wantTargets task.Targets |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + {"one", `T({ |
| 20 | + name: "name", |
| 21 | + url: "../test.local", |
| 22 | + up: ["echo", "hello world"] |
| 23 | + })`, Targets{ |
| 24 | + { |
| 25 | + Name: "name", |
| 26 | + RepoURL: "../test.local", |
| 27 | + Up: []string{"echo", "hello world"}, |
| 28 | + }, |
| 29 | + }, false}, |
| 30 | + {"variable", ` |
| 31 | + var url = "https://github.com/Southclaws/"; |
| 32 | +
|
| 33 | + T({name: "1", url: url + "project1", up: ["sleep"]}); |
| 34 | + T({name: "2", url: url + "project2", up: ["sleep"]}); |
| 35 | + T({name: "3", url: url + "project3", up: ["sleep"]}); |
| 36 | +
|
| 37 | + console.log("done!"); |
| 38 | + `, Targets{ |
| 39 | + {Name: "1", RepoURL: "https://github.com/Southclaws/project1", Up: []string{"sleep"}}, |
| 40 | + {Name: "2", RepoURL: "https://github.com/Southclaws/project2", Up: []string{"sleep"}}, |
| 41 | + {Name: "3", RepoURL: "https://github.com/Southclaws/project3", Up: []string{"sleep"}}, |
| 42 | + }, false}, |
| 43 | + {"envmap", ` |
| 44 | + var url = "https://github.com/Southclaws/"; |
| 45 | +
|
| 46 | + T({name: "1", url: url + "project1", up: ["sleep"], env: {PASSWORD: "nope"}}); |
| 47 | + T({name: "2", url: url + "project2", up: ["sleep"], env: {PASSWORD: "nope"}}); |
| 48 | + T({name: "3", url: url + "project3", up: ["sleep"], env: {PASSWORD: "nope"}}); |
| 49 | +
|
| 50 | + console.log("done!"); |
| 51 | + `, Targets{ |
| 52 | + {Name: "1", RepoURL: "https://github.com/Southclaws/project1", Up: []string{"sleep"}, Env: map[string]string{"PASSWORD": "nope"}}, |
| 53 | + {Name: "2", RepoURL: "https://github.com/Southclaws/project2", Up: []string{"sleep"}, Env: map[string]string{"PASSWORD": "nope"}}, |
| 54 | + {Name: "3", RepoURL: "https://github.com/Southclaws/project3", Up: []string{"sleep"}, Env: map[string]string{"PASSWORD": "nope"}}, |
| 55 | + }, false}, |
| 56 | + {"badtype", `T({name: "name", url: "../test.local", up: 1.23})`, Targets{{}}, true}, |
| 57 | + {"missingkey", `T({name: "name", url: "../test.local"})`, Targets{{}}, true}, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + cb := configBuilder{ |
| 63 | + vm: otto.New(), |
| 64 | + state: new(State), |
| 65 | + scripts: []string{tt.script}, |
| 66 | + } |
| 67 | + |
| 68 | + err := cb.construct() |
| 69 | + if tt.wantErr { |
| 70 | + assert.Error(t, err) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + assert.NoError(t, err) |
| 75 | + assert.Equal(t, tt.wantTargets, cb.state.Targets) |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
0 commit comments