Skip to content

Commit 2ec5610

Browse files
authored
Merge pull request #446 from jfly/consistent-handling-of-stdin
fix: normalize and enforce stdin paths as well
2 parents ff3de21 + 9ce6522 commit 2ec5610

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

cmd/format/format.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,31 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
168168
return fmt.Errorf("invalid walk type: %w", err)
169169
}
170170

171-
if walkType == walk.Stdin {
171+
if walkType == walk.Stdin && len(paths) != 1 {
172172
// check we have only received one path arg which we use for the file extension / matching to formatters
173-
if len(paths) != 1 {
174-
return fmt.Errorf("exactly one path should be specified when using the --stdin flag")
173+
return fmt.Errorf("exactly one path should be specified when using the --stdin flag")
174+
}
175+
176+
// checks all paths are contained within the tree root and exist
177+
// also "normalize" paths so they're relative to cfg.TreeRoot
178+
for i, path := range paths {
179+
absolutePath, err := filepath.Abs(path)
180+
if err != nil {
181+
return fmt.Errorf("error computing absolute path of %s: %w", path, err)
175182
}
176-
} else {
177-
// checks all paths are contained within the tree root and exist
178-
// also "normalize" paths so they're relative to cfg.TreeRoot
179-
for i, path := range paths {
180-
absolutePath, err := filepath.Abs(path)
181-
if err != nil {
182-
return fmt.Errorf("error computing absolute path of %s: %w", path, err)
183-
}
184183

185-
relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath)
186-
if err != nil {
187-
return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err)
188-
}
184+
relativePath, err := filepath.Rel(cfg.TreeRoot, absolutePath)
185+
if err != nil {
186+
return fmt.Errorf("error computing relative path from %s to %s: %s", cfg.TreeRoot, absolutePath, err)
187+
}
189188

190-
if strings.HasPrefix(relativePath, "..") {
191-
return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot)
192-
}
189+
if strings.HasPrefix(relativePath, "..") {
190+
return fmt.Errorf("path %s not inside the tree root %s", path, cfg.TreeRoot)
191+
}
193192

194-
paths[i] = relativePath
193+
paths[i] = relativePath
195194

195+
if walkType != walk.Stdin {
196196
if _, err = os.Stat(absolutePath); err != nil {
197197
return fmt.Errorf("path %s not found", path)
198198
}

cmd/root_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,14 @@ func TestStdin(t *testing.T) {
748748
as.Equal(`{ ...}: "hello"
749749
`, string(out))
750750

751+
// try a file that's outside of the project root
752+
contents = `{ foo, ... }: "hello"`
753+
os.Stdin = test.TempFile(t, "", "stdin", &contents)
754+
755+
out, _, err = treefmt(t, "-C", tempDir, "--allow-missing-formatter", "--stdin", "../test.nix")
756+
as.Errorf(err, "path ../test.nix not inside the tree root %s", tempDir)
757+
as.Equal("", string(out))
758+
751759
// try some markdown instead
752760
contents = `
753761
| col1 | col2 |

0 commit comments

Comments
 (0)