-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_test.go
More file actions
91 lines (71 loc) · 1.93 KB
/
api_test.go
File metadata and controls
91 lines (71 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package codescan
import (
"flag"
"io"
"log"
"os"
"path/filepath"
"testing"
"github.com/go-openapi/testify/v2/require"
)
// Public-API smoke suite. Fixture-heavy tests live in internal/integration.
var enableDebug bool //nolint:gochecknoglobals // test flag registered in init
func init() { //nolint:gochecknoinits // registers test flags before TestMain
flag.BoolVar(&enableDebug, "enable-debug", false, "enable debug output in tests")
}
func TestMain(m *testing.M) {
flag.Parse()
if !enableDebug {
log.SetOutput(io.Discard)
} else {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetOutput(os.Stderr)
}
os.Exit(m.Run())
}
func TestApplication_DebugLogging(t *testing.T) {
// Exercises the logger.DebugLogf code path with Debug: true.
_, err := Run(&Options{
Packages: []string{"./goparsing/petstore/..."},
WorkDir: "fixtures",
ScanModels: true,
Debug: true,
})
require.NoError(t, err)
}
func TestRun_InvalidWorkDir(t *testing.T) {
// Exercises the Run() error path when package loading fails.
_, err := Run(&Options{
Packages: []string{"./..."},
WorkDir: "/nonexistent/directory",
})
require.Error(t, err)
}
func TestSetEnumDoesNotPanic(t *testing.T) {
// Regression: ensure Run() does not panic on minimal source with an enum.
dir := t.TempDir()
src := `
package failure
// swagger:model Order
type Order struct {
State State ` + "`json:\"state\"`" + `
}
// State represents the state of an order.
// enum: ["created","processed"]
type State string
`
err := os.WriteFile(filepath.Join(dir, "model.go"), []byte(src), 0o600)
require.NoError(t, err)
goMod := `
module failure
go 1.23`
err = os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goMod), 0o600)
require.NoError(t, err)
_, err = Run(&Options{
WorkDir: dir,
ScanModels: true,
})
require.NoError(t, err)
}