|
1 | 1 | package gen |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "go/ast" |
5 | 6 | "go/parser" |
6 | 7 | "go/token" |
@@ -78,6 +79,58 @@ func TestGeneratorWithQueryInterface(t *testing.T) { |
78 | 79 | } |
79 | 80 | } |
80 | 81 |
|
| 82 | +func TestExcludeInterfacesSkipsInvalidInterfaces(t *testing.T) { |
| 83 | + writeSample := func(dir string, withExclude bool) string { |
| 84 | + if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module temp.test\n\ngo 1.21\n"), 0o644); err != nil { |
| 85 | + t.Fatalf("write go.mod: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + cfg := "ExcludeInterfaces: []any{Entity(nil)}," |
| 89 | + if !withExclude { |
| 90 | + cfg = "" |
| 91 | + } |
| 92 | + |
| 93 | + src := fmt.Sprintf(`package sample |
| 94 | +
|
| 95 | +import "gorm.io/cli/gorm/genconfig" |
| 96 | +
|
| 97 | +var _ = genconfig.Config{ |
| 98 | + %s |
| 99 | +} |
| 100 | +
|
| 101 | +type Entity interface { |
| 102 | + TableName() string |
| 103 | +} |
| 104 | +`, cfg) |
| 105 | + |
| 106 | + path := filepath.Join(dir, "sample.go") |
| 107 | + if err := os.WriteFile(path, []byte(src), 0o644); err != nil { |
| 108 | + t.Fatalf("write sample.go: %v", err) |
| 109 | + } |
| 110 | + return path |
| 111 | + } |
| 112 | + |
| 113 | + runGen := func(file string) error { |
| 114 | + g := &Generator{Files: map[string]*File{}, outPath: filepath.Join(filepath.Dir(file), "out")} |
| 115 | + if err := g.Process(file); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + return g.Gen() |
| 119 | + } |
| 120 | + |
| 121 | + withExcludeDir := t.TempDir() |
| 122 | + withExcludeFile := writeSample(withExcludeDir, true) |
| 123 | + if err := runGen(withExcludeFile); err != nil { |
| 124 | + t.Fatalf("generator should succeed when interface is excluded: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + withoutExcludeDir := t.TempDir() |
| 128 | + withoutExcludeFile := writeSample(withoutExcludeDir, false) |
| 129 | + if err := runGen(withoutExcludeFile); err == nil { |
| 130 | + t.Fatalf("expected generator failure when interface is not excluded") |
| 131 | + } |
| 132 | +} |
| 133 | + |
81 | 134 | func TestProcessStructType(t *testing.T) { |
82 | 135 | fileset := token.NewFileSet() |
83 | 136 | file, err := parser.ParseFile(fileset, "../../examples/models/user.go", nil, parser.AllErrors) |
|
0 commit comments