From 5556be7402b1c5b4015bf6da8f4012c7011ecab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 9 Dec 2019 16:47:59 +0000 Subject: [PATCH] make the tool work on Windows, enable tests The tests required a few last tweaks to work on Windows. --- .github/workflows/test.yml | 2 +- main.go | 29 +++++++++++++++++++++++++++-- main_test.go | 2 +- testdata/scripts/basic.txt | 10 ++++++---- testdata/scripts/imports.txt | 6 +++--- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 434af18..4f28148 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,7 +5,7 @@ jobs: strategy: matrix: go-version: [1.13.x] - platform: [ubuntu-latest, macos-latest] + platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: - name: Install Go diff --git a/main.go b/main.go index 5ba8504..55110f1 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "golang.org/x/tools/go/ast/astutil" @@ -125,10 +126,17 @@ func main1() int { } _, tool := filepath.Split(args[0]) - // TODO: trim ".exe" for windows? + if runtime.GOOS == "windows" { + tool = strings.TrimSuffix(tool, ".exe") + } + transform, ok := transformFuncs[tool] + if !ok { + fmt.Fprintf(os.Stderr, "unknown tool: %q", tool) + return 1 + } transformed := args[1:] // log.Println(tool, transformed) - if transform := transformFuncs[tool]; transform != nil { + if transform != nil { var err error if transformed, err = transform(transformed); err != nil { fmt.Fprintln(os.Stderr, err) @@ -155,6 +163,23 @@ func main1() int { var transformFuncs = map[string]func([]string) ([]string, error){ "compile": transformCompile, "link": transformLink, + + "addr2line": nil, + "api": nil, + "asm": nil, + "buildid": nil, + "cgo": nil, + "cover": nil, + "dist": nil, + "doc": nil, + "fix": nil, + "nm": nil, + "objdump": nil, + "pack": nil, + "pprof": nil, + "test2json": nil, + "trace": nil, + "vet": nil, } func transformCompile(args []string) ([]string, error) { diff --git a/main_test.go b/main_test.go index a1bc4fb..2e513cb 100644 --- a/main_test.go +++ b/main_test.go @@ -38,7 +38,7 @@ func TestScripts(t *testing.T) { if runtime.GOOS == "windows" { binfile += ".exe" } - if err := os.Symlink(os.Args[0], filepath.Join(bindir, "garble")); err != nil { + if err := os.Symlink(os.Args[0], binfile); err != nil { return err } env.Vars = append(env.Vars, fmt.Sprintf("PATH=%s%c%s", bindir, filepath.ListSeparator, os.Getenv("PATH"))) diff --git a/testdata/scripts/basic.txt b/testdata/scripts/basic.txt index 6a268cf..809312f 100644 --- a/testdata/scripts/basic.txt +++ b/testdata/scripts/basic.txt @@ -9,7 +9,9 @@ cmp stderr main.stderr [!windows] [exec:readelf] stdout '\.symtab' # The default build includes full non-trimmed paths, as well as our names. -bingrep main$exe ${WORK@R} 'globalVar' 'globalFunc' +# Only check $WORK on non-windows, because it's difficult to do it there. +bingrep main$exe 'globalVar' 'globalFunc' +[!windows] bingrep main$exe ${WORK@R} # Check that we fail if the user forgot -trimpath. ! exec go build -a -toolexec=garble main.go @@ -28,11 +30,11 @@ cmp stderr main.stderr # Finally, check that the 'garble build' shortcut works, and produces the same # binary. -cp main main_old -rm main +cp main$exe main_old$exe +rm main$exe garble build main.go ! bingrep main$exe 'globalVar' -bincmp main main_old +bincmp main$exe main_old$exe -- main.go -- package main diff --git a/testdata/scripts/imports.txt b/testdata/scripts/imports.txt index d12d645..9b81579 100644 --- a/testdata/scripts/imports.txt +++ b/testdata/scripts/imports.txt @@ -5,10 +5,10 @@ cmp stdout main.stdout ! bingrep main$exe 'ImportedVar' 'ImportedConst' 'ImportedFunc' 'ImportedType' # Also check that the binary is reproducible when many imports are involved. -cp main main_old -rm main +cp main$exe main_old$exe +rm main$exe garble build -bincmp main main_old +bincmp main$exe main_old$exe -- go.mod -- module foo.com/main