From 9e60c5875b4f9bb50fa48c0eecc2e486a706096f Mon Sep 17 00:00:00 2001 From: Nathan Rijksen Date: Wed, 30 Aug 2023 15:29:33 -0700 Subject: [PATCH] Add test that surfaces issue with output containing invalid data --- test/helloworld/go.mod | 13 +++++++++++++ test/helloworld/go.sum | 11 +++++++++++ test/helloworld/helloworld.go | 7 +++++++ test/helloworld/helloworld_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 test/helloworld/go.mod create mode 100644 test/helloworld/go.sum create mode 100644 test/helloworld/helloworld.go create mode 100644 test/helloworld/helloworld_test.go diff --git a/test/helloworld/go.mod b/test/helloworld/go.mod new file mode 100644 index 0000000..20492c3 --- /dev/null +++ b/test/helloworld/go.mod @@ -0,0 +1,13 @@ +module github.com/ActiveState/termtest/test/helloworld + +go 1.20 + +replace github.com/ActiveState/termtest => ../../ + +require github.com/ActiveState/termtest v0.0.0-00010101000000-000000000000 + +require ( + github.com/ActiveState/pty v0.0.0-20230628221854-6fb90eb08a14 // indirect + github.com/hinshun/vt10x v0.0.0-20220301184237-5011da428d02 // indirect + golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49 // indirect +) diff --git a/test/helloworld/go.sum b/test/helloworld/go.sum new file mode 100644 index 0000000..378da9f --- /dev/null +++ b/test/helloworld/go.sum @@ -0,0 +1,11 @@ +github.com/ActiveState/pty v0.0.0-20230628221854-6fb90eb08a14 h1:RdhhSiwmgyUaaF2GBNrbqTwE5SM+MaVjwf91Ua+CK8c= +github.com/ActiveState/pty v0.0.0-20230628221854-6fb90eb08a14/go.mod h1:5mM6vNRQwshCjlkOnVpwC//4ZpkiC6nmZr8lPOxJdXs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/hinshun/vt10x v0.0.0-20220301184237-5011da428d02 h1:AgcIVYPa6XJnU3phs104wLj8l5GEththEw6+F79YsIY= +github.com/hinshun/vt10x v0.0.0-20220301184237-5011da428d02/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49 h1:TMjZDarEwf621XDryfitp/8awEhiZNiwgphKlTMGRIg= +golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/test/helloworld/helloworld.go b/test/helloworld/helloworld.go new file mode 100644 index 0000000..91e7378 --- /dev/null +++ b/test/helloworld/helloworld.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello World") +} diff --git a/test/helloworld/helloworld_test.go b/test/helloworld/helloworld_test.go new file mode 100644 index 0000000..7741915 --- /dev/null +++ b/test/helloworld/helloworld_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "os/exec" + "path/filepath" + "runtime" + "testing" + + "github.com/ActiveState/termtest" +) + +func Test_ExactOutput(t *testing.T) { + _, filename, _, ok := runtime.Caller(0) + if !ok { + t.Fatal("unable to get the current filename") + } + cmd := exec.Command("go", "run", filepath.Join(filepath.Dir(filename), "helloworld.go")) + + tt, err := termtest.New(cmd, termtest.OptTestErrorHandler(t)) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + tt.ExpectExitCode(0) + + output := tt.Output() + if output != "Hello World" { + t.Errorf("Output should be 'Hello World' and nothing else, got: %s", output) + } +}