Skip to content

Commit

Permalink
Added test for generate
Browse files Browse the repository at this point in the history
  • Loading branch information
bmonkman committed Jun 17, 2020
1 parent b273fe1 commit 1a1ce93
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
41 changes: 31 additions & 10 deletions internal/generate/generate_test.go
@@ -1,24 +1,45 @@
package generate
package generate_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/commitdev/zero/internal/generate"
"github.com/stretchr/testify/assert"
)

func setupTeardown(t *testing.T) func(t *testing.T) {
outputPath := "tmp/test"
os.RemoveAll(outputPath)
const baseTestFixturesDir = "../../tests/test_data/generate/"

func setupTeardown(t *testing.T) (func(t *testing.T), string) {
tmpDir := filepath.Join(os.TempDir(), "generate")
os.MkdirAll(tmpDir, 0755)
os.RemoveAll(tmpDir)
return func(t *testing.T) {
os.RemoveAll(outputPath)
}
os.RemoveAll(tmpDir)
}, tmpDir
}

func TestGenerateModules(t *testing.T) {
teardown := setupTeardown(t)
teardown, tmpDir := setupTeardown(t)
defer teardown(t)

// TODO organize test utils and write assertions
// generatorConfig := config.LoadGeneratorConfig("../../tests/test_data/configs/zero-basic.yml")
projectConfig := projectconfig.ZeroProjectConfig{
Name: "foo",
Modules: projectconfig.Modules{
"mod1": projectconfig.NewModule(map[string]string{"test": "bar"}, tmpDir, "n/a", baseTestFixturesDir),
},
}

generate.Generate(projectConfig)

content, err := ioutil.ReadFile(filepath.Join(tmpDir, "file_to_template.txt"))
assert.NoError(t, err)

// GenerateModules(generatorConfig)
expectedContent := `Name is foo
Params.test is bar
`
assert.Equal(t, string(content), expectedContent)
}
2 changes: 2 additions & 0 deletions tests/test_data/generate/file_to_template.txt
@@ -0,0 +1,2 @@
Name is {{.Name}}
Params.test is {{.Params.test}}
15 changes: 15 additions & 0 deletions tests/test_data/generate/zero-module.yml
@@ -0,0 +1,15 @@
name: test-generate
description: 'generation test module'
author: 'Commit'

template:
strictMode: true
delimiters:
- '{{'
- '}}'
inputDir: '.'
outputDir: 'test'

requiredCredentials:

parameters:

0 comments on commit 1a1ce93

Please sign in to comment.