Skip to content

Commit db487ea

Browse files
committed
Update parseFile: ignore missing requested interfaces, adjust tests accordingly
1 parent 89fa9e9 commit db487ea

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

mockgen/parse.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"os"
3131
"path"
3232
"path/filepath"
33-
"sort"
3433
"strconv"
3534
"strings"
3635

@@ -268,10 +267,7 @@ func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Packag
268267

269268
delete(p.includeNamesSet, name)
270269
}
271-
if len(p.includeNamesSet) > 0 {
272-
missing := slices.Sorted(maps.Keys))
273-
return nil, fmt.Errorf("requested interfaces not found: %s", strings.Join(missing, ", "))
274-
}
270+
275271
return &model.Package{
276272
Name: file.Name.String(),
277273
PkgPath: importPath,

mockgen/parse_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,27 @@ func TestParseFile_IncludeOnlyRequested(t *testing.T) {
170170
}
171171
}
172172

173-
func TestParseFile_IncludeMissing_ReturnsError(t *testing.T) {
174-
fs := token.NewFileSet()
175-
file, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0)
176-
if err != nil {
173+
// When requested interface is missing, parser should ignore it (no error, no interfaces).
174+
func TestParseFile_IncludeMissing_Ignored(t *testing.T) {
175+
fs := token.NewFileSet()
176+
file, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0)
177+
if err != nil {
178+
t.Fatalf("Unexpected error: %v", err)
179+
}
180+
181+
p := fileParser{
182+
fileSet: fs,
183+
imports: make(map[string]importedPackage),
184+
importedInterfaces: newInterfaceCache(),
185+
includeNamesSet: map[string]struct{}{"DoesNotExist": {}},
186+
}
187+
188+
pkg, err := p.parseFile("", file)
189+
if err != nil {
177190
t.Fatalf("Unexpected error: %v", err)
178191
}
179-
180-
p := fileParser{
181-
fileSet: fs,
182-
imports: make(map[string]importedPackage),
183-
importedInterfaces: newInterfaceCache(),
184-
includeNamesSet: map[string]struct{}{"DoesNotExist": {}},
185-
}
186-
187-
_, err = p.parseFile("", file)
188-
if err == nil || !strings.Contains(err.Error(), "requested interfaces not found") {
189-
t.Fatalf("Expected missing interface error, got %v", err)
192+
if len(pkg.Interfaces) != 0 {
193+
t.Fatalf("Expected no interfaces, got %v", pkg.Interfaces)
190194
}
191195
}
192196

0 commit comments

Comments
 (0)