github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/generator/build_test.go (about)

     1  package generator_test
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/go-swagger/go-swagger/cmd/swagger/commands/generate"
    14  	flags "github.com/jessevdk/go-flags"
    15  )
    16  
    17  const (
    18  	defaultAPIPackage    = "operations"
    19  	defaultClientPackage = "client"
    20  	defaultModelPackage  = "models"
    21  )
    22  
    23  func TestGenerateAndBuild(t *testing.T) {
    24  	// This test generates and actually compiles the output
    25  	// of generated clients.
    26  	//
    27  	// We run this in parallel now. Therefore it is no more
    28  	// possible to assert the output on stdout.
    29  	//
    30  	// NOTE: test cases are randomized (map)
    31  	t.Parallel()
    32  
    33  	defer func() {
    34  		log.SetOutput(os.Stdout)
    35  	}()
    36  
    37  	cases := map[string]struct {
    38  		spec string
    39  	}{
    40  		"issue 844": {
    41  			"../fixtures/bugs/844/swagger.json",
    42  		},
    43  		"issue 844 (with params)": {
    44  			"../fixtures/bugs/844/swagger-bis.json",
    45  		},
    46  		"issue 1216": {
    47  			"../fixtures/bugs/1216/swagger.yml",
    48  		},
    49  		"issue 2111": {
    50  			"../fixtures/bugs/2111/fixture-2111.yaml",
    51  		},
    52  		"issue 2278": {
    53  			"../fixtures/bugs/2278/fixture-2278.yaml",
    54  		},
    55  		"issue 2163": {
    56  			"../fixtures/enhancements/2163/fixture-2163.yaml",
    57  		},
    58  		"issue 1771": {
    59  			"../fixtures/enhancements/1771/fixture-1771.yaml",
    60  		},
    61  	}
    62  
    63  	t.Run("build client", func(t *testing.T) {
    64  		for name, cas := range cases {
    65  			t.Run(name, func(t *testing.T) {
    66  				t.Parallel()
    67  				log.SetOutput(io.Discard)
    68  
    69  				spec := filepath.FromSlash(cas.spec)
    70  
    71  				generated, err := os.MkdirTemp(filepath.Dir(spec), "generated")
    72  				require.NoErrorf(t, err, "TempDir()=%s", generated)
    73  				defer func() { _ = os.RemoveAll(generated) }()
    74  
    75  				require.NoErrorf(t, newTestClient(spec, generated).Execute(nil), "Execute()=%s", err)
    76  
    77  				packages := filepath.Join(generated, "...")
    78  
    79  				goExecInDir(t, "", "get")
    80  
    81  				goExecInDir(t, "", "build", packages)
    82  			})
    83  		}
    84  	})
    85  }
    86  
    87  func newTestClient(input, output string) *generate.Client {
    88  	c := &generate.Client{}
    89  	c.DefaultScheme = "http"
    90  	c.DefaultProduces = "application/json"
    91  	c.Shared.Spec = flags.Filename(input)
    92  	c.Shared.Target = flags.Filename(output)
    93  	c.Operations.APIPackage = defaultAPIPackage
    94  	c.Models.ModelPackage = defaultModelPackage
    95  	c.ClientPackage = defaultClientPackage
    96  	return c
    97  }
    98  
    99  func goExecInDir(t testing.TB, target string, args ...string) {
   100  	cmd := exec.Command("go", args...)
   101  	cmd.Dir = target
   102  	p, err := cmd.CombinedOutput()
   103  	require.NoErrorf(t, err, "unexpected error: %s: %v\n%s", cmd.String(), err, string(p))
   104  }