1

The problem

After adding another test function to an existing test file running go test -v ./... fails due to several no such file or directory build errors after adding another test case. The error messages are seemingly unrelated to the changes, however.

The added test case can be found in the relevant code section at the bottom.

The error messages are:

open /tmp/go-build842273301/b118/vet.cfg: no such file or directory
open /tmp/go-build842273301/b155/vet.cfg: no such file or directory
# tornadowarnung.xyz/riotwatch/riot/static
vet: in tornadowarnung.xyz/riotwatch/riot/static, can't import facts for package "encoding/json": open $WORK/b036/vet.out: no such file or directory
# tornadowarnung.xyz/riotwatch/web/server/endpoint/static
vet: open $WORK/b121/vet.cfg: no such file or directory
open /tmp/go-build842273301/b115/vet.cfg: no such file or directory
open /tmp/go-build842273301/b001/vet.cfg: no such file or directory
# tornadowarnung.xyz/riotwatch/web/server
vet: open $WORK/b152/vet.cfg: no such file or directory
# tornadowarnung.xyz/riotwatch/web/server/endpoint/static
vet: open $WORK/b159/vet.cfg: no such file or directory

Because of that, some packages show their build failed:

FAIL    tornadowarnung.xyz/riotwatch/riot/static [build failed]
FAIL    tornadowarnung.xyz/riotwatch/web/server [build failed]
FAIL    tornadowarnung.xyz/riotwatch/web/server/endpoint [build failed]
FAIL    tornadowarnung.xyz/riotwatch/web/server/endpoint/static [build failed]

Relevant code

func TestLoader_ProfileIcon(t *testing.T) {
    tempDir := os.TempDir()
    l := Loader{
        profileIconPath: tempDir,
    }
    defer os.RemoveAll(tempDir)

    t.Run("returns expected content", func(t *testing.T) {
        want := bytes.NewBufferString("image data")
        fileName := "123456"
        if err := createTestFile(t, tempDir, fileName, want); err != nil {
            t.Fatal(err)
        }

        got, err := l.ProfileIcon(123456)
        if err != nil {
            t.Error(err)
        }

        if !reflect.DeepEqual(got, want) {
            t.Errorf("got %v, want %v", got, want)
        }
    })

    t.Run("does not panic on missing file", func(t *testing.T) {
        res, err := l.ProfileIcon(-1)

        if err == nil {
            t.Errorf("Expected an error but got error %v and result %v", nil, res)
        }
    })
}

func createTestFile(t *testing.T, tempDir string, fileName string, content *bytes.Buffer) error {
    t.Helper()
    f, err := os.Create(path2.Join(tempDir, fmt.Sprintf("%v.png", fileName)))
    if err != nil {
        return err
    }
    _, err = f.Write(content.Bytes())
    if err != nil {
        return err
    }
    return nil
}

Reproducing the error is difficult

On my Ubuntu machine having go 1.15 installed the error only occurs sometimes when I'm cloning the repository again or when I'm cleaning the test cache.

When running the image used in the Gitlab job golang:alpine locally and running the same commands I cannot reproduce this error every time. Sometimes it occurs but most of the time it doesn't.

What I've tried

I have tried to switch between go versions 1.13, 1.14, and 1.15 but every version yields the same result.

Switching to any other images like golang:latest, golang:1.14 or golang:1.13 doesn't help either.

I've tried googling for the error that occurs but I haven't found any results that are relevant or contain any solution I haven't already tried.

Reverting said commit will make the tests pass again. I've also reverted the commit and slowly tried to introduce the changes again manually. This makes the problems occur again.

0

2 Answers 2

2

os.TempDir doesn't create a new temporary directory for you, it returns the system's temp dir. By calling os.RemoveAll on it, you're blowing away the entire thing, including some scratch files used by the build and test process.

Sign up to request clarification or add additional context in comments.

Comments

1

I could validate the behaviour on a MacOS.

Seems like there is something wrong with the os.TempDir().

Your tests ran when I created the dir myself with os.Mkdir(...).

You should create an Issue in the Go repository.

1 Comment

You're right! When removing the os.TempDir() and replacing it with os.MkDir it actually works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.