|
| 1 | +package ci_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/commitdev/commit0/internal/config" |
| 11 | + "github.com/commitdev/commit0/internal/generate/ci" |
| 12 | + "github.com/commitdev/commit0/internal/templator" |
| 13 | + "github.com/gobuffalo/packr/v2" |
| 14 | +) |
| 15 | + |
| 16 | +var testData = "../../test_data/ci/" |
| 17 | + |
| 18 | +// setupTeardown removes all the generated test files before and after |
| 19 | +// the test runs to ensure clean data. |
| 20 | +func setupTeardown(t *testing.T) func(t *testing.T) { |
| 21 | + os.RemoveAll("../../test_data/ci/actual") |
| 22 | + return func(t *testing.T) { |
| 23 | + os.RemoveAll("../../test_data/ci/actual") |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestGenerateJenkins(t *testing.T) { |
| 28 | + teardown := setupTeardown(t) |
| 29 | + defer teardown(t) |
| 30 | + |
| 31 | + templates := packr.New("templates", "../../../templates") |
| 32 | + testTemplator := templator.NewTemplator(templates) |
| 33 | + |
| 34 | + var waitgroup *sync.WaitGroup |
| 35 | + |
| 36 | + testConf := &config.Commit0Config{ |
| 37 | + Language: "go", |
| 38 | + CI: config.CI{ |
| 39 | + System: "jenkins", |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + err := ci.Generate(testTemplator.CI, testConf, testData+"/actual", waitgroup) |
| 44 | + if err != nil { |
| 45 | + t.Errorf("Error when executing test. %s", err) |
| 46 | + } |
| 47 | + |
| 48 | + actual, err := ioutil.ReadFile(testData + "actual/Jenkinsfile") |
| 49 | + if err != nil { |
| 50 | + t.Errorf("Error reading created file: %s", err.Error()) |
| 51 | + } |
| 52 | + expected, err := ioutil.ReadFile(testData + "/expected/Jenkinsfile") |
| 53 | + if err != nil { |
| 54 | + t.Errorf("Error reading created file: %s", err.Error()) |
| 55 | + } |
| 56 | + |
| 57 | + if !bytes.Equal(expected, actual) { |
| 58 | + t.Errorf("want:\n%s\n\n, got:\n%s\n\n", string(expected), string(actual)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestGenerateCircleCI(t *testing.T) { |
| 63 | + teardown := setupTeardown(t) |
| 64 | + defer teardown(t) |
| 65 | + |
| 66 | + templates := packr.New("templates", "../../../templates") |
| 67 | + testTemplator := templator.NewTemplator(templates) |
| 68 | + |
| 69 | + var waitgroup *sync.WaitGroup |
| 70 | + |
| 71 | + testConf := &config.Commit0Config{ |
| 72 | + Language: "go", |
| 73 | + CI: config.CI{ |
| 74 | + System: "circleci", |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + err := ci.Generate(testTemplator.CI, testConf, testData+"/actual", waitgroup) |
| 79 | + if err != nil { |
| 80 | + t.Errorf("Error when executing test. %s", err) |
| 81 | + } |
| 82 | + |
| 83 | + actual, err := ioutil.ReadFile(testData + "actual/.circleci/config.yml") |
| 84 | + if err != nil { |
| 85 | + t.Errorf("Error reading created file: %s", err.Error()) |
| 86 | + } |
| 87 | + expected, err := ioutil.ReadFile(testData + "/expected/.circleci/config.yml") |
| 88 | + if err != nil { |
| 89 | + t.Errorf("Error reading created file: %s", err.Error()) |
| 90 | + } |
| 91 | + |
| 92 | + if !bytes.Equal(expected, actual) { |
| 93 | + t.Errorf("want:\n%s\n\ngot:\n%s\n\n", string(expected), string(actual)) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestGenerateTravisCI(t *testing.T) { |
| 98 | + teardown := setupTeardown(t) |
| 99 | + defer teardown(t) |
| 100 | + |
| 101 | + templates := packr.New("templates", "../../../templates") |
| 102 | + testTemplator := templator.NewTemplator(templates) |
| 103 | + |
| 104 | + var waitgroup *sync.WaitGroup |
| 105 | + |
| 106 | + testConf := &config.Commit0Config{ |
| 107 | + Language: "go", |
| 108 | + CI: config.CI{ |
| 109 | + System: "travisci", |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + err := ci.Generate(testTemplator.CI, testConf, testData+"/actual", waitgroup) |
| 114 | + if err != nil { |
| 115 | + t.Errorf("Error when executing test. %s", err) |
| 116 | + } |
| 117 | + |
| 118 | + actual, err := ioutil.ReadFile(testData + "actual/.travis.yml") |
| 119 | + if err != nil { |
| 120 | + t.Errorf("Error reading created file: %s", err.Error()) |
| 121 | + } |
| 122 | + expected, err := ioutil.ReadFile(testData + "/expected/.travis.yml") |
| 123 | + if err != nil { |
| 124 | + t.Errorf("Error reading created file: %s", err.Error()) |
| 125 | + } |
| 126 | + |
| 127 | + if !bytes.Equal(expected, actual) { |
| 128 | + t.Errorf("want:\n%s\n\n, got:\n%s\n\n", string(expected), string(actual)) |
| 129 | + } |
| 130 | +} |
0 commit comments