filesnap

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package filesnap simplifies reading and writing snapshots of files and their contents.

Here is an example of how to use this package during testing:

import (
	"testing"

	"github.com/echocrow/fsnap/filesnap"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type fsf = filesnap.Files

func TestMyFilesConcat(t *testing.T) {
	dir := t.TempDir()

	// Source files snapshot.
	require.NoError(t, fsf{
		"a_0.txt": []byte("prefix"),
		"a_1.txt": []byte("suffix"),
	}.Write(dir))

	// Expected files snapshot.
	want := fsf{
		"a.txt": []byte("prefix\nsuffix"),
	}

	err := MyFilesConcat(dir)
	assert.NoError(t, err)

	// Actual files snapshot.
	got, err := filesnap.Read(dir, -1)
	require.NoError(t, err)
	assert.Equal(t, want, got)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Files

type Files map[string][]byte

Files represents a snapshot of files in a directory and its subdirectories.

Keys represent the subpath of a file and values hold the contents of the respective file.

func Read

func Read(dir string, n int) (Files, error)

Read scans a directory and returs its Files.

If n < 0, Read will scan all subdirectories.

If n >= 0, Read will descend at most n directory levels below directory dir.

func ReadFS

func ReadFS(fsys fs.FS, dir string, n int) (Files, error)

ReadFS scans a fsys directory dir and returs its Files.

See Read().

func (Files) Write

func (f Files) Write(dir string) error

Write writes Files f into directory dir, creating new files and folders accordingly.

If a file already exists, its contents will be overwritten.

Jump to

Keyboard shortcuts

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