Skip to content

Commit b7ef7cd

Browse files
authored
feat: Adding CI detection (#6)
Adding CI detection into the test group: - Env var "CI" is set as a `true` in [github actions](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables). - If odize detects it is in a CI environment, and the group of tests has a test option of 'Only', It will fail the group with a message. - Note that the Test option 'Skip' will not cause a failure.
1 parent 0d3efeb commit b7ef7cd

4 files changed

Lines changed: 82 additions & 10 deletions

File tree

examples/examples_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ func TestSkipTestWithinGroup(t *testing.T) {
179179
}
180180

181181
func TestRunOnlyOneWithinGroup(t *testing.T) {
182+
183+
t.Setenv(odize.ENV_CI, "false")
184+
185+
defer func() {
186+
t.Setenv(odize.ENV_CI, "true")
187+
}()
188+
182189
group := odize.NewGroup(t, nil)
183190
err := group.
184191
Test("should equal 1", func(t *testing.T) {

odize.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
const (
1313
// ODIZE_TAGS is the environment variable that is used to filter tests
1414
ODIZE_TAGS = "ODIZE_TAGS"
15+
// ENV variable declared in pipelines such as Github Actions
16+
ENV_CI = "CI"
17+
)
18+
19+
var (
20+
ErrTestOptionNotAllowedInCI = fmt.Errorf("test option 'Only' not allowed in CI environment")
1521
)
1622

1723
// NewGroup - Create a new test group.
@@ -23,14 +29,13 @@ func NewGroup(t *testing.T, tags *[]string) *TestGroup {
2329
groupTags = &[]string{}
2430
}
2531

26-
envTags := env.GetAsSlice(ODIZE_TAGS, ",")
27-
2832
tg := &TestGroup{
2933
t: t,
3034
groupTags: *groupTags,
31-
envTags: envTags,
35+
envTags: env.GetAsSlice(ODIZE_TAGS, ","),
3236
registry: []TestRegistryEntry{},
3337
cache: map[string]struct{}{},
38+
isCIEnv: env.GetAsBool(ENV_CI),
3439
}
3540

3641
tg.registerCleanupTasks()
@@ -93,7 +98,12 @@ func (tg *TestGroup) Run() error {
9398

9499
tg.beforeAll()
95100

96-
entries := filterExecutableTests(tg.t, tg.registry)
101+
entries, err := filterExecutableTests(tg.t, tg.isCIEnv, tg.registry)
102+
if err != nil {
103+
// Stop Run, suite is in an invalid state
104+
tg.complete = true
105+
return fmt.Errorf("Test group \"%s\" error: %w", tg.t.Name(), err)
106+
}
97107

98108
for _, entry := range entries {
99109
tg.beforeEach()
@@ -177,12 +187,16 @@ func shouldSkipTests(groupTags []string, envTags []string) bool {
177187
}
178188

179189
// filterExecutableTests filters tests that are executable within the test group
180-
func filterExecutableTests(t *testing.T, tests []TestRegistryEntry) []TestRegistryEntry {
181-
filtered := filterOnlyAllowedTests(t, tests)
190+
// Note that test option 'Only' is only used for debugging tests, and should not be used in a CI env.
191+
func filterExecutableTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
192+
filtered, err := filterOnlyAllowedTests(t, isCIEnv, tests)
193+
if err != nil {
194+
return filtered, err
195+
}
182196

183197
if len(filtered) > 0 {
184198
// if there are tests that are marked as only, then return 'only' those tests
185-
return filtered
199+
return filtered, nil
186200
}
187201

188202
for _, test := range tests {
@@ -200,18 +214,23 @@ func filterExecutableTests(t *testing.T, tests []TestRegistryEntry) []TestRegist
200214
filtered = append(filtered, test)
201215
}
202216

203-
return filtered
217+
return filtered, nil
204218
}
205219

206220
// filterOnlyAllowedTests filters tests that are marked as only within a test group
207-
func filterOnlyAllowedTests(t *testing.T, tests []TestRegistryEntry) []TestRegistryEntry {
221+
// If the framework detects that the test is running under a CI environment and the group has tests with 'Only', then it will return an error
222+
func filterOnlyAllowedTests(t *testing.T, isCIEnv bool, tests []TestRegistryEntry) ([]TestRegistryEntry, error) {
208223
filtered := []TestRegistryEntry{}
209224

210225
for _, test := range tests {
226+
if test.options.Only && isCIEnv {
227+
return filtered, ErrTestOptionNotAllowedInCI
228+
}
229+
211230
if test.options.Only {
212231
filtered = append(filtered, test)
213232
}
214233
}
215234

216-
return filtered
235+
return filtered, nil
217236
}

odize_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package odize
22

33
import (
4+
"errors"
45
"testing"
56
)
67

@@ -287,6 +288,12 @@ func TestOptionSkip(t *testing.T) {
287288
}
288289

289290
func TestOptionOnly(t *testing.T) {
291+
t.Setenv(ENV_CI, "false")
292+
293+
defer func() {
294+
t.Setenv(ENV_CI, "true")
295+
}()
296+
290297
tg := NewGroup(t, nil)
291298

292299
testCall := 0
@@ -301,4 +308,42 @@ func TestOptionOnly(t *testing.T) {
301308
Run()
302309
AssertNoError(t, err)
303310
AssertEqual(t, 1, testCall)
311+
312+
}
313+
314+
func TestCITest(t *testing.T) {
315+
t.Setenv(ENV_CI, "true")
316+
317+
defer func() {
318+
t.Setenv(ENV_CI, "false")
319+
}()
320+
321+
tg := NewGroup(t, nil)
322+
323+
err := tg.
324+
Test("should fail group if ENV_CI is true", func(t *testing.T) {
325+
AssertTrue(t, true)
326+
}, Only()).
327+
Test("should not execute test", func(t *testing.T) {
328+
AssertTrue(t, false)
329+
}).
330+
Run()
331+
AssertTrue(t, errors.Is(err, ErrTestOptionNotAllowedInCI))
332+
}
333+
334+
func TestCITestNoOnly(t *testing.T) {
335+
t.Setenv(ENV_CI, "true")
336+
337+
defer func() {
338+
t.Setenv(ENV_CI, "false")
339+
}()
340+
341+
tg := NewGroup(t, nil)
342+
343+
err := tg.
344+
Test("should pass", func(t *testing.T) {
345+
AssertTrue(t, true)
346+
}).
347+
Run()
348+
AssertNoError(t, err)
304349
}

types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type TestGroup struct {
1818
registry []TestRegistryEntry
1919
cache map[string]struct{}
2020
errors ErrorList
21+
isCIEnv bool
2122
}
2223

2324
// TestFn - Test function

0 commit comments

Comments
 (0)