github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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 flags "github.com/jessevdk/go-flags" 13 "github.com/thetreep/go-swagger/cmd/swagger/commands/generate" 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( 58 tt.name, func(t *testing.T) { 59 60 path := filepath.Join(base, "fixtures/codegen", tt.spec) 61 generated, err := os.MkdirTemp(filepath.Dir(path), "generated") 62 if err != nil { 63 t.Fatalf("TempDir()=%s", generated) 64 } 65 defer func() { 66 _ = os.RemoveAll(generated) 67 }() 68 m := &generate.Client{} 69 _, _ = flags.Parse(m) 70 m.Shared.Spec = flags.Filename(path) 71 m.Shared.Target = flags.Filename(generated) 72 m.Shared.Template = tt.template 73 74 if tt.prepare != nil { 75 tt.prepare(m) 76 } 77 78 err = m.Execute([]string{}) 79 if tt.wantError { 80 assert.Error(t, err) 81 } else { 82 assert.NoError(t, err) 83 } 84 }, 85 ) 86 } 87 } 88 89 func TestGenerateClient_Check(t *testing.T) { 90 log.SetOutput(io.Discard) 91 defer log.SetOutput(os.Stdout) 92 93 m := &generate.Client{} 94 _, _ = flags.Parse(m) 95 m.Shared.CopyrightFile = "nullePart" 96 err := m.Execute([]string{}) 97 assert.Error(t, err) 98 }