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

     1  package commands
     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/stretchr/testify/require"
    13  
    14  	"github.com/thetreep/go-swagger/cmd/swagger/commands/generate"
    15  )
    16  
    17  type executable interface {
    18  	Execute([]string) error
    19  }
    20  
    21  func testValidRefs(t *testing.T, v executable) {
    22  	log.SetOutput(io.Discard)
    23  	defer log.SetOutput(os.Stdout)
    24  
    25  	specDoc := filepath.Join(fixtureBase, "expansion", "invalid-refs.json")
    26  	result := v.Execute([]string{specDoc})
    27  	assert.Error(t, result)
    28  }
    29  
    30  func testRequireParam(t *testing.T, v executable) {
    31  	log.SetOutput(io.Discard)
    32  	defer log.SetOutput(os.Stdout)
    33  
    34  	result := v.Execute([]string{})
    35  	assert.Error(t, result)
    36  
    37  	result = v.Execute([]string{"nowhere.json"})
    38  	assert.Error(t, result)
    39  }
    40  
    41  func getOutput(t *testing.T, specDoc, prefix, filename string) (string, string) {
    42  	outDir, err := os.MkdirTemp(filepath.Dir(specDoc), "flatten")
    43  	if !assert.NoError(t, err) {
    44  		t.FailNow()
    45  	}
    46  	return outDir, filepath.Join(outDir, filename)
    47  }
    48  
    49  func testProduceOutput(t *testing.T, v executable, specDoc, output string) {
    50  	log.SetOutput(io.Discard)
    51  	defer log.SetOutput(os.Stdout)
    52  
    53  	result := v.Execute([]string{specDoc})
    54  	assert.NoError(t, result)
    55  	_, exists := os.Stat(output)
    56  	assert.True(t, !os.IsNotExist(exists))
    57  }
    58  
    59  // Commands requires at least one arg
    60  func TestCmd_Flatten(t *testing.T) {
    61  	v := &FlattenSpec{}
    62  	testRequireParam(t, v)
    63  }
    64  
    65  func TestCmd_Flatten_Default(t *testing.T) {
    66  	specDoc := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536.yaml")
    67  	outDir, output := getOutput(t, specDoc, "flatten", "fixture-1536-flat-minimal.json")
    68  	defer os.RemoveAll(outDir)
    69  	v := &FlattenSpec{
    70  		Format:  "json",
    71  		Compact: true,
    72  		Output:  flags.Filename(output),
    73  	}
    74  	testProduceOutput(t, v, specDoc, output)
    75  }
    76  
    77  func TestCmd_Flatten_Error(t *testing.T) {
    78  	v := &FlattenSpec{}
    79  	testValidRefs(t, v)
    80  }
    81  
    82  func TestCmd_Flatten_Issue2919(t *testing.T) {
    83  	specDoc := filepath.Join(fixtureBase, "bugs", "2919", "edge-api", "client.yml")
    84  	outDir, output := getOutput(t, specDoc, "flatten", "fixture-2919-flat-minimal.yml")
    85  	defer os.RemoveAll(outDir)
    86  
    87  	v := &FlattenSpec{
    88  		Format:  "yaml",
    89  		Compact: true,
    90  		Output:  flags.Filename(output),
    91  	}
    92  	testProduceOutput(t, v, specDoc, output)
    93  }
    94  
    95  func TestCmd_FlattenKeepNames_Issue2334(t *testing.T) {
    96  	specDoc := filepath.Join(fixtureBase, "bugs", "2334", "swagger.yaml")
    97  	outDir, output := getOutput(t, specDoc, "flatten", "fixture-2334-flat-keep-names.yaml")
    98  	defer os.RemoveAll(outDir)
    99  
   100  	v := &FlattenSpec{
   101  		Format:  "yaml",
   102  		Compact: true,
   103  		Output:  flags.Filename(output),
   104  		FlattenCmdOptions: generate.FlattenCmdOptions{
   105  			WithFlatten: []string{"keep-names"},
   106  		},
   107  	}
   108  	testProduceOutput(t, v, specDoc, output)
   109  	buf, err := os.ReadFile(output)
   110  	require.NoError(t, err)
   111  	spec := string(buf)
   112  
   113  	require.Contains(t, spec, "$ref: '#/definitions/Bar'")
   114  	require.Contains(t, spec, "Bar:")
   115  	require.Contains(t, spec, "Baz:")
   116  }