@@ -135,7 +135,15 @@ func testgo(t *testing.T) *testgoData {
135
135
t .Skip ("skipping external tests on %s/%s" , runtime .GOOS , runtime .GOARCH )
136
136
}
137
137
138
- return & testgoData {t : t }
138
+ tg := & testgoData {t : t }
139
+
140
+ // Hide user's local .gitconfig from git invocations.
141
+ // In particular, people using Github 2FA may configure
142
+ // https://github.com/ to redirect to ssh://[email protected] /
143
+ // using an insteadOf configuration, and that will break various
144
+ // of our tests.
145
+ tg .setenv ("HOME" , "/test-go-home-does-not-exist" )
146
+ return tg
139
147
}
140
148
141
149
// must gives a fatal error if err is not nil.
@@ -2063,6 +2071,16 @@ func TestCoverageUsesActualSettingToOverrideEvenForRace(t *testing.T) {
2063
2071
checkCoverage (tg , data )
2064
2072
}
2065
2073
2074
+ func TestCoverageImportMainLoop (t * testing.T ) {
2075
+ tg := testgo (t )
2076
+ defer tg .cleanup ()
2077
+ tg .setenv ("GOPATH" , filepath .Join (tg .pwd (), "testdata" ))
2078
+ tg .runFail ("test" , "importmain/test" )
2079
+ tg .grepStderr ("not an importable package" , "did not detect import main" )
2080
+ tg .runFail ("test" , "-cover" , "importmain/test" )
2081
+ tg .grepStderr ("not an importable package" , "did not detect import main" )
2082
+ }
2083
+
2066
2084
func TestBuildDryRunWithCgo (t * testing.T ) {
2067
2085
if ! canCgo {
2068
2086
t .Skip ("skipping because cgo not enabled" )
@@ -2462,21 +2480,238 @@ func TestGoGetHTTPS404(t *testing.T) {
2462
2480
}
2463
2481
2464
2482
// Test that you cannot import a main package.
2465
- func TestIssue4210 (t * testing.T ) {
2483
+ // See golang.org/issue/4210 and golang.org/issue/17475.
2484
+ func TestImportMain (t * testing.T ) {
2466
2485
tg := testgo (t )
2467
2486
defer tg .cleanup ()
2487
+
2488
+ // Importing package main from that package main's test should work.
2468
2489
tg .tempFile ("src/x/main.go" , `package main
2469
2490
var X int
2470
2491
func main() {}` )
2471
- tg .tempFile ("src/y/main.go" , `package main
2472
- import "fmt"
2492
+ tg .tempFile ("src/x/main_test.go" , `package main_test
2473
2493
import xmain "x"
2474
- func main() {
2475
- fmt.Println(xmain.X)
2476
- }` )
2494
+ import "testing"
2495
+ var _ = xmain.X
2496
+ func TestFoo(t *testing.T) {}
2497
+ ` )
2477
2498
tg .setenv ("GOPATH" , tg .path ("." ))
2478
- tg .runFail ("build" , "y" )
2479
- tg .grepBoth ("is a program" , `did not find expected error message ("is a program")` )
2499
+ tg .run ("build" , "x" )
2500
+ tg .run ("test" , "x" )
2501
+
2502
+ // Importing package main from another package should fail.
2503
+ tg .tempFile ("src/p1/p.go" , `package p1
2504
+ import xmain "x"
2505
+ var _ = xmain.X
2506
+ ` )
2507
+ tg .runFail ("build" , "p1" )
2508
+ tg .grepStderr ("import \" x\" is a program, not an importable package" , "did not diagnose package main" )
2509
+
2510
+ // ... even in that package's test.
2511
+ tg .tempFile ("src/p2/p.go" , `package p2
2512
+ ` )
2513
+ tg .tempFile ("src/p2/p_test.go" , `package p2
2514
+ import xmain "x"
2515
+ import "testing"
2516
+ var _ = xmain.X
2517
+ func TestFoo(t *testing.T) {}
2518
+ ` )
2519
+ tg .run ("build" , "p2" )
2520
+ tg .runFail ("test" , "p2" )
2521
+ tg .grepStderr ("import \" x\" is a program, not an importable package" , "did not diagnose package main" )
2522
+
2523
+ // ... even if that package's test is an xtest.
2524
+ tg .tempFile ("src/p3/p.go" , `package p
2525
+ ` )
2526
+ tg .tempFile ("src/p3/p_test.go" , `package p_test
2527
+ import xmain "x"
2528
+ import "testing"
2529
+ var _ = xmain.X
2530
+ func TestFoo(t *testing.T) {}
2531
+ ` )
2532
+ tg .run ("build" , "p3" )
2533
+ tg .runFail ("test" , "p3" )
2534
+ tg .grepStderr ("import \" x\" is a program, not an importable package" , "did not diagnose package main" )
2535
+
2536
+ // ... even if that package is a package main
2537
+ tg .tempFile ("src/p4/p.go" , `package main
2538
+ func main() {}
2539
+ ` )
2540
+ tg .tempFile ("src/p4/p_test.go" , `package main
2541
+ import xmain "x"
2542
+ import "testing"
2543
+ var _ = xmain.X
2544
+ func TestFoo(t *testing.T) {}
2545
+ ` )
2546
+ tg .run ("build" , "p4" )
2547
+ tg .runFail ("test" , "p4" )
2548
+ tg .grepStderr ("import \" x\" is a program, not an importable package" , "did not diagnose package main" )
2549
+
2550
+ // ... even if that package is a package main using an xtest.
2551
+ tg .tempFile ("src/p5/p.go" , `package main
2552
+ func main() {}
2553
+ ` )
2554
+ tg .tempFile ("src/p5/p_test.go" , `package main_test
2555
+ import xmain "x"
2556
+ import "testing"
2557
+ var _ = xmain.X
2558
+ func TestFoo(t *testing.T) {}
2559
+ ` )
2560
+ tg .run ("build" , "p5" )
2561
+ tg .runFail ("test" , "p5" )
2562
+ tg .grepStderr ("import \" x\" is a program, not an importable package" , "did not diagnose package main" )
2563
+ }
2564
+
2565
+ // Test that you cannot use a local import in a package
2566
+ // accessed by a non-local import (found in a GOPATH/GOROOT).
2567
+ // See golang.org/issue/17475.
2568
+ func TestImportLocal (t * testing.T ) {
2569
+ tg := testgo (t )
2570
+ defer tg .cleanup ()
2571
+
2572
+ // Importing package main from that package main's test should work.
2573
+ tg .tempFile ("src/dir/x/x.go" , `package x
2574
+ var X int
2575
+ ` )
2576
+ tg .setenv ("GOPATH" , tg .path ("." ))
2577
+ tg .run ("build" , "dir/x" )
2578
+
2579
+ // Ordinary import should work.
2580
+ tg .tempFile ("src/dir/p0/p.go" , `package p0
2581
+ import "dir/x"
2582
+ var _ = x.X
2583
+ ` )
2584
+ tg .run ("build" , "dir/p0" )
2585
+
2586
+ // Relative import should not.
2587
+ tg .tempFile ("src/dir/p1/p.go" , `package p1
2588
+ import "../x"
2589
+ var _ = x.X
2590
+ ` )
2591
+ tg .runFail ("build" , "dir/p1" )
2592
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2593
+
2594
+ // ... even in a test.
2595
+ tg .tempFile ("src/dir/p2/p.go" , `package p2
2596
+ ` )
2597
+ tg .tempFile ("src/dir/p2/p_test.go" , `package p2
2598
+ import "../x"
2599
+ import "testing"
2600
+ var _ = x.X
2601
+ func TestFoo(t *testing.T) {}
2602
+ ` )
2603
+ tg .run ("build" , "dir/p2" )
2604
+ tg .runFail ("test" , "dir/p2" )
2605
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2606
+
2607
+ // ... even in an xtest.
2608
+ tg .tempFile ("src/dir/p2/p_test.go" , `package p2_test
2609
+ import "../x"
2610
+ import "testing"
2611
+ var _ = x.X
2612
+ func TestFoo(t *testing.T) {}
2613
+ ` )
2614
+ tg .run ("build" , "dir/p2" )
2615
+ tg .runFail ("test" , "dir/p2" )
2616
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2617
+
2618
+ // Relative import starting with ./ should not work either.
2619
+ tg .tempFile ("src/dir/d.go" , `package dir
2620
+ import "./x"
2621
+ var _ = x.X
2622
+ ` )
2623
+ tg .runFail ("build" , "dir" )
2624
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2625
+
2626
+ // ... even in a test.
2627
+ tg .tempFile ("src/dir/d.go" , `package dir
2628
+ ` )
2629
+ tg .tempFile ("src/dir/d_test.go" , `package dir
2630
+ import "./x"
2631
+ import "testing"
2632
+ var _ = x.X
2633
+ func TestFoo(t *testing.T) {}
2634
+ ` )
2635
+ tg .run ("build" , "dir" )
2636
+ tg .runFail ("test" , "dir" )
2637
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2638
+
2639
+ // ... even in an xtest.
2640
+ tg .tempFile ("src/dir/d_test.go" , `package dir_test
2641
+ import "./x"
2642
+ import "testing"
2643
+ var _ = x.X
2644
+ func TestFoo(t *testing.T) {}
2645
+ ` )
2646
+ tg .run ("build" , "dir" )
2647
+ tg .runFail ("test" , "dir" )
2648
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2649
+
2650
+ // Relative import plain ".." should not work.
2651
+ tg .tempFile ("src/dir/x/y/y.go" , `package dir
2652
+ import ".."
2653
+ var _ = x.X
2654
+ ` )
2655
+ tg .runFail ("build" , "dir/x/y" )
2656
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2657
+
2658
+ // ... even in a test.
2659
+ tg .tempFile ("src/dir/x/y/y.go" , `package y
2660
+ ` )
2661
+ tg .tempFile ("src/dir/x/y/y_test.go" , `package y
2662
+ import ".."
2663
+ import "testing"
2664
+ var _ = x.X
2665
+ func TestFoo(t *testing.T) {}
2666
+ ` )
2667
+ tg .run ("build" , "dir/x/y" )
2668
+ tg .runFail ("test" , "dir/x/y" )
2669
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2670
+
2671
+ // ... even in an x test.
2672
+ tg .tempFile ("src/dir/x/y/y_test.go" , `package y_test
2673
+ import ".."
2674
+ import "testing"
2675
+ var _ = x.X
2676
+ func TestFoo(t *testing.T) {}
2677
+ ` )
2678
+ tg .run ("build" , "dir/x/y" )
2679
+ tg .runFail ("test" , "dir/x/y" )
2680
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2681
+
2682
+ // Relative import "." should not work.
2683
+ tg .tempFile ("src/dir/x/xx.go" , `package x
2684
+ import "."
2685
+ var _ = x.X
2686
+ ` )
2687
+ tg .runFail ("build" , "dir/x" )
2688
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2689
+
2690
+ // ... even in a test.
2691
+ tg .tempFile ("src/dir/x/xx.go" , `package x
2692
+ ` )
2693
+ tg .tempFile ("src/dir/x/xx_test.go" , `package x
2694
+ import "."
2695
+ import "testing"
2696
+ var _ = x.X
2697
+ func TestFoo(t *testing.T) {}
2698
+ ` )
2699
+ tg .run ("build" , "dir/x" )
2700
+ tg .runFail ("test" , "dir/x" )
2701
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2702
+
2703
+ // ... even in an xtest.
2704
+ tg .tempFile ("src/dir/x/xx.go" , `package x
2705
+ ` )
2706
+ tg .tempFile ("src/dir/x/xx_test.go" , `package x_test
2707
+ import "."
2708
+ import "testing"
2709
+ var _ = x.X
2710
+ func TestFoo(t *testing.T) {}
2711
+ ` )
2712
+ tg .run ("build" , "dir/x" )
2713
+ tg .runFail ("test" , "dir/x" )
2714
+ tg .grepStderr ("local import.*in non-local package" , "did not diagnose local import" )
2480
2715
}
2481
2716
2482
2717
func TestGoGetInsecure (t * testing.T ) {
0 commit comments