Commit 75ba7ebe authored by Jacob Vosmaer's avatar Jacob Vosmaer

Make "go test" work without path helper

parent 4d86b01f
PREFIX=/usr/local
VERSION=$(shell git describe)-$(shell date -u +%Y%m%d.%H%M%S)
export GOPATH=$(shell pwd)/_build
BUILD_DIR = $(shell pwd)
export GOPATH=${BUILD_DIR}/_build
GOBUILD=go build -ldflags "-X main.Version=${VERSION}"
PKG=gitlab.com/gitlab-org/gitlab-workhorse
all: clean-build gitlab-zip-cat gitlab-zip-metadata gitlab-workhorse
gitlab-zip-cat: _build $(shell find cmd/gitlab-zip-cat/ -name '*.go')
${GOBUILD} -o $@ ${PKG}/cmd/$@
gitlab-zip-cat: ${BUILD_DIR}/_build $(shell find cmd/gitlab-zip-cat/ -name '*.go')
${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}/cmd/$@
gitlab-zip-metadata: _build $(shell find cmd/gitlab-zip-metadata/ -name '*.go')
${GOBUILD} -o $@ ${PKG}/cmd/$@
gitlab-zip-metadata: ${BUILD_DIR}/_build $(shell find cmd/gitlab-zip-metadata/ -name '*.go')
${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}/cmd/$@
gitlab-workhorse: _build $(shell find . -name '*.go' | grep -v '^\./_')
${GOBUILD} -o $@ ${PKG}
gitlab-workhorse: ${BUILD_DIR}/_build $(shell find . -name '*.go' | grep -v '^\./_')
${GOBUILD} -o ${BUILD_DIR}/$@ ${PKG}
install: gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata
mkdir -p $(DESTDIR)${PREFIX}/bin/
install gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata ${DESTDIR}${PREFIX}/bin/
cd ${BUILD_DIR} && install gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata ${DESTDIR}${PREFIX}/bin/
_build:
${BUILD_DIR}/_build:
mkdir -p $@/src/${PKG}
tar -cf - --exclude $@ --exclude .git . | (cd $@/src/${PKG} && tar -xf -)
tar -cf - --exclude _build --exclude .git . | (cd $@/src/${PKG} && tar -xf -)
touch $@
.PHONY: test
test: clean-build clean-workhorse all
go fmt ${PKG}/... | awk '{ print } END { if (NR > 0) { print "Please run go fmt"; exit 1 } }'
_support/path go test ${PKG}/...
go test ${PKG}/...
@echo SUCCESS
coverage:
......@@ -44,8 +45,8 @@ clean: clean-workhorse clean-build
.PHONY: clean-workhorse
clean-workhorse:
rm -f gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata
cd ${BUILD_DIR} && rm -f gitlab-workhorse gitlab-zip-cat gitlab-zip-metadata
.PHONY: clean-build
clean-build:
rm -rf _build
rm -rf ${BUILD_DIR}/_build
#!/bin/sh
exec env PATH=$(pwd):${PATH} "$@"
package artifacts
import (
"log"
"os"
"testing"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/testhelper"
)
func TestMain(m *testing.M) {
cleanup, err := testhelper.BuildExecutables()
if err != nil {
log.Printf("Test setup: failed to build executables: %v", err)
os.Exit(1)
}
os.Exit(func() int {
defer cleanup()
return m.Run()
}())
}
package testhelper
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path"
"regexp"
"runtime"
"testing"
)
var executables = []string{
"gitlab-workhorse",
"gitlab-zip-cat",
"gitlab-zip-metadata",
}
func AssertResponseCode(t *testing.T, response *httptest.ResponseRecorder, expectedCode int) {
if response.Code != expectedCode {
t.Fatalf("for HTTP request expected to get %d, got %d instead", expectedCode, response.Code)
......@@ -43,3 +56,67 @@ func TestServerWithHandler(url *regexp.Regexp, handler http.HandlerFunc) *httpte
handler(w, r)
}))
}
func BuildExecutables() (func(), error) {
_, currentFile, _, ok := runtime.Caller(0)
if !ok {
return nil, errors.New("BuildExecutables: calling runtime.Caller failed")
}
rootDir := path.Join(path.Dir(currentFile), "../..")
// This method will be invoked more than once due to Go test
// parallelization. We must use a unique temp directory for each
// invokation so that they do not trample each other's builds.
testDir, err := ioutil.TempDir("", "gitlab-workhorse-test")
if err != nil {
return nil, errors.New("could not create temp directory")
}
for _, executable := range executables {
utilPath := path.Join(testDir, executable)
if err := os.Remove(utilPath); err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to remove: %v", utilPath)
}
}
makeCmd := exec.Command("make", "BUILD_DIR="+testDir)
makeCmd.Dir = rootDir
makeCmd.Stderr = os.Stderr
makeCmd.Stdout = os.Stdout
if err := makeCmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v in %v", makeCmd, rootDir)
}
for _, executable := range executables {
utilPath := path.Join(testDir, executable)
if _, err := os.Stat(utilPath); err != nil {
return nil, fmt.Errorf("could not stat %v", utilPath)
}
}
// Try to ensure the list of executables is complete by asking "make" to only build files in the list.
cleanArgs := append([]string{"clean-workhorse"}, executables...)
cleanArgs = append(cleanArgs, "BUILD_DIR="+testDir)
makeCmd = exec.Command("make", cleanArgs...)
makeCmd.Dir = rootDir
makeCmd.Stderr = os.Stderr
makeCmd.Stdout = os.Stdout
if err := makeCmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v in %v", makeCmd, rootDir)
}
oldPath := os.Getenv("PATH")
testPath := fmt.Sprintf("%s:%s", testDir, oldPath)
for _, setting := range []struct{ key, value string }{
{"PATH", testPath},
} {
if err := os.Setenv(setting.key, setting.value); err != nil {
return nil, fmt.Errorf("failed to set %v to %v", setting.key, setting.value)
}
}
return func() {
os.Setenv("PATH", oldPath)
os.RemoveAll(testDir)
}, nil
}
......@@ -43,11 +43,21 @@ func TestMain(m *testing.M) {
testCmd.Stderr = os.Stderr
if err := testCmd.Run(); err != nil {
log.Printf("Test setup: failed to run %v", testCmd)
os.Exit(-1)
}
}
os.Exit(m.Run())
cleanup, err := testhelper.BuildExecutables()
if err != nil {
log.Printf("Test setup: failed to build executables: %v", err)
os.Exit(1)
}
os.Exit(func() int {
defer cleanup()
return m.Run()
}())
}
func TestAllowedClone(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment