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

     1  package cmd_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  
    10  	"github.com/snyk/vervet/v6/internal/cmd"
    11  	"github.com/snyk/vervet/v6/testdata"
    12  )
    13  
    14  func cd(c *qt.C, path string) {
    15  	cwd, err := os.Getwd()
    16  	c.Assert(err, qt.IsNil)
    17  	err = os.Chdir(path)
    18  	c.Assert(err, qt.IsNil)
    19  	c.Cleanup(func() {
    20  		err := os.Chdir(cwd)
    21  		c.Assert(err, qt.IsNil)
    22  	})
    23  }
    24  
    25  func TestResourceFiles(t *testing.T) {
    26  	c := qt.New(t)
    27  	tmp := c.TempDir()
    28  	tmpFile := filepath.Join(tmp, "out")
    29  	c.Run("cmd", func(c *qt.C) {
    30  		output, err := os.Create(tmpFile)
    31  		c.Assert(err, qt.IsNil)
    32  		defer output.Close()
    33  		c.Patch(&os.Stdout, output)
    34  		cd(c, testdata.Path("."))
    35  		err = cmd.Vervet.Run([]string{"vervet", "resource", "files"})
    36  		c.Assert(err, qt.IsNil)
    37  	})
    38  	out, err := os.ReadFile(tmpFile)
    39  	c.Assert(err, qt.IsNil)
    40  	c.Assert(string(out), qt.Equals, `
    41  resources/_examples/hello-world/2021-06-01/spec.yaml
    42  resources/_examples/hello-world/2021-06-07/spec.yaml
    43  resources/_examples/hello-world/2021-06-13/spec.yaml
    44  resources/projects/2021-06-04/spec.yaml
    45  resources/projects/2021-08-20/spec.yaml
    46  resources/projects/2023-06-03/spec.yaml
    47  resources/users/2023-06-01/spec.yaml
    48  resources/users/2023-06-02/spec.yaml
    49  `[1:])
    50  }
    51  
    52  func TestResourceInfo(t *testing.T) {
    53  	c := qt.New(t)
    54  	tmp := c.TempDir()
    55  	tmpFile := filepath.Join(tmp, "out")
    56  	c.Run("cmd", func(c *qt.C) {
    57  		output, err := os.Create(tmpFile)
    58  		c.Assert(err, qt.IsNil)
    59  		defer output.Close()
    60  		c.Patch(&os.Stdout, output)
    61  		cd(c, testdata.Path("."))
    62  		err = cmd.Vervet.Run([]string{"vervet", "resource", "info"})
    63  		c.Assert(err, qt.IsNil)
    64  	})
    65  	out, err := os.ReadFile(tmpFile)
    66  	c.Assert(err, qt.IsNil)
    67  	c.Assert(string(out), qt.Equals, `
    68  +----------+-------------+-------------------------+--------------------------------------+--------+-------------------+
    69  |   API    |  RESOURCE   |         VERSION         |                 PATH                 | METHOD |     OPERATION     |
    70  +----------+-------------+-------------------------+--------------------------------------+--------+-------------------+
    71  | testdata | hello-world | 2021-06-01~experimental | /examples/hello-world/{id}           | GET    | helloWorldGetOne  |
    72  | testdata | projects    | 2021-06-04~experimental | /orgs/{orgId}/projects               | GET    | getOrgsProjects   |
    73  | testdata | hello-world | 2021-06-07~experimental | /examples/hello-world/{id}           | GET    | helloWorldGetOne  |
    74  | testdata | hello-world | 2021-06-13~beta         | /examples/hello-world                | POST   | helloWorldCreate  |
    75  | testdata | hello-world | 2021-06-13~beta         | /examples/hello-world/{id}           | GET    | helloWorldGetOne  |
    76  | testdata | projects    | 2021-08-20~experimental | /orgs/{org_id}/projects/{project_id} | DELETE | deleteOrgsProject |
    77  | testdata | users       | 2023-06-01~experimental | /users                               | GET    | getUsers          |
    78  | testdata | users       | 2023-06-02~experimental | /users                               | GET    | getUsers          |
    79  | testdata | projects    | 2023-06-03~experimental | /orgs/{org_id}/projects/{project_id} | DELETE | deleteOrgsProject |
    80  +----------+-------------+-------------------------+--------------------------------------+--------+-------------------+
    81  `[1:])
    82  }
    83  
    84  func TestResourceInfoResource(t *testing.T) {
    85  	c := qt.New(t)
    86  	tmp := c.TempDir()
    87  	tmpFile := filepath.Join(tmp, "out")
    88  	c.Run("cmd", func(c *qt.C) {
    89  		output, err := os.Create(tmpFile)
    90  		c.Assert(err, qt.IsNil)
    91  		defer output.Close()
    92  		c.Patch(&os.Stdout, output)
    93  		cd(c, testdata.Path("."))
    94  		err = cmd.Vervet.Run([]string{"vervet", "resource", "info", "testdata", "projects"})
    95  		c.Assert(err, qt.IsNil)
    96  	})
    97  	out, err := os.ReadFile(tmpFile)
    98  	c.Assert(err, qt.IsNil)
    99  	c.Assert(string(out), qt.Equals, `
   100  +----------+----------+-------------------------+--------------------------------------+--------+-------------------+
   101  |   API    | RESOURCE |         VERSION         |                 PATH                 | METHOD |     OPERATION     |
   102  +----------+----------+-------------------------+--------------------------------------+--------+-------------------+
   103  | testdata | projects | 2021-06-04~experimental | /orgs/{orgId}/projects               | GET    | getOrgsProjects   |
   104  | testdata | projects | 2021-08-20~experimental | /orgs/{org_id}/projects/{project_id} | DELETE | deleteOrgsProject |
   105  | testdata | projects | 2023-06-03~experimental | /orgs/{org_id}/projects/{project_id} | DELETE | deleteOrgsProject |
   106  +----------+----------+-------------------------+--------------------------------------+--------+-------------------+
   107  `[1:])
   108  }