diff --git a/config.go b/config.go index 93ebfd6..fb1e879 100644 --- a/config.go +++ b/config.go @@ -28,13 +28,15 @@ func getConfig() *config.Config { debugMode := false slomo := false - flag.BoolVar(&showVersion, "version", showVersion, "Output version information") - flag.BoolVar(&ignoreConfig, "ignore-config", ignoreConfig, "Ignore user config files and use defaults") - flag.StringVar(&shell, "shell", shell, "Specify the shell to use") - flag.BoolVar(&debugMode, "debug", debugMode, "Enable debug logging") - flag.BoolVar(&slomo, "slomo", slomo, "Render in slow motion (useful for debugging)") + if flag.Parsed() == false { + flag.BoolVar(&showVersion, "version", showVersion, "Output version information") + flag.BoolVar(&ignoreConfig, "ignore-config", ignoreConfig, "Ignore user config files and use defaults") + flag.StringVar(&shell, "shell", shell, "Specify the shell to use") + flag.BoolVar(&debugMode, "debug", debugMode, "Enable debug logging") + flag.BoolVar(&slomo, "slomo", slomo, "Render in slow motion (useful for debugging)") - flag.Parse() // actual parsing and fetching flags from the command line + flag.Parse() // actual parsing and fetching flags from the command line + } actuallyProvidedFlags := getActuallyProvidedFlags() if showVersion { diff --git a/main.go b/main.go index 66f257b..aff7af0 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ func main() { initialize(nil) } -func initialize(fn callback) { +func initialize(unitTestfunc callback) { conf := getConfig() logger, err := getLogger(conf) if err != nil { @@ -63,17 +63,17 @@ func initialize(fn callback) { logger.Fatalf("Cannot start: %s", err) } - if fn != nil { - go fn(terminal, g) + if unitTestfunc != nil { + go unitTestfunc(terminal, g) + } else { + go func() { + if err := guestProcess.Wait(); err != nil { + logger.Fatalf("Failed to wait for guest process: %s", err) + } + g.Close() + }() } - go func() { - if err := guestProcess.Wait(); err != nil { - logger.Fatalf("Failed to wait for guest process: %s", err) - } - g.Close() - }() - if err := g.Render(); err != nil { logger.Fatalf("Render error: %s", err) } diff --git a/main_test.go b/main_test.go index c761761..5856eb8 100644 --- a/main_test.go +++ b/main_test.go @@ -3,8 +3,10 @@ package main import ( + "flag" "fmt" "os" + "strings" "testing" "time" @@ -14,6 +16,9 @@ import ( "github.com/carlogit/phash" ) +var termRef *terminal.Terminal +var guiRef *gui.GUI + func terminate(msg string) int { defer fmt.Print(msg) return 1 @@ -36,12 +41,13 @@ func hash(path string) string { return imageHash } -func compareImages(img1 string, img2 string) { - template := hash(img1) - screen := hash(img2) +func compareImages(expected string, actual string) { + template := hash(expected) + screen := hash(actual) distance := phash.GetDistance(template, screen) if distance != 0 { - os.Exit(terminate(fmt.Sprintf("Screenshot doesn't match expected image. Distance of hashes difference: %d\n", distance))) + os.Exit(terminate(fmt.Sprintf("Screenshot \"%s\" doesn't match expected image \"%s\". Distance of hashes difference: %d\n", + actual, expected, distance))) } } @@ -53,50 +59,108 @@ func enter(terminal *terminal.Terminal) { terminal.Write([]byte("\n")) } +func validateScreen(img string) { + guiRef.Screenshot(img) + compareImages(strings.Join([]string{"vttest/", img}, ""), img) + + enter(termRef) + sleep() +} + func TestMain(m *testing.M) { - testCursorMovement() -} + flag.Parse() -func testCursorMovement() { - testFunc := func(term *terminal.Terminal, g *gui.GUI) { - sleep() - send(term, "vttest\n") - sleep() - send(term, "1\n") - sleep() + go m.Run() - if term.ActiveBuffer().CompareViewLines("vttest/test-cursor-movement-1") == false { - os.Exit(terminate(fmt.Sprintf("ActiveBuffer doesn't match vttest template vttest/test-cursor-movement-1"))) - } - g.Screenshot ("test-cursor-movement-1.png") - compareImages("vttest/test-cursor-movement-1.png", "test-cursor-movement-1.png") - - enter(term) - sleep() - g.Screenshot ("test-cursor-movement-2.png") - compareImages("vttest/test-cursor-movement-2.png", "test-cursor-movement-2.png") - - enter(term) - sleep() - g.Screenshot ("test-cursor-movement-3.png") - compareImages("vttest/test-cursor-movement-3.png", "test-cursor-movement-3.png") - - enter(term) - sleep() - g.Screenshot ("test-cursor-movement-4.png") - compareImages("vttest/test-cursor-movement-4.png", "test-cursor-movement-4.png") - - enter(term) - sleep() - g.Screenshot ("test-cursor-movement-5.png") - compareImages("vttest/test-cursor-movement-5.png", "test-cursor-movement-5.png") - - enter(term) - sleep() - g.Screenshot ("test-cursor-movement-6.png") - compareImages("vttest/test-cursor-movement-6.png", "test-cursor-movement-6.png") - os.Exit(0) + for f := range tests { + f() } - - initialize(testFunc) +} + +var tests = make(chan func()) + +func runMain(f func()) { + complete := make(chan bool, 1) + tests <- func() { + f() + complete <- true + } + <-complete +} + +func TestCursorMovement(t *testing.T) { + runMain(func() { + + testFunc := func(term *terminal.Terminal, g *gui.GUI) { + termRef = term; guiRef = g + + sleep() + send(term, "vttest\n") + sleep() + send(term, "1\n") + sleep() + + if term.ActiveBuffer().CompareViewLines("vttest/test-cursor-movement-1") == false { + os.Exit(terminate(fmt.Sprintf("ActiveBuffer doesn't match vttest template vttest/test-cursor-movement-1"))) + } + + validateScreen("test-cursor-movement-1.png") + validateScreen("test-cursor-movement-2.png") + validateScreen("test-cursor-movement-3.png") + validateScreen("test-cursor-movement-4.png") + validateScreen("test-cursor-movement-5.png") + validateScreen("test-cursor-movement-6.png") + + g.Close() + } + + initialize(testFunc) + }) +} + +func TestScreenFeatures(t *testing.T) { + runMain(func() { + + testFunc := func(term *terminal.Terminal, g *gui.GUI) { + termRef = term; guiRef = g + + sleep() + send(term, "vttest\n") + sleep() + send(term, "2\n") + sleep() + + validateScreen("test-screen-features-1.png") + validateScreen("test-screen-features-2.png") + validateScreen("test-screen-features-3.png") + validateScreen("test-screen-features-4.png") + validateScreen("test-screen-features-5.png") + validateScreen("test-screen-features-6.png") + validateScreen("test-screen-features-7.png") + validateScreen("test-screen-features-8.png") + validateScreen("test-screen-features-9.png") + validateScreen("test-screen-features-10.png") + + // 11th screen test is not passing https://github.com/liamg/aminal/issues/207 + //g.Screenshot("vttest/test-screen-features-11.png") + //compareImages("vttest/test-screen-features-11.png", "vttest/test-screen-features-11.png") + + enter(term) + sleep() + + validateScreen("test-screen-features-12.png") + validateScreen("test-screen-features-13.png") + validateScreen("test-screen-features-14.png") + validateScreen("test-screen-features-15.png") + + g.Close() + } + + initialize(testFunc) + }) +} + +// Last Test should terminate main goroutine since it's infinity looped to execute others GUI tests in main goroutine +func TestExit(t *testing.T) { + os.Exit(0) } diff --git a/vttest/test-screen-features-1.png b/vttest/test-screen-features-1.png new file mode 100644 index 0000000..13b885a Binary files /dev/null and b/vttest/test-screen-features-1.png differ diff --git a/vttest/test-screen-features-10.png b/vttest/test-screen-features-10.png new file mode 100644 index 0000000..d5ae691 Binary files /dev/null and b/vttest/test-screen-features-10.png differ diff --git a/vttest/test-screen-features-12.png b/vttest/test-screen-features-12.png new file mode 100644 index 0000000..f899678 Binary files /dev/null and b/vttest/test-screen-features-12.png differ diff --git a/vttest/test-screen-features-13.png b/vttest/test-screen-features-13.png new file mode 100644 index 0000000..e180c23 Binary files /dev/null and b/vttest/test-screen-features-13.png differ diff --git a/vttest/test-screen-features-14.png b/vttest/test-screen-features-14.png new file mode 100644 index 0000000..b4906d5 Binary files /dev/null and b/vttest/test-screen-features-14.png differ diff --git a/vttest/test-screen-features-15.png b/vttest/test-screen-features-15.png new file mode 100644 index 0000000..29b3be0 Binary files /dev/null and b/vttest/test-screen-features-15.png differ diff --git a/vttest/test-screen-features-2.png b/vttest/test-screen-features-2.png new file mode 100644 index 0000000..3b8d705 Binary files /dev/null and b/vttest/test-screen-features-2.png differ diff --git a/vttest/test-screen-features-3.png b/vttest/test-screen-features-3.png new file mode 100644 index 0000000..b58e470 Binary files /dev/null and b/vttest/test-screen-features-3.png differ diff --git a/vttest/test-screen-features-4.png b/vttest/test-screen-features-4.png new file mode 100644 index 0000000..3c95d9f Binary files /dev/null and b/vttest/test-screen-features-4.png differ diff --git a/vttest/test-screen-features-5.png b/vttest/test-screen-features-5.png new file mode 100644 index 0000000..b7f5502 Binary files /dev/null and b/vttest/test-screen-features-5.png differ diff --git a/vttest/test-screen-features-6.png b/vttest/test-screen-features-6.png new file mode 100644 index 0000000..ced20c0 Binary files /dev/null and b/vttest/test-screen-features-6.png differ diff --git a/vttest/test-screen-features-7.png b/vttest/test-screen-features-7.png new file mode 100644 index 0000000..bd02f64 Binary files /dev/null and b/vttest/test-screen-features-7.png differ diff --git a/vttest/test-screen-features-8.png b/vttest/test-screen-features-8.png new file mode 100644 index 0000000..e102ba6 Binary files /dev/null and b/vttest/test-screen-features-8.png differ diff --git a/vttest/test-screen-features-9.png b/vttest/test-screen-features-9.png new file mode 100644 index 0000000..b2f00d3 Binary files /dev/null and b/vttest/test-screen-features-9.png differ