Skip to content

Commit

Permalink
Merge pull request #963 from nhooyr/cli-tests-740f-b475
Browse files Browse the repository at this point in the history
e2etests-cli: Add PNG test
  • Loading branch information
nhooyr authored Mar 3, 2023
2 parents 7bc4955 + ae689fc commit 09544af
Show file tree
Hide file tree
Showing 19 changed files with 415 additions and 10 deletions.
3 changes: 1 addition & 2 deletions ci/release/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ RUN apt-get update && apt-get install -y ca-certificates curl dumb-init sudo

RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash -s - && \
apt-get install -y nodejs
# See https://github.com/microsoft/playwright/issues/18319
RUN npx playwright@1.31.1 install-deps chromium
RUN npx playwright@1.31.1 install --with-deps chromium

RUN adduser --gecos '' --disabled-password debian \
&& echo "debian ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/nopasswd
Expand Down
1 change: 1 addition & 0 deletions ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ fi

if [ "${CI:-}" ]; then
export FORCE_COLOR=1
npx playwright@1.31.1 install --with-deps chromium
fi
go test --timeout=30m "$@"
6 changes: 6 additions & 0 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
outputPath = renameExt(inputPath, ".svg")
}
}
inputPath = filepath.Join(ms.PWD, inputPath)
d, err := os.Stat(inputPath)
if err == nil && d.IsDir() {
inputPath = filepath.Join(inputPath, "index.d2")
}
outputPath = filepath.Join(ms.PWD, outputPath)

match := d2themescatalog.Find(*themeFlag)
if match == (d2themes.Theme{}) {
Expand Down
179 changes: 174 additions & 5 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,125 @@ package e2etests_cli

import (
"context"
"os"
"path/filepath"
"testing"
"time"

"oss.terrastruct.com/d2/d2cli"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff"
"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)
name string
skipCI bool
run func(t *testing.T, ctx context.Context, dir string, env *xos.Env)
}{
{
name: "hello_world",
run: func(t *testing.T, ctx context.Context) {},
name: "hello_world_png",
skipCI: true,
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "hello-world.d2", `x -> y`)
err := runTestMain(t, ctx, dir, env, "hello-world.d2", "hello-world.png")
assert.Success(t, err)
png := readFile(t, dir, "hello-world.png")
testdataIgnoreDiff(t, ".png", png)
},
},
{
name: "hello_world_png_pad",
skipCI: true,
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "hello-world.d2", `x -> y`)
err := runTestMain(t, ctx, dir, env, "--pad=400", "hello-world.d2", "hello-world.png")
assert.Success(t, err)
png := readFile(t, dir, "hello-world.png")
testdataIgnoreDiff(t, ".png", png)
},
},
{
name: "hello_world_png_sketch",
skipCI: true,
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "hello-world.d2", `x -> y`)
err := runTestMain(t, ctx, dir, env, "--sketch", "hello-world.d2", "hello-world.png")
assert.Success(t, err)
png := readFile(t, dir, "hello-world.png")
// https://github.com/terrastruct/d2/pull/963#pullrequestreview-1323089392
testdataIgnoreDiff(t, ".png", png)
},
},
{
name: "multiboard/life",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "life.d2", `x -> y
layers: {
core: {
belief
food
diet
}
broker: {
mortgage
realtor
}
stocks: {
TSX
NYSE
NASDAQ
}
}
scenarios: {
why: {
y -> x
}
}
`)
err := runTestMain(t, ctx, dir, env, "life.d2")
assert.Success(t, err)

assert.TestdataDir(t, filepath.Join(dir, "life"))
},
},
{
name: "multiboard/life_index_d2",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "life/index.d2", `x -> y
layers: {
core: {
belief
food
diet
}
broker: {
mortgage
realtor
}
stocks: {
TSX
NYSE
NASDAQ
}
}
scenarios: {
why: {
y -> x
}
}
`)
err := runTestMain(t, ctx, dir, env, "life")
assert.Success(t, err)

assert.TestdataDir(t, filepath.Join(dir, "life"))
},
},
}

Expand All @@ -25,10 +130,74 @@ func TestCLI_E2E(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if tc.skipCI && os.Getenv("CI") != "" {
t.SkipNow()
}

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)
err := tms.Wait(ctx)
if err != nil {
return err
}
removeD2Files(tb, dir)
return nil
}

func writeFile(tb testing.TB, dir, fp, data string) {
tb.Helper()
err := os.MkdirAll(filepath.Dir(filepath.Join(dir, fp)), 0755)
assert.Success(tb, err)
assert.WriteFile(tb, filepath.Join(dir, fp), []byte(data), 0644)
}

func readFile(tb testing.TB, dir, fp string) []byte {
tb.Helper()
return assert.ReadFile(tb, filepath.Join(dir, fp))
}

func removeD2Files(tb testing.TB, dir string) {
ea, err := os.ReadDir(dir)
assert.Success(tb, err)

for _, e := range ea {
if e.IsDir() {
removeD2Files(tb, filepath.Join(dir, e.Name()))
continue
}
ext := filepath.Ext(e.Name())
if ext == ".d2" {
assert.Remove(tb, filepath.Join(dir, e.Name()))
}
}
}

func testdataIgnoreDiff(tb testing.TB, ext string, got []byte) {
_ = diff.Testdata(filepath.Join("testdata", tb.Name()), ext, got)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions e2etests-cli/testdata/TestCLI_E2E/multiboard/life/index.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.

4 changes: 2 additions & 2 deletions go.sum

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

0 comments on commit 09544af

Please sign in to comment.