e2etests-cli: Add stdin test

Closes https://github.com/terrastruct/d2-vscode/issues/56
This commit is contained in:
Anmol Sethi 2023-03-03 12:46:53 -08:00
parent 505183aa21
commit 8262ac6d74
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
5 changed files with 55 additions and 8 deletions

View file

@ -153,12 +153,16 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
outputPath = renameExt(inputPath, ".svg") outputPath = renameExt(inputPath, ".svg")
} }
} }
inputPath = filepath.Join(ms.PWD, inputPath) if inputPath != "-" {
d, err := os.Stat(inputPath) inputPath = filepath.Join(ms.PWD, inputPath)
if err == nil && d.IsDir() { d, err := os.Stat(inputPath)
inputPath = filepath.Join(inputPath, "index.d2") 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) match := d2themescatalog.Find(*themeFlag)
if match == (d2themes.Theme{}) { if match == (d2themes.Theme{}) {
@ -320,6 +324,9 @@ func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plug
boardOutputPath := outputPath boardOutputPath := outputPath
if len(diagram.Layers) > 0 || len(diagram.Scenarios) > 0 || len(diagram.Steps) > 0 { 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. // Boards with subboards must be self-contained folders.
ext := filepath.Ext(boardOutputPath) ext := filepath.Ext(boardOutputPath)
boardOutputPath = strings.TrimSuffix(boardOutputPath, ext) boardOutputPath = strings.TrimSuffix(boardOutputPath, ext)

View file

@ -1,6 +1,7 @@
package e2etests_cli package e2etests_cli
import ( import (
"bytes"
"context" "context"
"os" "os"
"path/filepath" "path/filepath"
@ -144,6 +145,22 @@ layers: {
testdataIgnoreDiff(t, ".pdf", pdf) 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() ctx := context.Background()

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 328 KiB

2
go.mod generated
View file

@ -23,7 +23,7 @@ require (
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
gonum.org/v1/plot v0.12.0 gonum.org/v1/plot v0.12.0
nhooyr.io/websocket v1.8.7 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 ( require (

4
go.sum generated
View file

@ -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= 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 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0= 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-20230303204624-090ad7923dcb h1:Zgb7Wf7mTpXmIiYv0bIJ8IULcdJVIxiRihnUvDP9YMQ=
oss.terrastruct.com/util-go v0.0.0-20230303172031-56bcfcdf965d/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4= oss.terrastruct.com/util-go v0.0.0-20230303204624-090ad7923dcb/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4= rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=