0.0.0 • Published 2 years ago

@zzzzbov/fs-snapshot v0.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

fs-snapshot

fsSnapshot(path) produces a snapshot of the filesystem at path recursively walking through child directories. This is useful for unit testing filesystem state.

The snapshot includes:

  • directories, represented as objects with each key being the name of the item in the directory.
  • files, represented as utf8 strings
  • symlinks, represented as arrays containing a single utf8 string of the target

⚠ Warning

Because fs-snapshot runs synchronously through all files recursively, and serializes all data to utf-8 strings, it is not recommended for use with:

  • large directories
  • large files
  • nonutf-8 data

fs-snapshot is best used when testing file creation in small batches within a temp directory or alongside a library such as mock-fs.

Example

If your filesystem includes the following structure:

CWD
 |
 +-- Foo
 |    |
 |    +-- Bar
 |         |
 |         +-- Baz.txt with "Hello, World"
 |
 +-- Lorem
      |
      +-- Ipsum symlink to "../Foo/Bar/Baz.txt"

running fsSnapshot() will produce a result of:

{
  "Foo": {
    "Bar": {
      "Baz.txt": "Hello, World!"
    }
  },
  "Lorem": {
    "Ipsum": ["../Foo/Bar/Baz.txt"]
  }
}

likewise running fsSnapshot("Foo") will produce a result of:

{
  "Bar": {
    "Baz.txt": "Hello, World!"
  }
}