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")
}
Tests that a local replace works. Adapted from mod_local_replace.txt.
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$`)
}