Skip to content

Commit

Permalink
Attempt to make git-repo tests less flakey (#463)
Browse files Browse the repository at this point in the history
* Remove git test project fs race

* Assert git project is ready for testing

* No longer a map; name accordingly
  • Loading branch information
sourishkrout authored Jan 4, 2024
1 parent b899d85 commit a3055ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@ wasm:
cp $(GO_ROOT)/misc/wasm/wasm_exec.js $(WASM_OUTPUT)
GOOS=js GOARCH=wasm go build -o $(WASM_OUTPUT)/runme.wasm -ldflags="$(LDFLAGS)" ./web

.PHONY: test
test: PKGS ?= "./..."
test: build
.PHONY: test/execute
test/execute: PKGS ?= "./..."
test/execute: build test/prep-git-project
@TZ=UTC go test -ldflags="$(LDTESTFLAGS)" -timeout=30s -covermode=atomic -coverprofile=cover.out -coverpkg=./... $(PKGS)

.PHONY: test/prep-git-project
test/prep-git-project:
@cp -r -f internal/project/testdata/git-project/.git.bkp internal/project/testdata/git-project/.git
@cp -r -f internal/project/testdata/git-project/.gitignore.bkp internal/project/testdata/git-project/.gitignore
@cp -r -f internal/project/testdata/git-project/nested/.gitignore.bkp internal/project/testdata/git-project/nested/.gitignore

.PHONY: test
test: test/execute
@rm -r -f internal/project/testdata/git-project/.git
@rm -r -f internal/project/testdata/git-project/.gitignore
@rm -r -f internal/project/testdata/git-project/nested/.gitignore

.PHONY: test/update-snapshots
test/update-snapshots:
@TZ=UTC UPDATE_SNAPSHOTS=true go test ./...
Expand Down
3 changes: 1 addition & 2 deletions internal/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func TestExtractDataFromLoadEvent(t *testing.T) {
}

func TestMain(m *testing.M) {
testdata.PrepareGitProject()
defer testdata.CleanupGitProject()
testdata.AssertGitProject()

code := m.Run()
os.Exit(code)
Expand Down
3 changes: 1 addition & 2 deletions internal/project/projectservice/project_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
)

func TestMain(m *testing.M) {
testdata.PrepareGitProject()
defer testdata.CleanupGitProject()
testdata.AssertGitProject()

code := m.Run()
os.Exit(code)
Expand Down
44 changes: 10 additions & 34 deletions internal/project/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package testdata

import (
"log"
"os/exec"
"os"
"path/filepath"
"runtime"
)
Expand Down Expand Up @@ -33,48 +33,24 @@ func testdataDir() string {
return filepath.Dir(b)
}

func PrepareGitProject() {
prepareGitProject()
func AssertGitProject() {
assertGitProject()
}

func CleanupGitProject() {
cleanupGitProject()
}

// prepareGitProject copies .git.bkp from the ./testdata/git-project to .git in order to
// make ./testdata/git-project a valid git project.
func prepareGitProject() {
cleanupGitProject()

dir := GitProjectPath()

srcBkpFilesToDestFiles := map[string]string{
filepath.Join(dir, ".git.bkp"): filepath.Join(dir, ".git"),
filepath.Join(dir, ".gitignore.bkp"): filepath.Join(dir, ".gitignore"),
filepath.Join(dir, "nested", ".gitignore.bkp"): filepath.Join(dir, "nested", ".gitignore"),
}

for src, dest := range srcBkpFilesToDestFiles {
cmd := exec.Command("cp", "-f", "-r", src, dest)
if output, err := cmd.CombinedOutput(); err != nil {
log.Fatalf("failed to prepare %s: %v; output: %s", dest, err, output)
}
}
}

func cleanupGitProject() {
// assertGitProject checks that ./testdata/git-project is a valid git project.
// If it's not it will fail with a call to action to run the right make targets.
func assertGitProject() {
dir := GitProjectPath()

files := []string{
gitProjectDestFiles := []string{
filepath.Join(dir, ".git"),
filepath.Join(dir, ".gitignore"),
filepath.Join(dir, "nested", ".gitignore"),
}

for _, file := range files {
cmd := exec.Command("rm", "-r", "-f", file)
if output, err := cmd.CombinedOutput(); err != nil {
log.Fatalf("failed clean up %s: %v; output: %s", file, err, output)
for _, dest := range gitProjectDestFiles {
if _, err := os.Stat(dest); err != nil {
log.Fatalf("failed to assert %s: %v; please run maket target 'test/prepare-git-project'.", dest, err)
}
}
}

0 comments on commit a3055ef

Please sign in to comment.