2015-10-14 11:24:18 -06:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-06-09 06:59:18 -06:00
|
|
|
// +build go1.9
|
|
|
|
|
2015-10-14 11:24:18 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2016-02-20 15:33:56 -07:00
|
|
|
"os"
|
2015-10-14 11:24:18 -06:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/buildutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBundle(t *testing.T) {
|
|
|
|
load := func(name string) string {
|
|
|
|
data, err := ioutil.ReadFile(name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt = buildutil.FakeContext(map[string]map[string]string{
|
|
|
|
"initial": {
|
|
|
|
"a.go": load("testdata/src/initial/a.go"),
|
|
|
|
"b.go": load("testdata/src/initial/b.go"),
|
2016-06-05 00:20:26 -06:00
|
|
|
"c.go": load("testdata/src/initial/c.go"),
|
2015-10-14 11:24:18 -06:00
|
|
|
},
|
2016-05-11 22:39:32 -06:00
|
|
|
"domain.name/importdecl": {
|
|
|
|
"p.go": load("testdata/src/domain.name/importdecl/p.go"),
|
|
|
|
},
|
2015-10-14 11:24:18 -06:00
|
|
|
"fmt": {
|
|
|
|
"print.go": `package fmt; func Println(...interface{})`,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-02-20 15:33:56 -07:00
|
|
|
os.Args = os.Args[:1] // avoid e.g. -test=short in the output
|
|
|
|
out, err := bundle("initial", "github.com/dest", "dest", "prefix")
|
|
|
|
if err != nil {
|
2015-10-14 11:24:18 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-02-20 15:33:56 -07:00
|
|
|
if got, want := string(out), load("testdata/out.golden"); got != want {
|
2015-10-14 11:24:18 -06:00
|
|
|
t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)
|
|
|
|
|
2016-02-20 15:33:56 -07:00
|
|
|
if err := ioutil.WriteFile("testdata/out.got", out, 0644); err != nil {
|
2015-10-14 11:24:18 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-02-20 15:33:56 -07:00
|
|
|
t.Log(diff("testdata/out.golden", "testdata/out.got"))
|
2015-10-14 11:24:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diff(a, b string) string {
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
cmd = exec.Command("/bin/diff", "-c", a, b)
|
|
|
|
default:
|
|
|
|
cmd = exec.Command("/usr/bin/diff", "-u", a, b)
|
|
|
|
}
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
cmd.Stderr = &out
|
|
|
|
cmd.Run() // nonzero exit is expected
|
|
|
|
if out.Len() == 0 {
|
|
|
|
return "(failed to compute diff)"
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|