Golang afero: Difference between revisions

From wikinotes
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
Afero defines and implements an interface to access the filesystem.<br>
Afero defines and implements an interface to access the filesystem.<br>
You can then pass in an abstraction of real os calls, or a stub stub interface you can make assertions against.
You can then pass in an abstraction of real os calls, or a stub stub interface you can make assertions against.<br>
It is maintained by the go developers.


= Documentation =
= Documentation =
<blockquote>
<blockquote>
{| class="wikitable"
{| class="wikitable"
|-
| api docs || https://pkg.go.dev/github.com/spf13/afero
|-
|-
| github || https://github.com/spf13/afero
| github || https://github.com/spf13/afero
Line 16: Line 19:
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// internal/fs/fs.go
import "github.com/spf13/afero"
import "github.com/spf13/afero"


appfs := afero.NewOsFs()     // real 'os' calls
Fs = Fs: afero.NewOsFs() // real 'os' calls
appfs := afero.NewMemMapFs() // memory backed fake filesystem
</syntaxhighlight>
// there are several additional options, like sftp, CopyOnWriteFs, ...
 
<syntaxhighlight lang="go">
// foo.go
package foo
import (
    "example.com/x/mypkg/internal/fs"
    "github.com/spf13/afero"
)


appfs.Create("/var/tmp/foo.txt') // either creates on disk, or in memory
func DoThing() {
    Os = afero.Afero{Fs: Fs}
    // os.* exposed on Fs
    Fs.Create("/var/tmp/foo.txt")
 
    // io, ioutil exposed in Afero{}
    Os.ReadFile("/var/tmp/foo.txt")
}
</syntaxhighlight>
</syntaxhighlight>


There are no assertions, when testing use the API to check the presence/contents of the file.
<syntaxhighlight lang="go">
// foo_test.go
package foo
 
func TestDoThing(t *testing.T) {
    var fs.Fs = afero.NewMemMapFs()
    t.Run("Does Thing", func(t *testing.T) {
        err := foo.DoThing()
        assert.Nil(t, err)
    })
}
</syntaxhighlight>
</blockquote><!-- Basics -->
</blockquote><!-- Basics -->
== Common Tasks ==
<blockquote>
<syntaxhighlight lang="go">
Fs := afero.NewOsFs()
Os := afero.Afero{Fs: Fs}
path, err := Os.TempDir(os.TempDir(), "my-prefix-")
exists, err := Os.Exists("/var/tmp/foo")
</syntaxhighlight>
</blockquote><!-- Common Tasks -->
</blockquote><!-- Usage -->
</blockquote><!-- Usage -->

Latest revision as of 19:43, 30 July 2022

Afero defines and implements an interface to access the filesystem.
You can then pass in an abstraction of real os calls, or a stub stub interface you can make assertions against.
It is maintained by the go developers.

Documentation

api docs https://pkg.go.dev/github.com/spf13/afero
github https://github.com/spf13/afero

Usage

Basics

// internal/fs/fs.go
import "github.com/spf13/afero"

Fs = Fs: afero.NewOsFs() // real 'os' calls
// foo.go
package foo
import (
    "example.com/x/mypkg/internal/fs"
    "github.com/spf13/afero"
)

func DoThing() {
    Os = afero.Afero{Fs: Fs}
    // os.* exposed on Fs
    Fs.Create("/var/tmp/foo.txt")

    // io, ioutil exposed in Afero{}
    Os.ReadFile("/var/tmp/foo.txt")
}
// foo_test.go
package foo

func TestDoThing(t *testing.T) {
    var fs.Fs = afero.NewMemMapFs()
    t.Run("Does Thing", func(t *testing.T) {
        err := foo.DoThing()
        assert.Nil(t, err)
    })
}

Common Tasks

Fs := afero.NewOsFs()
Os := afero.Afero{Fs: Fs}

path, err := Os.TempDir(os.TempDir(), "my-prefix-")
exists, err := Os.Exists("/var/tmp/foo")