Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jul 13, 2023
1 parent f30ad1e commit 4447f85
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 81 deletions.
121 changes: 51 additions & 70 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -46,32 +48,51 @@ func runBinary(args []string) ([]byte, error) {
}

func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) {
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
fmt.Println("Running binary with file input", bytesToWrite)
fmt.Printf("Executing command: %s", binaryPath)

cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata")

tmpInputFile, err := ioutil.TempFile("", "")
stdout, err := cmd.StdoutPipe()
stdin, err := cmd.StdinPipe()

defer os.Remove(tmpInputFile.Name())
cmd.Stderr = cmd.Stdout

if err != nil {
fmt.Printf("could not create temp file: %v", err)
return nil, err
}

if _, err := tmpInputFile.Write(bytesToWrite); err != nil {
panic(err)
if err = cmd.Start(); err != nil {
return nil, err
}

if _, err := tmpInputFile.Seek(0, 0); err != nil {
panic(err)
time.Sleep(1 * time.Second)

_, err = stdin.Write(bytesToWrite)

if err != nil {
fmt.Println("Error writing to stdin", err)
}

os.Stdin = tmpInputFile
time.Sleep(15 * time.Second)

fmt.Printf("Executing command: %s", binaryPath)
if err = cmd.Process.Signal(os.Interrupt); err != nil {
fmt.Println("signal")
return nil, err
}

cmd := exec.Command(binaryPath, args...)
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata")
return cmd.CombinedOutput()
out, _ := ioutil.ReadAll(stdout)

if err = cmd.Wait(); err != nil {
if strings.Contains(err.Error(), "interrupt") {
return out, nil
} else {
return nil, err
}
}

return nil, err
}

func TestStaticCliArgs(t *testing.T) {
Expand All @@ -80,13 +101,7 @@ func TestStaticCliArgs(t *testing.T) {
args []string
fixture string
}{
// {"no arguments", []string{}, "no-args.golden"},
{"help args", []string{"--help"}, "help.golden"},
// {"search test", []string{"search", "Eloquent JavaScript"}, "eloquent.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 {
Expand All @@ -97,8 +112,6 @@ func TestStaticCliArgs(t *testing.T) {
t.Fatal(err)
}

fmt.Println(string(output))

if *update {
writeFixture(t, tt.fixture, output)
}
Expand All @@ -121,76 +134,44 @@ func TestSearch(t *testing.T) {
fixture string
bytesToWrite []byte
}{
// {"no arguments", []string{}, "no-args.golden"},
{"search test", []string{"search \"Eloquent JavaScript\""}, "eloquent.golden", []byte{14, 10}},
// {"one argument", []string{"ciao"}, "one-argument.golden"},
// {"multiple arguments", []string{"ciao", "hello"}, "multiple-arguments.golden"},
// {"shout arg", []string{"--shout", "ciao"}, "shout-arg.golden"},
{"search test", []string{"search", "Eloquent JavaScript"}, "eloquent.golden", []byte{14, 14, 10}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output, err := runBinaryWithFileInput(tt.args, tt.bytesToWrite)

if err != nil {
fmt.Println("error", err.Error())
t.Fatal(err)
}

fmt.Println(string(output))

if *update {
writeFixture(t, tt.fixture, output)
}

actual := string(output)
actual := removeLastLine(string(output), 1)
expected := strings.TrimSpace(loadFixture(t, tt.fixture))

expected := loadFixture(t, tt.fixture)
fmt.Println("\nactual:\n", actual)
fmt.Println("\nexpected:\n", expected)

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("actual = %s, expected = %s", actual, expected)
t.Fatalf("actual: \n'%s'\n, expected: \n'%s'\n", actual, expected)
}
})
}

}

// func TestSearch(t *testing.T) {
// tests := []struct {
// name string
// args []string
// fixture string
// }{
// // {"no arguments", []string{}, "no-args.golden"},
// {"search test", []string{"search \"Eloquent JavaScript\""}, "eloquent.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 removeLastLine(str string, num int) string {
lines := strings.Split(str, "\n")

if len(lines) > 0 {
lines = lines[:len(lines) - num]
}

return strings.Join(lines, "\n")
}

func writeFixture(t *testing.T, goldenFile string, actual []byte) {
t.Helper()
Expand Down
11 changes: 0 additions & 11 deletions testdata/eloquent.golden
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
Searching for: Eloquent JavaScript
https://libgen.is/search.php?req=Eloquent+JavaScript&res=25&view=simple&phrase=1&column=title
Search complete, parsing the document...
Use the arrow keys to navigate: ↓ ↑ → ←
? Select Title:
▸ pdf 1 Mb | Eloquent JavaScript Marijn Haverbeke
pdf 7 Mb | Eloquent JavaScript: A Modern ... Marijn Haverbeke
epub 3 Mb | Eloquent JavaScript: A Modern ... Marijn Haverbeke
pdf 2 Mb | Eloquent JavaScript: A Modern ... Marijn Haverbeke
epub 4 Mb | Eloquent JavaScript: A Modern ... Marijn Haverbeke

0 comments on commit 4447f85

Please sign in to comment.