|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + yaml "gopkg.in/yaml.v2" |
| 8 | +) |
| 9 | + |
| 10 | +const customConf = ` |
| 11 | +--- |
| 12 | +HelpText: "Please refer to https://github.com/haproxy/haproxy/blob/master/CONTRIBUTING#L632" |
| 13 | +PatchScopes: |
| 14 | + HAProxy Standard Scope: |
| 15 | + - MINOR |
| 16 | + - MEDIUM |
| 17 | + - MAJOR |
| 18 | + - CRITICAL |
| 19 | +PatchTypes: |
| 20 | + SPECIAL patch: |
| 21 | + Values: |
| 22 | + - SPEC |
| 23 | + HAProxy Standard Patch: |
| 24 | + Values: |
| 25 | + - BUG |
| 26 | + - BUILD |
| 27 | + - CLEANUP |
| 28 | + - DOC |
| 29 | + - LICENSE |
| 30 | + - OPTIM |
| 31 | + - RELEASE |
| 32 | + - REORG |
| 33 | + - TEST |
| 34 | + - REVERT |
| 35 | + Scope: HAProxy Standard Scope |
| 36 | + HAProxy Standard Feature Commit: |
| 37 | + Values: |
| 38 | + - MINOR |
| 39 | + - MEDIUM |
| 40 | + - MAJOR |
| 41 | + - CRITICAL |
| 42 | +TagOrder: |
| 43 | + - PatchTypes: |
| 44 | + - SPECIAL patch |
| 45 | + Optional: true |
| 46 | + - PatchTypes: |
| 47 | + - HAProxy Standard Patch |
| 48 | + - HAProxy Standard Feature Commit |
| 49 | +` |
| 50 | + |
| 51 | +func LoadCommitPolicyData(config string) (CommitPolicyConfig, error) { |
| 52 | + var commitPolicy CommitPolicyConfig |
| 53 | + |
| 54 | + if err := yaml.Unmarshal([]byte(config), &commitPolicy); err != nil { |
| 55 | + return CommitPolicyConfig{}, fmt.Errorf("error loading commit policy: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + return commitPolicy, nil |
| 59 | +} |
| 60 | + |
| 61 | +func TestDifferentPolicy(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + c, _ := LoadCommitPolicyData(customConf) |
| 65 | + |
| 66 | + testsSpec := []struct { |
| 67 | + name string |
| 68 | + subject string |
| 69 | + wantErr bool |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "valid type and severity", |
| 73 | + subject: "SPEC: BUG/MEDIUM: config: add default location of path to the configuration file", |
| 74 | + wantErr: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "invalid type RANDOM", |
| 78 | + subject: "RANDOM: BUG/MEDIUM: config: add default location of path to the configuration file", |
| 79 | + wantErr: true, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "invalid type", |
| 83 | + subject: "SPEC: HEHEEEEEE/MEDIUM: config: add default location of path to the configuration file", |
| 84 | + wantErr: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "invalid severity", |
| 88 | + subject: "SPEC: BUG/HEHEEEEEE: config: add default location of path to the configuration file", |
| 89 | + wantErr: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "no existant aditional type", |
| 93 | + subject: "SPEC: BUG/MINOR: CI: config: add default location of path to the configuration file", |
| 94 | + wantErr: true, |
| 95 | + }, |
| 96 | + } |
| 97 | + testsSpec = append(testsSpec, tests...) |
| 98 | + |
| 99 | + for _, tt := range testsSpec { |
| 100 | + tt := tt |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + t.Parallel() |
| 103 | + if err := c.CheckSubject([]byte(tt.subject)); (err != nil) != tt.wantErr { |
| 104 | + t.Errorf("checkSubject() error = %v, wantErr %v", err, tt.wantErr) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments