|
1 | 1 | package batches |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
4 | 7 |
|
5 | 8 | func TestParseBatchSpec(t *testing.T) { |
6 | 9 | t.Run("valid", func(t *testing.T) { |
@@ -87,4 +90,84 @@ changesetTemplate: |
87 | 90 | t.Fatalf("wrong error. want=%q, have=%q", wantErr, haveErr) |
88 | 91 | } |
89 | 92 | }) |
| 93 | + |
| 94 | + t.Run("uses unsupported conditional exec", func(t *testing.T) { |
| 95 | + const spec = ` |
| 96 | +name: hello-world |
| 97 | +description: Add Hello World to READMEs |
| 98 | +on: |
| 99 | + - repositoriesMatchingQuery: file:README.md |
| 100 | +steps: |
| 101 | + - run: echo Hello World | tee -a $(find -name README.md) |
| 102 | + if: "false" |
| 103 | + container: alpine:3 |
| 104 | +
|
| 105 | +changesetTemplate: |
| 106 | + title: Hello World |
| 107 | + body: My first batch change! |
| 108 | + branch: hello-world |
| 109 | + commit: |
| 110 | + message: Append Hello World to all README.md files |
| 111 | + published: false |
| 112 | +` |
| 113 | + |
| 114 | + _, err := ParseBatchSpec([]byte(spec), FeatureFlags{}) |
| 115 | + if err == nil { |
| 116 | + t.Fatal("no error returned") |
| 117 | + } |
| 118 | + |
| 119 | + wantErr := `1 error occurred: |
| 120 | + * step 1 in batch spec uses the 'if' attribute for conditional execution, which is not supported in this Sourcegraph version |
| 121 | +
|
| 122 | +` |
| 123 | + haveErr := err.Error() |
| 124 | + if haveErr != wantErr { |
| 125 | + t.Fatalf("wrong error. want=%q, have=%q", wantErr, haveErr) |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + t.Run("parsing if attribute", func(t *testing.T) { |
| 130 | + const specTemplate = ` |
| 131 | +name: hello-world |
| 132 | +description: Add Hello World to READMEs |
| 133 | +on: |
| 134 | + - repositoriesMatchingQuery: file:README.md |
| 135 | +steps: |
| 136 | + - run: echo Hello World | tee -a $(find -name README.md) |
| 137 | + if: %s |
| 138 | + container: alpine:3 |
| 139 | +
|
| 140 | +changesetTemplate: |
| 141 | + title: Hello World |
| 142 | + body: My first batch change! |
| 143 | + branch: hello-world |
| 144 | + commit: |
| 145 | + message: Append Hello World to all README.md files |
| 146 | + published: false |
| 147 | +` |
| 148 | + |
| 149 | + for _, tt := range []struct { |
| 150 | + raw string |
| 151 | + want string |
| 152 | + }{ |
| 153 | + {raw: `"true"`, want: "true"}, |
| 154 | + {raw: `"false"`, want: "false"}, |
| 155 | + {raw: `true`, want: "true"}, |
| 156 | + {raw: `false`, want: "false"}, |
| 157 | + {raw: `"${{ foobar }}"`, want: "${{ foobar }}"}, |
| 158 | + {raw: `${{ foobar }}`, want: "${{ foobar }}"}, |
| 159 | + {raw: `foobar`, want: "foobar"}, |
| 160 | + } { |
| 161 | + spec := fmt.Sprintf(specTemplate, tt.raw) |
| 162 | + batchSpec, err := ParseBatchSpec([]byte(spec), FeatureFlags{AllowConditionalExec: true}) |
| 163 | + if err != nil { |
| 164 | + t.Fatal(err) |
| 165 | + } |
| 166 | + |
| 167 | + fmt.Printf("len(batchSpec.Steps)=%d", len(batchSpec.Steps)) |
| 168 | + if batchSpec.Steps[0].IfCondition() != tt.want { |
| 169 | + t.Fatalf("wrong IfCondition. want=%q, got=%q", tt.want, batchSpec.Steps[0].IfCondition()) |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
90 | 173 | } |
0 commit comments