Skip to content

Commit

Permalink
Fix CI generation code. Added integration test to cover usecase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Direside committed Nov 1, 2019
1 parent b71e472 commit 0d20a5c
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 3 deletions.
8 changes: 6 additions & 2 deletions internal/generate/ci/generate.go
Expand Up @@ -3,6 +3,7 @@ package ci
import (
"fmt"
"sync"
"text/template"

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

var ciConfigPath string
var ciFilename string
var ciTemp *template.Template

switch config.CI.System {
case "jenkins":
ciConfigPath = basePath
ciFilename = "Jenkinsfile"
ciTemp = templator.Jenkins
case "circleci":
ciConfigPath = fmt.Sprintf("%s/%s", basePath, ".circleci/")
ciFilename = "config.yml"
ciTemp = templator.CircleCI
case "travisci":
ciConfigPath = basePath
ciFilename = ".travis.yml"
ciTemp = templator.TravisCI
default:
return &CIGenerationError{"Unsupported CI System", config}
}

util.TemplateFileIfDoesNotExist(ciConfigPath, ciFilename, templator.TravisCI, wg, config)
util.TemplateFileIfDoesNotExist(ciConfigPath, ciFilename, ciTemp, wg, config)

return nil
}
3 changes: 2 additions & 1 deletion templates/ci/circleci.tmpl
Expand Up @@ -12,12 +12,13 @@ jobs:

test:
docker:
- image: {{ .CI.BuildImage }}
steps:
- checkout
- run:
name: Test
command: |
{{ .CI.BuildCommand }}
{{ .CI.TestCommand }}


workflow:
Expand Down
130 changes: 130 additions & 0 deletions tests/integration/ci/ci_test.go
@@ -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))
}
}
29 changes: 29 additions & 0 deletions tests/test_data/ci/expected/.circleci/config.yml
@@ -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
7 changes: 7 additions & 0 deletions tests/test_data/ci/expected/.travis.yml
@@ -0,0 +1,7 @@
language: go
go:
- 1.12

scripts:
- make build
- make test
29 changes: 29 additions & 0 deletions tests/test_data/ci/expected/Jenkinsfile
@@ -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'
}
}
}
}
}
}

0 comments on commit 0d20a5c

Please sign in to comment.