2024-01-02 15:12:47 +03:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2024-02-15 12:20:01 +03:00
|
|
|
"git.numtide.com/numtide/treefmt/config"
|
2024-01-15 13:46:49 +03:00
|
|
|
|
2024-01-02 15:12:47 +03:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
cp "github.com/otiai10/copy"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
func WriteConfig(t *testing.T, path string, cfg config.Config) {
|
2024-01-02 15:12:47 +03:00
|
|
|
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 TempExamples(t *testing.T) string {
|
|
|
|
tempDir := t.TempDir()
|
2024-02-15 12:20:01 +03:00
|
|
|
require.NoError(t, cp.Copy("../test/examples", tempDir), "failed to copy test data to temp dir")
|
2024-01-02 15:12:47 +03:00
|
|
|
return tempDir
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
func RecreateSymlink(t *testing.T, path string) error {
|
|
|
|
t.Helper()
|
|
|
|
src, err := os.Readlink(path)
|
|
|
|
require.NoError(t, err, "failed to read symlink")
|
|
|
|
require.NoError(t, os.Remove(path), "failed to remove symlink")
|
|
|
|
return os.Symlink(src, path)
|
|
|
|
}
|