Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions cmd/webtest/main.go
Original file line number Diff line number Diff line change
@@ -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
}
79 changes: 79 additions & 0 deletions cmd/webtest/main_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
41 changes: 41 additions & 0 deletions cmd/webtest/validate.go
Original file line number Diff line number Diff line change
@@ -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 <scenario.hcl>",
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
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Loading