Skip to content

Commit

Permalink
cli: Handle invalid board paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Nov 7, 2023
1 parent 7f9dcf7 commit 46bf849
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 5 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
- Correctly reports errors from invalid values set by globs. [#1691](https://github.com/terrastruct/d2/pull/1691)
- Fixes panic when spread substitution referenced a nonexistant var. [#1695](https://github.com/terrastruct/d2/pull/1695)
- Fixes incorrect appendix icon numbering. [#1704](https://github.com/terrastruct/d2/pull/1704)
- Fixes crash when using `--watch` and navigating to an invalid board path [#1693](https://github.com/terrastruct/d2/pull/1693)
2 changes: 1 addition & 1 deletion d2cli/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func fmtCmd(ctx context.Context, ms *xmain.State) (err error) {
defer xdefer.Errorf(&err, "failed to fmt")

ms.Opts = xmain.NewOpts(ms.Env, ms.Log, ms.Opts.Flags.Args()[1:])
ms.Opts = xmain.NewOpts(ms.Env, ms.Opts.Flags.Args()[1:])
if len(ms.Opts.Args) == 0 {
return xmain.UsageErrorf("fmt must be passed at least one file to be formatted")
}
Expand Down
5 changes: 4 additions & 1 deletion d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package d2cli

import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -490,6 +491,8 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, la
default:
compileDur := time.Since(start)
if animateInterval <= 0 {
b, _ := json.MarshalIndent(diagram, "", " ")
println("\033[1;31m--- DEBUG:", string(b), "\033[m")
// Rename all the "root.layers.x" to the paths that the boards get output to
linkToOutput, err := resolveLinks("root", outputPath, diagram)
if err != nil {
Expand All @@ -503,7 +506,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, la

board := diagram.GetBoard(boardPath)
if board == nil {
return nil, false, fmt.Errorf("Diagram with path %s not found", boardPath)
return nil, false, fmt.Errorf(`Diagram with path "%s" not found. Did you mean to specify a board like "layers.%s"?`, boardPath, boardPath)
}

boards, err := render(ctx, ms, compileDur, plugin, renderOpts, inputPath, outputPath, bundle, forceAppendix, page, ruler, board)
Expand Down
2 changes: 1 addition & 1 deletion d2plugin/plugin_dagre.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (p *dagrePlugin) HydrateOpts(opts []byte) error {
func (p *dagrePlugin) Info(ctx context.Context) (*PluginInfo, error) {
p.mu.Lock()
defer p.mu.Unlock()
opts := xmain.NewOpts(nil, nil, nil)
opts := xmain.NewOpts(nil, nil)
flags, err := p.Flags(ctx)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion d2plugin/plugin_elk.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (p *elkPlugin) HydrateOpts(opts []byte) error {
}

func (p elkPlugin) Info(ctx context.Context) (*PluginInfo, error) {
opts := xmain.NewOpts(nil, nil, nil)
opts := xmain.NewOpts(nil, nil)
flags, err := p.Flags(ctx)
if err != nil {
return nil, err
Expand Down
76 changes: 76 additions & 0 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package e2etests_cli
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

"github.com/davecgh/go-spew/spew"
"nhooyr.io/websocket"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/diff"
"oss.terrastruct.com/util-go/xmain"
Expand Down Expand Up @@ -544,6 +549,77 @@ i used to read
assert.Equal(t, "x -> y\n", string(gotBar))
},
},
{
name: "watch",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "index.d2", `
a -> b
b.link: cream
layers: {
cream: {
c -> b
}
}`)
stderr := &bytes.Buffer{}
tms := testMain(dir, env, "--watch", "--browser=0", "index.d2")
tms.Stderr = stderr

doneChan := make(chan struct{}, 1)

tms.Start(t, ctx)
defer tms.Cleanup(t)

go tms.Wait(ctx)

ticker := time.NewTicker(100 * time.Millisecond)
urlRE := regexp.MustCompile(`127.0.0.1:([0-9]+)`)
compiled := false
go func() {
var url string
for i := 0; i < 10 && url == ""; i++ {
select {
case <-ticker.C:
out := string(stderr.Bytes())
url = urlRE.FindString(out)
compiled, _ = regexp.MatchString(`failed to recompile`, out)
println("\033[1;31m--- DEBUG:", compiled, "\033[m")
case <-ctx.Done():
ticker.Stop()
return
}
}

if url != "" {
c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://%s/watch", url), nil)
assert.Success(t, err)

req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/cream", url), nil)
assert.Success(t, err)
var httpClient = &http.Client{}
resp, err := httpClient.Do(req)
assert.Success(t, err)
defer resp.Body.Close()
assert.Equal(t, 200, resp.StatusCode)

time.Sleep(1000)
spew.Dump(string(stderr.Bytes()))

_, _, err = c.Read(ctx)
spew.Dump(err)

defer c.Close(websocket.StatusNormalClosure, "")
}

doneChan <- struct{}{}
}()

<-doneChan

err := tms.Signal(ctx, os.Interrupt)
assert.Error(t, err)
},
},
}

ctx := context.Background()
Expand Down
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.

6 changes: 6 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 46bf849

Please sign in to comment.