From 8262ac6d74f89a7049c9d9bf1010b319e029d440 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 3 Mar 2023 12:46:53 -0800 Subject: [PATCH] e2etests-cli: Add stdin test Closes https://github.com/terrastruct/d2-vscode/issues/56 --- d2cli/main.go | 17 ++++++++++---- e2etests-cli/main_test.go | 17 ++++++++++++++ .../testdata/TestCLI_E2E/stdin.exp.svg | 23 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 5 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 e2etests-cli/testdata/TestCLI_E2E/stdin.exp.svg diff --git a/d2cli/main.go b/d2cli/main.go index 024e98557..838b086a4 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -153,12 +153,16 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { outputPath = renameExt(inputPath, ".svg") } } - inputPath = filepath.Join(ms.PWD, inputPath) - d, err := os.Stat(inputPath) - if err == nil && d.IsDir() { - inputPath = filepath.Join(inputPath, "index.d2") + if inputPath != "-" { + inputPath = filepath.Join(ms.PWD, inputPath) + d, err := os.Stat(inputPath) + if err == nil && d.IsDir() { + inputPath = filepath.Join(inputPath, "index.d2") + } + } + if outputPath != "-" { + outputPath = filepath.Join(ms.PWD, outputPath) } - outputPath = filepath.Join(ms.PWD, outputPath) match := d2themescatalog.Find(*themeFlag) if match == (d2themes.Theme{}) { @@ -320,6 +324,9 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug boardOutputPath := outputPath if len(diagram.Layers) > 0 || len(diagram.Scenarios) > 0 || len(diagram.Steps) > 0 { + if outputPath == "-" { + return nil, fmt.Errorf("multiboard output cannot be written to stdout") + } // Boards with subboards must be self-contained folders. ext := filepath.Ext(boardOutputPath) boardOutputPath = strings.TrimSuffix(boardOutputPath, ext) diff --git a/e2etests-cli/main_test.go b/e2etests-cli/main_test.go index e715449d9..0b666d8f0 100644 --- a/e2etests-cli/main_test.go +++ b/e2etests-cli/main_test.go @@ -1,6 +1,7 @@ package e2etests_cli import ( + "bytes" "context" "os" "path/filepath" @@ -144,6 +145,22 @@ layers: { testdataIgnoreDiff(t, ".pdf", pdf) }, }, + { + name: "stdin", + run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) { + stdin := bytes.NewBufferString(`x -> y`) + stdout := &bytes.Buffer{} + tms := testMain(dir, env, "-") + tms.Stdin = stdin + tms.Stdout = stdout + tms.Start(t, ctx) + defer tms.Cleanup(t) + err := tms.Wait(ctx) + assert.Success(t, err) + + assert.Testdata(t, ".svg", stdout.Bytes()) + }, + }, } ctx := context.Background() diff --git a/e2etests-cli/testdata/TestCLI_E2E/stdin.exp.svg b/e2etests-cli/testdata/TestCLI_E2E/stdin.exp.svg new file mode 100644 index 000000000..4787423c1 --- /dev/null +++ b/e2etests-cli/testdata/TestCLI_E2E/stdin.exp.svg @@ -0,0 +1,23 @@ +xy + + + diff --git a/go.mod b/go.mod index 8213955c3..cc17fe272 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 gonum.org/v1/plot v0.12.0 nhooyr.io/websocket v1.8.7 - oss.terrastruct.com/util-go v0.0.0-20230303172031-56bcfcdf965d + oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb ) require ( diff --git a/go.sum b/go.sum index afe037195..141fb313e 100644 --- a/go.sum +++ b/go.sum @@ -277,6 +277,6 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= -oss.terrastruct.com/util-go v0.0.0-20230303172031-56bcfcdf965d h1:NytIu7ehF8FL5mb8Rw3yTTa3eyTu3xpi3cUejeAv1cE= -oss.terrastruct.com/util-go v0.0.0-20230303172031-56bcfcdf965d/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= +oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb h1:Zgb7Wf7mTpXmIiYv0bIJ8IULcdJVIxiRihnUvDP9YMQ= +oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=