diff --git a/d2cli/main.go b/d2cli/main.go index 11f534569..c68f9ef0c 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 22d88dac1..1718566f8 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 000000000..45c9386f9 Binary files /dev/null and b/e2etests-cli/testdata/TestCLI_E2E/hello_world.exp.png differ diff --git a/go.mod b/go.mod index 34b2a85ea..fdcf380e4 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-20230301015829-35b30391c74d + oss.terrastruct.com/util-go v0.0.0-20230303033840-484c630faa46 ) require ( diff --git a/go.sum b/go.sum index 4c1b4fb99..1a687a2ae 100644 --- a/go.sum +++ b/go.sum @@ -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= 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-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=