filebuilder

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 8 Imported by: 0

README

filebuilder - A quick uitility for creating file structures and comparing them

go get github.com/jackmordaunt/filebuilder

Why?

I had trouble trying to mock out the filesystem for command line apps. Creating the files and comparing the files procedurally is tedious. I wanted a declarative way to specify "these are the files I have", "these are the files I want", and "compare this directory with that directory".

To that end, I don't know if this is the 'best' way to do it, or even a good way to do it.

Code suggestions and project suggestions very welcome.

func main() {

        // Declare the files you want, you can nest with relative paths or use 
        // a flat list, or both.
        entries := []filebuilder.Entry{
                filebuilder.File{Path: "foo/bar/baz.exe"},
                filebuilder.File{Path: "foo/baz.exe"},
                filebuilder.File{Path: "baz.exe"},
                filebuilder.Dir{Path: "bar", Entries: []filebuilder.Entry{
                        filebuilder.File{Path: "baz.txt", Content: []byte("foo")},
                        filebuilder.File{Path: "foo/bar/baz.txt", Content: []byte("bar")},
                }},
        }
        
        // Grab a filesystem implementation, or use the default by passing in nil.
        // The optional root will be the parent of the provided entries.
        fs := afero.NewMemMapFs()
        cleanup, err := filebuilder.Build(fs, "parent", entries...)
        if err != nil {
                log.Fatalf("failed creating entries: %v", err)
        }

        // Optional cleanup func which erases all files created. 
        defer func() {
                if err := cleanup(); err != nil {
                        log.Fatalf("failed cleanup of files: %v", err)
                }
        }()

        // fs is stateful, you can build up the file tree over multiple calls.
        _, err = filebuilder.Build(fs, "inside/this/folder", entries)
        if err != nil {
                log.Fatalf("failed creating entries: %v", err)
        }

        // Compare the directories.
        diff, ok, err := filebuilder.CompareDirectories(fs, "parent", "inside/this/folder")
        if err != nil {
                log.Fatalf("error while comparing directories: %v", err)
        }
        if !ok {
                log.Printf("directories are not equivalent, %v\n", diff)
        }
}

Documentation

Overview

Package filebuilder provides a simple API for declaratively creating files and directories. Useful for mocking the file system during testing for CLI apps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanFunc

type CleanFunc func() error

CleanFunc removes the files created.

func Build

func Build(fs afero.Fs, root string, entries ...Entry) (CleanFunc, error)

Build creates a file structure based on the entries given, resolved against root. fs defaults to package "os" (afero.OsFs). If package "os" is used without a specified root an error is returned to avoid operations on the host OS's root.

type Diff

type Diff struct {
	// contains filtered or unexported fields
}

Diff is used to record and compute the differences between two sets of file paths. Generally speaking any two string slices can be used, however this object is designed specifically for lists of file paths.

func CompareDirectories

func CompareDirectories(fs afero.Fs, left, right string) (difference *Diff, ok bool, err error)

CompareDirectories creates a diff of any differences found. If the directories are not comparable, ok == false. If the directories are comparable, ok == true.

func (*Diff) Diff

func (d *Diff) Diff() map[string][]string

Diff returns a map containing the contents of each list that is not in the other.

func (*Diff) EqualTo

func (d *Diff) EqualTo(other *Diff) bool

EqualTo reports whether d and other are considered equal.

func (*Diff) IsEmpty

func (d *Diff) IsEmpty() bool

IsEmpty reports whether there is a non-zero amount of diffs.

func (*Diff) String

func (d *Diff) String() string

type Dir

type Dir = Directory

Dir represents a folder.

type Directory

type Directory struct {
	Path    string
	Entries []Entry
}

Directory represents a folder.

func (Directory) Create

func (d Directory) Create(fs afero.Fs, base string) error

Create the directory at the given path with the given entries.

type Entries

type Entries []Entry

Entries represents a list of entries. This wrapper allows []Entry to satisfy the Entry interface.

func (Entries) Create

func (entries Entries) Create(fs afero.Fs, root string) error

Create all entries in the list.

type Entry

type Entry interface {
	// Create implementations should default to `os` when fs is nil.
	// If base is empty, the entry's path is interpreted as absolute.
	Create(fs afero.Fs, base string) error
}

Entry represents a file system entry, typically a file or a directory.

type File

type File struct {
	Path    string
	Content []byte
}

File represents a file.

func (File) Create

func (f File) Create(fs afero.Fs, base string) error

Create the file at the given path with the given content.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL