github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/compute/validate_test.go (about) 1 package compute_test 2 3 import ( 4 "bytes" 5 "io" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/fastly/cli/pkg/app" 11 "github.com/fastly/cli/pkg/global" 12 "github.com/fastly/cli/pkg/testutil" 13 ) 14 15 func TestValidate(t *testing.T) { 16 args := testutil.Args 17 scenarios := []testutil.TestScenario{ 18 { 19 Name: "success", 20 Args: args("compute validate --package pkg/package.tar.gz"), 21 WantError: "", 22 WantOutput: "Validated package", 23 }, 24 } 25 for testcaseIdx := range scenarios { 26 testcase := &scenarios[testcaseIdx] 27 t.Run(testcase.Name, func(t *testing.T) { 28 // We're going to chdir to a deploy environment, 29 // so save the PWD to return to, afterwards. 30 pwd, err := os.Getwd() 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 // Create test environment 36 rootdir := testutil.NewEnv(testutil.EnvOpts{ 37 T: t, 38 Copy: []testutil.FileIO{ 39 { 40 Src: filepath.Join("testdata", "deploy", "pkg", "package.tar.gz"), 41 Dst: filepath.Join("pkg", "package.tar.gz"), 42 }, 43 }, 44 }) 45 defer os.RemoveAll(rootdir) 46 47 // Before running the test, chdir into the build environment. 48 // When we're done, chdir back to our original location. 49 // This is so we can reliably copy the testdata/ fixtures. 50 if err := os.Chdir(rootdir); err != nil { 51 t.Fatal(err) 52 } 53 defer func() { 54 _ = os.Chdir(pwd) 55 }() 56 57 var stdout bytes.Buffer 58 app.Init = func(_ []string, _ io.Reader) (*global.Data, error) { 59 return testutil.MockGlobalData(testcase.Args, &stdout), nil 60 } 61 err = app.Run(testcase.Args, nil) 62 testutil.AssertErrorContains(t, err, testcase.WantError) 63 testutil.AssertStringContains(t, stdout.String(), testcase.WantOutput) 64 }) 65 } 66 }