GithubHelp home page GithubHelp logo

flaque / filet Goto Github PK

View Code? Open in Web Editor NEW
74.0 3.0 5.0 19 KB

๐Ÿ– A small temporary file utility for Go testing.

Home Page: https://flaque.github.io/filet/

License: Apache License 2.0

Go 100.00%
go golang testing library

filet's Introduction

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"))
}

filet's People

Contributors

bartgee avatar bartoszmajsak avatar flaque avatar jclebreton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

filet's Issues

Fails at parallel cleanup

I like to use Filet for tests, to create temporary structures. I use the examples from your readme. Today I started running my tests in parallal (t.Parallel()), and had weird issues. It appears Filet does not support parallel testing...

Consider the following test file (main_test.go):

package main_test

import (
	"testing"
	"time"

	"github.com/Flaque/filet"
	"github.com/stretchr/testify/assert"
)

func TestMain(t *testing.T) {
	theTest(t)
}

func TestMain2(t *testing.T) {
	theTest(t)
}

func theTest(t *testing.T) {
	t.Parallel()

	defer filet.CleanUp(t)

	filet.TmpDir(t, "")

	time.Sleep(200 * time.Millisecond)
	assert.Len(t, filet.Files, 1)
}

So we have a trivially simple TmpDir being created and cleaned up, and have to test functions doing that. (It is important that we have at least two functions, since one function is never run in parallel with itself!)

When running the tests, we can control the parallelism:

$ go test -parallel 1 -count 10 main_test.go 
ok  	command-line-arguments	4.023s
$ go test -parallel 2 -count 10 main_test.go 
--- FAIL: TestMain2 (0.20s)
    main_test.go:27: 
        	Error Trace:	main_test.go:27
        	            				main_test.go:16
        	Error:      	"[/tmp/dir587929604 /tmp/dir894240147]" should have 1 item(s), but has 2
        	Test:       	TestMain2
[...]
FAIL
FAIL	command-line-arguments	2.021s
FAIL

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.