diff --git a/libs/template/materialize_test.go b/libs/template/materialize_test.go new file mode 100644 index 000000000..dff678fa6 --- /dev/null +++ b/libs/template/materialize_test.go @@ -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")) +} diff --git a/libs/template/testdata/skip_dir/schema.json b/libs/template/testdata/skip_dir/schema.json new file mode 100644 index 000000000..9687bce76 --- /dev/null +++ b/libs/template/testdata/skip_dir/schema.json @@ -0,0 +1,14 @@ +{ + "a": { + "type": "string" + }, + "b": { + "type": "string" + }, + "c": { + "type": "string" + }, + "d": { + "type": "string" + } +} diff --git a/libs/template/testdata/skip_dir/template/{{.a}}/.gitkeep b/libs/template/testdata/skip_dir/template/{{.a}}/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/libs/template/testdata/skip_dir/template/{{.c}}/foo b/libs/template/testdata/skip_dir/template/{{.c}}/foo new file mode 100644 index 000000000..9925ff1dc --- /dev/null +++ b/libs/template/testdata/skip_dir/template/{{.c}}/foo @@ -0,0 +1,4 @@ +{{if eq .d "skipping"}} +{{skipThisFile}} +{{end}} +Hello!