Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / cmd / bundle / main_test.go
1 // Copyright 2015 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
5 package main
6
7 import (
8         "bytes"
9         "io/ioutil"
10         "os"
11         "os/exec"
12         "runtime"
13         "testing"
14
15         "golang.org/x/tools/go/packages/packagestest"
16 )
17
18 func TestBundle(t *testing.T) { packagestest.TestAll(t, testBundle) }
19 func testBundle(t *testing.T, x packagestest.Exporter) {
20         load := func(name string) string {
21                 data, err := ioutil.ReadFile(name)
22                 if err != nil {
23                         t.Fatal(err)
24                 }
25                 return string(data)
26         }
27
28         e := packagestest.Export(t, x, []packagestest.Module{
29                 {
30                         Name: "initial",
31                         Files: map[string]interface{}{
32                                 "a.go": load("testdata/src/initial/a.go"),
33                                 "b.go": load("testdata/src/initial/b.go"),
34                                 "c.go": load("testdata/src/initial/c.go"),
35                         },
36                 },
37                 {
38                         Name: "domain.name/importdecl",
39                         Files: map[string]interface{}{
40                                 "p.go": load("testdata/src/domain.name/importdecl/p.go"),
41                         },
42                 },
43         })
44         defer e.Cleanup()
45         testingOnlyPackagesConfig = e.Config
46
47         os.Args = os.Args[:1] // avoid e.g. -test=short in the output
48         out, err := bundle("initial", "github.com/dest", "dest", "prefix", "tag")
49         if err != nil {
50                 t.Fatal(err)
51         }
52
53         if got, want := string(out), load("testdata/out.golden"); got != want {
54                 t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)
55
56                 if err := ioutil.WriteFile("testdata/out.got", out, 0644); err != nil {
57                         t.Fatal(err)
58                 }
59                 t.Log(diff("testdata/out.golden", "testdata/out.got"))
60         }
61 }
62
63 func diff(a, b string) string {
64         var cmd *exec.Cmd
65         switch runtime.GOOS {
66         case "plan9":
67                 cmd = exec.Command("/bin/diff", "-c", a, b)
68         default:
69                 cmd = exec.Command("/usr/bin/diff", "-u", a, b)
70         }
71         var out bytes.Buffer
72         cmd.Stdout = &out
73         cmd.Stderr = &out
74         cmd.Run() // nonzero exit is expected
75         if out.Len() == 0 {
76                 return "(failed to compute diff)"
77         }
78         return out.String()
79 }