github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/cmd/swagger/commands/generate/client_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 flags "github.com/jessevdk/go-flags" 14 ) 15 16 func TestGenerateClient(t *testing.T) { 17 log.SetOutput(io.Discard) 18 defer log.SetOutput(os.Stdout) 19 20 base := filepath.FromSlash("../../../../") 21 22 tests := []struct { 23 name string 24 spec string 25 template string 26 wantError bool 27 prepare func(c *generate.Client) 28 }{ 29 { 30 name: "tasklist_basic", 31 spec: "tasklist.basic.yml", 32 wantError: false, 33 }, 34 { 35 name: "tasklist_simplequery", 36 spec: "todolist.simplequery.yml", 37 wantError: false, 38 prepare: func(c *generate.Client) { 39 c.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE")) 40 }, 41 }, 42 { 43 name: "generate_client_with_invalid_template", 44 spec: "todolist.simplequery.yml", 45 template: "NonExistingContributorTemplate", 46 wantError: true, 47 }, 48 { 49 name: "Existing_contributor", 50 spec: "todolist.simplequery.yml", 51 template: "stratoscale", 52 wantError: false, 53 }, 54 } 55 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 59 path := filepath.Join(base, "fixtures/codegen", tt.spec) 60 generated, err := os.MkdirTemp(filepath.Dir(path), "generated") 61 if err != nil { 62 t.Fatalf("TempDir()=%s", generated) 63 } 64 defer func() { 65 _ = os.RemoveAll(generated) 66 }() 67 m := &generate.Client{} 68 _, _ = flags.Parse(m) 69 m.Shared.Spec = flags.Filename(path) 70 m.Shared.Target = flags.Filename(generated) 71 m.Shared.Template = tt.template 72 73 if tt.prepare != nil { 74 tt.prepare(m) 75 } 76 77 err = m.Execute([]string{}) 78 if tt.wantError { 79 assert.Error(t, err) 80 } else { 81 assert.NoError(t, err) 82 } 83 }) 84 } 85 } 86 87 func TestGenerateClient_Check(t *testing.T) { 88 log.SetOutput(io.Discard) 89 defer log.SetOutput(os.Stdout) 90 91 m := &generate.Client{} 92 _, _ = flags.Parse(m) 93 m.Shared.CopyrightFile = "nullePart" 94 err := m.Execute([]string{}) 95 assert.Error(t, err) 96 }