e2etests-cli: Add PNG test

Closes #903
Closes #918
This commit is contained in:
Anmol Sethi 2023-03-02 19:41:24 -08:00
parent 7bc49556c7
commit 9227021bc9
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
5 changed files with 43 additions and 4 deletions

View file

@ -152,6 +152,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
outputPath = renameExt(inputPath, ".svg") outputPath = renameExt(inputPath, ".svg")
} }
} }
inputPath = filepath.Join(ms.PWD, inputPath)
outputPath = filepath.Join(ms.PWD, outputPath)
match := d2themescatalog.Find(*themeFlag) match := d2themescatalog.Find(*themeFlag)
if match == (d2themes.Theme{}) { if match == (d2themes.Theme{}) {

View file

@ -2,8 +2,14 @@ package e2etests_cli
import ( import (
"context" "context"
"path/filepath"
"testing" "testing"
"time" "time"
"oss.terrastruct.com/d2/d2cli"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/xmain"
"oss.terrastruct.com/util-go/xos"
) )
func TestCLI_E2E(t *testing.T) { func TestCLI_E2E(t *testing.T) {
@ -11,11 +17,17 @@ func TestCLI_E2E(t *testing.T) {
tca := []struct { tca := []struct {
name string name string
run func(t *testing.T, ctx context.Context) run func(t *testing.T, ctx context.Context, dir string, env *xos.Env)
}{ }{
{ {
name: "hello_world", name: "hello_world",
run: func(t *testing.T, ctx context.Context) {}, run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
assert.WriteFile(t, filepath.Join(dir, "hello-world.d2"), []byte(`x -> y`), 0644)
err := runTestMain(t, ctx, dir, env, "hello-world.d2", "hello-world.png")
assert.Success(t, err)
png := assert.ReadFile(t, filepath.Join(dir, "hello-world.png"))
assert.Testdata(t, ".png", png)
},
}, },
} }
@ -28,7 +40,30 @@ func TestCLI_E2E(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, time.Minute*5) ctx, cancel := context.WithTimeout(ctx, time.Minute*5)
defer cancel() defer cancel()
tc.run(t, ctx) dir, cleanup := assert.TempDir(t)
defer cleanup()
env := xos.NewEnv(nil)
tc.run(t, ctx, dir, env)
}) })
} }
} }
// We do not run the CLI in its own process even though that makes it not truly e2e to
// test whether we're cleaning up state correctly.
func testMain(dir string, env *xos.Env, args ...string) *xmain.TestState {
return &xmain.TestState{
Run: d2cli.Run,
Env: env,
Args: append([]string{"e2etests-cli/d2"}, args...),
PWD: dir,
}
}
func runTestMain(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)
return tms.Wait(ctx)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3 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-20230301015829-35b30391c74d oss.terrastruct.com/util-go v0.0.0-20230303033840-484c630faa46
) )
require ( require (

2
go.sum generated
View file

@ -279,4 +279,6 @@ 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-20230301015829-35b30391c74d h1:+1Bp2bYA7bieedJuqbiwOLhnMs6GQQLB4sNX7BcDbSQ= oss.terrastruct.com/util-go v0.0.0-20230301015829-35b30391c74d h1:+1Bp2bYA7bieedJuqbiwOLhnMs6GQQLB4sNX7BcDbSQ=
oss.terrastruct.com/util-go v0.0.0-20230301015829-35b30391c74d/go.mod h1:Fwy72FDIOOM4K8F96ScXkxHHppR1CPfUyo9+x9c1PBU= oss.terrastruct.com/util-go v0.0.0-20230301015829-35b30391c74d/go.mod h1:Fwy72FDIOOM4K8F96ScXkxHHppR1CPfUyo9+x9c1PBU=
oss.terrastruct.com/util-go v0.0.0-20230303033840-484c630faa46 h1:pVcaRgqhNDXtWlsHg1cAy7BRycDiOaAEBexCFgMyUxQ=
oss.terrastruct.com/util-go v0.0.0-20230303033840-484c630faa46/go.mod h1:Fwy72FDIOOM4K8F96ScXkxHHppR1CPfUyo9+x9c1PBU=
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4= rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=