Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
main: support installing github.com/myitcv/gobin (#17)
Browse files Browse the repository at this point in the history
This is initial support that is unlikely to work on Windows, hence the
TODO in the code.

Unfortunately, I've had to mark the test as skip for now because we
can't install "self" from the module under test. I.e. it's not possible
to run go install for a package in the module under test, i.e.
github.com/myitcv/gobin.
  • Loading branch information
myitcv authored Oct 25, 2018
1 parent 25678ef commit daa7241
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ func mainerr() error {
gobin := filepath.Join(gobinCache, mp.Dir)
target := filepath.Join(gobin, path.Base(mp.ImportPath))

// optimistically remove our target in case we are installing over self
// TODO work out what to do for Windows
_ = os.Remove(target)

proxy := "file://" + modDlCache

var stdout, stderr bytes.Buffer
Expand Down Expand Up @@ -320,7 +324,12 @@ func mainerr() error {
}
defer src.Close()
bin := filepath.Join(installBin, path.Base(mp.ImportPath))
dest, err := os.Create(bin)

// optimistically remove our target in case we are installing over self
// TODO work out what to do for Windows
_ = os.Remove(bin)

dest, err := os.OpenFile(bin, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0755)
if err != nil {
return fmt.Errorf("failed to open %v for writing: %v", bin, err)
}
Expand Down
43 changes: 33 additions & 10 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package main
import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/rogpeppe/go-internal/goproxytest"
"github.com/rogpeppe/go-internal/gotooltest"
"github.com/rogpeppe/go-internal/testscript"
)

var proxyURL string
var (
proxyURL string
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(gobinMain{m}, map[string]func() int{
Expand All @@ -23,14 +26,6 @@ type gobinMain struct {
}

func (m gobinMain) Run() int {
if os.Getenv("GO_GCFLAGS") != "" {
fmt.Fprintf(os.Stderr, "testing: warning: no tests to run\n") // magic string for cmd/go
fmt.Printf("cmd/go test is not compatible with $GO_GCFLAGS being set\n")
fmt.Printf("SKIP\n")
return 0
}
os.Unsetenv("GOROOT_FINAL")

// Start the Go proxy server running for all tests.
srv, err := goproxytest.NewServer("testdata/mod", "")
if err != nil {
Expand All @@ -43,10 +38,38 @@ func (m gobinMain) Run() int {
}

func TestScripts(t *testing.T) {
var (
pathToMod string // local path to this module
modTestGOPATH string // GOPATH set when running tests in this module
)

// set pathToMod
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed to get working directory for test: %v", err)
}
pathToMod = wd

// set modTestGOPATH
cmd := exec.Command("go", "env", "GOPATH")
out, err := cmd.Output()
if err != nil {
var stderr []byte
if err, ok := err.(*exec.ExitError); ok {
stderr = err.Stderr
}
t.Fatalf("failed to get GOPATH for test: %v\n%s", err, stderr)
}
modTestGOPATH = string(out)

p := testscript.Params{
Dir: "testdata",
Setup: func(e *testscript.Env) error {
e.Vars = append(e.Vars, "GOPROXY="+proxyURL)
e.Vars = append(e.Vars,
"TESTGOPATH="+modTestGOPATH,
"GOBINMODPATH="+pathToMod,
"GOPROXY="+proxyURL,
)
return nil
},
}
Expand Down
21 changes: 21 additions & 0 deletions testdata/install-self.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
skip 'Skipping because of https://github.com/golang/go/issues/26241'

env GOPROXY=file://$TESTGOPATH/pkg/mod/cache/download
env GOBIN=$WORK/install
env PATH=$GOBIN:$PATH

env GOPATH=$TESTGOPATH
cd $GOBINMODPATH
go install
exists $GOBIN/gobin
exec which gobin
stdout ^$GOBIN/gobin'$'

cd $WORK/repo
exec go mod edit -replace=myitcv.io=$GOBINMODPATH
exec gobin -m github.com/myitcv/gobin

-- repo/go.mod --
module mod

require github.com/myitcv/gobin v0.0.0

0 comments on commit daa7241

Please sign in to comment.