github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/cmd/dist/deps_test.go (about)

     1  // Copyright 2017 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_test
     6  
     7  import (
     8  	"bytes"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestDeps(t *testing.T) {
    18  	if testing.Short() && testenv.Builder() == "" {
    19  		t.Skip("skipping in short mode")
    20  	}
    21  
    22  	current, err := ioutil.ReadFile("deps.go")
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	bash, err := exec.LookPath("bash")
    28  	if err != nil {
    29  		t.Skipf("skipping because bash not found: %v", err)
    30  	}
    31  
    32  	outf, err := ioutil.TempFile("", "dist-deps-test")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	outf.Close()
    37  	outname := outf.Name()
    38  	defer os.Remove(outname)
    39  
    40  	out, err := exec.Command(bash, "mkdeps.bash", outname).CombinedOutput()
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	t.Logf("%s", out)
    45  
    46  	updated, err := ioutil.ReadFile(outname)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	if !bytes.Equal(current, updated) {
    52  		// Very simple minded diff.
    53  		t.Log("-current +generated")
    54  		clines := strings.Split(string(current), "\n")
    55  		ulines := strings.Split(string(updated), "\n")
    56  		for len(clines) > 0 {
    57  			cl := clines[0]
    58  			switch {
    59  			case len(ulines) == 0:
    60  				t.Logf("-%s", cl)
    61  				clines = clines[1:]
    62  			case cl == ulines[0]:
    63  				clines = clines[1:]
    64  				ulines = ulines[1:]
    65  			case pkg(cl) == pkg(ulines[0]):
    66  				t.Logf("-%s", cl)
    67  				t.Logf("+%s", ulines[0])
    68  				clines = clines[1:]
    69  				ulines = ulines[1:]
    70  			case pkg(cl) < pkg(ulines[0]):
    71  				t.Logf("-%s", cl)
    72  				clines = clines[1:]
    73  			default:
    74  				cp := pkg(cl)
    75  				for len(ulines) > 0 && pkg(ulines[0]) < cp {
    76  					t.Logf("+%s", ulines[0])
    77  					ulines = ulines[1:]
    78  				}
    79  			}
    80  		}
    81  
    82  		t.Error("cmd/dist/deps.go is out of date; run cmd/dist/mkdeps.bash")
    83  	}
    84  }
    85  
    86  // pkg returns the package of a line in deps.go.
    87  func pkg(line string) string {
    88  	i := strings.Index(line, `"`)
    89  	if i < 0 {
    90  		return ""
    91  	}
    92  	line = line[i+1:]
    93  	i = strings.Index(line, `"`)
    94  	if i < 0 {
    95  		return ""
    96  	}
    97  	return line[:i]
    98  }