github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/cmd/swagger/commands/generate/operation_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 TestGenerateOperation(t *testing.T) {
    17  	testGenerateOperation(t, false)
    18  }
    19  
    20  func TestGenerateOperationStrict(t *testing.T) {
    21  	testGenerateOperation(t, true)
    22  }
    23  
    24  func testGenerateOperation(t *testing.T, strict bool) {
    25  	specs := []string{
    26  		"tasklist.basic.yml",
    27  	}
    28  	log.SetOutput(io.Discard)
    29  	defer log.SetOutput(os.Stdout)
    30  
    31  	base := filepath.FromSlash("../../../../")
    32  	for i, spec := range specs {
    33  		_ = t.Run(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.Operation{}
    43  			if i == 0 {
    44  				m.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE"))
    45  			}
    46  			_, _ = flags.ParseArgs(m, []string{"--name=listTasks"})
    47  			m.Shared.Spec = flags.Filename(path)
    48  			m.Shared.Target = flags.Filename(generated)
    49  			m.Shared.StrictResponders = strict
    50  
    51  			if err := m.Execute([]string{}); err != nil {
    52  				t.Error(err)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestGenerateOperation_Check(t *testing.T) {
    59  	log.SetOutput(io.Discard)
    60  	defer log.SetOutput(os.Stdout)
    61  
    62  	m := &generate.Operation{}
    63  	_, _ = flags.ParseArgs(m, []string{"--name=op1", "--name=op2"})
    64  	m.Shared.DumpData = true
    65  	m.Name = []string{"op1", "op2"}
    66  	err := m.Execute([]string{})
    67  	assert.Error(t, err)
    68  }