[dev.go2go] fix the go2go playground build for 1.16

The go2go playground Docker build was failing due to lack of code in
GOPATH.  Following CL 295649, simply remove this unnecessary logic.

Also apply a patch from Alex to fix the failing tests, which have
probably been failing on the dev.go2go branch for some time. Thanks
Alex!

Change-Id: I49cfc073e07d918fc5247494f0eaa3c5bd8215ea
Reviewed-on: https://go-review.googlesource.com/c/playground/+/298889
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Alexander Rakoczy <alex@golang.org>
diff --git a/Dockerfile b/Dockerfile
index 77fb188..3699cb4 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -67,26 +67,6 @@
 ENV GOPATH /go
 ENV PATH /usr/local/go-faketime/bin:$GOPATH/bin:$PATH
 
-# Add and compile tour packages
-RUN go get \
-    golang.org/x/tour/pic \
-    golang.org/x/tour/reader \
-    golang.org/x/tour/tree \
-    golang.org/x/tour/wc \
-    golang.org/x/talks/content/2016/applicative/google && \
-    rm -rf $GOPATH/src/golang.org/x/tour/.git && \
-    rm -rf $GOPATH/src/golang.org/x/talks/.git
-
-# Add tour packages under their old import paths (so old snippets still work)
-RUN mkdir -p $GOPATH/src/code.google.com/p/go-tour && \
-    cp -R $GOPATH/src/golang.org/x/tour/* $GOPATH/src/code.google.com/p/go-tour/ && \
-    sed -i 's_// import_// public import_' $(find $GOPATH/src/code.google.com/p/go-tour/ -name *.go) && \
-    go install \
-    code.google.com/p/go-tour/pic \
-    code.google.com/p/go-tour/reader \
-    code.google.com/p/go-tour/tree \
-    code.google.com/p/go-tour/wc
-
 RUN mkdir /app
 
 COPY --from=build-playground /go/bin/playground /app
diff --git a/fmt_test.go b/fmt_test.go
index ceea152..4747e27 100644
--- a/fmt_test.go
+++ b/fmt_test.go
@@ -33,13 +33,6 @@
 			want:   "package main\n\nfunc main() {}\n",
 		},
 		{
-			name:    "classic_goimports",
-			method:  http.MethodPost,
-			body:    " package main\nvar _ = fmt.Printf",
-			imports: true,
-			want:    "package main\n\nimport \"fmt\"\n\nvar _ = fmt.Printf\n",
-		},
-		{
 			name:   "single_go_with_header",
 			method: http.MethodPost,
 			body:   "-- prog.go --\n  package main",
diff --git a/sandbox.go b/sandbox.go
index 58a86fc..66ca6dd 100644
--- a/sandbox.go
+++ b/sandbox.go
@@ -14,8 +14,6 @@
 	"encoding/json"
 	"errors"
 	"fmt"
-	"go/ast"
-	"go/doc"
 	"go/parser"
 	"go/token"
 	"io"
@@ -29,7 +27,6 @@
 	"strconv"
 	"strings"
 	"sync"
-	"text/template"
 	"time"
 	"unicode"
 	"unicode/utf8"
@@ -184,30 +181,6 @@
 	return fmt.Sprintf("%s-%s-%x", prefix, runtime.Version(), h.Sum(nil))
 }
 
-// isTestFunc tells whether fn has the type of a testing function.
-func isTestFunc(fn *ast.FuncDecl) bool {
-	if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
-		fn.Type.Params.List == nil ||
-		len(fn.Type.Params.List) != 1 ||
-		len(fn.Type.Params.List[0].Names) > 1 {
-		return false
-	}
-	ptr, ok := fn.Type.Params.List[0].Type.(*ast.StarExpr)
-	if !ok {
-		return false
-	}
-	// We can't easily check that the type is *testing.T
-	// because we don't know how testing has been imported,
-	// but at least check that it's *T or *something.T.
-	if name, ok := ptr.X.(*ast.Ident); ok && name.Name == "T" {
-		return true
-	}
-	if sel, ok := ptr.X.(*ast.SelectorExpr); ok && sel.Sel.Name == "T" {
-		return true
-	}
-	return false
-}
-
 // isTest tells whether name looks like a test (or benchmark, according to prefix).
 // It is a Test (say) if there is a character after Test that is not a lower-case letter.
 // We don't want mistaken Testimony or erroneous Benchmarking.
@@ -222,119 +195,6 @@
 	return !unicode.IsLower(r)
 }
 
-// getTestProg returns source code that executes all valid tests and examples in src.
-// If the main function is present or there are no tests or examples, it returns nil.
-// getTestProg emulates the "go test" command as closely as possible.
-// Benchmarks are not supported because of sandboxing.
-func getTestProg(src []byte) []byte {
-	fset := token.NewFileSet()
-	// Early bail for most cases.
-	f, err := parser.ParseFile(fset, progName, src, parser.ImportsOnly)
-	if err != nil || f.Name.Name != "main" {
-		return nil
-	}
-
-	// importPos stores the position to inject the "testing" import declaration, if needed.
-	importPos := fset.Position(f.Name.End()).Offset
-
-	var testingImported bool
-	for _, s := range f.Imports {
-		if s.Path.Value == `"testing"` && s.Name == nil {
-			testingImported = true
-			break
-		}
-	}
-
-	// Parse everything and extract test names.
-	f, err = parser.ParseFile(fset, progName, src, parser.ParseComments)
-	if err != nil {
-		return nil
-	}
-
-	var tests []string
-	for _, d := range f.Decls {
-		n, ok := d.(*ast.FuncDecl)
-		if !ok {
-			continue
-		}
-		name := n.Name.Name
-		switch {
-		case name == "main":
-			// main declared as a method will not obstruct creation of our main function.
-			if n.Recv == nil {
-				return nil
-			}
-		case isTest(name, "Test") && isTestFunc(n):
-			tests = append(tests, name)
-		}
-	}
-
-	// Tests imply imported "testing" package in the code.
-	// If there is no import, bail to let the compiler produce an error.
-	if !testingImported && len(tests) > 0 {
-		return nil
-	}
-
-	// We emulate "go test". An example with no "Output" comment is compiled,
-	// but not executed. An example with no text after "Output:" is compiled,
-	// executed, and expected to produce no output.
-	var ex []*doc.Example
-	// exNoOutput indicates whether an example with no output is found.
-	// We need to compile the program containing such an example even if there are no
-	// other tests or examples.
-	exNoOutput := false
-	for _, e := range doc.Examples(f) {
-		if e.Output != "" || e.EmptyOutput {
-			ex = append(ex, e)
-		}
-		if e.Output == "" && !e.EmptyOutput {
-			exNoOutput = true
-		}
-	}
-
-	if len(tests) == 0 && len(ex) == 0 && !exNoOutput {
-		return nil
-	}
-
-	if !testingImported && (len(ex) > 0 || exNoOutput) {
-		// In case of the program with examples and no "testing" package imported,
-		// add import after "package main" without modifying line numbers.
-		importDecl := []byte(`;import "testing";`)
-		src = bytes.Join([][]byte{src[:importPos], importDecl, src[importPos:]}, nil)
-	}
-
-	data := struct {
-		Tests    []string
-		Examples []*doc.Example
-	}{
-		tests,
-		ex,
-	}
-	code := new(bytes.Buffer)
-	if err := testTmpl.Execute(code, data); err != nil {
-		panic(err)
-	}
-	src = append(src, code.Bytes()...)
-	return src
-}
-
-var testTmpl = template.Must(template.New("main").Parse(`
-func main() {
-	matchAll := func(t string, pat string) (bool, error) { return true, nil }
-	tests := []testing.InternalTest{
-{{range .Tests}}
-		{"{{.}}", {{.}}},
-{{end}}
-	}
-	examples := []testing.InternalExample{
-{{range .Examples}}
-		{"Example{{.Name}}", Example{{.Name}}, {{printf "%q" .Output}}, {{.Unordered}}},
-{{end}}
-	}
-	testing.Main(matchAll, tests, nil, examples)
-}
-`))
-
 var failedTestPattern = "--- FAIL"
 
 // compileAndRun tries to build and run a user program.
@@ -578,19 +438,11 @@
 	return execRes, nil
 }
 
-// allowModuleDownloads reports whether the code snippet in src should be allowed
-// to download modules.
-func allowModuleDownloads(files *fileSet) bool {
-	if files.Num() == 1 && bytes.Contains(files.Data(progName), []byte(`"code.google.com/p/go-tour/`)) {
-		// This domain doesn't exist anymore but we want old snippets using
-		// these packages to still run, so the Dockerfile adds these packages
-		// at this name in $GOPATH. Any snippets using this old name wouldn't
-		// have expected (or been able to use) third-party packages anyway,
-		// so disabling modules and proxy fetches is acceptable.
-		return false
-	}
-	v, _ := strconv.ParseBool(os.Getenv("ALLOW_PLAY_MODULE_DOWNLOADS"))
-	return v
+// allowModuleDownloads reports whether the code snippet in src should
+// be allowed to download modules.
+func allowModuleDownloads(_ *fileSet) bool {
+	// Disabled for go2go.
+	return false
 }
 
 // playgroundGoproxy returns the GOPROXY environment config the playground should use.
diff --git a/server_test.go b/server_test.go
index 7b6f77b..1573d0e 100644
--- a/server_test.go
+++ b/server_test.go
@@ -63,7 +63,6 @@
 		respBody   []byte
 	}{
 		{"OPTIONS no-op", http.MethodOptions, "https://play.golang.org/p/foo", http.StatusOK, nil, nil},
-		{"foo.play.golang.org to play.golang.org", http.MethodGet, "https://foo.play.golang.org", http.StatusFound, map[string]string{"Location": "https://play.golang.org"}, nil},
 		{"Non-existent page", http.MethodGet, "https://play.golang.org/foo", http.StatusNotFound, nil, nil},
 		{"Unknown snippet", http.MethodGet, "https://play.golang.org/p/foo", http.StatusNotFound, nil, nil},
 		{"Existing snippet", http.MethodGet, "https://play.golang.org/p/" + id, http.StatusOK, nil, nil},
@@ -299,37 +298,6 @@
 	}
 }
 
-func TestAllowModuleDownloads(t *testing.T) {
-	const envKey = "ALLOW_PLAY_MODULE_DOWNLOADS"
-	defer func(old string) { os.Setenv(envKey, old) }(os.Getenv(envKey))
-
-	tests := []struct {
-		src  string
-		env  string
-		want bool
-	}{
-		{src: "package main", want: true},
-		{src: "package main", env: "false", want: false},
-		{src: `import "code.google.com/p/go-tour/"`, want: false},
-	}
-	for i, tt := range tests {
-		if tt.env != "" {
-			os.Setenv(envKey, tt.env)
-		} else {
-			os.Setenv(envKey, "true")
-		}
-		files, err := splitFiles([]byte(tt.src))
-		if err != nil {
-			t.Errorf("%d. splitFiles = %v", i, err)
-			continue
-		}
-		got := allowModuleDownloads(files)
-		if got != tt.want {
-			t.Errorf("%d. allow = %v; want %v; files:\n%s", i, got, tt.want, filesAsString(files))
-		}
-	}
-}
-
 func TestPlaygroundGoproxy(t *testing.T) {
 	const envKey = "PLAY_GOPROXY"
 	defer os.Setenv(envKey, os.Getenv(envKey))
diff --git a/tests.go b/tests.go
index 6c60e62..968924a 100644
--- a/tests.go
+++ b/tests.go
@@ -178,30 +178,6 @@
 	}
 }
 `, want: "timers fired as expected"},
-
-	{
-		name: "old_tour_pkgs_in_gopath",
-		prog: `
-package main
-
-import (
-	"code.google.com/p/go-tour/pic"
-	"code.google.com/p/go-tour/reader"
-	"code.google.com/p/go-tour/tree"
-	"code.google.com/p/go-tour/wc"
-)
-
-var (
-	_ = pic.Show
-	_ = reader.Validate
-	_ = tree.New
-	_ = wc.Test
-)
-
-func main() {
-	println("ok")
-}
-`, want: "ok"},
 	{
 		name: "must_be_package_main",
 		prog: `
@@ -275,143 +251,6 @@
 		},
 	},
 	{
-		name: "test_passes",
-		prog: `
-package main
-
-import "testing"
-
-func TestSanity(t *testing.T) {
-	if 1+1 != 2 {
-		t.Error("uhh...")
-	}
-}
-`, want: `=== RUN   TestSanity
---- PASS: TestSanity (0.00s)
-PASS`},
-
-	{
-		name: "test_without_import",
-		prog: `
-package main
-
-func TestSanity(t *testing.T) {
-	t.Error("uhh...")
-}
-
-func ExampleNotExecuted() {
-	// Output: it should not run
-}
-`, want: "", errors: "./prog.go:4:20: undefined: testing\n"},
-
-	{
-		name: "test_with_import_ignored",
-		prog: `
-package main
-
-import (
-	"fmt"
-	"testing"
-)
-
-func TestSanity(t *testing.T) {
-	t.Error("uhh...")
-}
-
-func main() {
-	fmt.Println("test")
-}
-`, want: "test"},
-
-	{
-		name: "example_runs",
-		prog: `
-package main//comment
-
-import "fmt"
-
-func ExampleOutput() {
-	fmt.Println("The output")
-	// Output: The output
-}
-`, want: `=== RUN   ExampleOutput
---- PASS: ExampleOutput (0.00s)
-PASS`},
-
-	{
-		name: "example_unordered",
-		prog: `
-package main//comment
-
-import "fmt"
-
-func ExampleUnorderedOutput() {
-	fmt.Println("2")
-	fmt.Println("1")
-	fmt.Println("3")
-	// Unordered output: 3
-	// 2
-	// 1
-}
-`, want: `=== RUN   ExampleUnorderedOutput
---- PASS: ExampleUnorderedOutput (0.00s)
-PASS`},
-
-	{
-		name: "example_fail",
-		prog: `
-package main
-
-import "fmt"
-
-func ExampleEmptyOutput() {
-	// Output:
-}
-
-func ExampleEmptyOutputFail() {
-	fmt.Println("1")
-	// Output:
-}
-`, want: `=== RUN   ExampleEmptyOutput
---- PASS: ExampleEmptyOutput (0.00s)
-=== RUN   ExampleEmptyOutputFail
---- FAIL: ExampleEmptyOutputFail (0.00s)
-got:
-1
-want:
-
-FAIL`},
-
-	// Run program without executing this example function.
-	{
-		name: "example_no_output_skips_run",
-		prog: `
-package main
-
-func ExampleNoOutput() {
-	panic(1)
-}
-`, want: `testing: warning: no tests to run
-PASS`},
-
-	{
-		name: "example_output",
-		prog: `
-package main
-
-import "fmt"
-
-func ExampleShouldNotRun() {
-	fmt.Println("The output")
-	// Output: The output
-}
-
-func main() {
-	fmt.Println("Main")
-}
-`, want: "Main"},
-
-	{
 		name: "stdout_stderr_merge",
 		prog: `
 package main
@@ -458,21 +297,10 @@
 			{"B\n", "stderr", time.Second - 2*time.Nanosecond},
 			{"A\n", "stdout", time.Second},
 		}},
-
-	{
-		name: "third_party_imports",
-		prog: `
-package main
-import ("fmt"; "github.com/bradfitz/iter")
-func main() { for i := range iter.N(5) { fmt.Println(i) } }
-`,
-		want: "0\n1\n2\n3\n4\n",
-	},
-
 	{
 		name:          "compile_with_vet",
 		withVet:       true,
-		wantVetErrors: "./prog.go:5:2: Printf format %v reads arg #1, but call has 0 args\n",
+		wantVetErrors: "./prog.go2:5: Printf format %v reads arg #1, but call has 0 args\n",
 		prog: `
 package main
 import "fmt"
@@ -493,61 +321,6 @@
 }
 `,
 	},
-
-	{
-		name:          "compile_modules_with_vet",
-		withVet:       true,
-		wantVetErrors: "./prog.go:6:2: Printf format %v reads arg #1, but call has 0 args\n",
-		prog: `
-package main
-import ("fmt"; "github.com/bradfitz/iter")
-func main() {
-	for i := range iter.N(5) { fmt.Println(i) }
-	fmt.Printf("hi %v")
-}
-`,
-	},
-
-	{
-		name: "multi_file_basic",
-		prog: `
-package main
-const foo = "bar"
-
--- two.go --
-package main
-func main() {
-  println(foo)
-}
-`,
-		wantEvents: []Event{
-			{"bar\n", "stderr", 0},
-		},
-	},
-
-	{
-		name:    "multi_file_use_package",
-		withVet: true,
-		prog: `
-package main
-
-import "play.test/foo"
-
-func main() {
-    foo.Hello()
-}
-
--- go.mod --
-module play.test
-
--- foo/foo.go --
-package foo
-
-import "fmt"
-
-func Hello() { fmt.Println("hello world") }
-`,
-	},
 	{
 		name: "timeouts_handled_gracefully",
 		prog: `