Skip to content

Commit b3b6f00

Browse files
committed
preprocessor: fix comparator regex for column-aligned stats output
The patternComparator for the log-statistics-from-the-cue-evaluator page used the regex (?m)\d+,?$ which only normalizes digits at line ends. CUE evaluator stats use right-aligned columns, so when numbers change digit count between runs the whitespace padding before them also changes. This caused the comparator to fail, rewriting the page on every run when Preprocessor-No-Write-Cache was set. Change the regex to (?m)\s+\d+,?$ so that both the whitespace padding and the digits are normalized together, correctly handling column alignment differences. Add a test that verifies comparators handle column-aligned output with different digit counts when --nowritecache is set with a cache miss. Signed-off-by: Paul Jolly <paul@myitcv.io> Change-Id: Ifdec2fbafcf3d013bcffa09a8024d0af4417869c Reviewed-on: https://review.gerrithub.io/c/cue-lang/cuelang.org/+/1232616 TryBot-Result: CUEcueckoo <cueckoo@cuelang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
1 parent baccf64 commit b3b6f00

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

content/docs/howto/log-statistics-from-the-cue-evaluator/page.cue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ content: docs: howto: "log-statistics-from-the-cue-evaluator": {
44
page: comparators: [{
55
kind: "patternComparator"
66
commandPrefix: "cat stats.cue"
7-
pattern: expr: #"(?m)\d+,?$"#
7+
pattern: expr: #"(?m)\s+\d+,?$"#
88
}]
99
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Test that preprocessor comparators correctly handle column-aligned output
2+
# when --nowritecache is set with a cache miss, specifically when numbers
3+
# have different digit counts between the cached and actual outputs.
4+
#
5+
# CUE evaluator stats output uses right-aligned numeric columns. When numbers
6+
# change digit count between runs, the column padding (spaces before numbers)
7+
# changes too. A comparator pattern of (?m)\d+,?$ only normalizes digits,
8+
# not the preceding whitespace, causing false mismatches. The fix is to use
9+
# (?m)\s+\d+,?$ which normalizes both the whitespace padding and the digits.
10+
11+
unquote content/dir/en.md
12+
13+
# Run the preprocessor to generate initial cache and output files.
14+
exec preprocessor execute --debug=all
15+
16+
# Save copies
17+
cp content/dir/en.md content/dir/en.md.original
18+
19+
# Modify gen_cache.cue to:
20+
# 1. Change the hash to trigger cache miss (simulating preprocessor change)
21+
# 2. Change the cached step output to have different digit counts:
22+
# FieldA: "42" (2 digits) -> "1" (1 digit)
23+
# FieldB: "123456" (6 digits) -> "1" (1 digit)
24+
# FieldC: "7" (1 digit) -> "12345" (5 digits)
25+
exec sed -i 's/hash: "[^"]*"/hash: "INVALIDHASHVALUE"/' content/dir/gen_cache.cue
26+
exec sed -i 's/FieldA: 42/FieldA: 1/' content/dir/gen_cache.cue
27+
exec sed -i 's/FieldB: 123456/FieldB: 1/' content/dir/gen_cache.cue
28+
exec sed -i 's/FieldC: 7/FieldC: 12345/' content/dir/gen_cache.cue
29+
cp content/dir/gen_cache.cue content/dir/gen_cache.cue.modified
30+
31+
# Run with --nowritecache. The script re-runs (cache miss) and produces
32+
# the original output. The comparator normalizes both the actual and
33+
# cached outputs (including whitespace padding) and determines they're
34+
# equivalent, then uses the cached version.
35+
exec preprocessor execute --debug=all --nowritecache
36+
stderr $WORK${/}'content'${/}'dir'${/}'en.md: cache miss for multi-step script'
37+
38+
# gen_cache.cue was NOT updated (nowritecache prevented it)
39+
cmp content/dir/gen_cache.cue content/dir/gen_cache.cue.modified
40+
41+
# Source file was not changed
42+
cmp content/dir/en.md content/dir/en.md.original
43+
44+
# The target file should contain the CACHED version of the output (with the
45+
# modified numbers), because the comparator determined equivalence between
46+
# actual and cached outputs despite different digit counts in column-aligned
47+
# fields. If the target instead has the original numbers, the comparator
48+
# failed to handle the whitespace/digit-count variation.
49+
exec grep 'FieldA: 1' hugo/content/en/dir/index.md
50+
51+
-- hugo/.keep --
52+
-- site.cue --
53+
package site
54+
55+
import "strings"
56+
57+
versions: cue: [x=string]: var: "CUELANG_CUE_\(strings.ToUpper(x))"
58+
59+
versions: cue: default: v: "v0.9.0-alpha.3"
60+
-- content/dir/page.cue --
61+
package site
62+
63+
content: dir: page: {
64+
leftDelim: "{{{"
65+
rightDelim: "}}}"
66+
67+
comparators: [{
68+
kind: "patternComparator"
69+
commandPrefix: "cat stats.txt"
70+
pattern: expr: #"(?m)\s+\d+,?$"#
71+
}]
72+
}
73+
-- content/dir/en.md --
74+
>---
75+
>title: Stats Test
76+
>---
77+
>{{{with upload "en" "stats file"}}}
78+
>-- stats.txt --
79+
>Stats: {
80+
> FieldA: 42
81+
> FieldB: 123456
82+
> FieldC: 7
83+
>}
84+
>{{{end}}}
85+
>{{{with script "en" "read stats"}}}
86+
>cat stats.txt
87+
>{{{end}}}

0 commit comments

Comments
 (0)