github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/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  	"github.com/go-swagger/go-swagger/cmd/swagger/commands/generate"
    11  	flags "github.com/jessevdk/go-flags"
    12  	"github.com/stretchr/testify/assert"
    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(spec, func(t *testing.T) {
    33  			path := filepath.Join(base, "fixtures/codegen", spec)
    34  			generated, err := os.MkdirTemp(filepath.Dir(path), "generated")
    35  			if err != nil {
    36  				t.Fatalf("TempDir()=%s", generated)
    37  			}
    38  			defer func() {
    39  				_ = os.RemoveAll(generated)
    40  			}()
    41  			m := &generate.Model{}
    42  			_, _ = flags.Parse(m)
    43  			if i == 0 {
    44  				m.Models.ExistingModels = "nonExisting"
    45  			}
    46  			m.Shared.Spec = flags.Filename(path)
    47  			m.Shared.Target = flags.Filename(generated)
    48  
    49  			if err := m.Execute([]string{}); err != nil {
    50  				t.Error(err)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestGenerateModel_Check(t *testing.T) {
    57  	log.SetOutput(io.Discard)
    58  	defer log.SetOutput(os.Stdout)
    59  
    60  	m := &generate.Model{}
    61  	_, _ = flags.Parse(m)
    62  	m.Shared.DumpData = true
    63  	m.Name = []string{"model1", "model2"}
    64  	err := m.Execute([]string{})
    65  	assert.Error(t, err)
    66  }