cmd/cue/cmd: add MainTest

Add separate MainTest to avoid having to expose inTest.

This allows testScript-like usage outside of cmd/cue/cmd.

Note that auto-detecting tests, although working fine
on AppVeyor, does not work on Github. This is much
better anyway.

Closes #151
https://github.com/cuelang/cue/pull/151

GitOrigin-RevId: 922099bdbccc1937c4428658a29a02fd55ad2303
Change-Id: Ie5f05a091e47f8efa4af29a0184801983e39502b
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/3560
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cmd/cue/cmd/common.go b/cmd/cue/cmd/common.go
index a2b9c7b..2cec1e8 100644
--- a/cmd/cue/cmd/common.go
+++ b/cmd/cue/cmd/common.go
@@ -16,7 +16,6 @@
 
 import (
 	"bytes"
-	"flag"
 	"io"
 	"os"
 	"strings"
@@ -35,12 +34,6 @@
 
 var inTest = false
 
-func init() {
-	if flag.Lookup("test.v") != nil || strings.Contains(os.Args[0], "/_test/") {
-		inTest = true
-	}
-}
-
 func mustParseFlags(t *testing.T, cmd *cobra.Command, flags ...string) {
 	if err := cmd.ParseFlags(flags); err != nil {
 		t.Fatal(err)
diff --git a/cmd/cue/cmd/root.go b/cmd/cue/cmd/root.go
index 2f89cb4..6fcc8ce 100644
--- a/cmd/cue/cmd/root.go
+++ b/cmd/cue/cmd/root.go
@@ -102,6 +102,12 @@
 	return c
 }
 
+// MainTest is like Main, runs the cue tool and returns the code for passing to os.Exit.
+func MainTest() int {
+	inTest = true
+	return Main()
+}
+
 // Main runs the cue tool and returns the code for passing to os.Exit.
 func Main() int {
 	err := mainErr(context.Background(), os.Args[1:])
diff --git a/cmd/cue/cmd/script_test.go b/cmd/cue/cmd/script_test.go
index 67a3b69..13f23ea 100644
--- a/cmd/cue/cmd/script_test.go
+++ b/cmd/cue/cmd/script_test.go
@@ -18,8 +18,7 @@
 	// Setting inTest causes filenames printed in error messages
 	// to be normalized so the output looks the same on Unix
 	// as Windows.
-	inTest = true
 	os.Exit(testscript.RunMain(m, map[string]func() int{
-		"cue": Main,
+		"cue": MainTest,
 	}))
 }
diff --git a/doc/tutorial/basics/0_intro/47_validation.txt b/doc/tutorial/basics/0_intro/47_validation.txt
index 095fd2f..0f3c4fe 100644
--- a/doc/tutorial/basics/0_intro/47_validation.txt
+++ b/doc/tutorial/basics/0_intro/47_validation.txt
@@ -1,4 +1,5 @@
 ! cue vet schema.cue data.yaml
+cmp stderr expect-stderr
 
 -- frontmatter.toml --
 title = "Validation"
diff --git a/doc/tutorial/basics/script_test.go b/doc/tutorial/basics/script_test.go
index 6b68152..c1879e4 100644
--- a/doc/tutorial/basics/script_test.go
+++ b/doc/tutorial/basics/script_test.go
@@ -61,6 +61,6 @@
 
 func TestMain(m *testing.M) {
 	os.Exit(testscript.RunMain(m, map[string]func() int{
-		"cue": cmd.Main,
+		"cue": cmd.MainTest,
 	}))
 }