|
1 | 1 | package values_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/ghodss/yaml" |
7 | 9 | "github.com/maxatome/go-testdeep/td" |
8 | 10 | "github.com/ovh/utask/engine/values" |
| 11 | + "github.com/ovh/utask/pkg/utils" |
9 | 12 | ) |
10 | 13 |
|
11 | 14 | func TestTmpl(t *testing.T) { |
@@ -37,3 +40,113 @@ func TestTmpl(t *testing.T) { |
37 | 40 | td.CmpNil(t, err) |
38 | 41 | td.Cmp(t, string(output), "example.org") |
39 | 42 | } |
| 43 | + |
| 44 | +func TestJsonNumber(t *testing.T) { |
| 45 | + input := ` |
| 46 | +{ |
| 47 | + "step": { |
| 48 | + "first": { |
| 49 | + "output": { |
| 50 | + "my-payload": { |
| 51 | + "foo": 0 |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +` |
| 58 | + |
| 59 | + assert, require := td.AssertRequire(t) |
| 60 | + |
| 61 | + obj := map[string]map[string]map[string]interface{}{} |
| 62 | + err := utils.JSONnumberUnmarshal(strings.NewReader(input), &obj) |
| 63 | + require.Nil(err) |
| 64 | + |
| 65 | + v := values.NewValues() |
| 66 | + v.SetOutput("first", obj["step"]["first"]["output"]) |
| 67 | + v.SetVariables([]values.Variable{ |
| 68 | + { |
| 69 | + Name: "foobar0", |
| 70 | + Value: "ok", |
| 71 | + }, |
| 72 | + { |
| 73 | + Name: "foobar1", |
| 74 | + Value: "{{ `ok` }}", |
| 75 | + }, |
| 76 | + { |
| 77 | + Name: "foobar2", |
| 78 | + Expression: `var foo = {"foo":"bar"}; |
| 79 | +foo.foo;`, |
| 80 | + }, |
| 81 | + }) |
| 82 | + |
| 83 | + output := v.GetOutput("first") |
| 84 | + outputMap, ok := output.(map[string]interface{}) |
| 85 | + require.True(ok) |
| 86 | + innerOutputMap, ok := outputMap["my-payload"].(map[string]interface{}) |
| 87 | + require.True(ok) |
| 88 | + |
| 89 | + assert.Cmp(fmt.Sprintf("%T", innerOutputMap["foo"]), "json.Number") |
| 90 | + |
| 91 | + outputba, err := v.Apply("{{ eval `foobar0` }}", nil, "") |
| 92 | + require.Nil(err) |
| 93 | + assert.Cmp(string(outputba), "ok") |
| 94 | + |
| 95 | + outputba, err = v.Apply("{{ eval `foobar1` }}", nil, "") |
| 96 | + require.Nil(err) |
| 97 | + assert.Cmp(string(outputba), "ok") |
| 98 | + |
| 99 | + outputba, err = v.Apply("{{ evalCache `foobar2` }}", nil, "") |
| 100 | + require.Nil(err) |
| 101 | + assert.Cmp(string(outputba), "bar") |
| 102 | + |
| 103 | + newV, err := v.Clone() |
| 104 | + require.Nil(err, "received unexpected error: %s", err) |
| 105 | + |
| 106 | + newOutput := newV.GetOutput("first") |
| 107 | + newOutputMap, ok := newOutput.(map[string]interface{}) |
| 108 | + require.True(ok) |
| 109 | + newInnerOutputMap, ok := newOutputMap["my-payload"].(map[string]interface{}) |
| 110 | + require.True(ok) |
| 111 | + |
| 112 | + assert.Cmp(fmt.Sprintf("%T", newInnerOutputMap["foo"]), "json.Number") |
| 113 | + |
| 114 | + outputba, err = newV.Apply("{{ eval `foobar0` }}", nil, "") |
| 115 | + require.Nil(err) |
| 116 | + assert.Cmp(string(outputba), "ok") |
| 117 | + |
| 118 | + outputba, err = newV.Apply("{{ eval `foobar1` }}", nil, "") |
| 119 | + require.Nil(err) |
| 120 | + assert.Cmp(string(outputba), "ok") |
| 121 | + |
| 122 | + outputba, err = newV.Apply("{{ evalCache `foobar2` }}", nil, "") |
| 123 | + require.Nil(err) |
| 124 | + assert.Cmp(string(outputba), "bar") |
| 125 | + |
| 126 | + // with iterator now |
| 127 | + iterator := map[string]map[string][]string{ |
| 128 | + "foo": map[string][]string{ |
| 129 | + "bar": []string{"foobar", "buzz"}, |
| 130 | + }, |
| 131 | + } |
| 132 | + v.SetIterator(iterator) |
| 133 | + |
| 134 | + outputba, err = v.Apply("{{ index .iterator.foo.bar 1 }}", nil, "") |
| 135 | + require.Nil(err) |
| 136 | + assert.Cmp(string(outputba), "buzz") |
| 137 | + |
| 138 | + newV, err = v.Clone() |
| 139 | + require.Nil(err, "received unexpected error: %s", err) |
| 140 | + |
| 141 | + newOutput = newV.GetOutput("first") |
| 142 | + newOutputMap, ok = newOutput.(map[string]interface{}) |
| 143 | + require.True(ok) |
| 144 | + newInnerOutputMap, ok = newOutputMap["my-payload"].(map[string]interface{}) |
| 145 | + require.True(ok) |
| 146 | + |
| 147 | + assert.Cmp(fmt.Sprintf("%T", newInnerOutputMap["foo"]), "json.Number") |
| 148 | + |
| 149 | + outputba, err = newV.Apply("{{ index .iterator.foo.bar 1 }}", nil, "") |
| 150 | + require.Nil(err) |
| 151 | + assert.Cmp(string(outputba), "buzz") |
| 152 | +} |
0 commit comments