|
| 1 | +package condition_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/commitdev/zero/internal/condition" |
| 9 | + "github.com/commitdev/zero/internal/config/projectconfig" |
| 10 | +) |
| 11 | + |
| 12 | +func testSetup(paramKey, paramValue string) (string, projectconfig.Module) { |
| 13 | + bytes := make([]byte, 15) |
| 14 | + _, _ = rand.Read(bytes) |
| 15 | + name := string(bytes[:]) |
| 16 | + |
| 17 | + _, _ = os.Create(name) |
| 18 | + |
| 19 | + params := make(projectconfig.Parameters) |
| 20 | + params[paramKey] = paramValue |
| 21 | + |
| 22 | + mod := projectconfig.Module{ |
| 23 | + Parameters: params, |
| 24 | + Files: projectconfig.Files{ |
| 25 | + Directory: ".", |
| 26 | + Source: ".", |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + return name, mod |
| 31 | +} |
| 32 | + |
| 33 | +func TestPerformIgnoreFileConditionNotMet(t *testing.T) { |
| 34 | + field := "testField" |
| 35 | + value := "trigger" |
| 36 | + |
| 37 | + filename, mod := testSetup(field, "other value") |
| 38 | + defer os.Remove(filename) |
| 39 | + |
| 40 | + cond := projectconfig.Condition{ |
| 41 | + Action: "ignoreFile", |
| 42 | + MatchField: field, |
| 43 | + WhenValue: value, |
| 44 | + Data: []string{filename}, |
| 45 | + } |
| 46 | + condition.Perform(cond, mod) |
| 47 | + |
| 48 | + _, err := os.Stat(filename) |
| 49 | + if err != nil && !os.IsExist(err) { |
| 50 | + t.Errorf("Expected %v not to be removed\n", filename) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestPerformIgnoreFileConditionMet(t *testing.T) { |
| 55 | + field := "testField" |
| 56 | + value := "trigger" |
| 57 | + |
| 58 | + filename, mod := testSetup(field, value) |
| 59 | + defer os.Remove(filename) |
| 60 | + |
| 61 | + cond := projectconfig.Condition{ |
| 62 | + Action: "ignoreFile", |
| 63 | + MatchField: field, |
| 64 | + WhenValue: value, |
| 65 | + Data: []string{filename}, |
| 66 | + } |
| 67 | + condition.Perform(cond, mod) |
| 68 | + |
| 69 | + _, err := os.Stat(filename) |
| 70 | + if !os.IsNotExist(err) { |
| 71 | + t.Errorf("Expected %v to be removed\n", filename) |
| 72 | + } |
| 73 | +} |
0 commit comments