filet

package module
v0.0.0-...-45f6844 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2020 License: Apache-2.0 Imports: 4 Imported by: 9

README

Build Status Go Report Card

Filet 🍖

A small temporary file utility for Go testing. Built on Afero and heavily inspired by the way Afero tests itself.

Install via: $ go get github.com/Flaque/filet

Then import with:

import (
  filet "github.com/Flaque/filet"
)

Quick overview at GoDocs.

Creating temporaries

Creating temporary directories:
func TestFoo(t *testing.T) {
  filet.TmpDir(t, "") // Creates a temporary dir with no parent directory
  filet.TmpDir(t, "myPath") // Creates a temporary dir at `myPath`
}
Creating temporary files:
func TestFoo(t *testing.T) {
  filet.TmpFile(t, "", "") // Creates a temporary file with no parent dir

  // Creates a temporary file with string "some content"
  filet.TmpFile(t, "", "some content")

  // Creates a temporary file with string "some content"
  filet.TmpFile(t, "myDir", "some content")
}
Creating specified files:
func TestFoo(t *testing.T) {
  filet.File(t, "conf.yaml", "") // Creates a specified file

  // Creates a specified file with string "some content"
  filet.File(t, "/tmp/conf.yaml", "some content")
}
Cleaning up after yourself:

Filet lets you clean up after your files with CleanUp, which is most cleanly used at the beginning of a function with defer. For example:

func TestFoo(t *testing.T) {
  defer filet.CleanUp(t)

  // Create a bunch of temporary stuff here
}

CleanUp will call t.Error if something goes wrong when removing the file.

You can also access the Files itself if you want to add a specificly named file to the cleanup list.

filet.Files = append(filet.Files, "path/to/my/named/file")

Helpers

Filet comes with a few helper functions that are useful for working with your temporary files.

Checking Existence

You can test if a file exists if you want.

func TestFoo(t *testing.T) {
  myBool := filet.Exists(t, "path/to/my/file")
}
Checking DirContains

You can test if a folder contains a file or another directory.

func TestFoo(t *testing.T) {
  myBool := filet.DirContains(t, "path/to/myFolder", "myFile")
}
Checking if a FileSays what you want

You can check if a file's contents says what you want it to with FileSays.

func TestFoo(t *testing.T) {
  myBool := filet.FileSays(t, "path/to/my/dir", []byte("my content here"))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Files []string

Files keeps track of files that we've used so we can clean up.

Functions

func CleanUp

func CleanUp(t TestReporter)

CleanUp removes all files in our test registry and calls `t.Error` if something goes wrong.

func DirContains

func DirContains(t TestReporter, dir string, path string) bool

DirContains returns true if the dir contains the path. Calls t.Error if something goes wrong while checking.

func Exists

func Exists(t TestReporter, path string) bool

Exists returns true if the file exists. Calls t.Error if something goes wrong while checking.

func File

func File(t TestReporter, path string, content string) afero.File

File Creates a specified file for us to use when testing

func FileSays

func FileSays(t TestReporter, path string, expected []byte) bool

FileSays returns true if the file at the path contains the expected byte array content.

func TmpDir

func TmpDir(t TestReporter, dir string) string

TmpDir Creates a tmp directory for us to use.

func TmpFile

func TmpFile(t TestReporter, dir string, content string) afero.File

TmpFile Creates a tmp file for us to use when testing

Types

type TestReporter

type TestReporter interface {
	Error(args ...interface{})
}

TestReporter can be used to report test failures. It is satisfied by the standard library's *testing.T.

Jump to

Keyboard shortcuts

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