github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/cmd/swagger/commands/mixin_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  )
    13  
    14  var fixtureBase = filepath.FromSlash("../../../fixtures")
    15  
    16  // Commands requires at least one arg
    17  func TestCmd_Mixin(t *testing.T) {
    18  	log.SetOutput(io.Discard)
    19  	defer log.SetOutput(os.Stdout)
    20  
    21  	v := MixinSpec{}
    22  	result := v.Execute([]string{})
    23  	assert.Error(t, result)
    24  
    25  	result = v.Execute([]string{"nowhere.json"})
    26  	assert.Error(t, result)
    27  
    28  	result = v.Execute([]string{"nowhere.json", "notThere.json"})
    29  	assert.Error(t, result)
    30  
    31  	specDoc1 := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536.yaml")
    32  	result = v.Execute([]string{specDoc1, "notThere.json"})
    33  	assert.Error(t, result)
    34  }
    35  
    36  func TestCmd_Mixin_NoError(t *testing.T) {
    37  	log.SetOutput(io.Discard)
    38  	defer log.SetOutput(os.Stdout)
    39  
    40  	specDoc1 := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536.yaml")
    41  	specDoc2 := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536-2.yaml")
    42  	outDir, err := os.MkdirTemp(filepath.Dir(specDoc1), "mixed")
    43  	assert.NoError(t, err)
    44  	defer os.RemoveAll(outDir)
    45  	v := MixinSpec{
    46  		ExpectedCollisionCount: 3,
    47  		Format:                 "yaml",
    48  		Output:                 flags.Filename(filepath.Join(outDir, "fixture-1536-mixed.yaml")),
    49  	}
    50  
    51  	result := v.Execute([]string{specDoc1, specDoc2})
    52  	assert.NoError(t, result)
    53  	_, exists := os.Stat(filepath.Join(outDir, "fixture-1536-mixed.yaml"))
    54  	assert.True(t, !os.IsNotExist(exists))
    55  }
    56  
    57  func TestCmd_Mixin_BothConflictsAndIgnoreConflictsSpecified(t *testing.T) {
    58  	v := MixinSpec{
    59  		ExpectedCollisionCount: 1,
    60  		IgnoreConflicts:        true,
    61  	}
    62  	err := v.Execute([]string{"test.json", "test2.json"})
    63  	assert.Error(t, err)
    64  	assert.Equal(t, ignoreConflictsAndCollisionsSpecified, err.Error())
    65  }
    66  
    67  func TestCmd_Mixin_IgnoreConflicts(t *testing.T) {
    68  	log.SetOutput(io.Discard)
    69  	defer log.SetOutput(os.Stdout)
    70  
    71  	specDoc1 := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536.yaml")
    72  	specDoc2 := filepath.Join(fixtureBase, "bugs", "1536", "fixture-1536-2.yaml")
    73  	outDir, err := os.MkdirTemp(filepath.Dir(specDoc1), "mixed")
    74  	assert.NoError(t, err)
    75  	defer os.RemoveAll(outDir)
    76  	v := MixinSpec{
    77  		IgnoreConflicts: true,
    78  		Format:          "yaml",
    79  		Output:          flags.Filename(filepath.Join(outDir, "fixture-1536-mixed.yaml")),
    80  	}
    81  
    82  	result := v.Execute([]string{specDoc1, specDoc2})
    83  	assert.NoError(t, result)
    84  	_, exists := os.Stat(filepath.Join(outDir, "fixture-1536-mixed.yaml"))
    85  	assert.True(t, !os.IsNotExist(exists))
    86  }