1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8	"fmt"
9	"os"
10	"testing"
11
12	"github.com/rogpeppe/go-internal/goproxytest"
13	"github.com/rogpeppe/go-internal/gotooltest"
14	"github.com/rogpeppe/go-internal/testscript"
15)
16
17var proxyURL string
18
19func TestMain(m *testing.M) {
20	os.Exit(testscript.RunMain(gobinMain{m}, map[string]func() int{
21		"txtar-addmod": main1,
22	}))
23}
24
25type gobinMain struct {
26	m *testing.M
27}
28
29func (m gobinMain) Run() int {
30	// Start the Go proxy server running for all tests.
31	srv, err := goproxytest.NewServer("testdata/mod", "")
32	if err != nil {
33		fmt.Fprintf(os.Stderr, "cannot start proxy: %v", err)
34		return 1
35	}
36	proxyURL = srv.URL
37
38	return m.m.Run()
39}
40
41func TestScripts(t *testing.T) {
42	p := testscript.Params{
43		Dir: "testdata",
44		Setup: func(e *testscript.Env) error {
45			e.Vars = append(e.Vars,
46				"GOPROXY="+proxyURL,
47				"GONOSUMDB=*",
48			)
49			return nil
50		},
51	}
52	if err := gotooltest.Setup(&p); err != nil {
53		t.Fatal(err)
54	}
55	testscript.Run(t, p)
56}
57