Skip to content

Commit d91fd8e

Browse files
committed
fix(golang): fix gocov exit status, default packages, mktemp, gocd GOPATH check, and completion load order
1 parent e599ad8 commit d91fd8e

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

plugins/golang/README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ Enable the plugin by adding it to your oh-my-bash `plugins` definition in `~/.ba
1010
plugins=(golang)
1111
```
1212

13-
Optionally, enable Go tab completions:
14-
15-
```sh
16-
completions=(go)
17-
```
13+
> **Note:** Tab completion for Go commands is included automatically — no separate `completions=(go)` entry is needed.
1814
1915
## Aliases
2016

@@ -109,17 +105,19 @@ completions=(go)
109105

110106
### `gocd <package>`
111107

112-
Navigate to a package's source directory under `$GOPATH/src`.
108+
Navigate to a package's source directory under `$GOPATH/src`. Prints a clear error if `GOPATH` is not set.
113109

114110
```bash
115111
gocd github.com/some/package
116112
```
117113

118-
### `gocov [go test flags]`
114+
### `gocov [-flags...] [packages]`
119115

120-
Run tests with a coverage report and open the result in your browser. Accepts the same flags as `go test`; defaults to `./...`.
116+
Run tests with a coverage report and open the result in your browser. Flags (arguments starting with `-`) are forwarded to `go test`; remaining arguments are treated as packages. If no packages are given, `./...` is used as the default.
121117

122118
```bash
123-
gocov # test all packages + open coverage report
124-
gocov ./pkg/... # test a specific subtree
119+
gocov # test ./... + open coverage report
120+
gocov ./pkg/... # test a specific subtree
121+
gocov -run=TestFoo # run a specific test across ./...
122+
gocov -run=TestFoo ./pkg/... # run a specific test in a subtree
125123
```

plugins/golang/golang.plugin.sh

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# Description: Aliases and utilities for Go development
33
# Inspired by the oh-my-zsh golang plugin (https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/golang)
44

5-
_omb_module_require completion:go
6-
75
if ! _omb_util_command_exists go; then
86
return
97
fi
108

9+
_omb_module_require completion:go
10+
1111
# Build
1212
alias gob='go build'
1313
alias goba='go build ./...'
@@ -87,17 +87,35 @@ function _omb_plugin_golang_cd {
8787
fi
8888
local gopath
8989
gopath=$(go env GOPATH)
90+
if [[ -z $gopath ]]; then
91+
_omb_util_print 'gocd: GOPATH is not set or empty' >&2
92+
return 1
93+
fi
9094
cd "$gopath/src/$pkg" || return $?
9195
}
9296
alias gocd='_omb_plugin_golang_cd'
9397

9498
# Run tests with a coverage report opened in the browser.
95-
# Usage: gocov [go test flags] (defaults to ./...)
99+
# Usage: gocov [-go-test-flags...] [packages] (defaults to ./...)
100+
# Flags (args starting with -) are passed to go test; remaining args are
101+
# treated as packages. If no packages are given, ./... is used as default.
96102
function _omb_plugin_golang_cov {
97-
local coverfile
98-
coverfile=$(mktemp /tmp/go-cover-XXXXXX.out)
99-
go test -coverprofile="$coverfile" "${@:-./...}" || { rm -f "$coverfile"; return $?; }
103+
local arg flag_args=() pkg_args=()
104+
for arg in "$@"; do
105+
if [[ $arg == -* ]]; then
106+
flag_args+=("$arg")
107+
else
108+
pkg_args+=("$arg")
109+
fi
110+
done
111+
[[ ${#pkg_args[@]} -eq 0 ]] && pkg_args=(./...)
112+
113+
local coverfile rc
114+
coverfile=$(_omb_util_mktemp) || return 1
115+
go test -coverprofile="$coverfile" "${flag_args[@]}" "${pkg_args[@]}" || { rm -f "$coverfile"; return $?; }
100116
go tool cover -html="$coverfile"
117+
rc=$?
101118
rm -f "$coverfile"
119+
return $rc
102120
}
103121
alias gocov='_omb_plugin_golang_cov'

0 commit comments

Comments
 (0)