diff --git a/internal/verbose/print_test.go b/internal/verbose/print_test.go new file mode 100644 index 0000000..010557a --- /dev/null +++ b/internal/verbose/print_test.go @@ -0,0 +1,67 @@ +package verbose_test + +import ( + "os" + "os/exec" + "strconv" + "strings" + "testing" + + "git.ophivana.moe/cat/fortify/internal/verbose" +) + +const ( + testVerbose = "GO_TEST_VERBOSE" + wantStdout = "fortify: println\nfortify: printf" +) + +func TestPrinter(t *testing.T) { + switch os.Getenv(testVerbose) { + case "0": + verbose.Set(false) + case "1": + verbose.Set(true) + default: + return + } + + verbose.Println("println") + verbose.Printf("%s", "printf") +} + +func TestPrintf_Println(t *testing.T) { + testPrintfPrintln(t, false) + testPrintfPrintln(t, true) + + // make -cover happy + stdout := os.Stdout + t.Cleanup(func() { + os.Stdout = stdout + }) + os.Stdout = nil + verbose.Set(true) + verbose.Printf("") + verbose.Println() +} + +func testPrintfPrintln(t *testing.T, v bool) { + t.Run("start verbose printer with verbose "+strconv.FormatBool(v), func(t *testing.T) { + stdout, stderr := new(strings.Builder), new(strings.Builder) + stdout.Grow(len(wantStdout)) + cmd := exec.Command(os.Args[0], "-test.run=TestPrinter") + cmd.Stdout, cmd.Stderr = stdout, stderr + if v { + cmd.Env = append(cmd.Env, testVerbose+"=1") + } else { + cmd.Env = append(cmd.Env, testVerbose+"=0") + } + if err := cmd.Run(); err != nil { + panic("cannot run printer process: " + err.Error() + " stderr: " + stderr.String()) + } + + if got := stdout.String(); strings.Contains(got, wantStdout) != v { + t.Errorf("Print: got %v; want %t", + got, v) + } + }) +} diff --git a/internal/verbose/state_test.go b/internal/verbose/state_test.go new file mode 100644 index 0000000..21a03be --- /dev/null +++ b/internal/verbose/state_test.go @@ -0,0 +1,20 @@ +package verbose_test + +import ( + "testing" + + "git.ophivana.moe/cat/fortify/internal/verbose" +) + +func TestGetSet(t *testing.T) { + verbose.Set(false) + if verbose.Get() { + t.Errorf("Get() = true, want false") + } + + verbose.Set(true) + if !verbose.Get() { + t.Errorf("Get() = false, want true") + } + +}