Link: github.com/twpayne/go-vfs

Package vfs provides an abstraction of the os and ioutil packages that is 
easy to test.

Key features:
- File system abstraction layer for commonly-used os and ioutil functions 
from the standard library.
- Powerful testing framework, vfst <https://github.com/twpayne/go-vfs/vfst>.
- Compatibility with github.com/spf13/afero.

Quick start:

Write your code that modifies the filesystem to take a vfs.FS interface 
<https://godoc.org/github.com/twpayne/go-vfs#FS> that includes almost all 
functions from the standard library's io and ioutil packages and make all 
changes through this interface. For example, replace os.Open with fs.Open.

Create a test virtual filesystem in a few lines of code, for example:

    fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
        "/home/user/.bashrc": "# contents of user's .bashrc\n",
    })
    defer cleanup()

You then pass fs (which implements vfs.FS) to the function that you want to 
test.

After your function has run, you can test properties of the changes, for 
example:

    vfst.RunTest(t, fs, "app_conf",
        vfst.PathTest("/home/user/app.conf",
            vfst.TestModeIsRegular,
            vfst.TestModePerm(0644),
            vfst.TestContentsString("app config"),
        ),
    )

There's a more complete example 
<https://godoc.org/github.com/twpayne/go-vfs/vfst#example-NewTestFS--Complex> 
in the docs.

Likely FAQs:

Why not use github.com/spf13/afero? Afero is great, but attempts to provide 
a complete virtual filesystem, including files, and so abstracts os.File to 
afero.File. Sadly, there are many bugs in afero.MemMapFs, and the project 
is not actively maintained. go-vfs is less ambitious, only wrapping the os 
and ioutil functions, and therefore should have fewer bugs.

I already use github.com/spf13/afero, can I use vfst to test my code? Yes, 
there's github.com/twpayne/go-vfsafero for exactly this purpose.

Feedback welcome,
Tom

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to