1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:14:42 -07:00
go/cmd/bundle/main_test.go
Alan Donovan 199b70b426 x/tools/cmd/bundle: a tool to concatenate source files, preserving reference integrity
+ Test.

For background, see "http -> http2 -> http import cycle" thread on golang-dev.

Change-Id: Idc422247e5935ef7615cd5e8b7e2c489f7f2bc31
Reviewed-on: https://go-review.googlesource.com/15850
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-10-29 19:10:01 +00:00

67 lines
1.5 KiB
Go

// 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.
package main
import (
"bytes"
"io/ioutil"
"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"),
},
"fmt": {
"print.go": `package fmt; func Println(...interface{})`,
},
})
var out bytes.Buffer
if err := bundle(&out, "initial", "dest", "prefix"); err != nil {
t.Fatal(err)
}
if got, want := out.String(), load("testdata/out.golden"); got != want {
t.Errorf("-- got --\n%s\n-- want --\n%s\n-- diff --", got, want)
if err := ioutil.WriteFile("testdata/out.got", out.Bytes(), 0644); err != nil {
t.Fatal(err)
}
t.Log(diff("testdata/out.got", "testdata/out.golden"))
}
}
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()
}