github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/cmd/swagger/commands/generate/server_test.go (about) 1 package generate_test 2 3 import ( 4 "io" 5 "log" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/go-swagger/go-swagger/cmd/swagger/commands/generate" 13 "github.com/jessevdk/go-flags" 14 ) 15 16 func TestGenerateServer(t *testing.T) { 17 testGenerateServer(t, false) 18 } 19 20 func TestGenerateServerStrict(t *testing.T) { 21 testGenerateServer(t, true) 22 } 23 24 func testGenerateServer(t *testing.T, strict bool) { 25 specs := []string{ 26 "billforward.discriminators.yml", 27 "todolist.simplequery.yml", 28 "todolist.simplequery.yml", 29 } 30 log.SetOutput(io.Discard) 31 defer log.SetOutput(os.Stdout) 32 33 base := filepath.FromSlash("../../../../") 34 for i, spec := range specs { 35 _ = t.Run(spec, func(t *testing.T) { 36 path := filepath.Join(base, "fixtures/codegen", spec) 37 generated, err := os.MkdirTemp(filepath.Dir(path), "generated") 38 if err != nil { 39 t.Fatalf("TempDir()=%s", generated) 40 } 41 defer func() { 42 _ = os.RemoveAll(generated) 43 }() 44 m := &generate.Server{} 45 _, _ = flags.Parse(m) 46 if i == 0 { 47 m.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE")) 48 } 49 switch i { 50 case 1: 51 m.FlagStrategy = "pflag" 52 case 2: 53 m.FlagStrategy = "flag" 54 } 55 m.Shared.Spec = flags.Filename(path) 56 m.Shared.Target = flags.Filename(generated) 57 m.Shared.StrictResponders = strict 58 59 if err := m.Execute([]string{}); err != nil { 60 t.Error(err) 61 } 62 }) 63 } 64 } 65 66 func TestGenerateServer_Checks(t *testing.T) { 67 log.SetOutput(io.Discard) 68 defer log.SetOutput(os.Stdout) 69 70 m := &generate.Server{} 71 _, _ = flags.Parse(m) 72 m.Shared.CopyrightFile = "nowhere" 73 err := m.Execute([]string{}) 74 assert.Error(t, err) 75 } 76 77 func TestRegressionIssue2601(t *testing.T) { 78 specs := []string{ 79 "impl.yml", 80 } 81 log.SetOutput(io.Discard) 82 defer log.SetOutput(os.Stdout) 83 84 base := filepath.FromSlash("../../../../") 85 for i, spec := range specs { 86 _ = t.Run(spec, func(t *testing.T) { 87 path := filepath.Join(base, "fixtures/codegen", spec) 88 generated, err := os.MkdirTemp(filepath.Dir(path), "generated") 89 if err != nil { 90 t.Fatalf("TempDir()=%s", generated) 91 } 92 defer func() { 93 _ = os.RemoveAll(generated) 94 }() 95 m := &generate.Server{} 96 _, _ = flags.Parse(m) 97 if i == 0 { 98 m.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE")) 99 } 100 m.Shared.Spec = flags.Filename(path) 101 m.Shared.Target = flags.Filename(generated) 102 103 // Error was coming from these two being set together 104 m.Shared.StrictResponders = true 105 m.ImplementationPackage = "github.com/go-swagger/go-swagger/fixtures/codegen/impl" 106 107 // Load new copy of template 108 m.Shared.AllowTemplateOverride = true 109 m.Shared.TemplateDir = flags.Filename(filepath.Join(base, "generator/templates")) 110 111 if err := m.Execute([]string{}); err != nil { 112 t.Error(err) 113 } 114 }) 115 } 116 }