dpbrrczhlwbv849228 2018-09-01 15:59
浏览 36
已采纳

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

I am working on testing an application of mine, for which I need to create temporary files with specific extensions. My goal is to create files in a temp directory that look similar to this example123.ac.json.

In order to do this I am using ioutil.TempDir and ioutil.TempFile.

Here is a small contrived example of what I am doing.

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
", file.Name())
}

When I run the tests locally on my Mac with go test the following is outputted from the 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

So it works as expected but when I run it in TravisCI the following is outputted from the Printf statement:

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

For some reason it is using the literal asterisk inside TravisCI but not when running on my own computer.

Here is a link to the TravisCI logs if interested.

For completeness here is my .travis.yml:

language: go
go:
  - "1.10"

Anyone have any idea what is going on here? Or am I missing something obvious?

  • 写回答

1条回答 默认 最新

  • dso407787736 2018-09-01 16:40
    关注

    The feature of replacing the first asterisk with the random value was added in Go 1.11. It looks like you are using go 1.10 for your Travis CI runs so the asterisk won't be replaced.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办