2023-12-23 18:00:39 +03:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2024-04-26 12:27:11 +03:00
|
|
|
"bufio"
|
2024-01-03 11:08:57 +03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-01-11 23:52:22 +03:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2024-04-26 12:27:11 +03:00
|
|
|
"regexp"
|
2023-12-23 18:00:39 +03:00
|
|
|
"testing"
|
|
|
|
|
2024-02-15 12:20:01 +03:00
|
|
|
config2 "git.numtide.com/numtide/treefmt/config"
|
|
|
|
"git.numtide.com/numtide/treefmt/format"
|
|
|
|
"git.numtide.com/numtide/treefmt/test"
|
2024-01-15 13:46:49 +03:00
|
|
|
|
2024-01-11 23:52:22 +03:00
|
|
|
"github.com/go-git/go-billy/v5/osfs"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/cache"
|
|
|
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2023-12-23 18:00:39 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2024-05-06 18:05:10 +03:00
|
|
|
func TestCpuProfile(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
tempDir := test.TempExamples(t)
|
|
|
|
|
|
|
|
// capture current cwd, so we can replace it after the test is finished
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
as.NoError(err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
// return to the previous working directory
|
|
|
|
as.NoError(os.Chdir(cwd))
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = cmd(t, "-C", tempDir, "--allow-missing-formatter", "--cpu-profile", "cpu.pprof")
|
|
|
|
as.NoError(err)
|
|
|
|
as.FileExists(filepath.Join(tempDir, "cpu.pprof"))
|
2024-05-07 19:18:49 +03:00
|
|
|
_, err = os.Stat(filepath.Join(tempDir, "cpu.pprof"))
|
2024-05-06 18:05:10 +03:00
|
|
|
as.NoError(err)
|
|
|
|
}
|
|
|
|
|
2023-12-23 18:00:39 +03:00
|
|
|
func TestAllowMissingFormatter(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
2024-04-26 12:27:11 +03:00
|
|
|
tempDir := test.TempExamples(t)
|
2023-12-23 18:00:39 +03:00
|
|
|
configPath := tempDir + "/treefmt.toml"
|
|
|
|
|
2024-02-15 12:20:01 +03:00
|
|
|
test.WriteConfig(t, configPath, config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2023-12-23 18:00:39 +03:00
|
|
|
"foo-fmt": {
|
|
|
|
Command: "foo-fmt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-12-25 15:26:18 +03:00
|
|
|
_, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-15 13:46:49 +03:00
|
|
|
as.ErrorIs(err, format.ErrCommandNotFound)
|
2023-12-25 15:26:18 +03:00
|
|
|
|
|
|
|
_, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "--allow-missing-formatter")
|
2023-12-23 18:00:39 +03:00
|
|
|
as.NoError(err)
|
2023-12-25 15:26:18 +03:00
|
|
|
}
|
2023-12-23 18:00:39 +03:00
|
|
|
|
2023-12-25 15:26:18 +03:00
|
|
|
func TestSpecifyingFormatters(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
2024-05-10 13:43:28 +03:00
|
|
|
cfg := config2.Config{
|
2024-02-15 12:20:01 +03:00
|
|
|
Formatters: map[string]*config2.Formatter{
|
2023-12-25 15:26:18 +03:00
|
|
|
"elm": {
|
2024-05-01 21:03:26 +03:00
|
|
|
Command: "touch",
|
2024-05-10 13:43:28 +03:00
|
|
|
Options: []string{"-m"},
|
2023-12-25 15:26:18 +03:00
|
|
|
Includes: []string{"*.elm"},
|
|
|
|
},
|
|
|
|
"nix": {
|
2024-05-01 21:03:26 +03:00
|
|
|
Command: "touch",
|
2024-05-10 13:43:28 +03:00
|
|
|
Options: []string{"-m"},
|
2023-12-25 15:26:18 +03:00
|
|
|
Includes: []string{"*.nix"},
|
|
|
|
},
|
|
|
|
"ruby": {
|
2024-05-01 21:03:26 +03:00
|
|
|
Command: "touch",
|
2024-05-10 13:43:28 +03:00
|
|
|
Options: []string{"-m"},
|
2023-12-25 15:26:18 +03:00
|
|
|
Includes: []string{"*.rb"},
|
|
|
|
},
|
|
|
|
},
|
2024-05-10 13:43:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var tempDir, configPath string
|
2023-12-25 15:26:18 +03:00
|
|
|
|
2024-05-10 13:43:28 +03:00
|
|
|
// we reset the temp dir between successive runs as it appears that touching the file and modifying the mtime can
|
|
|
|
// is not granular enough between assertions in quick succession
|
|
|
|
setup := func() {
|
|
|
|
tempDir = test.TempExamples(t)
|
|
|
|
configPath = tempDir + "/treefmt.toml"
|
|
|
|
test.WriteConfig(t, configPath, cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
setup()
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2023-12-23 18:00:39 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 3, 3)
|
2023-12-25 15:26:18 +03:00
|
|
|
|
2024-05-10 13:43:28 +03:00
|
|
|
setup()
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "elm,nix")
|
2023-12-25 15:26:18 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 2, 2)
|
2023-12-25 15:26:18 +03:00
|
|
|
|
2024-05-10 13:43:28 +03:00
|
|
|
setup()
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "ruby,nix")
|
2023-12-25 15:26:18 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 2, 2)
|
2023-12-25 15:26:18 +03:00
|
|
|
|
2024-05-10 13:43:28 +03:00
|
|
|
setup()
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "nix")
|
2023-12-25 15:26:18 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 1, 1)
|
2023-12-25 15:26:18 +03:00
|
|
|
|
|
|
|
// test bad names
|
2024-05-10 13:43:28 +03:00
|
|
|
setup()
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "foo")
|
2023-12-25 15:26:18 +03:00
|
|
|
as.Errorf(err, "formatter not found in config: foo")
|
2023-12-23 18:00:39 +03:00
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--formatters", "bar,foo")
|
2023-12-25 15:26:18 +03:00
|
|
|
as.Errorf(err, "formatter not found in config: bar")
|
2023-12-23 18:00:39 +03:00
|
|
|
}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
func TestIncludesAndExcludes(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
2024-05-01 21:03:26 +03:00
|
|
|
configPath := tempDir + "/touch.toml"
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
// test without any excludes
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-02 15:12:47 +03:00
|
|
|
"echo": {
|
|
|
|
Command: "echo",
|
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
// globally exclude nix files
|
2024-01-15 13:46:49 +03:00
|
|
|
cfg.Global.Excludes = []string{"*.nix"}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 30, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
// add haskell files to the global exclude
|
2024-01-15 13:46:49 +03:00
|
|
|
cfg.Global.Excludes = []string{"*.nix", "*.hs"}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 24, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
echo := cfg.Formatters["echo"]
|
2024-01-02 17:57:21 +03:00
|
|
|
|
2024-01-02 15:12:47 +03:00
|
|
|
// remove python files from the echo formatter
|
2024-01-02 17:57:21 +03:00
|
|
|
echo.Excludes = []string{"*.py"}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 22, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
// remove go files from the echo formatter
|
2024-01-02 17:57:21 +03:00
|
|
|
echo.Excludes = []string{"*.py", "*.go"}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 21, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
// adjust the includes for echo to only include elm files
|
2024-01-02 17:57:21 +03:00
|
|
|
echo.Includes = []string{"*.elm"}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 1, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
|
|
|
|
// add js files to echo formatter
|
2024-01-02 17:57:21 +03:00
|
|
|
echo.Includes = []string{"*.elm", "*.js"}
|
2024-01-02 15:12:47 +03:00
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
2024-01-02 15:12:47 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 2, 0)
|
2024-01-02 15:12:47 +03:00
|
|
|
}
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
2024-05-01 21:03:26 +03:00
|
|
|
configPath := tempDir + "/touch.toml"
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// test without any excludes
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-03 11:08:57 +03:00
|
|
|
"echo": {
|
|
|
|
Command: "echo",
|
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 11:08:57 +03:00
|
|
|
out, err := cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-02-15 12:20:01 +03:00
|
|
|
|
|
|
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
|
|
|
as.NoError(err)
|
2024-05-01 13:15:39 +03:00
|
|
|
assertFormatted(t, as, out, 0)
|
2024-02-15 12:20:01 +03:00
|
|
|
|
|
|
|
// clear cache
|
|
|
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "-c")
|
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
|
|
|
as.NoError(err)
|
2024-05-01 13:15:39 +03:00
|
|
|
assertFormatted(t, as, out, 0)
|
2024-02-15 12:20:01 +03:00
|
|
|
|
|
|
|
// clear cache
|
|
|
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "-c")
|
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-02-15 12:20:01 +03:00
|
|
|
|
|
|
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
|
|
|
as.NoError(err)
|
2024-05-01 13:15:39 +03:00
|
|
|
assertFormatted(t, as, out, 0)
|
2024-02-15 12:20:01 +03:00
|
|
|
|
|
|
|
// no cache
|
|
|
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "--no-cache")
|
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
}
|
|
|
|
|
2024-01-03 18:21:45 +03:00
|
|
|
func TestChangeWorkingDirectory(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
// capture current cwd, so we can replace it after the test is finished
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
as.NoError(err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
// return to the previous working directory
|
|
|
|
as.NoError(os.Chdir(cwd))
|
|
|
|
})
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
|
|
|
configPath := tempDir + "/treefmt.toml"
|
|
|
|
|
|
|
|
// test without any excludes
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-03 18:21:45 +03:00
|
|
|
"echo": {
|
|
|
|
Command: "echo",
|
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 18:21:45 +03:00
|
|
|
|
|
|
|
// by default, we look for ./treefmt.toml and use the cwd for the tree root
|
|
|
|
// this should fail if the working directory hasn't been changed first
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-C", tempDir)
|
2024-01-03 18:21:45 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-01-03 18:21:45 +03:00
|
|
|
}
|
|
|
|
|
2024-01-03 16:10:54 +03:00
|
|
|
func TestFailOnChange(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
2024-05-01 21:03:26 +03:00
|
|
|
configPath := tempDir + "/touch.toml"
|
2024-01-03 16:10:54 +03:00
|
|
|
|
|
|
|
// test without any excludes
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-05-01 21:03:26 +03:00
|
|
|
"touch": {
|
|
|
|
Command: "touch",
|
2024-01-03 16:10:54 +03:00
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 16:10:54 +03:00
|
|
|
_, err := cmd(t, "--fail-on-change", "--config-file", configPath, "--tree-root", tempDir)
|
|
|
|
as.ErrorIs(err, ErrFailOnChange)
|
|
|
|
}
|
|
|
|
|
2024-01-03 11:08:57 +03:00
|
|
|
func TestBustCacheOnFormatterChange(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
2024-05-01 21:03:26 +03:00
|
|
|
configPath := tempDir + "/touch.toml"
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// symlink some formatters into temp dir, so we can mess with their mod times
|
|
|
|
binPath := tempDir + "/bin"
|
|
|
|
as.NoError(os.Mkdir(binPath, 0o755))
|
|
|
|
|
|
|
|
binaries := []string{"black", "elm-format", "gofmt"}
|
|
|
|
|
|
|
|
for _, name := range binaries {
|
|
|
|
src, err := exec.LookPath(name)
|
|
|
|
as.NoError(err)
|
|
|
|
as.NoError(os.Symlink(src, binPath+"/"+name))
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepend our test bin directory to PATH
|
|
|
|
as.NoError(os.Setenv("PATH", binPath+":"+os.Getenv("PATH")))
|
|
|
|
|
|
|
|
// start with 2 formatters
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-03 11:08:57 +03:00
|
|
|
"python": {
|
|
|
|
Command: "black",
|
|
|
|
Includes: []string{"*.py"},
|
|
|
|
},
|
|
|
|
"elm": {
|
|
|
|
Command: "elm-format",
|
|
|
|
Options: []string{"--yes"},
|
|
|
|
Includes: []string{"*.elm"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 11:08:57 +03:00
|
|
|
args := []string{"--config-file", configPath, "--tree-root", tempDir}
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err := cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 3, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// tweak mod time of elm formatter
|
|
|
|
as.NoError(test.RecreateSymlink(t, binPath+"/"+"elm-format"))
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 3, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// check cache is working
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 0, 0, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// tweak mod time of python formatter
|
|
|
|
as.NoError(test.RecreateSymlink(t, binPath+"/"+"black"))
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 3, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// check cache is working
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 0, 0, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// add go formatter
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg.Formatters["go"] = &config2.Formatter{
|
2024-01-03 11:08:57 +03:00
|
|
|
Command: "gofmt",
|
|
|
|
Options: []string{"-w"},
|
|
|
|
Includes: []string{"*.go"},
|
|
|
|
}
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 4, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// check cache is working
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 0, 0, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// remove python formatter
|
2024-01-15 13:46:49 +03:00
|
|
|
delete(cfg.Formatters, "python")
|
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 2, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// check cache is working
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 0, 0, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// remove elm formatter
|
2024-01-15 13:46:49 +03:00
|
|
|
delete(cfg.Formatters, "elm")
|
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 1, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
|
|
|
|
// check cache is working
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, args...)
|
2024-01-03 11:08:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 0, 0, 0)
|
2024-01-03 11:08:57 +03:00
|
|
|
}
|
2024-01-11 23:52:22 +03:00
|
|
|
|
|
|
|
func TestGitWorktree(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
|
|
|
configPath := filepath.Join(tempDir, "/treefmt.toml")
|
|
|
|
|
|
|
|
// basic config
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-11 23:52:22 +03:00
|
|
|
"echo": {
|
|
|
|
Command: "echo",
|
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-01-15 13:46:49 +03:00
|
|
|
test.WriteConfig(t, configPath, cfg)
|
2024-01-11 23:52:22 +03:00
|
|
|
|
|
|
|
// init a git repo
|
|
|
|
repo, err := git.Init(
|
|
|
|
filesystem.NewStorage(
|
|
|
|
osfs.New(path.Join(tempDir, ".git")),
|
|
|
|
cache.NewObjectLRUDefault(),
|
|
|
|
),
|
|
|
|
osfs.New(tempDir),
|
|
|
|
)
|
|
|
|
as.NoError(err, "failed to init git repository")
|
|
|
|
|
|
|
|
// get worktree
|
|
|
|
wt, err := repo.Worktree()
|
|
|
|
as.NoError(err, "failed to get git worktree")
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
run := func(traversed int, emitted int, matched int, formatted int) {
|
2024-01-11 23:52:22 +03:00
|
|
|
out, err := cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir)
|
|
|
|
as.NoError(err)
|
2024-05-01 13:15:39 +03:00
|
|
|
assertFormatted(t, as, out, formatted)
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// run before adding anything to the worktree
|
2024-05-01 21:03:26 +03:00
|
|
|
run(0, 0, 0, 0)
|
2024-01-11 23:52:22 +03:00
|
|
|
|
|
|
|
// add everything to the worktree
|
|
|
|
as.NoError(wt.AddGlob("."))
|
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
run(31, 31, 31, 0)
|
2024-01-11 23:52:22 +03:00
|
|
|
|
|
|
|
// remove python directory
|
|
|
|
as.NoError(wt.RemoveGlob("python/*"))
|
2024-05-01 21:03:26 +03:00
|
|
|
run(28, 28, 28, 0)
|
2024-01-11 23:52:22 +03:00
|
|
|
|
|
|
|
// walk with filesystem instead of git
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-c", "--config-file", configPath, "--tree-root", tempDir, "--walk", "filesystem")
|
2024-01-11 23:52:22 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 59, 59, 59, 0)
|
2024-01-11 23:52:22 +03:00
|
|
|
}
|
2024-01-12 14:46:04 +03:00
|
|
|
|
2024-01-10 18:45:57 +03:00
|
|
|
func TestPathsArg(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
// capture current cwd, so we can replace it after the test is finished
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
as.NoError(err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
// return to the previous working directory
|
|
|
|
as.NoError(os.Chdir(cwd))
|
|
|
|
})
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
|
|
|
configPath := filepath.Join(tempDir, "/treefmt.toml")
|
|
|
|
|
|
|
|
// change working directory to temp root
|
|
|
|
as.NoError(os.Chdir(tempDir))
|
|
|
|
|
|
|
|
// basic config
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-10 18:45:57 +03:00
|
|
|
"echo": {
|
|
|
|
Command: "echo",
|
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.WriteConfig(t, configPath, cfg)
|
|
|
|
|
|
|
|
// without any path args
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-C", tempDir)
|
2024-01-10 18:45:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-01 21:03:26 +03:00
|
|
|
assertStats(t, as, 31, 31, 31, 0)
|
2024-01-10 18:45:57 +03:00
|
|
|
|
|
|
|
// specify some explicit paths
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-C", tempDir, "-c", "elm/elm.json", "haskell/Nested/Foo.hs")
|
2024-01-10 18:45:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-03 22:55:29 +03:00
|
|
|
assertStats(t, as, 2, 2, 2, 0)
|
2024-01-10 18:45:57 +03:00
|
|
|
|
|
|
|
// specify a bad path
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-C", tempDir, "-c", "elm/elm.json", "haskell/Nested/Bar.hs")
|
2024-01-10 18:45:57 +03:00
|
|
|
as.ErrorContains(err, "no such file or directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStdIn(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
// capture current cwd, so we can replace it after the test is finished
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
as.NoError(err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
// return to the previous working directory
|
|
|
|
as.NoError(os.Chdir(cwd))
|
|
|
|
})
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
|
|
|
configPath := filepath.Join(tempDir, "/treefmt.toml")
|
|
|
|
|
|
|
|
// change working directory to temp root
|
|
|
|
as.NoError(os.Chdir(tempDir))
|
|
|
|
|
|
|
|
// basic config
|
2024-02-15 12:20:01 +03:00
|
|
|
cfg := config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
2024-01-10 18:45:57 +03:00
|
|
|
"echo": {
|
|
|
|
Command: "echo",
|
|
|
|
Includes: []string{"*"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.WriteConfig(t, configPath, cfg)
|
|
|
|
|
|
|
|
// swap out stdin
|
|
|
|
prevStdIn := os.Stdin
|
|
|
|
stdin, err := os.CreateTemp("", "stdin")
|
|
|
|
as.NoError(err)
|
|
|
|
|
|
|
|
os.Stdin = stdin
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.Stdin = prevStdIn
|
|
|
|
_ = os.Remove(stdin.Name())
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := stdin.WriteString(`treefmt.toml
|
|
|
|
elm/elm.json
|
|
|
|
go/main.go
|
|
|
|
`)
|
|
|
|
as.NoError(err, "failed to write to stdin")
|
|
|
|
as.NoError(stdin.Sync())
|
|
|
|
_, _ = stdin.Seek(0, 0)
|
|
|
|
}()
|
|
|
|
|
2024-05-01 21:03:26 +03:00
|
|
|
_, err = cmd(t, "-C", tempDir, "--stdin")
|
2024-01-10 18:45:57 +03:00
|
|
|
as.NoError(err)
|
2024-05-03 22:55:29 +03:00
|
|
|
assertStats(t, as, 3, 3, 3, 0)
|
2024-01-10 18:45:57 +03:00
|
|
|
}
|
2024-04-26 12:27:11 +03:00
|
|
|
|
|
|
|
func TestDeterministicOrderingInPipeline(t *testing.T) {
|
|
|
|
as := require.New(t)
|
|
|
|
|
|
|
|
tempDir := test.TempExamples(t)
|
|
|
|
configPath := tempDir + "/treefmt.toml"
|
|
|
|
|
|
|
|
test.WriteConfig(t, configPath, config2.Config{
|
|
|
|
Formatters: map[string]*config2.Formatter{
|
|
|
|
// a and b should execute in lexicographical order as they have default priority 0, with c last since it has
|
|
|
|
// priority 1
|
|
|
|
"fmt-a": {
|
|
|
|
Command: "test-fmt",
|
|
|
|
Options: []string{"fmt-a"},
|
|
|
|
Includes: []string{"*.py"},
|
|
|
|
Pipeline: "foo",
|
|
|
|
},
|
|
|
|
"fmt-b": {
|
|
|
|
Command: "test-fmt",
|
|
|
|
Options: []string{"fmt-b"},
|
|
|
|
Includes: []string{"*.py"},
|
|
|
|
Pipeline: "foo",
|
|
|
|
},
|
|
|
|
"fmt-c": {
|
|
|
|
Command: "test-fmt",
|
|
|
|
Options: []string{"fmt-c"},
|
|
|
|
Includes: []string{"*.py"},
|
|
|
|
Pipeline: "foo",
|
|
|
|
Priority: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err := cmd(t, "-C", tempDir)
|
|
|
|
as.NoError(err)
|
|
|
|
|
|
|
|
matcher := regexp.MustCompile("^fmt-(.*)")
|
|
|
|
|
|
|
|
// check each affected file for the sequence of test statements which should be prepended to the end
|
|
|
|
sequence := []string{"fmt-a", "fmt-b", "fmt-c"}
|
|
|
|
paths := []string{"python/main.py", "python/virtualenv_proxy.py"}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
|
|
|
file, err := os.Open(filepath.Join(tempDir, p))
|
|
|
|
as.NoError(err)
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
matches := matcher.FindAllString(line, -1)
|
|
|
|
if len(matches) != 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
as.Equal(sequence[idx], matches[0])
|
|
|
|
idx += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|