Skip to content

Commit

Permalink
go-mod.mk: Add more tests (and fix discovered issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeShu committed Sep 5, 2019
1 parent 6a9751e commit 3ea1603
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
8 changes: 5 additions & 3 deletions go-mod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ endif
# Example: $(call _go.files2dirs,foo/bar/baz foo/baz/qux) => foo foo/bar foo/bar/baz foo/baz foo/baz/qux
_go.files2dirs = $(sort $(foreach f,$1,$(call _go.file2dirs,$f)))
# Without pruning sub-module packages (relative to ".", without "./" prefix")
# Use pwd(1) instead of $(CURDIR) because $(CURDIR) behaves differently than `go` in the presence of symlinks.
_go.raw.cwd := $(shell pwd)
# Usage: $(call _go.raw.list,ARGS)
_go.raw.list = $(call path.trimprefix,_$(CURDIR),$(shell GOPATH=/bogus GO111MODULE=off go list $1))
_go.raw.list = $(call path.trimprefix,_$(_go.raw.cwd),$(shell GOPATH=/bogus GO111MODULE=off go list $1))
_go.raw.pkgs := $(call _go.raw.list,./... 2>/dev/null)
_go.raw.submods := $(patsubst %/go.mod,%,$(wildcard $(addsuffix /go.mod,$(call _go.files2dirs,$(_go.raw.pkgs)))))
_go.raw.submods := $(filter-out .,$(patsubst %/go.mod,%,$(wildcard $(addsuffix /go.mod,$(call _go.files2dirs,$(_go.raw.pkgs))))))
# With pruning sub-module packages (relative to ".", without "./" prefix")
_go.pkgs = $(filter-out $(foreach m,$(_go.raw.submods),$m $m/%),$(_go.raw.pkgs))
# Usage: $(call _go.list,ARGS)
Expand All @@ -133,7 +135,7 @@ go-get: $(go.lock)
vendor: go-get FORCE
vendor: $(go.lock)
$(go.lock)go mod vendor
@test -d $@
@test -d $@ || test "$$(go mod edit -json|jq '.Require|length')" -eq 0

$(dir $(_go-mod.mk))go1%.src.tar.gz:
curl -o $@ --fail https://dl.google.com/go/$(@F)
Expand Down
92 changes: 92 additions & 0 deletions tests/go-mod.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,95 @@ setup() {
check_go_executable go-mod.mk GOLANGCI_LINT
# TODO: Check that $GOLANGCI_LINT behaves correctly
}

@test "go-mod.mk: go.lock" {
cat >>Makefile <<-'__EOT__'
include build-aux/go-mod.mk
$(info outside: $(go.lock))
all: ; @echo 'inside: $(go.lock)'
__EOT__

if [[ "$build_aux_unsupported_go" == true ]] || ! type go &>/dev/null; then
make_expecting_go_error
return 0
fi

make >actual
if [[ "$(go version)" != *' go1.11'* ]]; then
printf '%s\n' >expected \
"outside: " \
"inside: "
diff -u expected actual
else
[[ $(sed -n 1p actual) == 'outside: '* ]]
[[ $(sed -n 2p actual) == 'inside: '* ]]
[[ $(wc -l <actual) -eq 2 ]]

outside="$(sed -n 's/^outside: //p' actual)"
[[ "$outside" != *' '* ]]
[[ "$outside" == */flock ]]

inside="$(sed -n 's/^inside: //p' actual)"
read -ra inside_split <<<"$inside"
[[ "${#inside_split[@]}" -eq 2 ]]
[[ "${inside_split[0]}" == */flock ]]
[[ "${inside_split[1]}" == "$(go env GOPATH)/pkg/mod" ]]
fi
}

@test "go-mod.mk: build timestamps" {
cat >>Makefile <<-'__EOT__'
include build-aux/go-mod.mk
all: build
__EOT__

cat >main.go <<-'__EOT__'
package main
func main() {}
__EOT__

mkdir sub
cat >sub/main.go <<-'__EOT__'
package main
func main() {}
__EOT__

if [[ "$build_aux_unsupported_go" == true ]] || ! type go &>/dev/null; then
make_expecting_go_error
return 0
fi

mainexe="bin_$(go env GOHOSTOS)_$(go env GOHOSTARCH)/testlocal"
subexe="bin_$(go env GOHOSTOS)_$(go env GOHOSTARCH)/sub"

make

[[ -f "$subexe" && -x "$subexe" ]]
cp -a "$subexe" subexe.bak
[[ ! "$subexe" -nt subexe.bak ]]

[[ -f "$mainexe" && -x "$mainexe" ]]
cp -a "$mainexe" mainexe.bak
[[ ! "$mainexe" -nt mainexe.bak ]]

sleep 2

cat >sub/main.go <<-'__EOT__'
package main
import "fmt"
func main() { fmt.Println("Hello world") }
__EOT__

unset CI
make

[[ -f "$subexe" && -x "$subexe" ]]
[[ "$subexe" -nt subexe.bak ]]

[[ -f "$mainexe" && -x "$mainexe" ]]
[[ ! "$mainexe" -nt mainexe.bak ]]
}

0 comments on commit 3ea1603

Please sign in to comment.