github.com/ortery/cockroach@v2.2.0-alpha.20181217.0.20190116071909-251847e50223+incompatible/pkg/cmd/prereqs/prereqs_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/cockroachdb/cockroach/pkg/testutils"
    24  )
    25  
    26  var expectedA = `# Code generated by prereqs. DO NOT EDIT!
    27  
    28  bin/a: a/a.go a/ignore.go a/invalid.go a/cgo.go a/a.c a/a.f a b/b.go b b/vendor/foo.com/bar/bar.go b/vendor/foo.com/bar vendor/foo.com/foo/foo.go vendor/foo.com/foo
    29  
    30  a/a.go:
    31  a/ignore.go:
    32  a/invalid.go:
    33  a/cgo.go:
    34  a/a.c:
    35  a/a.f:
    36  a:
    37  b/b.go:
    38  b:
    39  b/vendor/foo.com/bar/bar.go:
    40  b/vendor/foo.com/bar:
    41  vendor/foo.com/foo/foo.go:
    42  vendor/foo.com/foo:
    43  `
    44  
    45  var expectedB = `# Code generated by prereqs. DO NOT EDIT!
    46  
    47  bin/b: b/b.go b b/vendor/foo.com/bar/bar.go b/vendor/foo.com/bar vendor/foo.com/foo/foo.go vendor/foo.com/foo
    48  
    49  b/b.go:
    50  b:
    51  b/vendor/foo.com/bar/bar.go:
    52  b/vendor/foo.com/bar:
    53  vendor/foo.com/foo/foo.go:
    54  vendor/foo.com/foo:
    55  `
    56  
    57  var expectedFoo = `# Code generated by prereqs. DO NOT EDIT!
    58  
    59  bin/foo: vendor/foo.com/foo/foo.go vendor/foo.com/foo
    60  
    61  vendor/foo.com/foo/foo.go:
    62  vendor/foo.com/foo:
    63  `
    64  
    65  var expectedSpecialChars = `# Code generated by prereqs. DO NOT EDIT!
    66  
    67  bin/specialchars: specialchars/a\[\]\*\?\~ $$%\#.go specialchars
    68  
    69  specialchars/a\[\]\*\?\~ $$%\#.go:
    70  specialchars:
    71  `
    72  
    73  var expectedTestNoDeps = `# Code generated by prereqs. DO NOT EDIT!
    74  
    75  bin/test: test
    76  
    77  test:
    78  `
    79  var expectedTestWithDeps = `# Code generated by prereqs. DO NOT EDIT!
    80  
    81  bin/test: test test/internal_test.go test/external_test.go vendor/foo.com/foo/foo.go vendor/foo.com/foo vendor/foo.com/baz/baz.go vendor/foo.com/baz
    82  
    83  test:
    84  test/internal_test.go:
    85  test/external_test.go:
    86  vendor/foo.com/foo/foo.go:
    87  vendor/foo.com/foo:
    88  vendor/foo.com/baz/baz.go:
    89  vendor/foo.com/baz:
    90  `
    91  
    92  func TestPrereqs(t *testing.T) {
    93  	gopath, err := filepath.Abs("testdata")
    94  	if err != nil {
    95  		t.Fatal(err)
    96  	}
    97  	buildCtx.GOPATH = gopath
    98  
    99  	const pkg = "testdata/src/example.com"
   100  	absPkg, err := filepath.Abs(pkg)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if err := os.Chdir(pkg); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	testutils.RunTrueAndFalse(t, "symlink", func(t *testing.T, symlink bool) {
   109  		if symlink {
   110  			tempDir, cleanup := testutils.TempDir(t)
   111  			defer cleanup()
   112  
   113  			link := filepath.Join(tempDir, "link")
   114  			if err := os.Symlink(absPkg, link); err != nil {
   115  				t.Fatal(err)
   116  			}
   117  
   118  			// You can't chdir into a symlink. Instead, you chdir into the physical
   119  			// path and set the PWD to the logical path. (At least, this is the hack
   120  			// that most shells employ, and os.Getwd respects it.)
   121  			if err := os.Setenv("PWD", link); err != nil {
   122  				t.Fatal(err)
   123  			}
   124  			if cwd, err := os.Getwd(); err != nil {
   125  				t.Fatal(err)
   126  			} else if cwd != link {
   127  				t.Fatalf("failed to chdir into symlink %s (os.Getwd reported %s)", link, cwd)
   128  			}
   129  		}
   130  
   131  		for _, tc := range []struct {
   132  			path        string
   133  			exp         string
   134  			includeTest bool
   135  		}{
   136  			{path: "example.com/a", exp: expectedA},
   137  			{path: "./b", exp: expectedB},
   138  			{path: "./a/../b", exp: expectedB},
   139  			{path: "example.com/a/../b", exp: expectedB},
   140  			{path: "example.com/b", exp: expectedB},
   141  			{path: "foo.com/foo", exp: expectedFoo},
   142  			{path: "./vendor/foo.com/foo", exp: expectedFoo},
   143  			{path: "example.com/vendor/foo.com/foo", exp: expectedFoo},
   144  			{path: "example.com/specialchars", exp: expectedSpecialChars},
   145  			{path: "example.com/test", exp: expectedTestNoDeps},
   146  			{path: "example.com/test", exp: expectedTestWithDeps, includeTest: true},
   147  		} {
   148  			t.Run(tc.path, func(t *testing.T) {
   149  				var buf bytes.Buffer
   150  				if err := run(&buf, tc.path, tc.includeTest, ""); err != nil {
   151  					t.Fatal(err)
   152  				}
   153  				if e, a := tc.exp, buf.String(); e != a {
   154  					t.Fatalf("expected:\n%s\nactual:\n%s\n", e, a)
   155  				}
   156  			})
   157  		}
   158  	})
   159  }