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

     1  package generate
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/go-openapi/analysis"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func resetDefaultOpts() *analysis.FlattenOpts {
    15  	return &analysis.FlattenOpts{
    16  		Verbose:      true,
    17  		Minimal:      true,
    18  		Expand:       false,
    19  		RemoveUnused: false,
    20  	}
    21  }
    22  
    23  func Test_Shared_SetFlattenOptions(t *testing.T) {
    24  	log.SetOutput(io.Discard)
    25  	defer log.SetOutput(os.Stdout)
    26  
    27  	// testing multiple options settings for flatten:
    28  	// - verbose | noverbose
    29  	// - remove-unused
    30  	// - expand
    31  	// - minimal
    32  
    33  	var fixt *FlattenCmdOptions
    34  
    35  	res := fixt.SetFlattenOptions(nil)
    36  	assert.NotNil(t, res)
    37  
    38  	defaultOpts := resetDefaultOpts()
    39  
    40  	res = fixt.SetFlattenOptions(defaultOpts)
    41  	if !assert.NotNil(t, res) {
    42  		t.FailNow()
    43  		return
    44  	}
    45  	assert.Equal(t, *defaultOpts, *res)
    46  
    47  	fixt = &FlattenCmdOptions{
    48  		WithExpand:  false,
    49  		WithFlatten: []string{"noverbose"},
    50  	}
    51  	res = fixt.SetFlattenOptions(defaultOpts)
    52  	assert.Equal(t, analysis.FlattenOpts{
    53  		Verbose:      false,
    54  		Minimal:      true,
    55  		Expand:       false,
    56  		RemoveUnused: false,
    57  	}, *res)
    58  
    59  	fixt = &FlattenCmdOptions{
    60  		WithExpand:  false,
    61  		WithFlatten: []string{"noverbose", "full"},
    62  	}
    63  	res = fixt.SetFlattenOptions(defaultOpts)
    64  	assert.Equal(t, analysis.FlattenOpts{
    65  		Verbose:      false,
    66  		Minimal:      false,
    67  		Expand:       false,
    68  		RemoveUnused: false,
    69  	}, *res)
    70  
    71  	fixt = &FlattenCmdOptions{
    72  		WithExpand:  false,
    73  		WithFlatten: []string{"verbose", "noverbose", "full"},
    74  	}
    75  	res = fixt.SetFlattenOptions(defaultOpts)
    76  	assert.Equal(t, analysis.FlattenOpts{
    77  		Verbose:      true,
    78  		Minimal:      false,
    79  		Expand:       false,
    80  		RemoveUnused: false,
    81  	}, *res)
    82  
    83  	fixt = &FlattenCmdOptions{
    84  		WithExpand:  false,
    85  		WithFlatten: []string{"verbose", "noverbose", "full", "expand", "remove-unused"},
    86  	}
    87  	res = fixt.SetFlattenOptions(defaultOpts)
    88  	assert.Equal(t, analysis.FlattenOpts{
    89  		Verbose:      true,
    90  		Minimal:      false,
    91  		Expand:       true,
    92  		RemoveUnused: true,
    93  	}, *res)
    94  
    95  	fixt = &FlattenCmdOptions{
    96  		WithExpand:  false,
    97  		WithFlatten: []string{"minimal", "verbose", "noverbose", "full"},
    98  	}
    99  	res = fixt.SetFlattenOptions(defaultOpts)
   100  	assert.Equal(t, analysis.FlattenOpts{
   101  		Verbose:      true,
   102  		Minimal:      true,
   103  		Expand:       false,
   104  		RemoveUnused: false,
   105  	}, *res)
   106  
   107  	fixt = &FlattenCmdOptions{
   108  		WithExpand:  true,
   109  		WithFlatten: []string{"minimal", "noverbose", "full"},
   110  	}
   111  	res = fixt.SetFlattenOptions(defaultOpts)
   112  	assert.Equal(t, analysis.FlattenOpts{
   113  		Verbose:      false,
   114  		Minimal:      true,
   115  		Expand:       true,
   116  		RemoveUnused: false,
   117  	}, *res)
   118  }
   119  
   120  func Test_Shared_ReadConfig(t *testing.T) {
   121  	tmpFile, errio := os.CreateTemp("", "tmp-config*.yaml")
   122  	require.NoError(t, errio)
   123  	defer func() {
   124  		_ = os.Remove(tmpFile.Name())
   125  	}()
   126  	tmpConfig := tmpFile.Name()
   127  	errio = os.WriteFile(tmpConfig, []byte(`param: 123
   128  other: abc
   129  `), 0600)
   130  	require.NoError(t, errio)
   131  	_ = tmpFile.Close()
   132  
   133  	for _, toPin := range []struct {
   134  		Name        string
   135  		Filename    string
   136  		ExpectError bool
   137  		Expected    map[string]interface{}
   138  	}{
   139  		{
   140  			Name:     "empty",
   141  			Filename: "",
   142  		},
   143  		{
   144  			Name:        "no file",
   145  			Filename:    "nowhere",
   146  			ExpectError: true,
   147  		},
   148  		{
   149  			Name:     "happy path",
   150  			Filename: tmpConfig,
   151  			Expected: map[string]interface{}{
   152  				"param": 123,
   153  				"other": "abc",
   154  			},
   155  		},
   156  	} {
   157  		testCase := toPin
   158  		t.Run(testCase.Name, func(t *testing.T) {
   159  			v, err := readConfig(testCase.Filename)
   160  			if testCase.ExpectError {
   161  				assert.Error(t, err)
   162  				return
   163  			}
   164  			require.NoError(t, err)
   165  			if testCase.Expected == nil {
   166  				require.Nil(t, v)
   167  				return
   168  			}
   169  			require.NotNil(t, v)
   170  			m := v.AllSettings()
   171  			for k, expectedValue := range testCase.Expected {
   172  				require.Contains(t, m, k)
   173  				assert.EqualValues(t, expectedValue, m[k])
   174  			}
   175  		})
   176  	}
   177  }