如何在Go中模拟/抽象文件系统?

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

How to mock/abstract filesystem in go?

问题

我想要能够记录我的Go应用程序对底层操作系统发出的每个写入/读取操作,并且(如果可能的话)完全替换文件系统,只在内存中存在。

这是否可能?如何实现?也许有现成的解决方案吗?

英文:

I would like to be able to log every write/read that my go app issues to the underlying OS, and also (if it's possible) completely replace FS with one that resides only in memory.

Is it possible? How? Maybe there is a ready-to-Go solution?

答案1

得分: 47

这是直接从Andrew Gerrand的10 things you (probably) don't know about Go中摘录的:

var fs fileSystem = osFS{}

type fileSystem interface {
    Open(name string) (file, error)
    Stat(name string) (os.FileInfo, error)
}

type file interface {
    io.Closer
    io.Reader
    io.ReaderAt
    io.Seeker
    Stat() (os.FileInfo, error)
}

// osFS使用本地磁盘实现fileSystem接口。
type osFS struct{}

func (osFS) Open(name string) (file, error)        { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }

为了使其工作,您需要编写代码以接受一个fileSystem参数(可以将其嵌入到其他类型中,或者让nil表示默认文件系统)。

英文:

This is straight from Andrew Gerrand's 10 things you (probably) don't know about Go:

var fs fileSystem = osFS{}

type fileSystem interface {
	Open(name string) (file, error)
	Stat(name string) (os.FileInfo, error)
}

type file interface {
	io.Closer
	io.Reader
	io.ReaderAt
	io.Seeker
	Stat() (os.FileInfo, error)
}

// osFS implements fileSystem using the local disk.
type osFS struct{}

func (osFS) Open(name string) (file, error)        { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }

For this to work, you will need to write your code to take a fileSystem argument (maybe embed it in some other type, or let nil denote the default filesystem).

答案2

得分: 33

对于那些在测试期间想要模拟文件系统的问题的人来说,可以查看@spf13的Afero库,https://github.com/spf13/afero。它做的事情与被接受的答案相同,但文档和示例更好。

英文:

For those looking to solve the problem of mocking out your filesystem during testing, checkout @spf13's Afero library, https://github.com/spf13/afero. It does everything that the accepted answer does, but with better documentation and examples.

答案3

得分: 21

你可以使用testing/fstest包:

package main
import "testing/fstest"

func main() {
   fs := fstest.MapFS{
      "hello.txt": {
         Data: []byte("hello, world"),
      },
   }
   data, err := fs.ReadFile("hello.txt")
   if err != nil {
      panic(err)
   }
   println(string(data) == "hello, world")
}

https://godocs.io/testing/fstest

英文:

You can use the testing/fstest package:

package main
import "testing/fstest"

func main() {
   fs := fstest.MapFS{
      "hello.txt": {
         Data: []byte("hello, world"),
      },
   }
   data, err := fs.ReadFile("hello.txt")
   if err != nil {
      panic(err)
   }
   println(string(data) == "hello, world")
}

https://godocs.io/testing/fstest

答案4

得分: 1

只是因为这个问题在谷歌搜索时排名很高:

我不知道如何记录读写操作,但对于仅驻留在内存中的文件系统,我找到了blang/vfs。我没有在生产中使用过,它说它是alpha版本,接口可能会改变。使用它要自担风险。

我想你可以实现它来记录读写操作。

英文:

Just because this question pops up pretty high when googling for this matter:

I don't know about logging reads and writes, but for a filesystem residing only in memory, I've found blang/vfs. I haven't used in production, and it says it's alpha and interfaces are likely to change. Use it at your own risk.

I suppose you could implement it to log reads and writes.

huangapple
  • 本文由 发表于 2013年5月25日 03:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/16742331.html
匿名

发表评论

匿名网友

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

确定