在TravisCI中的Go中创建带有扩展名的临时文件

时间:2018-09-01 15:59:16

标签: go travis-ci

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

为此,我使用ioutil.TempDirioutil.TempFile

Here是我所做的一个人为设计的小例子。

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())
}

当我使用go test在Mac上本地运行测试时,fmt.Printf is输出以下内容

$ 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中使用了文字星号,但在我自己的计算机上运行时却未使用。

Here是TravisCI日志的链接(如果有兴趣的话)。

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

language: go
go:
  - "1.10"

任何人都知道这里发生了什么吗?还是我缺少明显的东西?

1 个答案:

答案 0 :(得分:3)

用随机值替换第一个星号的功能是added in Go 1.11。看来您在Travis CI运行中使用的是go 1.10,因此不会替换星号。

相关问题