|
| 1 | +package testy |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +const ( |
| 10 | + TagUnit = "unit" |
| 11 | + TagIntegration = "integration" |
| 12 | + TagPostgres = "postgres" |
| 13 | + TagRabbit = "rabbit" |
| 14 | +) |
| 15 | + |
| 16 | +// TaggedTestsEnvVar defines the name of the environment variable for the tagged tests. |
| 17 | +// Example: |
| 18 | +// TaggedTestsEnvVar="TEST_TAGS" |
| 19 | +// env TEST_TAGS="unit" go test ./... |
| 20 | +var TaggedTestsEnvVar = "TEST_TAGS" |
| 21 | + |
| 22 | +// RequireTestTag runs the test if the provided tag matches at least one runtime tag. |
| 23 | +// Example: |
| 24 | +// func TestSomething(t *testing.T) { |
| 25 | +// RequireTestTag(t, "unit") |
| 26 | +// ... |
| 27 | +// } |
| 28 | +// Run with: |
| 29 | +// env TEST_TAGS="unit,integration" go test ./... |
| 30 | +func RequireTestTag(t *testing.T, testTag string) { |
| 31 | + if !getRuntimeTags().contains(testTag) { |
| 32 | + t.Skipf("skipping test '%s', requires '%s' tag", t.Name(), testTag) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// RequireOneOfTestTags runs the test if any of the provided test tags matches one of the runtime tags. |
| 37 | +func RequireOneOfTestTags(t *testing.T, testTags ...string) { |
| 38 | + if !getRuntimeTags().containsAny(testTags...) { |
| 39 | + t.Skipf("skipping test '%s', requires at least one of the following tags: '%s'", |
| 40 | + t.Name(), strings.Join(testTags, ", ")) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// RequireAllTestTags runs the test if all the provided test tags appear in runtime tags. |
| 45 | +func RequireAllTestTags(t *testing.T, testTags ...string) { |
| 46 | + if !getRuntimeTags().containsAll(testTags...) { |
| 47 | + t.Skipf("skipping test '%s', requires all of the following tags: '%s'", |
| 48 | + t.Name(), strings.Join(testTags, ", ")) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +type runtimeTags []string |
| 53 | + |
| 54 | +func getRuntimeTags() runtimeTags { |
| 55 | + return parseTags(os.Getenv(TaggedTestsEnvVar)) |
| 56 | +} |
| 57 | + |
| 58 | +func parseTags(rawTags string) runtimeTags { |
| 59 | + rawTags = strings.ReplaceAll(rawTags, " ", "") |
| 60 | + if rawTags == "" { |
| 61 | + return nil |
| 62 | + } |
| 63 | + return strings.Split(rawTags, ",") |
| 64 | +} |
| 65 | + |
| 66 | +func (rt runtimeTags) contains(targetTag string) bool { |
| 67 | + for _, tag := range rt { |
| 68 | + if tag == targetTag { |
| 69 | + return true |
| 70 | + } |
| 71 | + } |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +func (rt runtimeTags) containsAny(targetTags ...string) bool { |
| 76 | + for _, targetTag := range targetTags { |
| 77 | + if rt.contains(targetTag) { |
| 78 | + return true |
| 79 | + } |
| 80 | + } |
| 81 | + return false |
| 82 | +} |
| 83 | + |
| 84 | +func (rt runtimeTags) containsAll(targetTags ...string) bool { |
| 85 | + for _, targetTag := range targetTags { |
| 86 | + if !rt.contains(targetTag) { |
| 87 | + return false |
| 88 | + } |
| 89 | + } |
| 90 | + return true |
| 91 | +} |
0 commit comments