diff --git a/test/integration_test.go b/test/integration_test.go new file mode 100644 index 0000000..9af6dea --- /dev/null +++ b/test/integration_test.go @@ -0,0 +1,111 @@ +package test + +import ( + "flag" + "fmt" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "reflect" + "testing" +) + +var ( + update = flag.Bool("update", false, "update .golden files") +) + +var binaryName = "clibgen" +var binaryPath = "" + +func TestMain(m *testing.M) { + flag.Parse() + + err := os.Chdir("..") + if err != nil { + fmt.Printf("could not change dir: %v", err) + os.Exit(1) + } + + dir, err := os.Getwd() + + if err != nil { + fmt.Printf("could not get current dir: %v", err) + } + + binaryPath = filepath.Join(dir, binaryName) + + os.Exit(m.Run()) +} + + +func runBinary(args []string) ([]byte, error) { + cmd := exec.Command(binaryPath, args...) + cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata") + return cmd.CombinedOutput() +} + +func TestCliArgs(t *testing.T) { + tests := []struct { + name string + args []string + fixture string + }{ + // {"no arguments", []string{}, "no-args.golden"}, + {"help args", []string{"--help"}, "help.golden"}, + // {"one argument", []string{"ciao"}, "one-argument.golden"}, + // {"multiple arguments", []string{"ciao", "hello"}, "multiple-arguments.golden"}, + // {"shout arg", []string{"--shout", "ciao"}, "shout-arg.golden"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + output, err := runBinary(tt.args) + + if err != nil { + t.Fatal(err) + } + + fmt.Println(string(output)) + + if *update { + writeFixture(t, tt.fixture, output) + } + + actual := string(output) + + expected := loadFixture(t, tt.fixture) + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("actual = %s, expected = %s", actual, expected) + } + }) + } +} + +func writeFixture(t *testing.T, goldenFile string, actual []byte) { + t.Helper() + goldenPath := "testdata/" + goldenFile + + f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) + defer f.Close() + + _, err = f.WriteString(string(actual)) + + if err != nil { + t.Fatalf("Error writing to file %s: %s", goldenPath, err) + } +} + +func loadFixture(t *testing.T, goldenFile string) string { + goldenPath := "testdata/" + goldenFile + + f, err := os.OpenFile(goldenPath, os.O_RDWR, 0644) + + content, err := ioutil.ReadAll(f) + if err != nil { + t.Fatalf("Error opening file %s: %s", goldenPath, err) + } + + return string(content) +} diff --git a/testdata/help.golden b/testdata/help.golden new file mode 100644 index 0000000..692070e --- /dev/null +++ b/testdata/help.golden @@ -0,0 +1,17 @@ + +Clibgen is a CLI application to search and download epubs, pdfs, from library genesis. +Useful if you are lazy to open up a browser to download e-books/resources. + +Usage: + clibgen [command] + +Available Commands: + completion Generate the autocompletion script for the specified shell + help Help about any command + search search for a book, paper or article + +Flags: + -h, --help help for clibgen + -t, --toggle Help message for toggle + +Use "clibgen [command] --help" for more information about a command.