diff --git a/README.md b/README.md index 7891240..82b1488 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,12 @@ dot [-K engine] [-T format] [-o path] [input] | input | a DOT file, or `-` | standard input | Flags take Graphviz's glued form (`-Tpng`, `-oout.png`) as well as the spaced -one. `-V` prints the version, `-h` the usage. +one. `-?`, or `-h`, prints the usage. `-V` prints the version, one line on +standard error as Graphviz's `dot` prints its own and in the same shape, +`dot - graphviz version 16.1.1 (forkcloser/dot v1.5.0)`: the Graphviz release +the engine embeds, then this command's version where Graphviz's `dot` has its +build date. The first of these flags on the command line is the whole +command, as with Graphviz's `dot`: what follows it is not read. The exit statuses are those of Graphviz's `dot`: 0 on success, 2 when the input cannot be read (a file that is not there or may not be opened), and 1 diff --git a/cmd/dot/contract_test.go b/cmd/dot/contract_test.go index 5f25a96..288bbd6 100644 --- a/cmd/dot/contract_test.go +++ b/cmd/dot/contract_test.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" "testing" @@ -142,7 +143,12 @@ func TestExitStatusesAndStreams(t *testing.T) { {"--version", "", []string{"--version"}, 0, "", false}, {"-h beats a bad graph", "nonsense", []string{"-h"}, 0, "usage:", false}, {"-V beats a bad graph", "nonsense", []string{"-V"}, 0, "", false}, - {"-h with -V", "", []string{"-V", "-h"}, 0, "usage:", false}, + {"-?", "", []string{"-?"}, 0, "usage:", false}, + {"-h before -V", "", []string{"-h", "-V"}, 0, "usage:", false}, + {"-? before a bad flag", "", []string{"-?", "-Tgif"}, 0, "usage:", false}, + {"-V before a bad flag", "", []string{"-V", "-Tgif"}, 0, "", false}, + {"-V before -h", "", []string{"-V", "-h"}, 0, "", false}, + {"a bad flag before -?", "", []string{"-Tgif", "-?"}, 1, "", true}, {"unknown flag", graph, []string{"-x"}, 1, "", true}, {"unknown format", graph, []string{"-Tgif"}, 1, "", true}, {"unknown layout", graph, []string{"-Kbogus"}, 1, "", true}, @@ -169,15 +175,17 @@ func TestExitStatusesAndStreams(t *testing.T) { t.Fatalf("stdout %q, want a prefix %q", got.stdout, c.stdout) } - switch c.code { - case 0: + switch { + case c.code == 0 && c.stdout == "": + // -V: its one line is on standard error, where Graphviz's + // dot prints its own, and standard output has nothing. + if got.stdout != "" || !versionLine.MatchString(got.stderr) { + t.Fatalf("stdout %q, stderr %q; want nothing and the version line", got.stdout, got.stderr) + } + case c.code == 0: if got.stderr != "" { t.Fatalf("stderr on success: %q", got.stderr) } - - if c.stdout == "" && strings.TrimSpace(got.stdout) == "" { - t.Fatalf("no version printed: %q", got.stdout) - } default: if got.stdout != "" { t.Fatalf("stdout on failure: %q", got.stdout) @@ -278,6 +286,109 @@ func TestFailuresExitAsGraphvizsDot(t *testing.T) { } } +// versionLine is what -V prints: Graphviz's dot's line, with this command's +// version where that one has its build date. +var versionLine = regexp.MustCompile(`^dot - graphviz version (\d+\.\d+\.\d+) \(forkcloser/dot \S+\)\n$`) + +// -V prints one line on standard error and exits 0, as Graphviz's dot does, +// and the line has that one's shape, so what reads the Graphviz release out +// of the one reads it out of the other. The release is the one the engine +// embeds. +func TestVersionIsGraphvizsLine(t *testing.T) { + t.Parallel() + + got := dot(t, t.TempDir(), "", "-V") + + match := versionLine.FindStringSubmatch(got.stderr) + if got.code != 0 || got.stdout != "" || match == nil { + t.Fatalf( + "exit %d, stdout %q, stderr %q; want 0, nothing and the version line", + got.code, + got.stdout, + got.stderr, + ) + } + + t.Run("the release is the engine's", func(t *testing.T) { + t.Parallel() + + if pin := enginePin(t); match[1] != pin { + t.Errorf("-V names Graphviz %s, the engine is built from %s", match[1], pin) + } + }) + + t.Run("as Graphviz's dot has it", func(t *testing.T) { + t.Parallel() + + cmd := child(t, nativeDot(t), "-V") + + var stdout, stderr bytes.Buffer + + cmd.Stdout, cmd.Stderr = &stdout, &stderr + + if err := cmd.Run(); err != nil || stdout.Len() != 0 { + t.Fatalf("the native dot: %v, stdout %q", err, stdout.String()) + } + + // The same words up to the release, one line, a parenthesis after. + native := regexp.MustCompile(`^dot - graphviz version (\d+\.\d+\.\d+) \(.+\)\n$`). + FindStringSubmatch(stderr.String()) + if native == nil { + t.Fatalf("the native dot's line is %q, not the shape -V follows", stderr.String()) + } + + if majorMinor(native[1]) != majorMinor(match[1]) { + t.Errorf("the native dot is %s, -V names %s", native[1], match[1]) + } + }) +} + +// The first of -?, -h and -V on the command line is the whole command, as +// with Graphviz's dot: a bad flag after it is not read, one before it is the +// failure. Each status is held to the native dot when there is one (-h +// apart, which it does not have). +func TestTheFirstOfHelpAndVersionIsTheWholeCommand(t *testing.T) { + t.Parallel() + + cases := []struct { + args []string + code int + }{ + {[]string{"-?"}, 0}, + {[]string{"-?", "-Tnope"}, 0}, + {[]string{"-?", "-V"}, 0}, + {[]string{"-V", "-Tnope"}, 0}, + {[]string{"-V", "-?"}, 0}, + {[]string{"-Tnope", "-?"}, 1}, + {[]string{"-Tnope", "-V"}, 1}, + } + + for _, c := range cases { + t.Run(strings.Join(c.args, " "), func(t *testing.T) { + t.Parallel() + + if got := dot(t, t.TempDir(), "", c.args...); got.code != c.code { + t.Fatalf("exit %d, want %d\nstdout %q\nstderr %q", got.code, c.code, got.stdout, got.stderr) + } + + t.Run("as Graphviz's dot has it", func(t *testing.T) { + code := 0 + + err := child(t, nativeDot(t), c.args...).Run() + if exit, ok := errors.AsType[*exec.ExitError](err); ok { + code = exit.ExitCode() + } else if err != nil { + t.Fatal(err) + } + + if code != c.code { + t.Errorf("the native dot exits %d, want %d", code, c.code) + } + }) + }) + } +} + // A text with no graph in it, empty, blank or comments alone, is nothing to // draw and no failure, as with Graphviz's dot: exit 0, nothing on either // stream, and with -o no file made and the one at the path left as it was. diff --git a/cmd/dot/main.go b/cmd/dot/main.go index 85e2322..2e0ed42 100644 --- a/cmd/dot/main.go +++ b/cmd/dot/main.go @@ -35,8 +35,8 @@ const usage = `usage: dot [-K engine] [-T format] [-o path] [input] osage, patchwork -T format output format: dot (default), xdot, svg, png, jpg -o path output file (default: standard output) - -V print the version - -h print this help + -V print the version, on standard error + -h, -? print this help input DOT file (default, or "-": standard input) ` @@ -119,10 +119,14 @@ func parseArgs(args []string) (options, error) { arg := args[index] switch { - case arg == "-h", arg == "--help": + // As with Graphviz's dot, the first of these to be reached is the + // whole command: what follows it is not read, a bad flag included. + case arg == "-h", arg == "-?", arg == "--help": opt.help = true + return opt, nil case arg == "-V", arg == "--version": opt.version = true + return opt, nil case arg == "-", !strings.HasPrefix(arg, "-"): if opt.named { return opt, fmt.Errorf("%w: one input at most, got %q and %q", errUsage, opt.input, arg) @@ -184,12 +188,21 @@ func parseFlag(opt *options, args []string, index int) (int, error) { return index, nil } -func version() string { - if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" { - return bi.Main.Version +// version is the line -V prints, in the shape of Graphviz's dot's: the +// Graphviz release the engine embeds where Graphviz's dot names its own, and +// this command's version where that one puts its build date. +func version() (string, error) { + engine, err := graphviz.Version() + if err != nil { + return "", fmt.Errorf("the engine's version: %w", err) + } + + own := "devel" + if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" && bi.Main.Version != "(devel)" { + own = bi.Main.Version } - return "(devel)" + return fmt.Sprintf("dot - graphviz version %s (forkcloser/dot %s)", engine, own), nil } // render lays graph out and writes it to out in the format asked for. @@ -241,7 +254,16 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { fmt.Fprint(stdout, usage) return 0 case opt.version: - fmt.Fprintln(stdout, version()) + var line string + + if line, err = version(); err != nil { + fmt.Fprintln(stderr, "dot:", err) + return statusFailed + } + + // On standard error, where Graphviz's dot prints its own. + fmt.Fprintln(stderr, line) + return 0 } diff --git a/cmd/dot/main_test.go b/cmd/dot/main_test.go index a7949f7..c31db9e 100644 --- a/cmd/dot/main_test.go +++ b/cmd/dot/main_test.go @@ -32,6 +32,10 @@ func TestParseArgs(t *testing.T) { false, }, {[]string{"-h"}, options{layout: graphviz.DOT, format: graphviz.GV, help: true}, false}, + {[]string{"-?"}, options{layout: graphviz.DOT, format: graphviz.GV, help: true}, false}, + {[]string{"-?", "-Tgif"}, options{layout: graphviz.DOT, format: graphviz.GV, help: true}, false}, + {[]string{"-V", "-?"}, options{layout: graphviz.DOT, format: graphviz.GV, version: true}, false}, + {[]string{"-Tgif", "-?"}, options{}, true}, {[]string{"-V"}, options{layout: graphviz.DOT, format: graphviz.GV, version: true}, false}, {[]string{"-Tgif"}, options{}, true}, {[]string{"-Kbogus"}, options{}, true}, diff --git a/go.mod b/go.mod index d38108f..4958ede 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/forkcloser/dot go 1.26.0 require ( - github.com/forkcloser/go-graphviz v0.6.1-0.20261123080857-0b16160518c7 + github.com/forkcloser/go-graphviz v0.6.1-0.20261123161547-adc5c5a433b3 github.com/forkcloser/xos v0.1.2 ) diff --git a/go.sum b/go.sum index dd8295e..56b7557 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/forkcloser/go-graphviz v0.6.1-0.20261123080857-0b16160518c7 h1:X4pQ/98Ml4B2uYcKa0DE7IOUlAykvGxMgAkMJTU7zTE= -github.com/forkcloser/go-graphviz v0.6.1-0.20261123080857-0b16160518c7/go.mod h1:SC+ch2AgTanRZJH8JpOw99BOCnLQdBLEu1xz4QuihcE= +github.com/forkcloser/go-graphviz v0.6.1-0.20261123161547-adc5c5a433b3 h1:tffQ7H6scYj55WfxWXQS+VIW8xG9JPvRn+4uZnvf2iM= +github.com/forkcloser/go-graphviz v0.6.1-0.20261123161547-adc5c5a433b3/go.mod h1:SC+ch2AgTanRZJH8JpOw99BOCnLQdBLEu1xz4QuihcE= github.com/forkcloser/xos v0.1.2 h1:7TTH/bJ8hRIBIJPZp5WgIY//bgJFlyd+dpsgSEjnwOM= github.com/forkcloser/xos v0.1.2/go.mod h1:BxyGnZr+xW0m+0ph1xQ0qwHYs30hiXeUP7foOlKvxP0= github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZopYU=