github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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  	flags "github.com/jessevdk/go-flags"
    13  	"github.com/thetreep/go-swagger/cmd/swagger/commands/generate"
    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(
    34  			spec, func(t *testing.T) {
    35  				path := filepath.Join(base, "fixtures/codegen", spec)
    36  				generated, err := os.MkdirTemp(filepath.Dir(path), "generated")
    37  				if err != nil {
    38  					t.Fatalf("TempDir()=%s", generated)
    39  				}
    40  				defer func() {
    41  					_ = os.RemoveAll(generated)
    42  				}()
    43  				m := &generate.Operation{}
    44  				if i == 0 {
    45  					m.Shared.CopyrightFile = flags.Filename(filepath.Join(base, "LICENSE"))
    46  				}
    47  				_, _ = flags.ParseArgs(m, []string{"--name=listTasks"})
    48  				m.Shared.Spec = flags.Filename(path)
    49  				m.Shared.Target = flags.Filename(generated)
    50  				m.Shared.StrictResponders = strict
    51  
    52  				if err := m.Execute([]string{}); err != nil {
    53  					t.Error(err)
    54  				}
    55  			},
    56  		)
    57  	}
    58  }
    59  
    60  func TestGenerateOperation_Check(t *testing.T) {
    61  	log.SetOutput(io.Discard)
    62  	defer log.SetOutput(os.Stdout)
    63  
    64  	m := &generate.Operation{}
    65  	_, _ = flags.ParseArgs(m, []string{"--name=op1", "--name=op2"})
    66  	m.Shared.DumpData = true
    67  	m.Name = []string{"op1", "op2"}
    68  	err := m.Execute([]string{})
    69  	assert.Error(t, err)
    70  }