Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(e2e): stop assertions inverting on large outputs under pipefail

Darshan Jain3008e2c879fb
diff --git a/tests/e2e/helpers/api-assertions.sh b/tests/e2e/helpers/api-assertions.sh
index e0c3293d..ab42830e 100644
--- a/tests/e2e/helpers/api-assertions.sh
+++ b/tests/e2e/helpers/api-assertions.sh
@@ -98,7 +98,7 @@ assert_contains() {
local needle="$2"
local desc="${3:-contains '$needle'}"
- if echo "$haystack" | grep -q "$needle"; then
+ if grep -q -- "$needle" <<<"$haystack"; then
pass_assert "$desc"
else
fail_assert "$desc (not found)"
@@ -110,7 +110,7 @@ assert_not_contains() {
local needle="$2"
local desc="${3:-does not contain '$needle'}"
- if echo "$haystack" | grep -q "$needle"; then
+ if grep -q -- "$needle" <<<"$haystack"; then
fail_assert "$desc (found when should be absent)"
else
pass_assert "$desc"
@@ -162,7 +162,7 @@ assert_input_not_contains() {
local value
value=$(echo "$RESULT" | jq -r '.result // empty')
- if echo "$value" | grep -qi "$forbidden"; then
+ if grep -qi -- "$forbidden" <<<"$value"; then
fail_assert "$desc: found '$forbidden' in value '$value'"
return 1
fi
@@ -197,7 +197,7 @@ assert_contains_any() {
local patterns="$2"
local desc="${3:-contains expected pattern}"
- if echo "$haystack" | grep -qE "$patterns"; then
+ if grep -qE -- "$patterns" <<<"$haystack"; then
pass_assert "$desc"
else
fail_assert "$desc (no match for /$patterns/ in: $haystack)"
@@ -242,9 +242,9 @@ assert_table_page() {
local text="$1"
local checks=0
- echo "$text" | grep -q "Alice Johnson" && ((checks++))
- echo "$text" | grep -q "[email protected]" && ((checks++))
- echo "$text" | grep -q "Active" && ((checks++))
+ grep -q -- "Alice Johnson" <<<"$text" && ((checks++))
+ grep -q -- "[email protected]" <<<"$text" && ((checks++))
+ grep -q -- "Active" <<<"$text" && ((checks++))
if [ "$checks" -ge 3 ]; then
pass_assert "table.html: found expected table data"
@@ -256,7 +256,9 @@ assert_table_page() {
assert_index_page() {
local snap="$1"
- if echo "$snap" | jq -e '.title' | grep -q "E2E Test"; then
+ local snap_title
+ snap_title=$(jq -r '.title // empty' <<<"$snap")
+ if grep -q -- "E2E Test" <<<"$snap_title"; then
pass_assert "index.html: correct title"
else
fail_assert "index.html: wrong title"
diff --git a/tests/e2e/helpers/api-http.sh b/tests/e2e/helpers/api-http.sh
index af01c140..91fd0623 100644
--- a/tests/e2e/helpers/api-http.sh
+++ b/tests/e2e/helpers/api-http.sh
@@ -373,7 +373,7 @@ assert_instance_list_absent() {
assert_instance_id_prefix() {
local instance_id="$1"
- if echo "$instance_id" | grep -q "^inst_"; then
+ if grep -q -- "^inst_" <<<"$instance_id"; then
pass_assert "instance ID has inst_ prefix: $instance_id"
else
fail_assert "bad ID format: $instance_id"
diff --git a/tests/e2e/helpers/api-snapshot.sh b/tests/e2e/helpers/api-snapshot.sh
index 511ca089..6dbed22e 100644
--- a/tests/e2e/helpers/api-snapshot.sh
+++ b/tests/e2e/helpers/api-snapshot.sh
@@ -50,7 +50,7 @@ assert_value() {
local actual
actual=$(get_value "$role" "$name")
- if echo "$actual" | grep -qF "$expected"; then
+ if grep -qF -- "$expected" <<<"$actual"; then
pass_assert "$name = '$actual'"
else
fail_assert "$name: expected '$expected', got '$actual'"
diff --git a/tests/e2e/helpers/autosolver.sh b/tests/e2e/helpers/autosolver.sh
index 95101664..5da82639 100644
--- a/tests/e2e/helpers/autosolver.sh
+++ b/tests/e2e/helpers/autosolver.sh
@@ -278,7 +278,7 @@ autosolver_title_has_challenge() {
local pattern
for pattern in "${AUTOSOLVER_CHALLENGE_PATTERNS[@]}"; do
- if printf '%s' "$title_lower" | grep -q "$pattern"; then
+ if grep -q -- "$pattern" <<<"$title_lower"; then
return 0
fi
done
diff --git a/tests/e2e/helpers/cli.sh b/tests/e2e/helpers/cli.sh
index e22e6867..d53a613c 100644
--- a/tests/e2e/helpers/cli.sh
+++ b/tests/e2e/helpers/cli.sh
@@ -87,7 +87,7 @@ assert_output_contains() {
local expected="$1"
local desc="${2:-output contains '$expected'}"
- if echo "$PT_OUT" | grep -q "$expected"; then
+ if grep -q -- "$expected" <<<"$PT_OUT"; then
pass_assert "$desc"
else
fail_assert "$desc"
@@ -209,7 +209,7 @@ assert_output_not_contains() {
local forbidden="$1"
local desc="${2:-output does not contain '$forbidden'}"
- if echo "$PT_OUT" | grep -q "$forbidden"; then
+ if grep -q -- "$forbidden" <<<"$PT_OUT"; then
fail_assert "$desc"
echo -e " ${RED} output was: $PT_OUT${NC}"
else
diff --git a/tests/e2e/pipefail_rules_test.go b/tests/e2e/pipefail_rules_test.go
new file mode 100644
index 00000000..324f2128
--- /dev/null
+++ b/tests/e2e/pipefail_rules_test.go
@@ -0,0 +1,93 @@
+package e2e
+
+import (
+ "os"
+ "path/filepath"
+ "regexp"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+// The helper layer is small; if the walk finds fewer files than this it matched
+// the wrong directory and the rule below would pass for the wrong reason.
+const minHelperFilesScanned = 5
+
+// pipesIntoEarlyExitReader matches `… | grep -q …`, in any of the flag spellings
+// used in this tree (-q, -qi, -qE, -qF), reached through a pipe.
+//
+// Under `set -o pipefail` — which helpers/base.sh sets for every scenario — this
+// shape is a race, not a style preference. grep -q exits the moment it matches,
+// which closes the read end of the pipe; if the writer has not finished, it dies
+// of SIGPIPE with status 141 and pipefail promotes that to the status of the
+// whole pipeline. The assertion then reads the opposite of the truth:
+//
+// assert_output_contains reports a FAILURE on output that does contain the needle
+// assert_output_not_contains reports a PASS on output that does contain the needle
+//
+// The second is the dangerous one: it is how "token not leaked to stdout" is
+// checked. Both flip once the payload outgrows the pipe buffer, so they are
+// invisible until an endpoint's output grows.
+//
+// Read from a here-string instead: `grep -q -- "$needle" <<<"$haystack"`.
+var pipesIntoEarlyExitReader = regexp.MustCompile(`\|\s*grep\s+-[a-zA-Z]*q`)
+
+func loadHelperFiles(t *testing.T) map[string]string {
+ t.Helper()
+
+ files := map[string]string{}
+ entries, err := os.ReadDir("helpers")
+ if err != nil {
+ t.Fatalf("cannot read helpers/, so this rule would check nothing: %v", err)
+ }
+ for _, e := range entries {
+ if e.IsDir() || !strings.HasSuffix(e.Name(), ".sh") {
+ continue
+ }
+ path := filepath.Join("helpers", e.Name())
+ body, readErr := os.ReadFile(path)
+ if readErr != nil {
+ t.Fatalf("read %s: %v", path, readErr)
+ }
+ files[path] = string(body)
+ }
+ if len(files) < minHelperFilesScanned {
+ t.Fatalf("scanned %d helper files, want at least %d; the walk matched almost nothing and this rule would pass vacuously", len(files), minHelperFilesScanned)
+ }
+ return files
+}
+
+func TestNoHelperPipesIntoGrepQ(t *testing.T) {
+ var offenders []string
+ for path, body := range loadHelperFiles(t) {
+ for i, line := range strings.Split(body, "\n") {
+ if strings.HasPrefix(strings.TrimSpace(line), "#") {
+ continue
+ }
+ if pipesIntoEarlyExitReader.MatchString(line) {
+ offenders = append(offenders, path+":"+strconv.Itoa(i+1)+": "+strings.TrimSpace(line))
+ }
+ }
+ }
+ if len(offenders) > 0 {
+ t.Fatalf("piping into `grep -q` under `set -o pipefail` inverts the assertion "+
+ "once the payload outgrows the pipe buffer: grep exits early, the writer takes "+
+ "SIGPIPE, and pipefail returns 141 for a pipeline that actually matched. Read "+
+ "from a here-string instead — `grep -q -- \"$needle\" <<<\"$haystack\"`.\n %s",
+ strings.Join(offenders, "\n "))
+ }
+}
+
+// helpers/base.sh sets pipefail for every scenario, which is what makes the rule
+// above load-bearing rather than stylistic. If that line ever goes away the rule
+// should be revisited, not silently kept.
+func TestHelpersStillSetPipefail(t *testing.T) {
+ body, err := os.ReadFile(filepath.Join("helpers", "base.sh"))
+ if err != nil {
+ t.Fatalf("read helpers/base.sh: %v", err)
+ }
+ if !strings.Contains(string(body), "pipefail") {
+ t.Fatal("helpers/base.sh no longer sets pipefail; TestNoHelperPipesIntoGrepQ was written " +
+ "for that mode and its rationale needs rechecking before it is trusted or removed")
+ }
+}