Skip to content

Commit

Permalink
make Snapshot directory & file extension configurable (#39)
Browse files Browse the repository at this point in the history
* make Snapshot directory & file extension configurable

This let's users adjust the behaviour of abide's interaction with the filesystem,
exporting Snapshot Dir & Ext variables.

I've adjusted the LoadSnapshots function to directly set the internal allSnapshots
variable (this is the only way loadSnapshots was being used). This is intended as
a short-ish term stopgap, allowing users to adjust vars like SnapshotDir then call
LoadSnapshots() to have these changes take effect. I've added documentation of this
to the readme.md.

closes #37

* delay call to LoadDatasets with a sync.Once, add config to example pkg

turns out the call to LoadDatasets needs to be delayed if the path is to be
set outside of the abide package, otherwise abide will always create a dir called
`__snapshots__`, even when not in use. To accomplish this & try to keep the interface
simple, I've made 'loadSnapshots' private, wrapped it in a sync.Once, and added
calls within private methods. using sync the load function will only be called one time,
so the performance overhead of this is negligable.

Finally, I made `snapshotExt` private again, realizing that it's probably a bad idea to
encourage users to customize the file exension. Abide should proably enforce this
"new file format" across the board.
  • Loading branch information
b5 authored and sjkaliski committed May 18, 2018
1 parent 1aa6821 commit 9fd8f32
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 18 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ In some cases, attributes in a JSON response can by dynamic (e.g unique id's, da
```

When used with `AssertHTTPResponse`, for any response with `Content-Type: application/json`, the key-value pairs in `defaults` will be used to override the JSON response, allowing for consistent snapshot testing.


## Using custom `__snapshot__` directory

To write snapshots to a directory other than the default `__snapshot__`, adjust `abide.SnapshotDir` before any call to an Assert function. See `example/models` package for a working example

```golang
func init() {
abide.SnapshotDir = "testdata"
}
```
48 changes: 36 additions & 12 deletions abide.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ var (
allSnapshots snapshots
)

var (
// SnapshotsDir is the directory snapshots will be read to & written from
// relative directories are resolved to present-working-directory of the executing process
SnapshotsDir = "__snapshots__"
// snapshotsLoaded
snapshotsLoaded = sync.Once{}
)

const (
snapshotsDir = "__snapshots__"
snapshotExt = ".snapshot"
// snapshotExt is the file extension to use for a collection of snapshot records
snapshotExt = ".snapshot"
// snapshotSeparator deliniates records in the snapshots, not externally settable
snapshotSeparator = "/* snapshot: "
)

func init() {
// 1. Get arguments.
// Get arguments
args = getArguments()

// 2. Load snapshots.
allSnapshots, _ = loadSnapshots()
}

// Cleanup is an optional method which will execute cleanup operations
Expand Down Expand Up @@ -152,16 +158,26 @@ func encode(snaps snapshots) ([]byte, error) {
return bytes.TrimSpace(buf.Bytes()), nil
}

// loadSnapshots loads all snapshots in the current directory.
func loadSnapshots() (snapshots, error) {
// loadSnapshots loads all snapshots in the current directory, can only
// be called once
func loadSnapshots() (err error) {
snapshotsLoaded.Do(func() {
err = reloadSnapshots()
})
return err
}

// reloadSnapshots overwrites allSnapshots internal
// variable with the designated snapshots file
func reloadSnapshots() error {
dir, err := findOrCreateSnapshotDirectory()
if err != nil {
return nil, err
return err
}

files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
return err
}

paths := []string{}
Expand All @@ -172,11 +188,15 @@ func loadSnapshots() (snapshots, error) {
}
}

return parseSnapshotsFromPaths(paths)
allSnapshots, err = parseSnapshotsFromPaths(paths)
return err
}

// getSnapshot retrieves a snapshot by id.
func getSnapshot(id snapshotID) *snapshot {
if err := loadSnapshots(); err != nil {
panic(err)
}
return allSnapshots[id]
}

Expand All @@ -186,6 +206,10 @@ func createSnapshot(id snapshotID, value string) (*snapshot, error) {
return nil, errInvalidSnapshotID
}

if err := loadSnapshots(); err != nil {
return nil, err
}

dir, err := findOrCreateSnapshotDirectory()
if err != nil {
return nil, err
Expand Down Expand Up @@ -219,7 +243,7 @@ func findOrCreateSnapshotDirectory() (string, error) {
return "", errUnableToLocateTestPath
}

dir := filepath.Join(testingPath, snapshotsDir)
dir := filepath.Join(testingPath, SnapshotsDir)
_, err = os.Stat(dir)
if os.IsNotExist(err) {
err = os.Mkdir(dir, os.ModePerm)
Expand Down
11 changes: 6 additions & 5 deletions abide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func testingCleanup() {
os.RemoveAll(snapshotsDir)
os.RemoveAll(SnapshotsDir)
}

func testingSnapshot(id, value string) *snapshot {
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestCleanup(t *testing.T) {
t.Fatal(err)
}

allSnapshots, err = loadSnapshots()
err = loadSnapshots()
if err != nil {
t.Fatal(err)
}
Expand All @@ -57,7 +57,8 @@ func TestCleanup(t *testing.T) {
t.Fatal(err)
}

allSnapshots, err = loadSnapshots()
// call private reloadSnapshots to repeat once-executing function
err = reloadSnapshots()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -108,12 +109,12 @@ func TestLoadSnapshots(t *testing.T) {
t.Fatal(err)
}

sNew, err := loadSnapshots()
err = loadSnapshots()
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(s, sNew) {
if !reflect.DeepEqual(s, allSnapshots) {
t.Fatalf("Failed to load snapshots correctly.")
}
}
Expand Down
2 changes: 1 addition & 1 deletion assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func AssertReader(t *testing.T, id string, r io.Reader) {
}

func createOrUpdateSnapshot(t *testing.T, id, data string) {
var err error
snapshot := getSnapshot(snapshotID(id))

var err error
if snapshot == nil {
if !args.shouldUpdate {
t.Error(newSnapshotMessage(id, data))
Expand Down
6 changes: 6 additions & 0 deletions example/models/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
)

func TestMain(m *testing.M) {
// Optional: to set a custom directory to load snapshots from,
// set SnapshotsDir to a path relative to tests before calling
// any Assert functions
// here we change the default "__snapshots__" to "testdata"
abide.SnapshotsDir = "testdata"

exit := m.Run()
abide.Cleanup()
os.Exit(exit)
Expand Down
File renamed without changes.

0 comments on commit 9fd8f32

Please sign in to comment.