我正在测试我的应用程序,我需要创建具有特定扩展名的临时文件。我的目标是在temp目录中创建类似于这个example123.ac.json
的文件。
为了做到这一点,我使用ioutil.TempDir
和ioutil.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
输出以下内容
$ 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"
任何人都知道这里发生了什么?还是我错过了一些明显的东西?
用随机值替换第一个星号的功能是added in Go 1.11。看起来您正在使用go 1.10进行Travis CI运行,因此星号不会被替换。