Skip to content

Commit

Permalink
wcyd its down
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jun 29, 2023
1 parent fbee267 commit 39e40fa
Showing 1 changed file with 65 additions and 26 deletions.
91 changes: 65 additions & 26 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"reflect"
"syscall"
"testing"
)

Expand Down Expand Up @@ -43,47 +41,48 @@ func TestMain(m *testing.M) {

func runBinary(args []string) ([]byte, error) {
cmd := exec.Command(binaryPath, args...)
// Set the command's output to the standard output of the current process
// cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
cmd.Env = append(os.Environ(), "GOCOVERDIR=.coverdata")
return cmd.CombinedOutput()
}

// Start the command
// err := cmd.Start()
// if err != nil {
// fmt.Printf("Failed to start command: %s\n", err)
// return nil, err
// }

// Set up a signal channel to listen for interrupts
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
func runBinaryWithFileInput(args []string, bytesToWrite []byte) ([]byte, error) {
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()

go func() {
// Wait for a signal
<-sigChan
tmpInputFile, err := ioutil.TempFile("", "")

// Send the interrupt signal to the command's process
cmd.Process.Signal(os.Interrupt)
}()
defer os.Remove(tmpInputFile.Name())

// Wait for the command to complete
err := cmd.Wait()
if err != nil {
fmt.Printf("Command failed: %s\n", err)
fmt.Printf("could not create temp file: %v", err)
}

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

if _, err := tmpInputFile.Seek(0, 0); err != nil {
panic(err)
}

os.Stdin = tmpInputFile

fmt.Printf("Executing command: %s", binaryPath)

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

func TestCliArgs(t *testing.T) {
func TestStaticCliArgs(t *testing.T) {
tests := []struct {
name string
args []string
fixture string
}{
// {"no arguments", []string{}, "no-args.golden"},
{"help args", []string{"--help"}, "help.golden"},
{"search test", []string{"search", "Eloquent JavaScript"}, "eloquent.golden"},
// {"search test", []string{"search", "Eloquent JavaScript"}, "eloquent.golden"},

// {"one argument", []string{"ciao"}, "one-argument.golden"},
// {"multiple arguments", []string{"ciao", "hello"}, "multiple-arguments.golden"},
Expand Down Expand Up @@ -115,6 +114,46 @@ func TestCliArgs(t *testing.T) {
}
}

func TestSearch(t *testing.T) {
tests := []struct {
name string
args []string
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"},
}

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

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 TestSearch(t *testing.T) {
// tests := []struct {
// name string
Expand Down

0 comments on commit 39e40fa

Please sign in to comment.