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