github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/generate/model_test.go (about)

     1  package generate_test
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	flags "github.com/jessevdk/go-flags"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/thetreep/go-swagger/cmd/swagger/commands/generate"
    13  )
    14  
    15  func TestGenerateModel(t *testing.T) {
    16  	specs := []string{
    17  		"billforward.discriminators.yml",
    18  		"existing-model.yml",
    19  		"instagram.yml",
    20  		"shipyard.yml",
    21  		"sodabooth.json",
    22  		"tasklist.basic.yml",
    23  		"todolist.simpleform.yml",
    24  		"todolist.simpleheader.yml",
    25  		"todolist.simplequery.yml",
    26  	}
    27  	log.SetOutput(io.Discard)
    28  	defer log.SetOutput(os.Stdout)
    29  
    30  	base := filepath.FromSlash("../../../../")
    31  	for i, spec := range specs {
    32  		_ = t.Run(
    33  			spec, func(t *testing.T) {
    34  				path := filepath.Join(base, "fixtures/codegen", spec)
    35  				generated, err := os.MkdirTemp(filepath.Dir(path), "generated")
    36  				if err != nil {
    37  					t.Fatalf("TempDir()=%s", generated)
    38  				}
    39  				defer func() {
    40  					_ = os.RemoveAll(generated)
    41  				}()
    42  				m := &generate.Model{}
    43  				_, _ = flags.Parse(m)
    44  				if i == 0 {
    45  					m.Models.ExistingModels = "nonExisting"
    46  				}
    47  				m.Shared.Spec = flags.Filename(path)
    48  				m.Shared.Target = flags.Filename(generated)
    49  
    50  				if err := m.Execute([]string{}); err != nil {
    51  					t.Error(err)
    52  				}
    53  			},
    54  		)
    55  	}
    56  }
    57  
    58  func TestGenerateModel_Check(t *testing.T) {
    59  	log.SetOutput(io.Discard)
    60  	defer log.SetOutput(os.Stdout)
    61  
    62  	m := &generate.Model{}
    63  	_, _ = flags.Parse(m)
    64  	m.Shared.DumpData = true
    65  	m.Name = []string{"model1", "model2"}
    66  	err := m.Execute([]string{})
    67  	assert.Error(t, err)
    68  }