github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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/jessevdk/go-flags"
    13  	"github.com/thetreep/go-swagger/cmd/swagger/commands/generate"
    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(
    36  			spec, func(t *testing.T) {
    37  				path := filepath.Join(base, "fixtures/codegen", spec)
    38  				generated, err := os.MkdirTemp(filepath.Dir(path), "generated")
    39  				if err != nil {
    40  					t.Fatalf("TempDir()=%s", generated)
    41  				}
    42  				defer func() {
    43  					_ = os.RemoveAll(generated)
    44  				}()
    45  				m := &generate.Server{}
    46  				_, _ = flags.Parse(m)
    47  				if i == 0 {
    48  					m.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE"))
    49  				}
    50  				switch i {
    51  				case 1:
    52  					m.FlagStrategy = "pflag"
    53  				case 2:
    54  					m.FlagStrategy = "flag"
    55  				}
    56  				m.Shared.Spec = flags.Filename(path)
    57  				m.Shared.Target = flags.Filename(generated)
    58  				m.Shared.StrictResponders = strict
    59  
    60  				if err := m.Execute([]string{}); err != nil {
    61  					t.Error(err)
    62  				}
    63  			},
    64  		)
    65  	}
    66  }
    67  
    68  func TestGenerateServer_Checks(t *testing.T) {
    69  	log.SetOutput(io.Discard)
    70  	defer log.SetOutput(os.Stdout)
    71  
    72  	m := &generate.Server{}
    73  	_, _ = flags.Parse(m)
    74  	m.Shared.CopyrightFile = "nowhere"
    75  	err := m.Execute([]string{})
    76  	assert.Error(t, err)
    77  }
    78  
    79  func TestRegressionIssue2601(t *testing.T) {
    80  	specs := []string{
    81  		"impl.yml",
    82  	}
    83  	log.SetOutput(io.Discard)
    84  	defer log.SetOutput(os.Stdout)
    85  
    86  	base := filepath.FromSlash("../../../../")
    87  	for i, spec := range specs {
    88  		_ = t.Run(
    89  			spec, func(t *testing.T) {
    90  				path := filepath.Join(base, "fixtures/codegen", spec)
    91  				generated, err := os.MkdirTemp(filepath.Dir(path), "generated")
    92  				if err != nil {
    93  					t.Fatalf("TempDir()=%s", generated)
    94  				}
    95  				defer func() {
    96  					_ = os.RemoveAll(generated)
    97  				}()
    98  				m := &generate.Server{}
    99  				_, _ = flags.Parse(m)
   100  				if i == 0 {
   101  					m.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE"))
   102  				}
   103  				m.Shared.Spec = flags.Filename(path)
   104  				m.Shared.Target = flags.Filename(generated)
   105  
   106  				// Error was coming from these two being set together
   107  				m.Shared.StrictResponders = true
   108  				m.ImplementationPackage = "github.com/thetreep/go-swagger/fixtures/codegen/impl"
   109  
   110  				// Load new copy of template
   111  				m.Shared.AllowTemplateOverride = true
   112  				m.Shared.TemplateDir = flags.Filename(filepath.Join(base, "generator/templates"))
   113  
   114  				if err := m.Execute([]string{}); err != nil {
   115  					t.Error(err)
   116  				}
   117  			},
   118  		)
   119  	}
   120  }