From b9b0c1b5c379ffff56b9ee0bede5c3b1078650ba Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 10 Nov 2026 17:00:21 +0100 Subject: [PATCH] feat: cobra CLI skeleton, validate subcommand MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cmd/webtest/main.go: RootCmd()/osExit, matching go-compressions/lz4c's own convention (SetArgs/SetOut/SetErr-testable, SilenceUsage: true). validate : parses and schema-checks a scenario file with no render and no network — the fast check a scenario author or CI runs before ever pointing one at a real page. Prints hcl.Diagnostics to stderr and exits non-zero on anything invalid. The run subcommand (actually driving a page) and the security guardrails (session/dry-run/pin/audit-log/bounds/anti-bot) are not implemented yet — tracked separately in the plan file. Co-Authored-By: Claude Sonnet 5 --- cmd/webtest/main.go | 36 ++++++++++++++++++ cmd/webtest/main_test.go | 79 ++++++++++++++++++++++++++++++++++++++++ cmd/webtest/validate.go | 41 +++++++++++++++++++++ go.mod | 3 ++ go.sum | 10 +++++ 5 files changed, 169 insertions(+) create mode 100644 cmd/webtest/main.go create mode 100644 cmd/webtest/main_test.go create mode 100644 cmd/webtest/validate.go diff --git a/cmd/webtest/main.go b/cmd/webtest/main.go new file mode 100644 index 0000000..21ca190 --- /dev/null +++ b/cmd/webtest/main.go @@ -0,0 +1,36 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +// webtest drives a go-webengine/engine-rendered page through a declarative +// HCL scenario (click/fill/assert/submit), for browser-automation test +// suites. See the repository's own README for the scenario format. +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// osExit allows tests to override os.Exit. +var osExit = os.Exit + +func main() { + if err := RootCmd().Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + osExit(1) + } +} + +// RootCmd returns the top-level cobra command. Exported so tests in the +// same package can exercise the wiring without spawning a child process. +func RootCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "webtest", + Short: "Drive a go-webengine/engine page through a declarative HCL scenario", + SilenceUsage: true, + } + cmd.AddCommand(newValidateCmd()) + return cmd +} diff --git a/cmd/webtest/main_test.go b/cmd/webtest/main_test.go new file mode 100644 index 0000000..ccd0e71 --- /dev/null +++ b/cmd/webtest/main_test.go @@ -0,0 +1,79 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "bytes" + "os" + "path/filepath" + "testing" +) + +// runCmd drives RootCmd with the given args, capturing stdout and stderr. +func runCmd(t *testing.T, args ...string) (stdout, stderr string, err error) { + t.Helper() + c := RootCmd() + var out, errb bytes.Buffer + c.SetArgs(args) + c.SetOut(&out) + c.SetErr(&errb) + err = c.Execute() + return out.String(), errb.String(), err +} + +func writeScenario(t *testing.T, src string) string { + t.Helper() + path := filepath.Join(t.TempDir(), "scenario.hcl") + if err := os.WriteFile(path, []byte(src), 0o644); err != nil { + t.Fatal(err) + } + return path +} + +const validScenario = ` +scenario "login" { + url = "https://example.com/login" + step "click" { + selector = "#submit" + } +} +` + +func TestValidateValidScenario(t *testing.T) { + path := writeScenario(t, validScenario) + stdout, _, err := runCmd(t, "validate", path) + if err != nil { + t.Fatalf("validate: %v", err) + } + if !bytes.Contains([]byte(stdout), []byte(`"login" is valid (1 step(s))`)) { + t.Fatalf("stdout = %q, want it to report the scenario name and step count", stdout) + } +} + +func TestValidateInvalidScenario(t *testing.T) { + path := writeScenario(t, `scenario "s" { }`) // missing required url + _, stderr, err := runCmd(t, "validate", path) + if err == nil { + t.Fatal("validate of a scenario missing the required url attribute: want an error") + } + if stderr == "" { + t.Fatal("validate of an invalid scenario: want diagnostics on stderr") + } +} + +func TestValidateMissingFile(t *testing.T) { + _, _, err := runCmd(t, "validate", filepath.Join(t.TempDir(), "nope.hcl")) + if err == nil { + t.Fatal("validate of a nonexistent file: want an error") + } +} + +func TestValidateRequiresExactlyOneArg(t *testing.T) { + if _, _, err := runCmd(t, "validate"); err == nil { + t.Fatal("validate with no path argument: want an error") + } + if _, _, err := runCmd(t, "validate", "a.hcl", "b.hcl"); err == nil { + t.Fatal("validate with two path arguments: want an error") + } +} diff --git a/cmd/webtest/validate.go b/cmd/webtest/validate.go new file mode 100644 index 0000000..9973ceb --- /dev/null +++ b/cmd/webtest/validate.go @@ -0,0 +1,41 @@ +// Copyright (c) the go-webengine/webtest authors. +// SPDX-License-Identifier: BSD-3-Clause + +package main + +import ( + "fmt" + "os" + + "github.com/go-webengine/webtest/hclscenario" + "github.com/spf13/cobra" +) + +// newValidateCmd parses and schema-checks a scenario file WITHOUT +// rendering anything — the fast, no-network check a scenario author (or +// CI) runs before ever pointing it at a real page. +func newValidateCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "validate ", + Short: "Parse and validate a scenario file (no render, no network)", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + return runValidate(cmd, args[0]) + }, + } + return cmd +} + +func runValidate(cmd *cobra.Command, path string) error { + src, err := os.ReadFile(path) + if err != nil { + return fmt.Errorf("read %s: %w", path, err) + } + sc, diags := hclscenario.Parse(src, path) + if diags.HasErrors() { + fmt.Fprint(cmd.ErrOrStderr(), diags) + return fmt.Errorf("%s: invalid scenario", path) + } + fmt.Fprintf(cmd.OutOrStdout(), "%s: scenario %q is valid (%d step(s))\n", path, sc.Name, len(sc.Steps)) + return nil +} diff --git a/go.mod b/go.mod index ab855df..9145cea 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.27.3 require ( github.com/hashicorp/hcl/v2 v2.25.0 + github.com/spf13/cobra v1.10.2 github.com/zclconf/go-cty v1.19.0 ) @@ -13,7 +14,9 @@ require ( github.com/apparentlymart/go-textseg/v17 v17.0.1 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/google/go-cmp v0.7.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/mitchellh/go-wordwrap v1.0.1 // indirect + github.com/spf13/pflag v1.0.9 // indirect golang.org/x/mod v0.41.0 // indirect golang.org/x/sync v0.24.0 // indirect golang.org/x/text v0.43.0 // indirect diff --git a/go.sum b/go.sum index 649693a..84b8f2f 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= github.com/apparentlymart/go-textseg/v17 v17.0.1 h1:bpMXRgQ5cEoRNuQke1a80/Nl6w3G5eoIbWo9f3gXkAs= github.com/apparentlymart/go-textseg/v17 v17.0.1/go.mod h1:fa8X4jgGeevslICIY6LcdjkSecWnXmYd9Lk34z/VxZs= +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= @@ -12,12 +13,20 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/hashicorp/hcl/v2 v2.25.0 h1:HmmQVYRny4MaBo4b20TjmL46wyuUxpnMWkPZ4+NTbWk= github.com/hashicorp/hcl/v2 v2.25.0/go.mod h1:vR+FKETxoZAmRlHgFfKmuqivj+C4Izm/c66XkmZ3r7M= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= +github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= +github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/zclconf/go-cty v1.19.0 h1:IV8WdqYZc2c5rLX9bEoLNXKojBAp0MZPBHMIrCoa/s4= github.com/zclconf/go-cty v1.19.0/go.mod h1:12W89jGn3JCOIQi7infWr9m80rOkb5RNYJqXMZcN4c8= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940 h1:4r45xpDWB6ZMSMNJFMOjqrGHynW3DIBuR2H9j0ug+Mo= github.com/zclconf/go-cty-debug v0.0.0-20240509010212-0d6042c53940/go.mod h1:CmBdvvj3nqzfzJ6nTCIwDTPZ56aVGvDrmztiO5g3qrM= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c= golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o= golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= @@ -26,3 +35,4 @@ golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= golang.org/x/tools v0.51.0 h1:k4Xc/1Om9jwkBJBo4NVLMSARBoWtK10mx+W5BnXCeAI= golang.org/x/tools v0.51.0/go.mod h1:9eEncMayCV6zRMGhR5eZEC2iBx98qWcF1HZ9Z7wJOoA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=