Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix CI generation code. Added integration test to cover usecase.
- Loading branch information
Showing
6 changed files
with
203 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package ci_test | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/commitdev/commit0/internal/config" | ||
"github.com/commitdev/commit0/internal/generate/ci" | ||
"github.com/commitdev/commit0/internal/templator" | ||
"github.com/gobuffalo/packr/v2" | ||
) | ||
|
||
var testData = "../../test_data/ci/" | ||
|
||
// setupTeardown removes all the generated test files before and after | ||
// the test runs to ensure clean data. | ||
func setupTeardown(t *testing.T) func(t *testing.T) { | ||
os.RemoveAll("../../test_data/ci/actual") | ||
return func(t *testing.T) { | ||
os.RemoveAll("../../test_data/ci/actual") | ||
} | ||
} | ||
|
||
func TestGenerateJenkins(t *testing.T) { | ||
teardown := setupTeardown(t) | ||
defer teardown(t) | ||
|
||
templates := packr.New("templates", "../../../templates") | ||
testTemplator := templator.NewTemplator(templates) | ||
|
||
var waitgroup *sync.WaitGroup | ||
|
||
testConf := &config.Commit0Config{ | ||
Language: "go", | ||
CI: config.CI{ | ||
System: "jenkins", | ||
}, | ||
} | ||
|
||
err := ci.Generate(testTemplator.CI, testConf, testData+"/actual", waitgroup) | ||
if err != nil { | ||
t.Errorf("Error when executing test. %s", err) | ||
} | ||
|
||
actual, err := ioutil.ReadFile(testData + "actual/Jenkinsfile") | ||
if err != nil { | ||
t.Errorf("Error reading created file: %s", err.Error()) | ||
} | ||
expected, err := ioutil.ReadFile(testData + "/expected/Jenkinsfile") | ||
if err != nil { | ||
t.Errorf("Error reading created file: %s", err.Error()) | ||
} | ||
|
||
if !bytes.Equal(expected, actual) { | ||
t.Errorf("want:\n%s\n\n, got:\n%s\n\n", string(expected), string(actual)) | ||
} | ||
} | ||
|
||
func TestGenerateCircleCI(t *testing.T) { | ||
teardown := setupTeardown(t) | ||
defer teardown(t) | ||
|
||
templates := packr.New("templates", "../../../templates") | ||
testTemplator := templator.NewTemplator(templates) | ||
|
||
var waitgroup *sync.WaitGroup | ||
|
||
testConf := &config.Commit0Config{ | ||
Language: "go", | ||
CI: config.CI{ | ||
System: "circleci", | ||
}, | ||
} | ||
|
||
err := ci.Generate(testTemplator.CI, testConf, testData+"/actual", waitgroup) | ||
if err != nil { | ||
t.Errorf("Error when executing test. %s", err) | ||
} | ||
|
||
actual, err := ioutil.ReadFile(testData + "actual/.circleci/config.yml") | ||
if err != nil { | ||
t.Errorf("Error reading created file: %s", err.Error()) | ||
} | ||
expected, err := ioutil.ReadFile(testData + "/expected/.circleci/config.yml") | ||
if err != nil { | ||
t.Errorf("Error reading created file: %s", err.Error()) | ||
} | ||
|
||
if !bytes.Equal(expected, actual) { | ||
t.Errorf("want:\n%s\n\ngot:\n%s\n\n", string(expected), string(actual)) | ||
} | ||
} | ||
|
||
func TestGenerateTravisCI(t *testing.T) { | ||
teardown := setupTeardown(t) | ||
defer teardown(t) | ||
|
||
templates := packr.New("templates", "../../../templates") | ||
testTemplator := templator.NewTemplator(templates) | ||
|
||
var waitgroup *sync.WaitGroup | ||
|
||
testConf := &config.Commit0Config{ | ||
Language: "go", | ||
CI: config.CI{ | ||
System: "travisci", | ||
}, | ||
} | ||
|
||
err := ci.Generate(testTemplator.CI, testConf, testData+"/actual", waitgroup) | ||
if err != nil { | ||
t.Errorf("Error when executing test. %s", err) | ||
} | ||
|
||
actual, err := ioutil.ReadFile(testData + "actual/.travis.yml") | ||
if err != nil { | ||
t.Errorf("Error reading created file: %s", err.Error()) | ||
} | ||
expected, err := ioutil.ReadFile(testData + "/expected/.travis.yml") | ||
if err != nil { | ||
t.Errorf("Error reading created file: %s", err.Error()) | ||
} | ||
|
||
if !bytes.Equal(expected, actual) { | ||
t.Errorf("want:\n%s\n\n, got:\n%s\n\n", string(expected), string(actual)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
version: 2.1 | ||
jobs: | ||
build: | ||
docker: | ||
- image: golang/golang:1.12 | ||
steps: | ||
- checkout | ||
- run: | ||
name: Build | ||
command: | | ||
make build | ||
test: | ||
docker: | ||
- image: golang/golang:1.12 | ||
steps: | ||
- checkout | ||
- run: | ||
name: Test | ||
command: | | ||
make test | ||
workflow: | ||
version: 2.1 | ||
build_and_test: | ||
jobs: | ||
- build | ||
- test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
language: go | ||
go: | ||
- 1.12 | ||
|
||
scripts: | ||
- make build | ||
- make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
pipeline { | ||
agent none | ||
stages { | ||
stage('Build and Test') { | ||
parallel { | ||
stage('Build') { | ||
agent { | ||
docker { | ||
image 'golang/golang:1.12' | ||
} | ||
} | ||
steps { | ||
sh 'make build' | ||
} | ||
} | ||
stage('Test') { | ||
agent { | ||
docker { | ||
image 'golang/golang:1.12' | ||
} | ||
} | ||
steps { | ||
sh 'make test' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |