github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/fiximports/main_test.go (about)

     1  // Copyright 2015 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  // No testdata on Android.
     6  
     7  // +build !android
     8  
     9  package main
    10  
    11  import (
    12  	"bytes"
    13  	"os"
    14  	"path/filepath"
    15  	"runtime"
    16  	"strings"
    17  	"testing"
    18  )
    19  
    20  // TODO(adonovan):
    21  // - test introduction of renaming imports.
    22  // - test induced failures of rewriteFile.
    23  
    24  // Guide to the test packages:
    25  //
    26  // new.com/one		-- canonical name for old.com/one
    27  // old.com/one		-- non-canonical; has import comment "new.com/one"
    28  // old.com/bad		-- has a parse error
    29  // fruit.io/orange	\
    30  // fruit.io/banana	 } orange -> pear -> banana -> titanic.biz/bar
    31  // fruit.io/pear	/
    32  // titanic.biz/bar	-- domain is sinking; package has jumped ship to new.com/bar
    33  // titanic.biz/foo	-- domain is sinking but package has no import comment yet
    34  
    35  func TestFixImports(t *testing.T) {
    36  	gopath := filepath.Join(cwd, "testdata")
    37  	if err := os.Setenv("GOPATH", gopath); err != nil {
    38  		t.Fatalf("os.Setenv: %v", err)
    39  	}
    40  	defer func() {
    41  		stderr = os.Stderr
    42  		*badDomains = "code.google.com"
    43  		*replaceFlag = ""
    44  	}()
    45  
    46  	for i, test := range []struct {
    47  		packages    []string // packages to rewrite, "go list" syntax
    48  		badDomains  string   // -baddomains flag
    49  		replaceFlag string   // -replace flag
    50  		wantOK      bool
    51  		wantStderr  string
    52  		wantRewrite map[string]string
    53  	}{
    54  		// #0. No errors.
    55  		{
    56  			packages:   []string{"all"},
    57  			badDomains: "code.google.com",
    58  			wantOK:     true,
    59  			wantStderr: `
    60  testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
    61  fruit.io/banana
    62  	fixed: old.com/one -> new.com/one
    63  	fixed: titanic.biz/bar -> new.com/bar
    64  `,
    65  			wantRewrite: map[string]string{
    66  				"$GOPATH/src/fruit.io/banana/banana.go": `package banana
    67  
    68  import (
    69  	_ "new.com/bar"
    70  	_ "new.com/one"
    71  	_ "titanic.biz/foo"
    72  )`,
    73  			},
    74  		},
    75  		// #1. No packages needed rewriting.
    76  		{
    77  			packages:   []string{"titanic.biz/...", "old.com/...", "new.com/..."},
    78  			badDomains: "code.google.com",
    79  			wantOK:     true,
    80  			wantStderr: `
    81  testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
    82  `,
    83  		},
    84  		// #2. Some packages without import comments matched bad domains.
    85  		{
    86  			packages:   []string{"all"},
    87  			badDomains: "titanic.biz",
    88  			wantOK:     false,
    89  			wantStderr: `
    90  testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
    91  fruit.io/banana
    92  	testdata/src/fruit.io/banana/banana.go:6: import "titanic.biz/foo"
    93  	fixed: old.com/one -> new.com/one
    94  	fixed: titanic.biz/bar -> new.com/bar
    95  	ERROR: titanic.biz/foo has no import comment
    96  	imported directly by:
    97  		fruit.io/pear
    98  	imported indirectly by:
    99  		fruit.io/orange
   100  `,
   101  			wantRewrite: map[string]string{
   102  				"$GOPATH/src/fruit.io/banana/banana.go": `package banana
   103  
   104  import (
   105  	_ "new.com/bar"
   106  	_ "new.com/one"
   107  	_ "titanic.biz/foo"
   108  )`,
   109  			},
   110  		},
   111  		// #3. The -replace flag lets user supply missing import comments.
   112  		{
   113  			packages:    []string{"all"},
   114  			replaceFlag: "titanic.biz/foo=new.com/foo",
   115  			wantOK:      true,
   116  			wantStderr: `
   117  testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
   118  fruit.io/banana
   119  	fixed: old.com/one -> new.com/one
   120  	fixed: titanic.biz/bar -> new.com/bar
   121  	fixed: titanic.biz/foo -> new.com/foo
   122  `,
   123  			wantRewrite: map[string]string{
   124  				"$GOPATH/src/fruit.io/banana/banana.go": `package banana
   125  
   126  import (
   127  	_ "new.com/bar"
   128  	_ "new.com/foo"
   129  	_ "new.com/one"
   130  )`,
   131  			},
   132  		},
   133  		// #4. The -replace flag supports wildcards.
   134  		//     An explicit import comment takes precedence.
   135  		{
   136  			packages:    []string{"all"},
   137  			replaceFlag: "titanic.biz/...=new.com/...",
   138  			wantOK:      true,
   139  			wantStderr: `
   140  testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
   141  fruit.io/banana
   142  	fixed: old.com/one -> new.com/one
   143  	fixed: titanic.biz/bar -> new.com/bar
   144  	fixed: titanic.biz/foo -> new.com/foo
   145  `,
   146  			wantRewrite: map[string]string{
   147  				"$GOPATH/src/fruit.io/banana/banana.go": `package banana
   148  
   149  import (
   150  	_ "new.com/bar"
   151  	_ "new.com/foo"
   152  	_ "new.com/one"
   153  )`,
   154  			},
   155  		},
   156  		// #5. The -replace flag trumps -baddomains.
   157  		{
   158  			packages:    []string{"all"},
   159  			badDomains:  "titanic.biz",
   160  			replaceFlag: "titanic.biz/foo=new.com/foo",
   161  			wantOK:      true,
   162  			wantStderr: `
   163  testdata/src/old.com/bad/bad.go:2:43: expected 'package', found 'EOF'
   164  fruit.io/banana
   165  	fixed: old.com/one -> new.com/one
   166  	fixed: titanic.biz/bar -> new.com/bar
   167  	fixed: titanic.biz/foo -> new.com/foo
   168  `,
   169  			wantRewrite: map[string]string{
   170  				"$GOPATH/src/fruit.io/banana/banana.go": `package banana
   171  
   172  import (
   173  	_ "new.com/bar"
   174  	_ "new.com/foo"
   175  	_ "new.com/one"
   176  )`,
   177  			},
   178  		},
   179  	} {
   180  		*badDomains = test.badDomains
   181  		*replaceFlag = test.replaceFlag
   182  
   183  		stderr = new(bytes.Buffer)
   184  		gotRewrite := make(map[string]string)
   185  		writeFile = func(filename string, content []byte, mode os.FileMode) error {
   186  			filename = strings.Replace(filename, gopath, "$GOPATH", 1)
   187  			filename = filepath.ToSlash(filename)
   188  			gotRewrite[filename] = string(bytes.TrimSpace(content))
   189  			return nil
   190  		}
   191  
   192  		if runtime.GOOS == "windows" {
   193  			test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/old.com/bad/bad.go`, `testdata\src\old.com\bad\bad.go`, -1)
   194  			test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/fruit.io/banana/banana.go`, `testdata\src\fruit.io\banana\banana.go`, -1)
   195  		}
   196  
   197  		// Check status code.
   198  		if fiximports(test.packages...) != test.wantOK {
   199  			t.Errorf("#%d. fiximports() = %t", i, !test.wantOK)
   200  		}
   201  
   202  		// Compare stderr output.
   203  		if stderr.(*bytes.Buffer).String() != test.wantStderr {
   204  			t.Errorf("#%d. stderr: got <<%s>>, want <<%s>>",
   205  				i, stderr, test.wantStderr)
   206  		}
   207  
   208  		// Compare rewrites.
   209  		for k, v := range gotRewrite {
   210  			if test.wantRewrite[k] != v {
   211  				t.Errorf("#%d. rewrite[%s] = <<%s>>, want <<%s>>",
   212  					i, k, v, test.wantRewrite[k])
   213  			}
   214  			delete(test.wantRewrite, k)
   215  		}
   216  		for k, v := range test.wantRewrite {
   217  			t.Errorf("#%d. rewrite[%s] missing, want <<%s>>", i, k, v)
   218  		}
   219  	}
   220  }
   221  
   222  // TestDryRun tests that the -n flag suppresses calls to writeFile.
   223  func TestDryRun(t *testing.T) {
   224  	gopath := filepath.Join(cwd, "testdata")
   225  	if err := os.Setenv("GOPATH", gopath); err != nil {
   226  		t.Fatalf("os.Setenv: %v", err)
   227  	}
   228  
   229  	*dryrun = true
   230  	defer func() { *dryrun = false }() // restore
   231  	stderr = new(bytes.Buffer)
   232  	writeFile = func(filename string, content []byte, mode os.FileMode) error {
   233  		t.Fatalf("writeFile(%s) called in dryrun mode", filename)
   234  		return nil
   235  	}
   236  
   237  	if !fiximports("all") {
   238  		t.Fatalf("fiximports failed: %s", stderr)
   239  	}
   240  }