func TestNoMainModule(t *testing.T) {
	testenv.NeedsGo1Point(t, 12)
	mt := setup(t, `
-- x.go --
package x
`, "")
	defer mt.cleanup()
	if _, err := mt.env.invokeGo(context.Background(), "mod", "download", "rsc.io/quote@v1.5.1"); err != nil {
		t.Fatal(err)
	}
	mt.assertScanFinds("rsc.io/quote", "quote")
}
Tests that we handle GO111MODULE=on with no go.mod file. See #30855.
package imports
import (
	"archive/zip"
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strings"
	"sync"
	"testing"
	"github.com/rinchsan/gosimports/internal/gocommand"
	"github.com/rinchsan/gosimports/internal/gopathwalk"
	"github.com/rinchsan/gosimports/internal/proxydir"
	"github.com/rinchsan/gosimports/internal/testenv"
	"golang.org/x/mod/module"
	"golang.org/x/tools/txtar"
)
func TestScanStdlib(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
`, "")
	defer mt.cleanup()
	mt.assertScanFinds("fmt", "fmt")
}
func TestScanOutOfScopeNestedModule(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
-- x.go --
package x
-- v2/go.mod --
module x
-- v2/x.go --
package x`, "")
	defer mt.cleanup()
	pkg := mt.assertScanFinds("x/v2", "x")
	if pkg != nil && !strings.HasSuffix(filepath.ToSlash(pkg.dir), "main/v2") {
		t.Errorf("x/v2 was found in %v, wanted .../main/v2", pkg.dir)
	}
}
func TestScanNestedModuleInLocalReplace(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require y v0.0.0
replace y => ./y
-- x.go --
package x
-- y/go.mod --
module y
-- y/y.go --
package y
-- y/z/go.mod --
module y/z
-- y/z/z.go --
package z
`, "")
	defer mt.cleanup()
	mt.assertFound("y", "y")
	scan, err := scanToSlice(mt.resolver, nil)
	if err != nil {
		t.Fatal(err)
	}
	for _, pkg := range scan {
		if strings.HasSuffix(filepath.ToSlash(pkg.dir), "main/y/z") {
			t.Errorf("scan found a package %v in dir main/y/z, wanted none", pkg.importPathShort)
		}
	}
}
func TestModCase(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require rsc.io/QUOTE v1.5.2
-- x.go --
package x
import _ "rsc.io/QUOTE/QUOTE"
`, "")
	defer mt.cleanup()
	mt.assertFound("rsc.io/QUOTE/QUOTE", "QUOTE")
}
func TestModDomainRoot(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require example.com v1.0.0
-- x.go --
package x
import _ "example.com"
`, "")
	defer mt.cleanup()
	mt.assertFound("example.com", "x")
}
func TestModMultipleScans(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require example.com v1.0.0
-- x.go --
package x
import _ "example.com"
`, "")
	defer mt.cleanup()
	mt.assertScanFinds("example.com", "x")
	mt.assertScanFinds("example.com", "x")
}
func TestModMultipleScansWithSubdirs(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require rsc.io/quote v1.5.2
-- x.go --
package x
import _ "rsc.io/quote"
`, "")
	defer mt.cleanup()
	mt.assertScanFinds("rsc.io/quote", "quote")
	mt.assertScanFinds("rsc.io/quote", "quote")
}
func TestModCacheEditModFile(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require rsc.io/quote v1.5.2
-- x.go --
package x
import _ "rsc.io/quote"
`, "")
	defer mt.cleanup()
	found := mt.assertScanFinds("rsc.io/quote", "quote")
	if found == nil {
		t.Fatal("rsc.io/quote not found in initial scan.")
	}
	if err := os.Chmod(filepath.Join(found.dir, "go.mod"), 0644); err != nil {
		t.Fatal(err)
	}
	if err := ioutil.WriteFile(filepath.Join(found.dir, "go.mod"), []byte("module bad.com\n"), 0644); err != nil {
		t.Fatal(err)
	}
	mt.assertScanFinds("rsc.io/quote", "quote")
	if err := ioutil.WriteFile(filepath.Join(mt.env.WorkingDir, "go.mod"), []byte("module x\n"), 0644); err != nil {
		t.Fatal(err)
	}
	if err := ioutil.WriteFile(filepath.Join(mt.env.WorkingDir, "x.go"), []byte("package x\n"), 0644); err != nil {
		t.Fatal(err)
	}
	mt.resolver.ClearForNewMod()
	mt.assertScanFinds("rsc.io/quote", "quote")
}
func TestModVendorBuild(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module m
go 1.12
require rsc.io/sampler v1.3.1
-- x.go --
package x
import _ "rsc.io/sampler"
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `pkg.*mod.*/sampler@.*$`)
	if _, err := mt.env.invokeGo(context.Background(), "mod", "vendor"); err != nil {
		t.Fatal(err)
	}
	if _, err := mt.env.invokeGo(context.Background(), "clean", "-modcache"); err != nil {
		t.Fatal(err)
	}
	mt.resolver = newModuleResolver(mt.env)
	mt.env.Env["GOFLAGS"] = "-mod=vendor"
	mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `/vendor/`)
}
func TestModVendorAuto(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module m
go 1.14
require rsc.io/sampler v1.3.1
-- x.go --
package x
import _ "rsc.io/sampler"
`, "")
	defer mt.cleanup()
	if _, err := mt.env.invokeGo(context.Background(), "mod", "vendor"); err != nil {
		t.Fatal(err)
	}
	wantDir := `pkg.*mod.*/sampler@.*$`
	if testenv.Go1Point() >= 14 {
		wantDir = `/vendor/`
	}
	mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", wantDir)
}
func TestModList(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module x
require rsc.io/quote v1.5.1
replace rsc.io/sampler v1.3.0 => rsc.io/sampler v1.3.1
-- x.go --
package x
import _ "rsc.io/quote"
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("rsc.io/sampler", "sampler", `pkg.mod.*/sampler@v1.3.1$`)
}
func TestModLocalReplace(t *testing.T) {
	mt := setup(t, `
-- x/y/go.mod --
module x/y
require zz v1.0.0
replace zz v1.0.0 => ../z
-- x/y/y.go --
package y
import _ "zz"
-- x/z/go.mod --
module x/z
-- x/z/z.go --
package z
`, "x/y")
	defer mt.cleanup()
	mt.assertFound("zz", "z")
}
func TestModMultirepo1(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module rsc.io/quote
-- x.go --
package quote
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("rsc.io/quote", "quote", `/main`)
}
func TestModMultirepo3(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module rsc.io/quote
require rsc.io/quote/v2 v2.0.1
-- x.go --
package quote
import _ "rsc.io/quote/v2"
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("rsc.io/quote", "quote", `/main`)
	mt.assertModuleFoundInDir("rsc.io/quote/v2", "quote", `pkg.mod.*/v2@v2.0.1$`)
}
func TestModMultirepo4(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module rsc.io/quote
require rsc.io/quote/v2 v2.0.1
-- x.go --
package quote
import _ "rsc.io/quote/v2"
-- v2/go.mod --
package rsc.io/quote/v2
-- v2/x.go --
package quote
import _ "rsc.io/quote/v2"
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("rsc.io/quote", "quote", `/main`)
	mt.assertModuleFoundInDir("rsc.io/quote/v2", "quote", `pkg.mod.*/v2@v2.0.1$`)
}
func TestModReplace1(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module quoter
require rsc.io/quote/v3 v3.0.0
-- main.go --
package main
`, "")
	defer mt.cleanup()
	mt.assertFound("rsc.io/quote/v3", "quote")
}
func TestModReplace2(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module quoter
require rsc.io/quote/v3 v3.0.0
replace rsc.io/quote/v3 => ./local/rsc.io/quote/v3
-- main.go --
package main
-- local/rsc.io/quote/v3/go.mod --
module rsc.io/quote/v3
require rsc.io/sampler v1.3.0
-- local/rsc.io/quote/v3/quote.go --
package quote
import "rsc.io/sampler"
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("rsc.io/quote/v3", "quote", `/local/rsc.io/quote/v3`)
}
func TestModReplace3(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module quoter
require not-rsc.io/quote/v3 v3.1.0
replace not-rsc.io/quote/v3 v3.1.0 => ./local/rsc.io/quote/v3
-- usenewmodule/main.go --
package main
-- local/rsc.io/quote/v3/go.mod --
module rsc.io/quote/v3
require rsc.io/sampler v1.3.0
-- local/rsc.io/quote/v3/quote.go --
package quote
-- local/not-rsc.io/quote/v3/go.mod --
module not-rsc.io/quote/v3
-- local/not-rsc.io/quote/v3/quote.go --
package quote
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("not-rsc.io/quote/v3", "quote", "local/rsc.io/quote/v3")
}
func TestModReplaceImport(t *testing.T) {
	mt := setup(t, `
-- go.mod --
module example.com/m
replace (
	example.com/a => ./a
	example.com/a/b => ./b
)
replace (
	example.com/x => ./x
	example.com/x/v3 => ./v3
)
replace (
	example.com/y/z/w => ./w
	example.com/y => ./y
)
replace (
	example.com/vv v1.11.0 => ./v11
	example.com/vv v1.12.0 => ./v12
	example.com/vv => ./vv
)
require (
	example.com/a/b v0.0.0
	example.com/x/v3 v3.0.0
	example.com/y v0.0.0
	example.com/y/z/w v0.0.0
	example.com/vv v1.12.0
)
-- m.go --
package main
import (
	_ "example.com/a/b"
	_ "example.com/x/v3"
	_ "example.com/y/z/w"
	_ "example.com/vv"
)
func main() {}
-- a/go.mod --
module a.localhost
-- a/a.go --
package a
-- a/b/b.go--
package b
-- b/go.mod --
module a.localhost/b
-- b/b.go --
package b
-- x/go.mod --
module x.localhost
-- x/x.go --
package x
-- x/v3.go --
package v3
import _ "x.localhost/v3"
-- v3/go.mod --
module x.localhost/v3
-- v3/x.go --
package x
-- w/go.mod --
module w.localhost
-- w/skip/skip.go --
package skip
-- y/go.mod --
module y.localhost
-- y/z/w/w.go --
package w
-- v12/go.mod --
module v.localhost
-- v12/v.go --
package v
-- v11/go.mod --
module v.localhost
-- v11/v.go --
package v
-- vv/go.mod --
module v.localhost
-- vv/v.go --
package v
`, "")
	defer mt.cleanup()
	mt.assertModuleFoundInDir("example.com/a/b", "b", `main/b$`)
	mt.assertModuleFoundInDir("example.com/x/v3", "x", `main/v3$`)
	mt.assertModuleFoundInDir("example.com/y/z/w", "w", `main/y/z/w$`)
	mt.assertModuleFoundInDir("example.com/vv", "v", `main/v12$`)
}