testfile

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package testfile has test helpers that work by comparing files.

Example
package main

import (
	"strings"
	"testing"

	"github.com/carlmjohnson/be"
	"github.com/carlmjohnson/be/testfile"
)

func main() {
	t := &testing.T{}

	// Make some test data
	input := "Hello, World!"
	got := strings.ToUpper(input)
	// Write files
	testfile.Write(t, "example/upper.txt", got)
	// Read files
	file := testfile.Read(t, "example/upper.txt")
	// Do some testing
	be.Equal(t, file, got)
	// Use the equality helper
	testfile.Equal(t, "example/upper.txt", got)
	// If the files aren't equal,
	// got will be written to example/-failed-upper.txt

	// Or use JSON helpers for complex structs
	type testcase struct {
		Input, Output string
	}
	s := testcase{input, got}
	// Write out pretty printed JSON
	testfile.WriteJSON(t, "example/upper.json", s)
	// Read from a JSON file
	var s2 testcase
	testfile.ReadJSON(t, "example/upper.json", &s2)
	be.Equal(t, s, s2)
	// Test that s equals the contents of a file when serialized
	testfile.EqualJSON(t, "example/upper.json", s)

}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t testing.TB, wantFile, gotStr string)

Equal tests whether gotStr is equal to the contents of wantFile. If they are not equal, it writes gotStr to a file with -failed- prepended to its name and calls t.Fatalf.

If the environmental variable TESTFILE_UPDATE is set, an unequal file will be overwritten with gotStr, but the test will still fail to prevent accidental updates from going unnoticed.

func EqualJSON

func EqualJSON(t testing.TB, wantFile string, v any)

EqualJSON tests whether when v is mashaled as JSON, it is equal to the contents of wantFile. The contents of wantFile must have two spaces for indentation. EqualJSON just uses string equality and does not test for JSON equivalency. If they are not equal, it writes out a file with the contents of v and calls t.Fatalf. If there is an error, it calls t.Fatalf.

If the environmental variable TESTFILE_UPDATE is set, an unequal file will be overwritten with v, but the test will still fail to prevent accidental updates from going unnoticed.

func Equalish

func Equalish(t testing.TB, wantFile, gotStr string)

Equalish is like Equal, but it uses strings.TrimSpace before testing for equality.

If the environmental variable TESTFILE_UPDATE is set, an unequal file will be overwritten with gotStr, but the test will still fail to prevent accidental updates from going unnoticed.

func Ext added in v0.24.1

func Ext(path, ext string) string

Ext return path with its current extension stripped and ext added. It treats ext with and without a leading dot the same for simplicity of operation. If ext is "", path is returned with its current extension stripped off.

Example
package main

import (
	"fmt"

	"github.com/carlmjohnson/be/testfile"
)

func main() {
	for _, path := range []string{
		"foo.txt",
		"foo.bar/spam",
	} {
		fmt.Println(testfile.Ext(path, ""))
		fmt.Println(testfile.Ext(path, ".json"))
		fmt.Println(testfile.Ext(path, "gob"))
	}
}
Output:
foo
foo.json
foo.gob
foo.bar/spam
foo.bar/spam.json
foo.bar/spam.gob

func Read

func Read(t testing.TB, path string) string

Read returns the contents of file at path. It calls t.Fatalf if there is an error.

func ReadJSON

func ReadJSON(t testing.TB, path string, v any)

ReadJSON attempts to unmarshal the contents of a file at path into v. If there is an error, it calls t.Fatalf.

func Run

func Run(t *testing.T, glob string, f func(t *testing.T, match string))

Run a subtest for each file matching the provided glob pattern.

Example
package main

import (
	"strings"
	"testing"

	"github.com/carlmjohnson/be/testfile"
)

func main() {
	_ = func(t *testing.T) {
		// For each .txt file, start a sub-test
		testfile.Run(t, "testdata/*.txt", func(t *testing.T, path string) {
			// Read the file
			input := testfile.Read(t, path)

			// Do some conversion on it
			type myStruct struct{ Whatever string }
			got := myStruct{strings.ToUpper(input)}

			// See if the struct is equivalent to a .json file
			wantFile := testfile.Ext(path, ".json")
			testfile.EqualJSON(t, wantFile, got)

			// If it's not equivalent,
			// the got struct will be dumped
			// to a file named testdata/-failed-test-name.json
		})
	}
}

func Write

func Write(t testing.TB, path, data string)

Write the data to a file at path with 0644 permission bits. It attempts to create directories as needed. It calls t.Fatalf if there is an error.

func WriteJSON

func WriteJSON(t testing.TB, path string, v any)

WriteJSON writes v as JSON to a file at path. The JSON is created using two spaces for indentation. If there is an error, it calls t.Fatalf.

Types

This section is empty.

Jump to

Keyboard shortcuts

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