commit
83aad90f64
2 changed files with 29 additions and 1 deletions
10
d2cli/fmt.go
10
d2cli/fmt.go
|
|
@ -3,6 +3,8 @@ package d2cli
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"oss.terrastruct.com/util-go/xdefer"
|
||||
|
||||
|
|
@ -21,6 +23,14 @@ func fmtCmd(ctx context.Context, ms *xmain.State) (err error) {
|
|||
}
|
||||
|
||||
for _, inputPath := range ms.Opts.Args {
|
||||
if inputPath != "-" {
|
||||
inputPath = ms.AbsPath(inputPath)
|
||||
d, err := os.Stat(inputPath)
|
||||
if err == nil && d.IsDir() {
|
||||
inputPath = filepath.Join(inputPath, "index.d2")
|
||||
}
|
||||
}
|
||||
|
||||
input, err := ms.ReadPath(inputPath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -521,6 +521,16 @@ i used to read
|
|||
assert.Testdata(t, ".svg", svg)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "basic-fmt",
|
||||
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
|
||||
writeFile(t, dir, "hello-world.d2", `x ---> y`)
|
||||
err := runTestMainPersist(t, ctx, dir, env, "fmt", "hello-world.d2")
|
||||
assert.Success(t, err)
|
||||
got := readFile(t, dir, "hello-world.d2")
|
||||
assert.Equal(t, "x -> y\n", string(got))
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
|
@ -561,6 +571,15 @@ func testMain(dir string, env *xos.Env, args ...string) *xmain.TestState {
|
|||
}
|
||||
|
||||
func runTestMain(tb testing.TB, ctx context.Context, dir string, env *xos.Env, args ...string) error {
|
||||
err := runTestMainPersist(tb, ctx, dir, env, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
removeD2Files(tb, dir)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runTestMainPersist(tb testing.TB, ctx context.Context, dir string, env *xos.Env, args ...string) error {
|
||||
tms := testMain(dir, env, args...)
|
||||
tms.Start(tb, ctx)
|
||||
defer tms.Cleanup(tb)
|
||||
|
|
@ -568,7 +587,6 @@ func runTestMain(tb testing.TB, ctx context.Context, dir string, env *xos.Env, a
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
removeD2Files(tb, dir)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue