summaryrefslogtreecommitdiff
path: root/process_test.go
blob: 7441724c2e632402ea7578b7e2e91e64fb1c1cd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// +build !windows

package main

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

	"path/filepath"

	"log"

	"github.com/hairyhenderson/gomplate/data"
	"github.com/stretchr/testify/assert"
)

func TestReadInput(t *testing.T) {
	actual, err := readInputs("foo", nil)
	assert.Nil(t, err)
	assert.Equal(t, &input{"<arg>", "foo"}, actual[0])

	// stdin is "" because during tests it's given /dev/null
	actual, err = readInputs("", []string{"-"})
	assert.Nil(t, err)
	assert.Equal(t, &input{"-", ""}, actual[0])

	actual, err = readInputs("", []string{"process_test.go"})
	assert.Nil(t, err)
	thisFile, _ := os.Open("process_test.go")
	expected, _ := ioutil.ReadAll(thisFile)
	assert.Equal(t, &input{"process_test.go", string(expected)}, actual[0])
}

func TestInputDir(t *testing.T) {
	outDir, err := ioutil.TempDir(filepath.Join("test", "files", "input-dir"), "out-temp-")
	assert.Nil(t, err)
	defer (func() {
		if cerr := os.RemoveAll(outDir); cerr != nil {
			log.Fatalf("Error while removing temporary directory %s : %v", outDir, cerr)
		}
	})()

	src, err := data.ParseSource("config=test/files/input-dir/config.yml")
	assert.Nil(t, err)

	d := &data.Data{
		Sources: map[string]*data.Source{"config": src},
	}
	gomplate := NewGomplate(d, "{{", "}}")
	err = processInputDir(filepath.Join("test", "files", "input-dir", "in"), outDir, []string{"**/*.exclude.txt"}, gomplate)
	assert.Nil(t, err)

	top, err := ioutil.ReadFile(filepath.Join(outDir, "top.txt"))
	assert.Nil(t, err)
	assert.Equal(t, "eins", string(top))

	inner, err := ioutil.ReadFile(filepath.Join(outDir, "inner/nested.txt"))
	assert.Nil(t, err)
	assert.Equal(t, "zwei", string(inner))

	// excluded file should not exist in out dir
	_, err = ioutil.ReadFile(filepath.Join(outDir, "inner/exclude.txt"))
	assert.NotEmpty(t, err)
}