feat: refactor test helpers
Better organize and allow for re-use across packages. Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
256c6774c6
commit
d51ccf918b
|
@ -1,105 +1,21 @@
|
||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"git.numtide.com/numtide/treefmt/internal/test"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.numtide.com/numtide/treefmt/internal/format"
|
"git.numtide.com/numtide/treefmt/internal/format"
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
"github.com/alecthomas/kong"
|
|
||||||
cp "github.com/otiai10/copy"
|
cp "github.com/otiai10/copy"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeConfig(t *testing.T, path string, cfg format.Config) {
|
|
||||||
t.Helper()
|
|
||||||
f, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create a new config file: %v", err)
|
|
||||||
}
|
|
||||||
encoder := toml.NewEncoder(f)
|
|
||||||
if err = encoder.Encode(cfg); err != nil {
|
|
||||||
t.Fatalf("failed to write to config file: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newKong(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
|
|
||||||
t.Helper()
|
|
||||||
options = append([]kong.Option{
|
|
||||||
kong.Name("test"),
|
|
||||||
kong.Exit(func(int) {
|
|
||||||
t.Helper()
|
|
||||||
t.Fatalf("unexpected exit()")
|
|
||||||
}),
|
|
||||||
}, options...)
|
|
||||||
parser, err := kong.New(cli, options...)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
return parser
|
|
||||||
}
|
|
||||||
|
|
||||||
func tempFile(t *testing.T, path string) *os.File {
|
|
||||||
t.Helper()
|
|
||||||
file, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temporary file: %v", err)
|
|
||||||
}
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
|
|
||||||
func cmd(t *testing.T, args ...string) ([]byte, error) {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
// create a new kong context
|
|
||||||
p := newKong(t, &Cli)
|
|
||||||
ctx, err := p.Parse(args)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
tempDir := t.TempDir()
|
|
||||||
tempOut := tempFile(t, filepath.Join(tempDir, "combined_output"))
|
|
||||||
|
|
||||||
// capture standard outputs before swapping them
|
|
||||||
stdout := os.Stdout
|
|
||||||
stderr := os.Stderr
|
|
||||||
|
|
||||||
// swap them temporarily
|
|
||||||
os.Stdout = tempOut
|
|
||||||
os.Stderr = tempOut
|
|
||||||
|
|
||||||
// run the command
|
|
||||||
if err = ctx.Run(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset and read the temporary output
|
|
||||||
if _, err = tempOut.Seek(0, 0); err != nil {
|
|
||||||
return nil, fmt.Errorf("%w: failed to reset temp output for reading", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
out, err := io.ReadAll(tempOut)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%w: failed to read temp output", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// swap outputs back
|
|
||||||
os.Stdout = stdout
|
|
||||||
os.Stderr = stderr
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAllowMissingFormatter(t *testing.T) {
|
func TestAllowMissingFormatter(t *testing.T) {
|
||||||
as := require.New(t)
|
as := require.New(t)
|
||||||
|
|
||||||
tempDir := t.TempDir()
|
tempDir := t.TempDir()
|
||||||
configPath := tempDir + "/treefmt.toml"
|
configPath := tempDir + "/treefmt.toml"
|
||||||
|
|
||||||
writeConfig(t, configPath, format.Config{
|
test.WriteConfig(t, configPath, format.Config{
|
||||||
Formatters: map[string]*format.Formatter{
|
Formatters: map[string]*format.Formatter{
|
||||||
"foo-fmt": {
|
"foo-fmt": {
|
||||||
Command: "foo-fmt",
|
Command: "foo-fmt",
|
||||||
|
@ -122,7 +38,7 @@ func TestSpecifyingFormatters(t *testing.T) {
|
||||||
|
|
||||||
as.NoError(cp.Copy("../../test/examples", tempDir), "failed to copy test data to temp dir")
|
as.NoError(cp.Copy("../../test/examples", tempDir), "failed to copy test data to temp dir")
|
||||||
|
|
||||||
writeConfig(t, configPath, format.Config{
|
test.WriteConfig(t, configPath, format.Config{
|
||||||
Formatters: map[string]*format.Formatter{
|
Formatters: map[string]*format.Formatter{
|
||||||
"elm": {
|
"elm": {
|
||||||
Command: "echo",
|
Command: "echo",
|
||||||
|
|
69
internal/cli/helpers_test.go
Normal file
69
internal/cli/helpers_test.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.numtide.com/numtide/treefmt/internal/test"
|
||||||
|
"github.com/alecthomas/kong"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newKong(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
|
||||||
|
t.Helper()
|
||||||
|
options = append([]kong.Option{
|
||||||
|
kong.Name("test"),
|
||||||
|
kong.Exit(func(int) {
|
||||||
|
t.Helper()
|
||||||
|
t.Fatalf("unexpected exit()")
|
||||||
|
}),
|
||||||
|
}, options...)
|
||||||
|
parser, err := kong.New(cli, options...)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return parser
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmd(t *testing.T, args ...string) ([]byte, error) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
// create a new kong context
|
||||||
|
p := newKong(t, &Cli)
|
||||||
|
ctx, err := p.Parse(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
tempDir := t.TempDir()
|
||||||
|
tempOut := test.TempFile(t, filepath.Join(tempDir, "combined_output"))
|
||||||
|
|
||||||
|
// capture standard outputs before swapping them
|
||||||
|
stdout := os.Stdout
|
||||||
|
stderr := os.Stderr
|
||||||
|
|
||||||
|
// swap them temporarily
|
||||||
|
os.Stdout = tempOut
|
||||||
|
os.Stderr = tempOut
|
||||||
|
|
||||||
|
// run the command
|
||||||
|
if err = ctx.Run(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// reset and read the temporary output
|
||||||
|
if _, err = tempOut.Seek(0, 0); err != nil {
|
||||||
|
return nil, fmt.Errorf("%w: failed to reset temp output for reading", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := io.ReadAll(tempOut)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%w: failed to read temp output", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// swap outputs back
|
||||||
|
os.Stdout = stdout
|
||||||
|
os.Stderr = stderr
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
29
internal/test/temp.go
Normal file
29
internal/test/temp.go
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.numtide.com/numtide/treefmt/internal/format"
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WriteConfig(t *testing.T, path string, cfg format.Config) {
|
||||||
|
t.Helper()
|
||||||
|
f, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create a new config file: %v", err)
|
||||||
|
}
|
||||||
|
encoder := toml.NewEncoder(f)
|
||||||
|
if err = encoder.Encode(cfg); err != nil {
|
||||||
|
t.Fatalf("failed to write to config file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TempFile(t *testing.T, path string) *os.File {
|
||||||
|
t.Helper()
|
||||||
|
file, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create temporary file: %v", err)
|
||||||
|
}
|
||||||
|
return file
|
||||||
|
}
|
Reference in New Issue
Block a user