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") } }