Skip to content

Commit 0d20a5c

Browse files
committed
Fix CI generation code. Added integration test to cover usecase.
1 parent b71e472 commit 0d20a5c

File tree

6 files changed

+203
-3
lines changed

6 files changed

+203
-3
lines changed

internal/generate/ci/generate.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ci
33
import (
44
"fmt"
55
"sync"
6+
"text/template"
67

78
"github.com/commitdev/commit0/internal/config"
89
"github.com/commitdev/commit0/internal/templator"
@@ -47,22 +48,25 @@ func Generate(templator *templator.CITemplator, config *config.Commit0Config, ba
4748

4849
var ciConfigPath string
4950
var ciFilename string
51+
var ciTemp *template.Template
5052

5153
switch config.CI.System {
5254
case "jenkins":
5355
ciConfigPath = basePath
5456
ciFilename = "Jenkinsfile"
57+
ciTemp = templator.Jenkins
5558
case "circleci":
5659
ciConfigPath = fmt.Sprintf("%s/%s", basePath, ".circleci/")
5760
ciFilename = "config.yml"
61+
ciTemp = templator.CircleCI
5862
case "travisci":
5963
ciConfigPath = basePath
6064
ciFilename = ".travis.yml"
65+
ciTemp = templator.TravisCI
6166
default:
6267
return &CIGenerationError{"Unsupported CI System", config}
6368
}
64-
65-
util.TemplateFileIfDoesNotExist(ciConfigPath, ciFilename, templator.TravisCI, wg, config)
69+
util.TemplateFileIfDoesNotExist(ciConfigPath, ciFilename, ciTemp, wg, config)
6670

6771
return nil
6872
}

templates/ci/circleci.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ jobs:
1212

1313
test:
1414
docker:
15+
- image: {{ .CI.BuildImage }}
1516
steps:
1617
- checkout
1718
- run:
1819
name: Test
1920
command: |
20-
{{ .CI.BuildCommand }}
21+
{{ .CI.TestCommand }}
2122

2223

2324
workflow:

tests/integration/ci/ci_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: 2.1
2+
jobs:
3+
build:
4+
docker:
5+
- image: golang/golang:1.12
6+
steps:
7+
- checkout
8+
- run:
9+
name: Build
10+
command: |
11+
make build
12+
13+
test:
14+
docker:
15+
- image: golang/golang:1.12
16+
steps:
17+
- checkout
18+
- run:
19+
name: Test
20+
command: |
21+
make test
22+
23+
24+
workflow:
25+
version: 2.1
26+
build_and_test:
27+
jobs:
28+
- build
29+
- test
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: go
2+
go:
3+
- 1.12
4+
5+
scripts:
6+
- make build
7+
- make test
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
pipeline {
2+
agent none
3+
stages {
4+
stage('Build and Test') {
5+
parallel {
6+
stage('Build') {
7+
agent {
8+
docker {
9+
image 'golang/golang:1.12'
10+
}
11+
}
12+
steps {
13+
sh 'make build'
14+
}
15+
}
16+
stage('Test') {
17+
agent {
18+
docker {
19+
image 'golang/golang:1.12'
20+
}
21+
}
22+
steps {
23+
sh 'make test'
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)