From da8d72620a2756ae6bc1e43de40edeff79bc5872 Mon Sep 17 00:00:00 2001 From: Roxie Gibson Date: Wed, 20 Oct 2021 10:15:55 +0100 Subject: [PATCH] test: warning not to test cli [ci skip] --- cli/internal/errors_test.go | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 cli/internal/errors_test.go diff --git a/cli/internal/errors_test.go b/cli/internal/errors_test.go new file mode 100644 index 000000000..e1c4dca3b --- /dev/null +++ b/cli/internal/errors_test.go @@ -0,0 +1,38 @@ +package internal + +import ( + "github.com/urfave/cli/v2" +) + +// Testing functions that call os.Exit +// https://stackoverflow.com/questions/26225513/how-to-test-os-exit-scenarios-in-go +// https://talks.golang.org/2014/testing.slide#23 + +var testapp = &cli.App{ + Name: "abra", + Usage: `The Co-op Cloud command-line utility belt 🎩🐇`, +} + +// not testing output as that changes. just if it exits with code 1 +// does not work because of some weird errors on cli's part. Its a hard lib to test effectively. +// func TestShowSubcommandHelpAndError(t *testing.T) { +// if os.Getenv("HelpAndError") == "1" { +// ShowSubcommandHelpAndError(cli.NewContext(testapp, nil, nil), errors.New("Test error")) +// return +// } +// cmd := exec.Command(os.Args[0], "-test.run=TestShowSubcommandHelpAndError") +// cmd.Env = append(os.Environ(), "HelpAndError=1") +// var out bytes.Buffer +// cmd.Stderr = &out +// err := cmd.Run() +// println(out.String()) +// if !strings.Contains(out.String(), "Test error") { + +// t.Fatalf("expected command to show the error causing the exit, did not get correct stdout output") +// } + +// if e, ok := err.(*exec.ExitError); ok && !e.Success() { +// return +// } +// t.Fatalf("process ran with err %v, want exit status 1", err) +// }