add test for dir gen skip

This commit is contained in:
Shreyas Goenka 2023-05-22 10:16:40 +02:00
parent f6fc63a09e
commit 2511c18352
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package template
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupConfig(t *testing.T, config string) string {
// create target directory with the input config
tmp := t.TempDir()
f, err := os.Create(filepath.Join(tmp, "config.json"))
require.NoError(t, err)
_, err = f.WriteString(config)
f.Close()
require.NoError(t, err)
return tmp
}
func TestMaterializeEmptyDirsAreNotGenerated(t *testing.T) {
tmp := setupConfig(t, `
{
"a": "dir-with-file",
"b": "empty-dir",
"c": "dir-with-skipped-file",
"d": "skipping"
}`)
err := Materialize("./testdata/skip_dir", tmp, filepath.Join(tmp, "config.json"))
require.NoError(t, err)
assert.DirExists(t, filepath.Join(tmp, "dir-with-file"))
assert.FileExists(t, filepath.Join(tmp, "dir-with-file/.gitkeep"))
assert.NoDirExists(t, filepath.Join(tmp, "empty-dir"))
assert.NoDirExists(t, filepath.Join(tmp, "dir-with-skipped-file"))
tmp2 := setupConfig(t, `
{
"a": "dir-with-file",
"b": "empty-dir",
"c": "dir-not-skipped-this-time",
"d": "not-skipping"
}`)
err = Materialize("./testdata/skip_dir", tmp2, filepath.Join(tmp2, "config.json"))
require.NoError(t, err)
assert.DirExists(t, filepath.Join(tmp2, "dir-with-file"))
assert.FileExists(t, filepath.Join(tmp2, "dir-with-file/.gitkeep"))
assert.NoDirExists(t, filepath.Join(tmp2, "empty-dir"))
assert.DirExists(t, filepath.Join(tmp2, "dir-not-skipped-this-time"))
assert.FileExists(t, filepath.Join(tmp2, "dir-not-skipped-this-time/foo"))
}

View File

@ -0,0 +1,14 @@
{
"a": {
"type": "string"
},
"b": {
"type": "string"
},
"c": {
"type": "string"
},
"d": {
"type": "string"
}
}

View File

@ -0,0 +1,4 @@
{{if eq .d "skipping"}}
{{skipThisFile}}
{{end}}
Hello!