|
16 | 16 | package artifact
|
17 | 17 |
|
18 | 18 | import (
|
| 19 | + "regexp" |
| 20 | + "strings" |
19 | 21 | "testing"
|
20 | 22 | "time"
|
21 | 23 |
|
22 | 24 | "github.com/stretchr/testify/assert"
|
| 25 | + "github.com/stretchr/testify/require" |
23 | 26 |
|
24 | 27 | provifv1 "github.com/stacklok/minder/pkg/providers/v1"
|
25 | 28 | )
|
26 | 29 |
|
| 30 | +func TestBuildFilter(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + simpleRegex := `^simpleregex$` |
| 33 | + compiledSimpleRegex := regexp.MustCompile(simpleRegex) |
| 34 | + for _, tc := range []struct { |
| 35 | + name string |
| 36 | + tags []string |
| 37 | + regex string |
| 38 | + expected *filter |
| 39 | + mustErr bool |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "tags", |
| 43 | + tags: []string{"hello", "bye"}, |
| 44 | + mustErr: false, |
| 45 | + expected: &filter{ |
| 46 | + tagMatcher: &tagListMatcher{tags: []string{"hello", "bye"}}, |
| 47 | + retentionPeriod: time.Time{}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "empty-tag", |
| 52 | + tags: []string{"hello", ""}, |
| 53 | + mustErr: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "regex", |
| 57 | + tags: []string{}, |
| 58 | + regex: simpleRegex, |
| 59 | + mustErr: false, |
| 60 | + expected: &filter{ |
| 61 | + tagMatcher: &tagRegexMatcher{ |
| 62 | + re: compiledSimpleRegex, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "invalidregexp", |
| 68 | + tags: []string{}, |
| 69 | + regex: `$(invalid^`, |
| 70 | + mustErr: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "valid-long-regexp", |
| 74 | + tags: []string{}, |
| 75 | + regex: `^` + strings.Repeat("A", 1000) + `$`, |
| 76 | + mustErr: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "no-tags", |
| 80 | + tags: []string{}, |
| 81 | + mustErr: false, |
| 82 | + expected: &filter{ |
| 83 | + tagMatcher: &tagAllMatcher{}, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } { |
| 87 | + tc := tc |
| 88 | + t.Run(tc.name, func(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + f, err := BuildFilter(tc.tags, tc.regex) |
| 91 | + if tc.mustErr { |
| 92 | + require.Error(t, err) |
| 93 | + return |
| 94 | + } |
| 95 | + require.NoError(t, err) |
| 96 | + require.NotNil(t, f.tagMatcher) |
| 97 | + require.Equal(t, tc.expected.tagMatcher, f.tagMatcher) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
27 | 102 | func Test_filter_IsSkippable(t *testing.T) {
|
28 | 103 | t.Parallel()
|
29 | 104 |
|
|
0 commit comments