From 9227021bc96b07681f1b9b5b4f74702f434b28b8 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Thu, 2 Mar 2023 19:41:24 -0800 Subject: [PATCH] e2etests-cli: Add PNG test Closes #903 Closes #918 --- d2cli/main.go | 2 + e2etests-cli/main_test.go | 41 ++++++++++++++++-- .../testdata/TestCLI_E2E/hello_world.exp.png | Bin 0 -> 3041 bytes go.mod | 2 +- go.sum | 2 + 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 e2etests-cli/testdata/TestCLI_E2E/hello_world.exp.png diff --git a/d2cli/main.go b/d2cli/main.go index 11f5345694..c68f9ef0c2 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -152,6 +152,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) { outputPath = renameExt(inputPath, ".svg") } } + inputPath = filepath.Join(ms.PWD, inputPath) + outputPath = filepath.Join(ms.PWD, outputPath) match := d2themescatalog.Find(*themeFlag) if match == (d2themes.Theme{}) { diff --git a/e2etests-cli/main_test.go b/e2etests-cli/main_test.go index 22d88dac1b..1718566f80 100644 --- a/e2etests-cli/main_test.go +++ b/e2etests-cli/main_test.go @@ -2,8 +2,14 @@ package e2etests_cli import ( "context" + "path/filepath" "testing" "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) { @@ -11,11 +17,17 @@ func TestCLI_E2E(t *testing.T) { tca := []struct { 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", - 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) 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) +} diff --git a/e2etests-cli/testdata/TestCLI_E2E/hello_world.exp.png b/e2etests-cli/testdata/TestCLI_E2E/hello_world.exp.png new file mode 100644 index 0000000000000000000000000000000000000000..45c9386f922beeae80d8a9e9b8e54f2c0e36607e GIT binary patch literal 3041 zcmeH}`#;;)AIDG7FbU~}#if<1m6=xDrYT)ryi zGHbsTt6Pnkkdmxg*P=x!C8DM7RPuF8B-v+s>`&+(-|r9o>3!ah^LU-}e!R~6JkLo# z@8+bUtfdS9P;qfSeG!gz03ePjA>mPAko6!O5YZQ%96)XV!36+N#V)5$dBl@eCch3X z*ii3WcV8qy4aPQV+CO#lQO}&pnE%bD+Wg^Z>Y|0~M|xrV-&ZgFqA*$60wB~+0oRK>W74kC!MGnpkUQb?3++3q926s?T3 zxORCBdFBC7w;uCLd}1~YzbbnCiOTbYK4C`GP%|Kh1bi*iovFCoU* z%cN4a&X_*Mi`u>HlXl76T{Q*7qq!;gl>!6Y_9fLr&n{V2S7Ep@^TgO5jToMe?-9A-wWT0 zv>L?E_4`F_wHlSTN{j^UDB29O%=q6azN&d;$4L1^0_UQKy$vt|ItjD8xMO)6=fd-C z*I1<5=2gp`*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