Skip to content

Commit

Permalink
add integration test + testdata
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jun 29, 2023
1 parent f61a10e commit 08dcccf
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
111 changes: 111 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 17 additions & 0 deletions testdata/help.golden
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 08dcccf

Please sign in to comment.