Skip to content

Commit

Permalink
e2etests-cli: Add PNG test
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Mar 3, 2023
1 parent 7bc4955 commit 9227021
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
41 changes: 38 additions & 3 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ 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) {
t.Parallel()

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)
},
},
}

Expand All @@ -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)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9227021

Please sign in to comment.