github.com/circl-dev/go-swagger@v0.31.0/cmd/swagger/commands/generate/cli_test.go (about)

     1  package generate_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/circl-dev/go-swagger/cmd/swagger/commands/generate"
    12  	flags "github.com/jessevdk/go-flags"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // Make sure generated code compiles
    18  func TestGenerateCLI(t *testing.T) {
    19  	log.SetOutput(ioutil.Discard)
    20  	defer log.SetOutput(os.Stdout)
    21  
    22  	base := filepath.FromSlash("../../../../")
    23  
    24  	testcases := []struct {
    25  		name      string
    26  		spec      string
    27  		wantError bool
    28  	}{
    29  		{
    30  			name:      "tasklist_basic",
    31  			spec:      "tasklist.basic.yml",
    32  			wantError: false,
    33  		},
    34  		{
    35  			name:      "tasklist_allparams",
    36  			spec:      "todolist.allparams.yml",
    37  			wantError: false,
    38  		},
    39  		{
    40  			name:      "tasklist_arrayform",
    41  			spec:      "todolist.arrayform.yml",
    42  			wantError: false,
    43  		},
    44  		{
    45  			name:      "tasklist_arrayquery",
    46  			spec:      "todolist.arrayquery.yml",
    47  			wantError: false,
    48  		},
    49  		{
    50  			name:      "todolist_bodyparams",
    51  			spec:      "todolist.bodyparams.yml",
    52  			wantError: false,
    53  		},
    54  		{
    55  			name:      "tasklist_simplequery",
    56  			spec:      "todolist.simplequery.yml",
    57  			wantError: false,
    58  		},
    59  		{
    60  			name:      "todolist_responses",
    61  			spec:      "todolist.responses.yml",
    62  			wantError: false,
    63  		},
    64  		{
    65  			name:      "todo_simple-fixed",
    66  			spec:      "todolist.simple-fixed.yml",
    67  			wantError: false,
    68  		},
    69  		{
    70  			name:      "todo_simpleform",
    71  			spec:      "todolist.simpleform.yml",
    72  			wantError: false,
    73  		},
    74  		{
    75  			name:      "todo_simpleheader",
    76  			spec:      "todolist.simpleheader.yml",
    77  			wantError: false,
    78  		},
    79  		{
    80  			name:      "todo_simplepath",
    81  			spec:      "todolist.simplepath.yml",
    82  			wantError: false,
    83  		},
    84  	}
    85  
    86  	for _, tc := range testcases {
    87  		t.Run(tc.name, func(t *testing.T) {
    88  			path := filepath.Join(base, "fixtures/codegen", tc.spec)
    89  			generated, err := ioutil.TempDir(filepath.Dir(path), "generated")
    90  			if err != nil {
    91  				t.Fatalf("TempDir()=%s", generated)
    92  			}
    93  			defer func() {
    94  				_ = os.RemoveAll(generated)
    95  			}()
    96  			m := &generate.Cli{}
    97  			_, _ = flags.Parse(m)
    98  			m.Shared.Spec = flags.Filename(path)
    99  			m.Shared.Target = flags.Filename(generated)
   100  
   101  			err = m.Execute([]string{})
   102  			if tc.wantError {
   103  				assert.Error(t, err)
   104  			} else {
   105  				require.NoError(t, err)
   106  				// change to true to run go vet on generated files
   107  				runVet := false
   108  				if runVet {
   109  					vet := exec.Command("go", "vet", generated+"/...")
   110  					output, err := vet.CombinedOutput()
   111  					assert.NoError(t, err, string(output))
   112  				}
   113  			}
   114  		})
   115  	}
   116  }
   117  
   118  func TestGenerateCli_Check(t *testing.T) {
   119  	log.SetOutput(ioutil.Discard)
   120  	defer log.SetOutput(os.Stdout)
   121  
   122  	m := &generate.Cli{}
   123  	_, _ = flags.Parse(m)
   124  	err := m.Execute([]string{})
   125  	assert.Error(t, err)
   126  }
   127  
   128  // This test runs cli generation on various swagger specs, for sanity check.
   129  // Skipped in by default. Only run by developer locally.
   130  func TestVariousCli(t *testing.T) {
   131  	// comment out this skip to run test
   132  	t.Skip()
   133  
   134  	log.SetOutput(ioutil.Discard)
   135  	defer log.SetOutput(os.Stdout)
   136  
   137  	base := filepath.FromSlash("../../../../")
   138  
   139  	// change to true to run test case with runOnly set true
   140  	runOnlyTest := false
   141  
   142  	testcases := []struct {
   143  		skip          bool
   144  		name          string
   145  		spec          string
   146  		wantError     bool
   147  		wantVetError  bool
   148  		preserveFiles bool // force to preserve files
   149  		runOnly       bool // run only this test, and skip all others
   150  	}{
   151  		{
   152  			skip:         true, // do not run this since it is known to have bug
   153  			name:         "crazy-alias",
   154  			spec:         "fixtures/bugs/1260/fixture-realiased-types.yaml",
   155  			wantError:    false, // generate files should success
   156  			wantVetError: true,  // polymorphism is not supported. model import is not right. TODO: fix this.
   157  		},
   158  		{
   159  			name:          "multi-auth",
   160  			spec:          "examples/composed-auth/swagger.yml",
   161  			preserveFiles: true,
   162  		},
   163  		// not working because of model generation order.
   164  		// {
   165  		// 	name:          "enum",
   166  		// 	spec:          "fixtures/enhancements/1623/swagger.yml",
   167  		// 	preserveFiles: true,
   168  		// 	runOnly:       true,
   169  		// },
   170  	}
   171  
   172  	for _, tc := range testcases {
   173  		if runOnlyTest && !tc.runOnly {
   174  			continue
   175  		}
   176  		t.Run(tc.name, func(tt *testing.T) {
   177  			if tc.skip {
   178  				tt.Skip()
   179  			}
   180  			path := filepath.Join(base, tc.spec)
   181  			generated, err := ioutil.TempDir(filepath.Dir(path), "generated")
   182  			if err != nil {
   183  				t.Fatalf("TempDir()=%s", generated)
   184  			}
   185  			defer func() {
   186  				// only clean up if success, and leave the files around for developer to inspect
   187  				if !tt.Failed() {
   188  					if !tc.preserveFiles {
   189  						_ = os.RemoveAll(generated)
   190  					}
   191  				} else {
   192  					// stop all tests, since it will generate too many files to inspect
   193  					t.FailNow()
   194  				}
   195  			}()
   196  			m := &generate.Cli{}
   197  			_, _ = flags.Parse(m)
   198  			m.Shared.Spec = flags.Filename(path)
   199  			m.Shared.Target = flags.Filename(generated)
   200  
   201  			err = m.Execute([]string{})
   202  			if tc.wantError {
   203  				assert.Error(tt, err)
   204  			} else {
   205  				require.NoError(tt, err)
   206  				// always run go vet on generated files
   207  				runVet := true
   208  				if runVet {
   209  					vet := exec.Command("go", "vet", generated+"/...")
   210  					output, err := vet.CombinedOutput()
   211  					if !tc.wantVetError {
   212  						assert.NoError(tt, err, string(output))
   213  					} else {
   214  						assert.Error(t, err)
   215  					}
   216  				}
   217  			}
   218  		})
   219  	}
   220  }