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?

图片转代码服务由CSDN问答提供 功能建议

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

为此,我正在使用 ioutil.TempDir ioutil.TempFile

这里是我正在做的一个小小的虚构示例。

main.go:

 包main 
 
func main(){
} 
   
 
 

main_test.go: < / p>

 包main 
 
import(
“ fmt” 
“ io / ioutil” 
“ os” 
“ testing” 
)
 
func TestMain  (t * testing.T){
 dir,err:= ioutil.TempDir(“”,“ testing”)
如果err!= nil {
 t.Fatalf(“无法创建用于测试的临时目录”)  
} 
延迟os.RemoveAll(dir)
 
文件,错误:= ioutil.TempFile(dir,“ * .ac.json”)//创建扩展名为'.ac.json'的临时文件\  n如果err!= nil {
 t.Fatalf(“无法创建用于测试的临时文件”)
} 
 \  n fmt.Printf(“创建以下文件:%v 
”,file.Name())
} 
   
 
 

当我在本地运行测试时 我的Mac通过 go test fmt.Printf 输出以下内容是

  $ go test 
创建了以下内容 文件:/var/folders/tj/1_mxwn350_d2c5r9b_2zgy7m0000gn/T/testing566832606/900756901.ac.json
PASS
ok github.com/JonathonGore/travisci-bug 0.004s 
   
 
 <  p>所以它按预期工作,但是当我在TravisCI中运行它时,Printf语句输出以下内容: 
 
 

创建了以下文件:/tmp/testing768620677/*.ac .json193187872

由于某种原因,它在TravisCI内部使用了文字星号,但在我自己的计算机上运行时却未使用。

此处是TravisCI日志的链接(如果有兴趣的话)。 < p>为了完整性,这是我的 .travis.yml

 语言:  
go:
-“ 1.10” 
   
 
 

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    打赏 评论

相关推荐 更多相似问题