go - 在 TravisCI 中的 Go 中创建带有扩展名的临时文件
问题描述
我正在测试我的应用程序,为此我需要创建具有特定扩展名的临时文件。我的目标是在临时目录中创建与此类似的文件example123.ac.json
。
为了做到这一点,我正在使用ioutil.TempDir
and ioutil.TempFile
。
这是我正在做的一个小例子。
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())
}
当我在我的 Mac 上本地运行测试时,从isgo test
输出以下内容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 中使用文字星号,但在我自己的计算机上运行时却没有。
如果有兴趣,这里是 TravisCI 日志的链接。
为了完整起见,这是我的.travis.yml
:
language: go
go:
- "1.10"
有人知道这里发生了什么吗?还是我错过了一些明显的东西?
解决方案
Go 1.11 添加了用随机值替换第一个星号的功能。看起来您正在使用 go 1.10 运行 Travis CI,因此不会替换星号。
推荐阅读
- visual-studio-code - 每次我尝试从 VSCode 复制(在 mac 上使用 command + c)时,我的光标进入 --NORMAL-- 模式并且我的光标变成了块光标,如何修复?
- python - 为什么 numpy 不创建 *NEW* 随机值?
- android - AAPT:错误:找不到属性类型(又名 com.example.androidtrivia:type)
- python - 如何将字符串中的 DataFrame 值映射到 Python 中的浮点数
- numpy - 如何使用 np.trapz 计算近似傅立叶系数
- javascript - const { default: _ } = await import('lodash'); 中使用的语法是什么?
- apache - Apache 使用 .htaccess 重写对 build/public 文件夹的请求
- html - CSS:为什么我的身体选择器不起作用?
- python - 如何使用变量和字符串的混合标记 Matplotlib 图?
- python - Python 代码中的错误。如何修复跳过?