添加测试用例并运行go测试后,是什么导致“没有这样的文件或目录”? 问题

添加测试用例并运行go测试后,是什么导致“没有这样的文件或目录”? 问题,go,Go,在将另一个测试函数添加到运行go test-v的现有测试文件中之后。。。在添加另一个测试用例后,由于几个无此类文件或目录生成错误而失败。然而,错误消息似乎与更改无关 添加的测试用例可以在底部的相关代码部分找到 错误消息如下: open /tmp/go-build842273301/b118/vet.cfg: no such file or directory open /tmp/go-build842273301/b155/vet.cfg: no such file or directory #

在将另一个测试函数添加到运行go test-v的现有测试文件中之后。。。在添加另一个测试用例后,由于几个无此类文件或目录生成错误而失败。然而,错误消息似乎与更改无关

添加的测试用例可以在底部的相关代码部分找到

错误消息如下:

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
因此,一些包显示其构建失败:

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]
相关代码 再现错误是困难的 在我安装了go 1.15的Ubuntu机器上,只有当我再次克隆存储库或清理测试缓存时,才会出现错误

在本地运行Gitlab作业golang:alpine中使用的映像并运行相同的命令时,我无法每次都重现此错误。有时会发生,但大多数时候不会

我试过的 我曾尝试在go版本1.13、1.14和1.15之间切换,但每个版本都会产生相同的结果

切换到任何其他图像,如golang:latest、golang:1.14或golang:1.13也没有帮助

我曾尝试在谷歌上搜索发生的错误,但我没有找到任何相关的结果,也没有找到任何我尚未尝试过的解决方案


恢复所述提交将使测试再次通过。我还恢复了提交,并慢慢尝试再次手动引入更改。这使得问题再次出现。

我可以在MacOS上验证该行为

似乎os.TempDir有问题

您的测试是在我自己用os.Mkdir创建目录时运行的


您应该在Go存储库中创建一个问题。

我可以验证MacOS上的行为

似乎os.TempDir有问题

您的测试是在我自己用os.Mkdir创建目录时运行的


您应该在Go存储库中创建一个问题。

os.TempDir不会为您创建新的临时目录,而是返回系统的temp dir。通过在其上调用os.RemoveAll,您就失去了整个过程,包括构建和测试过程中使用的一些临时文件。

os.TempDir不会为您创建新的临时目录,而是返回系统的临时目录。通过在上面调用os.RemoveAll,您就彻底毁掉了整个过程,包括构建和测试过程中使用的一些临时文件。

您说得对!当删除os.TempDir并用os.MkDir替换它时,它实际上是有效的。你说得对!删除os.TempDir并将其替换为os.MkDir时,它实际上可以工作。
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
}