github.com/snyk/vervet/v6@v6.2.4/internal/cmd/compiler_test.go (about)

     1  package cmd_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  	"github.com/getkin/kin-openapi/openapi3"
    10  
    11  	"github.com/snyk/vervet/v6"
    12  	"github.com/snyk/vervet/v6/internal/cmd"
    13  	"github.com/snyk/vervet/v6/testdata"
    14  )
    15  
    16  func TestBuild(t *testing.T) {
    17  	c := qt.New(t)
    18  	dstDir := c.TempDir()
    19  	err := cmd.Vervet.Run([]string{"vervet", "build", testdata.Path("resources"), dstDir})
    20  	c.Assert(err, qt.IsNil)
    21  	tests := []struct {
    22  		version string
    23  		paths   []string
    24  	}{{
    25  		version: "2021-06-01~experimental",
    26  		paths:   []string{"/examples/hello-world/{id}"},
    27  	}, {
    28  		version: "2021-06-07~experimental",
    29  		paths:   []string{"/examples/hello-world/{id}"},
    30  	}, {
    31  		version: "2021-06-13~beta",
    32  		paths:   []string{"/examples/hello-world", "/examples/hello-world/{id}"},
    33  	}, {
    34  		version: "2021-06-04~experimental",
    35  		paths:   []string{"/examples/hello-world/{id}", "/orgs/{orgId}/projects"},
    36  	}}
    37  	for _, test := range tests {
    38  		c.Run("built version "+test.version, func(c *qt.C) {
    39  			doc, err := vervet.NewDocumentFile(dstDir + "/" + test.version + "/spec.yaml")
    40  			c.Assert(err, qt.IsNil)
    41  			c.Assert(doc.Validate(context.TODO()), qt.IsNil)
    42  			for _, path := range test.paths {
    43  				c.Assert(doc.Paths[path], qt.Not(qt.IsNil))
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func TestBuildConflict(t *testing.T) {
    50  	c := qt.New(t)
    51  	dstDir := c.TempDir()
    52  	err := cmd.Vervet.Run([]string{"vervet", "build", testdata.Path("conflict"), dstDir})
    53  	c.Assert(err, qt.ErrorMatches, `failed to load spec versions: conflict: .*`)
    54  }
    55  
    56  func TestBuildInclude(t *testing.T) {
    57  	c := qt.New(t)
    58  	dstDir := c.TempDir()
    59  	err := cmd.Vervet.Run(
    60  		[]string{
    61  			"vervet",
    62  			"build",
    63  			"-I",
    64  			testdata.Path("resources/include.yaml"),
    65  			testdata.Path("resources"),
    66  			dstDir,
    67  		},
    68  	)
    69  	c.Assert(err, qt.IsNil)
    70  
    71  	tests := []struct {
    72  		version string
    73  	}{{
    74  		version: "2021-06-01~experimental",
    75  	}, {
    76  		version: "2021-06-07~experimental",
    77  	}, {
    78  		version: "2021-06-13~beta",
    79  	}, {
    80  		version: "2021-06-04~experimental",
    81  	}}
    82  	for _, test := range tests {
    83  		c.Assert(err, qt.IsNil)
    84  		// Load just-built OpenAPI YAML file
    85  		doc, err := vervet.NewDocumentFile(dstDir + "/" + test.version + "/spec.yaml")
    86  		c.Assert(err, qt.IsNil)
    87  
    88  		expected, err := os.ReadFile(testdata.Path("output/" + test.version + "/spec.json"))
    89  		c.Assert(err, qt.IsNil)
    90  
    91  		// Servers will differ between the fixture output and the above, since
    92  		// testdata/.vervet.yaml contains an overlay that modifies the servers:
    93  		// section. This patches the output to match expected.
    94  		doc.Servers = []*openapi3.Server{{
    95  			Description: "Test REST API",
    96  			URL:         "https://example.com/api/rest",
    97  		}}
    98  
    99  		c.Assert(expected, qt.JSONEquals, doc)
   100  	}
   101  }