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

     1  package cmd_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	qt "github.com/frankban/quicktest"
     8  
     9  	"github.com/snyk/vervet/v6"
    10  	"github.com/snyk/vervet/v6/internal/cmd"
    11  	"github.com/snyk/vervet/v6/testdata"
    12  )
    13  
    14  func TestFilterInclude(t *testing.T) {
    15  	c := qt.New(t)
    16  	tmpOut := c.TempDir()
    17  
    18  	c.Run("filter include hello-world", func(c *qt.C) {
    19  		stdout, err := os.Create(tmpOut + "/spec.yaml")
    20  		c.Assert(err, qt.IsNil)
    21  		defer stdout.Close()
    22  		c.Patch(&os.Stdout, stdout)
    23  		err = cmd.Vervet.Run(
    24  			[]string{
    25  				"vervet",
    26  				"filter",
    27  				"-I",
    28  				"/examples/hello-world/{id}",
    29  				testdata.Path("output/2021-06-01~experimental/spec.json"),
    30  			},
    31  		)
    32  		c.Assert(err, qt.IsNil)
    33  	})
    34  
    35  	out, err := os.ReadFile(tmpOut + "/spec.yaml")
    36  	c.Assert(err, qt.IsNil)
    37  	c.Log(string(out))
    38  
    39  	doc, err := vervet.NewDocumentFile(tmpOut + "/spec.yaml")
    40  	c.Assert(err, qt.IsNil)
    41  
    42  	// Included paths and their referenced components are present
    43  	c.Assert(doc.Paths["/examples/hello-world/{id}"], qt.Not(qt.IsNil))
    44  	c.Assert(doc.Components.Schemas["HelloWorld"], qt.Not(qt.IsNil))
    45  
    46  	// Not-included paths are not present
    47  	c.Assert(doc.Paths["/openapi"], qt.IsNil)
    48  }
    49  
    50  func XestFilterExclude(t *testing.T) {
    51  	t.Helper()
    52  	c := qt.New(t)
    53  	tmpOut := c.TempDir()
    54  
    55  	c.Run("filter hello-world", func(c *qt.C) {
    56  		stdout, err := os.Create(tmpOut + "/spec.yaml")
    57  		c.Assert(err, qt.IsNil)
    58  		defer stdout.Close()
    59  		app := cmd.NewApp(&cmd.CLIApp, cmd.VervetParams{
    60  			Stdin:  os.Stdin,
    61  			Stdout: stdout,
    62  			Stderr: os.Stderr,
    63  			Prompt: cmd.Prompt{},
    64  		})
    65  		err = app.Run(
    66  			[]string{
    67  				"vervet",
    68  				"filter",
    69  				"-X",
    70  				"/examples/hello-world/{id}",
    71  				testdata.Path("output/2021-06-01~experimental/spec.json"),
    72  			},
    73  		)
    74  		c.Assert(err, qt.IsNil)
    75  	})
    76  
    77  	doc, err := vervet.NewDocumentFile(tmpOut + "/spec.yaml")
    78  	c.Assert(err, qt.IsNil)
    79  
    80  	// Excluded paths and components only these reference, are not present
    81  	c.Assert(doc.Paths["/examples/hello-world/{id}"], qt.IsNil)
    82  	c.Assert(doc.Components.Schemas["HelloWorld"], qt.IsNil)
    83  
    84  	// Not-excluded paths and referenced components are present
    85  	c.Assert(doc.Paths["/openapi"], qt.Not(qt.IsNil))
    86  	c.Assert(doc.Paths["/openapi/{version}"], qt.Not(qt.IsNil))
    87  	c.Assert(doc.Components.Headers["VersionRequestedResponseHeader"], qt.Not(qt.IsNil))
    88  }