Home

tmpl @d4c47101ab93639c8fca37aad97250dbeae577a6 - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / registry / registry_test.go
- raw
  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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package registry

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

var (
	tmplDir string
	regDir  string
	destDir string
	reg     *Registry
)

func TestMain(m *testing.M) {
	var err error
	destDir, err = ioutil.TempDir(os.TempDir(), "tmpl-dest")
	if err != nil {
		panic(err)
	}

	// Set up template
	setupTemplate()

	// Set up registry
	setupRegistry()

	status := m.Run()

	if err = os.RemoveAll(destDir); err != nil {
		fmt.Printf("could not clean up temp directory %s\n", destDir)
	}
	if err = os.RemoveAll(tmplDir); err != nil {
		fmt.Printf("could not clean up temp directory %s\n", tmplDir)
	}
	if err = os.RemoveAll(regDir); err != nil {
		fmt.Printf("could not clean up temp directory %s\n", regDir)
	}

	os.Exit(status)
}

func TestTemplate(t *testing.T) {
	t.Run("save", testSave)
	t.Run("get", testGet)
	t.Run("get-fail", testGetFail)
	t.Run("execute", testExecute)
}

func testSave(t *testing.T) {
	if _, err := reg.SaveTemplate("test", tmplDir); err != nil {
		t.Log("could not save template")
		t.FailNow()
	}
}

func testGet(t *testing.T) {
	_, err := reg.GetTemplate("test")
	if err != nil {
		t.Logf("could not get template")
		t.FailNow()
	}
}

func testGetFail(t *testing.T) {
	_, err := reg.GetTemplate("fail")
	if !IsErrTemplateNotFound(err) {
		t.Logf("template should not exist")
		t.FailNow()
	}
}

func setupTemplate() {
	var err error
	tmplDir, err = ioutil.TempDir(os.TempDir(), "tmpl-setup")
	if err != nil {
		panic(err)
	}

	// Template config
	fi, err := os.Create(filepath.Join(tmplDir, "template.toml"))
	if err != nil {
		panic(err)
	}
	_, err = fi.WriteString(tmplTemplate)
	if err != nil {
		panic(err)
	}
	if err := fi.Close(); err != nil {
		panic(err)
	}

	// Template directories
	pkgPath := filepath.Join(tmplDir, "template", "{{upper package}}")
	if err := os.MkdirAll(pkgPath, os.ModePerm); err != nil {
		panic(err)
	}
	fi, err = os.Create(filepath.Join(pkgPath, ".keep"))
	if err != nil {
		panic(err)
	}
	if err := fi.Close(); err != nil {
		panic(err)
	}

	// Template file
	fi, err = os.Create(filepath.Join(tmplDir, "template", "TEST"))
	if err != nil {
		panic(err)
	}
	_, err = fi.WriteString(tmplContents)
	if err != nil {
		panic(err)
	}
	if err := fi.Close(); err != nil {
		panic(err)
	}
}

func setupRegistry() {
	var err error
	regDir, err = ioutil.TempDir(os.TempDir(), "tmpl-reg")
	if err != nil {
		panic(err)
	}

	reg, err = Open(regDir)
	if err != nil {
		panic(err)
	}
}