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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog