Merge pull request #1523 from maxbrunet/fix/fmt/format-all

fix(cli): do not exit after 1st formatted file
This commit is contained in:
Alexander Wang 2023-08-02 18:36:03 -07:00 committed by GitHub
commit aff4810f3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View file

@ -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)

View file

@ -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

View file

@ -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()