@@ -1683,173 +1683,111 @@ func homeEnvName() string {
1683
1683
}
1684
1684
}
1685
1685
1686
- // Test go env missing GOPATH shows default.
1687
- func TestMissingGOPATHEnvShowsDefault (t * testing.T ) {
1686
+ func TestDefaultGOPATH (t * testing.T ) {
1688
1687
tg := testgo (t )
1689
1688
defer tg .cleanup ()
1690
1689
tg .parallel ()
1691
- tg .setenv ("GOPATH" , "" )
1692
- tg .run ("env" , "GOPATH" )
1693
-
1694
- want := filepath .Join (os .Getenv (homeEnvName ()), "go" )
1695
- got := strings .TrimSpace (tg .getStdout ())
1696
- if got != want {
1697
- t .Errorf ("got %q; want %q" , got , want )
1698
- }
1699
- }
1700
-
1701
- // Test go get missing GOPATH causes go get to warn if directory doesn't exist.
1702
- func TestMissingGOPATHGetWarnsIfNotExists (t * testing.T ) {
1703
- testenv .MustHaveExternalNetwork (t )
1690
+ tg .tempDir ("home/go" )
1691
+ tg .setenv (homeEnvName (), tg .path ("home" ))
1704
1692
1705
- if _ , err := exec .LookPath ("git" ); err != nil {
1706
- t .Skip ("skipping because git binary not found" )
1707
- }
1708
-
1709
- tg := testgo (t )
1710
- defer tg .cleanup ()
1711
-
1712
- // setenv variables for test and defer deleting temporary home directory.
1713
- tg .setenv ("GOPATH" , "" )
1714
- tmp , err := ioutil .TempDir ("" , "" )
1715
- if err != nil {
1716
- t .Fatalf ("could not create tmp home: %v" , err )
1717
- }
1718
- defer os .RemoveAll (tmp )
1719
- tg .setenv (homeEnvName (), tmp )
1693
+ tg .run ("env" , "GOPATH" )
1694
+ tg .grepStdout (regexp .QuoteMeta (tg .path ("home/go" )), "want GOPATH=$HOME/go" )
1720
1695
1721
- tg .run ("get" , "-v" , "github.com/golang/example/hello" )
1696
+ tg .setenv ("GOROOT" , tg .path ("home/go" ))
1697
+ tg .run ("env" , "GOPATH" )
1698
+ tg .grepStdoutNot ("." , "want unset GOPATH because GOROOT=$HOME/go" )
1722
1699
1723
- want := fmt .Sprintf ("created GOPATH=%s; see 'go help gopath'" , filepath .Join (tmp , "go" ))
1724
- got := strings .TrimSpace (tg .getStderr ())
1725
- if ! strings .Contains (got , want ) {
1726
- t .Errorf ("got %q; want %q" , got , want )
1727
- }
1700
+ tg .setenv ("GOROOT" , tg .path ("home/go" )+ "/" )
1701
+ tg .run ("env" , "GOPATH" )
1702
+ tg .grepStdoutNot ("." , "want unset GOPATH because GOROOT=$HOME/go/" )
1728
1703
}
1729
1704
1730
- // Test go get missing GOPATH causes no warning if directory exists.
1731
- func TestMissingGOPATHGetDoesntWarnIfExists (t * testing.T ) {
1705
+ func TestDefaultGOPATHGet (t * testing.T ) {
1732
1706
testenv .MustHaveExternalNetwork (t )
1733
1707
1734
- if _ , err := exec .LookPath ("git" ); err != nil {
1735
- t .Skip ("skipping because git binary not found" )
1736
- }
1737
-
1738
1708
tg := testgo (t )
1739
1709
defer tg .cleanup ()
1740
-
1741
- // setenv variables for test and defer resetting them.
1742
1710
tg .setenv ("GOPATH" , "" )
1743
- tmp , err := ioutil .TempDir ("" , "" )
1744
- if err != nil {
1745
- t .Fatalf ("could not create tmp home: %v" , err )
1746
- }
1747
- defer os .RemoveAll (tmp )
1748
- if err := os .Mkdir (filepath .Join (tmp , "go" ), 0777 ); err != nil {
1749
- t .Fatalf ("could not create $HOME/go: %v" , err )
1750
- }
1711
+ tg .tempDir ("home" )
1712
+ tg .setenv (homeEnvName (), tg .path ("home" ))
1751
1713
1752
- tg .setenv (homeEnvName (), tmp )
1714
+ // warn for creating directory
1715
+ tg .run ("get" , "-v" , "github.com/golang/example/hello" )
1716
+ tg .grepStderr ("created GOPATH=" + regexp .QuoteMeta (tg .path ("home/go" ))+ "; see 'go help gopath'" , "did not create GOPATH" )
1753
1717
1718
+ // no warning if directory already exists
1719
+ tg .must (os .RemoveAll (tg .path ("home/go" )))
1720
+ tg .tempDir ("home/go" )
1754
1721
tg .run ("get" , "github.com/golang/example/hello" )
1722
+ tg .grepStderrNot ("." , "expected no output on standard error" )
1755
1723
1756
- got := strings .TrimSpace (tg .getStderr ())
1757
- if got != "" {
1758
- t .Errorf ("got %q; wants empty" , got )
1759
- }
1760
- }
1761
-
1762
- // Test go get missing GOPATH fails if pointed file is not a directory.
1763
- func TestMissingGOPATHGetFailsIfItsNotDirectory (t * testing.T ) {
1764
- testenv .MustHaveExternalNetwork (t )
1765
-
1766
- tg := testgo (t )
1767
- defer tg .cleanup ()
1768
-
1769
- // setenv variables for test and defer resetting them.
1770
- tg .setenv ("GOPATH" , "" )
1771
- tmp , err := ioutil .TempDir ("" , "" )
1772
- if err != nil {
1773
- t .Fatalf ("could not create tmp home: %v" , err )
1774
- }
1775
- defer os .RemoveAll (tmp )
1776
-
1777
- path := filepath .Join (tmp , "go" )
1778
- if err := ioutil .WriteFile (path , nil , 0777 ); err != nil {
1779
- t .Fatalf ("could not create GOPATH at %s: %v" , path , err )
1780
- }
1781
- tg .setenv (homeEnvName (), tmp )
1782
-
1783
- const pkg = "github.com/golang/example/hello"
1784
- tg .runFail ("get" , pkg )
1785
-
1786
- msg := "not a directory"
1787
- if runtime .GOOS == "windows" {
1788
- msg = "The system cannot find the path specified."
1789
- }
1790
- want := fmt .Sprintf ("package %s: mkdir %s: %s" , pkg , filepath .Join (tmp , "go" ), msg )
1791
- got := strings .TrimSpace (tg .getStderr ())
1792
- if got != want {
1793
- t .Errorf ("got %q; wants %q" , got , want )
1794
- }
1724
+ // error if $HOME/go is a file
1725
+ tg .must (os .RemoveAll (tg .path ("home/go" )))
1726
+ tg .tempFile ("home/go" , "" )
1727
+ tg .runFail ("get" , "github.com/golang/example/hello" )
1728
+ tg .grepStderr (`mkdir .*[/\\]go: .*(not a directory|cannot find the path)` , "expected error because $HOME/go is a file" )
1795
1729
}
1796
1730
1797
- // Test go install of missing package when missing GOPATH fails and shows default GOPATH.
1798
- func TestMissingGOPATHInstallMissingPackageFailsAndShowsDefault (t * testing.T ) {
1731
+ func TestDefaultGOPATHPrintedSearchList (t * testing.T ) {
1799
1732
tg := testgo (t )
1800
1733
defer tg .cleanup ()
1801
-
1802
- // setenv variables for test and defer resetting them.
1803
1734
tg .setenv ("GOPATH" , "" )
1804
- tmp , err := ioutil .TempDir ("" , "" )
1805
- if err != nil {
1806
- t .Fatalf ("could not create tmp home: %v" , err )
1807
- }
1808
- defer os .RemoveAll (tmp )
1809
- if err := os .Mkdir (filepath .Join (tmp , "go" ), 0777 ); err != nil {
1810
- t .Fatalf ("could not create $HOME/go: %v" , err )
1811
- }
1812
- tg .setenv (homeEnvName (), tmp )
1813
-
1814
- const pkg = "github.com/golang/example/hello"
1815
- tg .runFail ("install" , pkg )
1735
+ tg .tempDir ("home" )
1736
+ tg .setenv (homeEnvName (), tg .path ("home" ))
1816
1737
1817
- pkgPath := filepath .Join (strings .Split (pkg , "/" )... )
1818
- want := fmt .Sprintf ("can't load package: package %s: cannot find package \" %s\" in any of:" , pkg , pkg ) +
1819
- fmt .Sprintf ("\n \t %s (from $GOROOT)" , filepath .Join (runtime .GOROOT (), "src" , pkgPath )) +
1820
- fmt .Sprintf ("\n \t %s (from $GOPATH)" , filepath .Join (tmp , "go" , "src" , pkgPath ))
1821
-
1822
- got := strings .TrimSpace (tg .getStderr ())
1823
- if got != want {
1824
- t .Errorf ("got %q; wants %q" , got , want )
1825
- }
1738
+ tg .runFail ("install" , "github.com/golang/example/hello" )
1739
+ tg .grepStderr (regexp .QuoteMeta (tg .path ("home/go/src/github.com/golang/example/hello" ))+ `.*from \$GOPATH` , "expected default GOPATH" )
1826
1740
}
1827
1741
1828
1742
// Issue 4186. go get cannot be used to download packages to $GOROOT.
1829
1743
// Test that without GOPATH set, go get should fail.
1830
- func TestWithoutGOPATHGoGetFails (t * testing.T ) {
1744
+ func TestGoGetIntoGOROOT (t * testing.T ) {
1831
1745
testenv .MustHaveExternalNetwork (t )
1832
1746
1833
1747
tg := testgo (t )
1834
1748
defer tg .cleanup ()
1835
1749
tg .parallel ()
1836
1750
tg .tempDir ("src" )
1837
- tg .setenv ("GOPATH" , "" )
1751
+
1752
+ // Fails because GOROOT=GOPATH
1753
+ tg .setenv ("GOPATH" , tg .path ("." ))
1838
1754
tg .setenv ("GOROOT" , tg .path ("." ))
1839
- tg .runFail ("get" , "-d" , "golang.org/x/codereview/cmd/hgpatch" )
1840
- }
1755
+ tg .runFail ("get" , "-d" , "github.com/golang/example/hello" )
1756
+ tg .grepStderr ("warning: GOPATH set to GOROOT" , "go should detect GOPATH=GOROOT" )
1757
+ tg .grepStderr (`\$GOPATH must not be set to \$GOROOT` , "go should detect GOPATH=GOROOT" )
1841
1758
1842
- // Test that with GOPATH=$GOROOT, go get should fail.
1843
- func TestWithGOPATHEqualsGOROOTGoGetFails (t * testing.T ) {
1844
- testenv .MustHaveExternalNetwork (t )
1759
+ // Fails because GOROOT=GOPATH after cleaning.
1760
+ tg .setenv ("GOPATH" , tg .path ("." )+ "/" )
1761
+ tg .setenv ("GOROOT" , tg .path ("." ))
1762
+ tg .runFail ("get" , "-d" , "github.com/golang/example/hello" )
1763
+ tg .grepStderr ("warning: GOPATH set to GOROOT" , "go should detect GOPATH=GOROOT" )
1764
+ tg .grepStderr (`\$GOPATH must not be set to \$GOROOT` , "go should detect GOPATH=GOROOT" )
1845
1765
1846
- tg := testgo (t )
1847
- defer tg .cleanup ()
1848
- tg .parallel ()
1849
- tg .tempDir ("src" )
1850
1766
tg .setenv ("GOPATH" , tg .path ("." ))
1851
- tg .setenv ("GOROOT" , tg .path ("." ))
1852
- tg .runFail ("get" , "-d" , "golang.org/x/codereview/cmd/hgpatch" )
1767
+ tg .setenv ("GOROOT" , tg .path ("." )+ "/" )
1768
+ tg .runFail ("get" , "-d" , "github.com/golang/example/hello" )
1769
+ tg .grepStderr ("warning: GOPATH set to GOROOT" , "go should detect GOPATH=GOROOT" )
1770
+ tg .grepStderr (`\$GOPATH must not be set to \$GOROOT` , "go should detect GOPATH=GOROOT" )
1771
+
1772
+ // Fails because GOROOT=$HOME/go so default GOPATH unset.
1773
+ tg .tempDir ("home/go" )
1774
+ tg .setenv (homeEnvName (), tg .path ("home" ))
1775
+ tg .setenv ("GOPATH" , "" )
1776
+ tg .setenv ("GOROOT" , tg .path ("home/go" ))
1777
+ tg .runFail ("get" , "-d" , "github.com/golang/example/hello" )
1778
+ tg .grepStderr (`\$GOPATH not set` , "expected GOPATH not set" )
1779
+
1780
+ tg .setenv (homeEnvName (), tg .path ("home" )+ "/" )
1781
+ tg .setenv ("GOPATH" , "" )
1782
+ tg .setenv ("GOROOT" , tg .path ("home/go" ))
1783
+ tg .runFail ("get" , "-d" , "github.com/golang/example/hello" )
1784
+ tg .grepStderr (`\$GOPATH not set` , "expected GOPATH not set" )
1785
+
1786
+ tg .setenv (homeEnvName (), tg .path ("home" ))
1787
+ tg .setenv ("GOPATH" , "" )
1788
+ tg .setenv ("GOROOT" , tg .path ("home/go" )+ "/" )
1789
+ tg .runFail ("get" , "-d" , "github.com/golang/example/hello" )
1790
+ tg .grepStderr (`\$GOPATH not set` , "expected GOPATH not set" )
1853
1791
}
1854
1792
1855
1793
func TestLdflagsArgumentsWithSpacesIssue3941 (t * testing.T ) {
0 commit comments