首页 > 解决方案 > 在 TravisCI 中的 Go 中创建带有扩展名的临时文件

问题描述

我正在测试我的应用程序,为此我需要创建具有特定扩展名的临时文件。我的目标是在临时目录中创建与此类似的文件example123.ac.json

为了做到这一点,我正在使用ioutil.TempDirand ioutil.TempFile

是我正在做的一个小例子。

main.go:

package main

func main() {
}

main_test.go:

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "testing"
)

func TestMain(t *testing.T) {
    dir, err := ioutil.TempDir("", "testing")
    if err != nil {
        t.Fatalf("unable to create temp directory for testing")
    }
    defer os.RemoveAll(dir)

    file, err := ioutil.TempFile(dir, "*.ac.json") // Create a temporary file with '.ac.json' extension
    if err != nil {
        t.Fatalf("unable to create temporary file for testing")
    }

    fmt.Printf("created the following file: %v\n", file.Name())
}

当我在我的 Mac 上本地运行测试时,从isgo test输出以下内容fmt.Printf

$ go test
created the following file: /var/folders/tj/1_mxwn350_d2c5r9b_2zgy7m0000gn/T/testing566832606/900756901.ac.json
PASS
ok      github.com/JonathonGore/travisci-bug    0.004s

所以它按预期工作,但是当我在 TravisCI 中运行它时,从 Printf 语句输出以下内容:

created the following file: /tmp/testing768620677/*.ac.json193187872

出于某种原因,它在 TravisCI 中使用文字星号,但在我自己的计算机上运行时却没有。

如果有兴趣,这里是 TravisCI 日志的链接。

为了完整起见,这是我的.travis.yml

language: go
go:
  - "1.10"

有人知道这里发生了什么吗?还是我错过了一些明显的东西?

标签: gotravis-ci

解决方案


Go 1.11 添加了用随机值替换第一个星号的功能。看起来您正在使用 go 1.10 运行 Travis CI,因此不会替换星号。


推荐阅读