Skip to content

Commit b68d328

Browse files
committed
feat: introduce tmp path for stdin files
Signed-off-by: Brian McGee <brian@bmcgee.ie>
1 parent 3a3b57c commit b68d328

11 files changed

Lines changed: 182 additions & 134 deletions

File tree

cmd/root_test.go

Lines changed: 139 additions & 113 deletions
Large diffs are not rendered by default.

format/formatter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File) error {
9090

9191
// append paths to the args
9292
for _, file := range files {
93-
args = append(args, file.RelPath)
93+
if file.TmpPath != "" {
94+
args = append(args, file.TmpPath)
95+
} else {
96+
args = append(args, file.RelPath)
97+
}
9498
}
9599

96100
// execute the command

nix/packages/treefmt/formatters.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ with pkgs; [
1717
shfmt
1818
statix
1919
deadnix
20+
just
2021
opentofu
2122
dos2unix
2223
yamlfmt

test/examples/just/justfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# print this message
2+
help:
3+
just --list --list-submodules --unsorted
4+
5+
# interactive recipes launcher
6+
choose:
7+
just --choose --unsorted

test/examples/treefmt.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ includes = ["*.yaml"]
8989

9090
[formatter.foo-fmt]
9191
command = "foo-fmt"
92+
93+
[formatter.just]
94+
command = "just"
95+
options = ["--fmt", "--unstable", "--justfile"]
96+
includes = ["**/justfile"]

walk/cached.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (c *CachedReader) Read(ctx context.Context, files []*File) (n int, err erro
106106

107107
// set a release function which inserts this file into the update channel
108108
file.AddReleaseFunc(func(ctx context.Context) error {
109-
if !GetNoCache(ctx) {
109+
if !GetNoCache(ctx) && file.TmpPath == "" {
110110
c.updateCh <- file
111111
}
112112

walk/filesystem_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var examplesPaths = []string{
3333
"html/index.html",
3434
"html/scripts/.gitkeep",
3535
"javascript/source/hello.js",
36+
"just/justfile",
3637
"nix/sources.nix",
3738
"nixpkgs.toml",
3839
"python/main.py",
@@ -78,8 +79,8 @@ func TestFilesystemReader(t *testing.T) {
7879
}
7980
}
8081

81-
as.Equal(32, count)
82-
as.Equal(32, statz.Value(stats.Traversed))
82+
as.Equal(33, count)
83+
as.Equal(33, statz.Value(stats.Traversed))
8384
as.Equal(0, statz.Value(stats.Matched))
8485
as.Equal(0, statz.Value(stats.Formatted))
8586
as.Equal(0, statz.Value(stats.Changed))

walk/git_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func TestGitReader(t *testing.T) {
2929
reader, err := walk.NewGitReader(tempDir, "", &statz)
3030
as.NoError(err)
3131

32-
files := make([]*walk.File, 33)
32+
files := make([]*walk.File, 34)
3333
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
3434
n, err := reader.Read(ctx, files)
3535

3636
cancel()
37-
as.Equal(32, n)
37+
as.Equal(33, n)
3838
as.ErrorIs(err, io.EOF)
3939

4040
// add everything to the git index
@@ -63,8 +63,8 @@ func TestGitReader(t *testing.T) {
6363
}
6464
}
6565

66-
as.Equal(32, count)
67-
as.Equal(32, statz.Value(stats.Traversed))
66+
as.Equal(33, count)
67+
as.Equal(33, statz.Value(stats.Traversed))
6868
as.Equal(0, statz.Value(stats.Matched))
6969
as.Equal(0, statz.Value(stats.Formatted))
7070
as.Equal(0, statz.Value(stats.Changed))

walk/jujutsu_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func TestJujutsuReader(t *testing.T) {
6767
}
6868
}
6969

70-
as.Equal(32, count)
71-
as.Equal(32, statz.Value(stats.Traversed))
70+
as.Equal(33, count)
71+
as.Equal(33, statz.Value(stats.Traversed))
7272
as.Equal(0, statz.Value(stats.Matched))
7373
as.Equal(0, statz.Value(stats.Formatted))
7474
as.Equal(0, statz.Value(stats.Changed))

walk/stdin.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path"
910
"path/filepath"
1011

1112
"github.com/numtide/treefmt/v2/stats"
@@ -25,10 +26,11 @@ func (s StdinReader) Read(_ context.Context, files []*File) (n int, err error) {
2526
return 0, io.EOF
2627
}
2728

28-
// read stdin into a temporary file with the same file extension
29-
pattern := "*" + filepath.Ext(s.path)
29+
// we attempt to preserve any file extensions as some formatters will not behave correctly without it
30+
pattern := "treefmt-stdin-*" + filepath.Ext(s.path)
3031

31-
file, err := os.CreateTemp(s.root, pattern)
32+
// create a temporary file to dump stdin into
33+
file, err := os.CreateTemp("", pattern)
3234
if err != nil {
3335
return 0, fmt.Errorf("failed to create a temporary file for processing stdin: %w", err)
3436
}
@@ -43,14 +45,10 @@ func (s StdinReader) Read(_ context.Context, files []*File) (n int, err error) {
4345
return 0, fmt.Errorf("failed to get file info for temporary file: %w", err)
4446
}
4547

46-
relPath, err := filepath.Rel(s.root, file.Name())
47-
if err != nil {
48-
return 0, fmt.Errorf("failed to get relative path for temporary file: %w", err)
49-
}
50-
5148
files[0] = &File{
52-
Path: file.Name(),
53-
RelPath: relPath,
49+
Path: path.Join(s.root, s.path),
50+
RelPath: s.path,
51+
TmpPath: file.Name(),
5452
Info: info,
5553
}
5654

0 commit comments

Comments
 (0)