All checks were successful
CI / Test (1.24) (push) Successful in -29m0s
CI / Test (1.25) (push) Successful in -29m10s
Integration Tests / Integration Tests (push) Successful in -29m6s
CI / Build (push) Successful in -30m1s
CI / Lint (push) Successful in -29m43s
Release / Build and Release (push) Successful in -29m56s
135 lines
3.6 KiB
Go
135 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// writeTestTemplate writes a minimal Go text template file.
|
|
func writeTestTemplate(t *testing.T, path string) {
|
|
t.Helper()
|
|
content := []byte(`{{.Name}}`)
|
|
if err := os.WriteFile(path, content, 0644); err != nil {
|
|
t.Fatalf("failed to write template file %s: %v", path, err)
|
|
}
|
|
}
|
|
|
|
func TestRunTempl_FromListMutuallyExclusiveWithFromPath(t *testing.T) {
|
|
saved := saveTemplState()
|
|
defer restoreTemplState(saved)
|
|
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "schema.json")
|
|
tmpl := filepath.Join(dir, "tmpl.tmpl")
|
|
writeTestJSON(t, file, []string{"users"})
|
|
writeTestTemplate(t, tmpl)
|
|
|
|
templSourceType = "json"
|
|
templSourcePath = file
|
|
templFromList = []string{file}
|
|
templTemplatePath = tmpl
|
|
templOutputPath = ""
|
|
templMode = "database"
|
|
templFilenamePattern = "{{.Name}}.txt"
|
|
|
|
err := runTempl(nil, nil)
|
|
if err == nil {
|
|
t.Error("expected error when --from-path and --from-list are both set")
|
|
}
|
|
}
|
|
|
|
func TestRunTempl_FromListSingleFile(t *testing.T) {
|
|
saved := saveTemplState()
|
|
defer restoreTemplState(saved)
|
|
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "schema.json")
|
|
tmpl := filepath.Join(dir, "tmpl.tmpl")
|
|
outFile := filepath.Join(dir, "output.txt")
|
|
writeTestJSON(t, file, []string{"users"})
|
|
writeTestTemplate(t, tmpl)
|
|
|
|
templSourceType = "json"
|
|
templSourcePath = ""
|
|
templSourceConn = ""
|
|
templFromList = []string{file}
|
|
templTemplatePath = tmpl
|
|
templOutputPath = outFile
|
|
templSchemaFilter = ""
|
|
templMode = "database"
|
|
templFilenamePattern = "{{.Name}}.txt"
|
|
|
|
if err := runTempl(nil, nil); err != nil {
|
|
t.Fatalf("runTempl() error = %v", err)
|
|
}
|
|
if _, err := os.Stat(outFile); os.IsNotExist(err) {
|
|
t.Error("expected output file to be created")
|
|
}
|
|
}
|
|
|
|
func TestRunTempl_FromListMultipleFiles(t *testing.T) {
|
|
saved := saveTemplState()
|
|
defer restoreTemplState(saved)
|
|
|
|
dir := t.TempDir()
|
|
file1 := filepath.Join(dir, "users.json")
|
|
file2 := filepath.Join(dir, "posts.json")
|
|
tmpl := filepath.Join(dir, "tmpl.tmpl")
|
|
outFile := filepath.Join(dir, "output.txt")
|
|
writeTestJSON(t, file1, []string{"users"})
|
|
writeTestJSON(t, file2, []string{"posts"})
|
|
writeTestTemplate(t, tmpl)
|
|
|
|
templSourceType = "json"
|
|
templSourcePath = ""
|
|
templSourceConn = ""
|
|
templFromList = []string{file1, file2}
|
|
templTemplatePath = tmpl
|
|
templOutputPath = outFile
|
|
templSchemaFilter = ""
|
|
templMode = "database"
|
|
templFilenamePattern = "{{.Name}}.txt"
|
|
|
|
if err := runTempl(nil, nil); err != nil {
|
|
t.Fatalf("runTempl() error = %v", err)
|
|
}
|
|
if _, err := os.Stat(outFile); os.IsNotExist(err) {
|
|
t.Error("expected output file to be created")
|
|
}
|
|
}
|
|
|
|
func TestRunTempl_FromListPathWithSpaces(t *testing.T) {
|
|
saved := saveTemplState()
|
|
defer restoreTemplState(saved)
|
|
|
|
spacedDir := filepath.Join(t.TempDir(), "my schema files")
|
|
if err := os.MkdirAll(spacedDir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file1 := filepath.Join(spacedDir, "users schema.json")
|
|
file2 := filepath.Join(spacedDir, "posts schema.json")
|
|
tmpl := filepath.Join(spacedDir, "my template.tmpl")
|
|
outFile := filepath.Join(spacedDir, "output file.txt")
|
|
writeTestJSON(t, file1, []string{"users"})
|
|
writeTestJSON(t, file2, []string{"posts"})
|
|
writeTestTemplate(t, tmpl)
|
|
|
|
templSourceType = "json"
|
|
templSourcePath = ""
|
|
templSourceConn = ""
|
|
templFromList = []string{file1, file2}
|
|
templTemplatePath = tmpl
|
|
templOutputPath = outFile
|
|
templSchemaFilter = ""
|
|
templMode = "database"
|
|
templFilenamePattern = "{{.Name}}.txt"
|
|
|
|
if err := runTempl(nil, nil); err != nil {
|
|
t.Fatalf("runTempl() with spaced paths error = %v", err)
|
|
}
|
|
if _, err := os.Stat(outFile); os.IsNotExist(err) {
|
|
t.Error("expected output file to be created")
|
|
}
|
|
}
|