mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-29 12:31:11 +00:00
Both commands discarded genFile's error, and genFile discarded the errors from rendering its templates and from pkg.FileCreate. A file that could not be written ended the process from inside FileCreate; with core v2.11.0 it would have been reported as success instead. genFile now returns every error, and both commands print it and exit non-zero. In `app`, a missing router template no longer panics on a nil template, and neither file is written unless both render.
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package migrate
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// chdirWithTemplates runs the test from an empty directory that has the
|
|
// repository's template/ directory in it, which is what genFile reads from
|
|
// the working directory.
|
|
func chdirWithTemplates(t *testing.T) string {
|
|
t.Helper()
|
|
tmpl, err := filepath.Abs(filepath.Join("..", "..", "template"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
if err := os.Symlink(tmpl, filepath.Join(dir, "template")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Chdir(dir)
|
|
return dir
|
|
}
|
|
|
|
func TestGenFileWritesTheMigration(t *testing.T) {
|
|
dir := chdirWithTemplates(t)
|
|
out := filepath.Join(dir, "cmd", "migrate", "migration", "version-local")
|
|
if err := os.MkdirAll(out, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := genFile(); err != nil {
|
|
t.Fatalf("genFile: %v", err)
|
|
}
|
|
files, err := filepath.Glob(filepath.Join(out, "*_migrate.go"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(files) != 1 {
|
|
t.Fatalf("generated files = %v, want one", files)
|
|
}
|
|
}
|
|
|
|
// Run from a directory without cmd/migrate/migration, writing the file fails.
|
|
// The failure used to end the process from inside pkg.FileCreate and, before
|
|
// that, was dropped by genFile's caller; it now reaches the caller.
|
|
func TestGenFileReportsAFileItCannotWrite(t *testing.T) {
|
|
chdirWithTemplates(t)
|
|
|
|
if err := genFile(); err == nil {
|
|
t.Fatal("genFile returned nil with no directory to write into")
|
|
}
|
|
}
|