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

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.