在Go中,临时文件系统只对进程可见。

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

Temporary filesystem only visible to the process in Go

问题

我正在为一个命令行应用编写端到端测试,其中涉及文件操作(如cpmvrmtouchmkdir)。这些测试在我的本地环境中可以正常执行。问题出现在跨平台的服务器上执行时,文件操作会相互干扰。以下是问题:

  1. 在测试代码中使用shell命令似乎是错误的,我应该只编写程序化的命令吗?
  2. 如果上述问题的答案是肯定的,是否有一种作为“临时文件系统”的东西,只对进程可见?这样,当测试在其他平台上运行时,文件不会被搞乱。
  3. 互斥锁似乎也可以起作用,但会拖慢整个构建过程。

抱歉,这既是一个一般性的问题,又是一个具体的问题。我怀疑不会有一个完美的答案,但我很想听听一些建议和意见,因为我对Go和测试都很新。感谢帮助!

英文:

I am writing e2e tests for a command line app where I have to do file manipulation (such as cp, mv, rm, touch, and mkdir). The tests can execute just fine in my local environment. The problem occurs when they are executed on the server across platforms, where the file manipulation gets interfered with each other. Questions are:

  1. It seems wrong to have shell command in test code to begin with, should I just code the commands programmatically?
  2. If above is yes, is there something that would work as a "temporary file system" that is only visible for the process? So that when the tests run on other platforms, the files would not get messed up?
  3. It seems like mutex lock can work as well but it would slow down the entire build.

Sorry this is more of a general and specific question at the same time. Doubt there will be a perfect answer but would love to hear some suggestions and opinions as I am new in both Go and testing. Appreciate the help!

答案1

得分: 1

在你的代码中使用操作系统命令没有问题,否则这些命令将无法使用,尽管根据目标环境的不同可能不兼容,并且可能会有一些限制,就像你现在面临的情况一样。
一个可以作为文件命令层的工具是Afero,你可以使用它来模拟内存操作和S3资源。

英文:

There is nothing wrong in using OS commands in your code otherwise these will not be available to be used, although it may be incompatible depending on the target environment and as you are facing now may have some restrictions.
One tool that can work as a layer to the file commands is Afero, where you can use it even to simulate in-memory operations and S3 resources.

huangapple
  • 本文由 发表于 2022年11月8日 23:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/74363644.html
匿名

发表评论

匿名网友

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

确定