summaryrefslogtreecommitdiff
path: root/process_test.go
blob: 23a4e1a79bcd2fda9dd3c9b65220987d38730242 (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
// +build !windows

package main

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

	"path/filepath"

	"log"

	"github.com/stretchr/testify/assert"
)

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

	// stdin is "" because during tests it's given /dev/null
	actual, err = readInputs("", []string{"-"})
	assert.Nil(t, err)
	assert.Equal(t, "", 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, 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 := ParseSource("config=test/files/input-dir/config.yml")
	assert.Nil(t, err)

	data := &Data{
		Sources: map[string]*Source{"config": src},
	}
	gomplate := NewGomplate(data, "{{", "}}")
	err = processInputDir(filepath.Join("test", "files", "input-dir", "in"), outDir, 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))
}