diff --git a/src/cmd/dist/deps_test.go b/src/cmd/dist/deps_test.go new file mode 100644 index 0000000000..8146375fff --- /dev/null +++ b/src/cmd/dist/deps_test.go @@ -0,0 +1,98 @@ +// Copyright 2017 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_test + +import ( + "bytes" + "internal/testenv" + "io/ioutil" + "os" + "os/exec" + "strings" + "testing" +) + +func TestDeps(t *testing.T) { + if testing.Short() && testenv.Builder() == "" { + t.Skip("skipping in short mode") + } + + current, err := ioutil.ReadFile("deps.go") + if err != nil { + t.Fatal(err) + } + + bash, err := exec.LookPath("bash") + if err != nil { + t.Skipf("skipping because bash not found: %v", err) + } + + outf, err := ioutil.TempFile("", "dist-deps-test") + if err != nil { + t.Fatal(err) + } + outf.Close() + outname := outf.Name() + defer os.Remove(outname) + + out, err := exec.Command(bash, "mkdeps.bash", outname).CombinedOutput() + if err != nil { + t.Fatal(err) + } + t.Logf("%s", out) + + updated, err := ioutil.ReadFile(outname) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(current, updated) { + // Very simple minded diff. + t.Log("-current +generated") + clines := strings.Split(string(current), "\n") + ulines := strings.Split(string(updated), "\n") + for len(clines) > 0 { + cl := clines[0] + switch { + case len(ulines) == 0: + t.Logf("-%s", cl) + clines = clines[1:] + case cl == ulines[0]: + clines = clines[1:] + ulines = ulines[1:] + case pkg(cl) == pkg(ulines[0]): + t.Logf("-%s", cl) + t.Logf("+%s", ulines[0]) + clines = clines[1:] + ulines = ulines[1:] + case pkg(cl) < pkg(ulines[0]): + t.Logf("-%s", cl) + clines = clines[1:] + default: + cp := pkg(cl) + for len(ulines) > 0 && pkg(ulines[0]) < cp { + t.Logf("+%s", ulines[0]) + ulines = ulines[1:] + } + } + } + + t.Error("cmd/dist/deps.go is out of date; run cmd/dist/mkdeps.bash") + } +} + +// pkg returns the package of a line in deps.go. +func pkg(line string) string { + i := strings.Index(line, `"`) + if i < 0 { + return "" + } + line = line[i+1:] + i = strings.Index(line, `"`) + if i < 0 { + return "" + } + return line[:i] +} diff --git a/src/cmd/dist/mkdeps.bash b/src/cmd/dist/mkdeps.bash index 71d3c371e4..37ce6d6719 100755 --- a/src/cmd/dist/mkdeps.bash +++ b/src/cmd/dist/mkdeps.bash @@ -5,6 +5,11 @@ set -e +output="$1" +if test -z "$output"; then + output=deps.go +fi + # We need to test enough GOOS/GOARCH combinations to pick up all the # package dependencies. gooslist="windows linux darwin solaris" @@ -42,6 +47,6 @@ deps_of $all >tmp.all.deps echo '},' done echo '}' -) |gofmt >deps.go +) |gofmt >$output rm -f tmp.all.deps