github.com/kaisawind/go-swagger@v0.19.0/cmd/swagger/commands/flatten_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	flags "github.com/jessevdk/go-flags"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type executable interface {
    15  	Execute([]string) error
    16  }
    17  
    18  func testValidRefs(t *testing.T, v executable) {
    19  	log.SetOutput(ioutil.Discard)
    20  	defer log.SetOutput(os.Stdout)
    21  
    22  	specDoc := filepath.Join(fixtureBase, "expansion", "invalid-refs.json")
    23  	result := v.Execute([]string{specDoc})
    24  	assert.Error(t, result)
    25  }
    26  
    27  func testRequireParam(t *testing.T, v executable) {
    28  	log.SetOutput(ioutil.Discard)
    29  	defer log.SetOutput(os.Stdout)
    30  
    31  	result := v.Execute([]string{})
    32  	assert.Error(t, result)
    33  
    34  	result = v.Execute([]string{"nowhere.json"})
    35  	assert.Error(t, result)
    36  }
    37  
    38  func getOutput(t *testing.T, specDoc, prefix, filename string) (string, string) {
    39  	outDir, err := ioutil.TempDir(filepath.Dir(specDoc), "flatten")
    40  	if !assert.NoError(t, err) {
    41  		t.FailNow()
    42  	}
    43  	return outDir, filepath.Join(outDir, filename)
    44  }
    45  
    46  func testProduceOutput(t *testing.T, v executable, specDoc, output string) {
    47  	log.SetOutput(ioutil.Discard)
    48  	defer log.SetOutput(os.Stdout)
    49  
    50  	result := v.Execute([]string{specDoc})
    51  	assert.NoError(t, result)
    52  	_, exists := os.Stat(output)
    53  	assert.True(t, !os.IsNotExist(exists))
    54  }
    55  
    56  // Commands requires at least one arg
    57  func TestCmd_Flatten(t *testing.T) {
    58  	v := &FlattenSpec{}
    59  	testRequireParam(t, v)
    60  }
    61  
    62  func TestCmd_Flatten_Default(t *testing.T) {
    63  	specDoc := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536.yaml")
    64  	outDir, output := getOutput(t, specDoc, "flatten", "fixture-1536-flat-minimal.json")
    65  	defer os.RemoveAll(outDir)
    66  	v := &FlattenSpec{
    67  		Format:  "json",
    68  		Compact: true,
    69  		Output:  flags.Filename(output),
    70  	}
    71  	testProduceOutput(t, v, specDoc, output)
    72  }
    73  
    74  func TestCmd_Flatten_Error(t *testing.T) {
    75  	v := &FlattenSpec{}
    76  	testValidRefs(t, v)
    77  }