Skip to content

Commit

Permalink
Merge pull request #1523 from maxbrunet/fix/fmt/format-all
Browse files Browse the repository at this point in the history
fix(cli): do not exit after 1st formatted file
  • Loading branch information
alixander authored Aug 3, 2023
2 parents 83aad90 + b560e3b commit aff4810
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#### Improvements 🧹

#### Bugfixes ⛑️

- Fixes `d2 fmt` to format all files passed as arguments rather than first non-formatted only [#1523](https://github.com/terrastruct/d2/issues/1523)
4 changes: 3 additions & 1 deletion d2cli/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func fmtCmd(ctx context.Context, ms *xmain.State) (err error) {

output := []byte(d2format.Format(m))
if !bytes.Equal(output, input) {
return ms.WritePath(inputPath, output)
if err := ms.WritePath(inputPath, output); err != nil {
return err
}
}
}
return nil
Expand Down
13 changes: 13 additions & 0 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,19 @@ i used to read
assert.Equal(t, "x -> y\n", string(got))
},
},
{
name: "fmt-multiple-files",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "foo.d2", `a ---> b`)
writeFile(t, dir, "bar.d2", `x ---> y`)
err := runTestMainPersist(t, ctx, dir, env, "fmt", "foo.d2", "bar.d2")
assert.Success(t, err)
gotFoo := readFile(t, dir, "foo.d2")
gotBar := readFile(t, dir, "bar.d2")
assert.Equal(t, "a -> b\n", string(gotFoo))
assert.Equal(t, "x -> y\n", string(gotBar))
},
},
}

ctx := context.Background()
Expand Down

0 comments on commit aff4810

Please sign in to comment.