Go中的资源文件

huangapple go评论108阅读模式
英文:

Resources files in Go

问题

我有一些二进制文件,这些文件是运行一些_test测试用例所需的。

目前,这些文件的相对路径被硬编码到测试中,我不喜欢这样做,因为如果在文件夹层次结构中更改任何内容,测试就会中断,使得测试变得脆弱。

有没有处理这个问题的首选最佳实践,以及一般资源文件的处理方式?

英文:

I've got some binary files that are required to run some _test cases.

Currently the relative paths to these files are hardcoded into the tests, which I don't like since the tests break if you change anything in the folder hierarchy and make the tests fragile.

Is there a preferred best practice for handling this, and resource files in general?

答案1

得分: 5

测试资源名称可能是硬编码的,但路径不必如此。

  1. (09:13) jnml@fsc-r550:~/src/tmp/SO/13854048$ ls -a
  2. . .. a_test.go
  3. (09:13) jnml@fsc-r550:~/src/tmp/SO/13854048$ cat a_test.go
  4. package foo
  5. import (
  6. "testing"
  7. "io/ioutil"
  8. )
  9. func Test(t *testing.T) {
  10. b, err := ioutil.ReadFile("foo")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. t.Logf("resource content is: %s", b)
  15. }
  16. (09:13) jnml@fsc-r550:~/src/tmp/SO/13854048$ go test -v
  17. === RUN Test
  18. --- FAIL: Test (0.00 seconds)
  19. a_test.go:11: open foo: no such file or directory
  20. FAIL
  21. exit status 1
  22. FAIL tmp/SO/13854048 0.005s
  23. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$

正确,没有这样的资源(尚未创建)。让我们创建它。

  1. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$ echo blah > foo
  2. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$ go test -v
  3. === RUN Test
  4. --- PASS: Test (0.00 seconds)
  5. a_test.go:14: resource content is: blah
  6. PASS
  7. ok tmp/SO/13854048 0.007s
  8. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$ cd
  9. (09:14) jnml@fsc-r550:~$ go test -v tmp/SO/13854048
  10. === RUN Test
  11. --- PASS: Test (0.00 seconds)
  12. a_test.go:14: resource content is: blah
  13. PASS
  14. ok tmp/SO/13854048 0.005s
  15. (09:14) jnml@fsc-r550:~$

注意(上面的最后一次运行),即使从其他地方调用go test,当前工作目录也是正确的。

英文:

The testing resource name may be hard coded but the path doesn't have to be.

  1. (09:13) jnml@fsc-r550:~/src/tmp/SO/13854048$ ls -a
  2. . .. a_test.go
  3. (09:13) jnml@fsc-r550:~/src/tmp/SO/13854048$ cat a_test.go
  4. package foo
  5. import (
  6. "testing"
  7. "io/ioutil"
  8. )
  9. func Test(t *testing.T) {
  10. b, err := ioutil.ReadFile("foo")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. t.Logf("resource content is: %s", b)
  15. }
  16. (09:13) jnml@fsc-r550:~/src/tmp/SO/13854048$ go test -v
  17. === RUN Test
  18. --- FAIL: Test (0.00 seconds)
  19. a_test.go:11: open foo: no such file or directory
  20. FAIL
  21. exit status 1
  22. FAIL tmp/SO/13854048 0.005s
  23. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$

Correct, no such resource (yet). Let's create it.

  1. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$ echo blah > foo
  2. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$ go test -v
  3. === RUN Test
  4. --- PASS: Test (0.00 seconds)
  5. a_test.go:14: resource content is: blah
  6. PASS
  7. ok tmp/SO/13854048 0.007s
  8. (09:14) jnml@fsc-r550:~/src/tmp/SO/13854048$ cd
  9. (09:14) jnml@fsc-r550:~$ go test -v tmp/SO/13854048
  10. === RUN Test
  11. --- PASS: Test (0.00 seconds)
  12. a_test.go:14: resource content is: blah
  13. PASS
  14. ok tmp/SO/13854048 0.005s
  15. (09:14) jnml@fsc-r550:~$

Note (in the last run above) that the cwd is correct even when go test is invoked from elsewhere.

huangapple
  • 本文由 发表于 2012年12月13日 14:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/13854048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定