Debian Codegraph @

testsuite/cookbook.00-init/005-init_ts.exp

raw
1 ##############################################################################
2 # Modules Revision 3.0
3 # Providing a flexible user environment
4 #
5 # File: cookbook.00-init/%M%
6 # Revision: %I%
7 # First Edition: 2026/08/13
8 # Last Mod.: %U%, %G%
9 #
10 # Authors: Xavier Delaruelle, [email protected]
11 #
12 # Description: Testuite testsequence
13 # Command:
14 # Sub-Command:
15 #
16 # Comment: %C{
17 # Defines all patterns used further in the testsuite
18 # }C%
19 #
20 ##############################################################################
21
22 proc find_bin {name} {
23 set path {}
24
25 if {[info exists ::env(PATH)]} {
26 foreach dir [split $::env(PATH) ":"] {
27 if {[file executable "$dir/$name"]} {
28 set path "$dir/$name"
29 break
30 }
31 }
32 }
33
34 return $path
35 }
36
37 if {$verbose} {
38 send_user "\tInitializing the testsuite ...\n"
39 }
40
41 set TCLSH $env(TCLSH)
42 set MODULECMD $env(MODULECMD)
43
44 # every cookbook recipe is documented at doc/source/cookbook/<name>.rst and
45 # ships its installable files under doc/example/<name>/
46 set cookbookdocdir "$env(TESTSUITEDIR)/../doc/source/cookbook"
47 set cookbookexampledir "$env(TESTSUITEDIR)/../doc/example"
48
49 # sandbox holding every recipe's fixture tree, recreated fresh for each run;
50 # each recipe test file builds its own "$cookbooksandbox/<name>" subtree and
51 # is responsible for removing it once done (see 999-cleanup.exp for the
52 # final sweep of the whole sandbox)
53 set cookbooksandbox "$env(TESTSUITEDIR)/cookbook-sandbox"
54 file delete -force $cookbooksandbox
55 file mkdir $cookbooksandbox
56
57 # escape a string so it is safe to embed in a Tcl regexp as a literal
58 proc cookbook_re_escape {str} {
59 return [regsub -all {[][$.+?*\\()&|^{}]} $str {\\&}]
60 }
61
62 # read a file shipped with a recipe under doc/example/<name>/, applying a
63 # 'string map' substitution list (e.g. to swap a hardcoded real-system path
64 # the recipe documents for this test's own sandbox path); used to build a
65 # recipe test's fixture from the exact files a user would install, instead
66 # of a hand-copied duplicate that could silently drift from them
67 proc cookbook_read_example_file {path submap} {
68 set fid [open $path r]
69 set content [read $fid]
70 close $fid
71 return [string map $submap $content]
72 }
73
74 # write 'content' to 'path', creating any missing parent directory
75 proc cookbook_write_file {path content} {
76 file mkdir [file dirname $path]
77 set fid [open $path w]
78 puts -nonewline $fid $content
79 close $fid
80 }
81
82 # strip Sphinx inline role markup (':rolename:`text`', used throughout this
83 # project's docs to color a parsed-literal transcript like a real terminal)
84 # down to the plain text it wraps
85 proc cookbook_strip_roles {text} {
86 return [regsub -all {:[A-Za-z0-9_]+:`([^`]*)`} $text {\1}]
87 }
88
89 # turn role-stripped illustrative output text into a regexp matching what a
90 # sandboxed test run actually produces: escape it as a literal, then widen
91 # the two kinds of detail a doc transcript shows for illustration but a
92 # sandboxed, non-interactive run cannot reproduce as-is -- a run of '-'
93 # padding whose exact width depends on terminal-width detection, and
94 # whatever literal substrings 'subs' (a {pattern replacement ...} list, e.g.
95 # a placeholder path mapped to this test's real sandbox path) says to widen
96 proc cookbook_output_re {text {subs {}}} {
97 set re [cookbook_re_escape $text]
98 set re [regsub -all -- {-{2,}} $re {[-]+}]
99 foreach {pattern replacement} $subs {
100 set re [string map [list [cookbook_re_escape $pattern] $replacement] $re]
101 }
102 return $re
103 }
104
105 # build a {command output} transcript step: 'curoutput' is the list of
106 # output lines collected so far for 'cmd', with any trailing blank lines
107 # dropped (blank lines in the middle, e.g. separating an avail listing from
108 # its trailing 'Key:' block, are kept) and role markup stripped
109 proc cookbook_flush_step_ {cmd curoutput} {
110 while {[llength $curoutput] && [lindex $curoutput end] eq {}} {
111 set curoutput [lrange $curoutput 0 end-1]
112 }
113 return [list $cmd [cookbook_strip_roles [join $curoutput \n]]]
114 }
115
116 # parse the '.. parsed-literal::' blocks of a cookbook recipe's rst file
117 # found under the section whose title is 'title' (matched as an exact line
118 # immediately followed by a '-' underline at least as long as the title,
119 # the section-heading convention used throughout this project's docs), and
120 # return the ordered list of {command output} pairs typed at/shown after
121 # the ':ps:`$`' prompt therein, with the output's role markup stripped
122 # (see cookbook_strip_roles). This lets a recipe test drive the same
123 # command sequence its documentation demonstrates, in the same order, and
124 # check its own real output is similar to the one illustrated there
125 # (see cookbook_output_re), instead of both being a hand-copied duplicate
126 # that could silently drift from the documentation
127 proc cookbook_parse_transcript {rstfile title} {
128 set fid [open $rstfile r]
129 set data [read $fid]
130 close $fid
131 set lines [split $data \n]
132 set n [llength $lines]
133
134 # locate '<title>\n<underline>'
135 set start {}
136 for {set i 0} {$i < $n - 1} {incr i} {
137 set nextline [lindex $lines [expr {$i + 1}]]
138 if {[lindex $lines $i] eq $title && [regexp {^-+$} $nextline]
139 && [string length $nextline] >= [string length $title]} {
140 set start [expr {$i + 2}]
141 break
142 }
143 }
144 if {$start eq {}} {
145 error "cookbook_parse_transcript: section '$title' not found in\
146 $rstfile"
147 }
148
149 # section ends at the next heading (a non-blank line immediately
150 # followed by an underline at least as long) or end of file
151 set end $n
152 for {set i $start} {$i < $n - 1} {incr i} {
153 set line [lindex $lines $i]
154 set nextline [lindex $lines [expr {$i + 1}]]
155 if {$line ne {} && [regexp {^[-=~]+$} $nextline]
156 && [string length $nextline] >= [string length $line]} {
157 set end $i
158 break
159 }
160 }
161
162 # walk the section line by line, only collecting content indented under
163 # a '.. parsed-literal::' directive (prose paragraphs in between are
164 # ignored); a ':ps:`$`' line starts a new command, every following
165 # indented line up to the next ':ps:`$`' line or the block's end (a
166 # dedented line) is that command's illustrated output. Only the block's
167 # own fixed indentation (its first content line's leading whitespace,
168 # normally 4 spaces) is stripped off each line, not all of it -- output
169 # can itself carry meaningful leading whitespace, e.g. a numbered
170 # 'module list' entry indented one space past its block indent
171 set steps {}
172 set inliteral 0
173 set blockindent {}
174 set havecmd 0
175 set cmd {}
176 set curoutput {}
177
178 foreach line [lrange $lines $start [expr {$end - 1}]] {
179 if {[regexp {^\s*\.\. parsed-literal::\s*$} $line]} {
180 if {$havecmd} {
181 lappend steps [cookbook_flush_step_ $cmd $curoutput]
182 set havecmd 0
183 }
184 set inliteral 1
185 set blockindent {}
186 set curoutput {}
187 continue
188 }
189 if {!$inliteral} {
190 continue
191 }
192 if {[string trim $line] eq {}} {
193 if {$havecmd} {
194 lappend curoutput {}
195 }
196 continue
197 }
198 if {$blockindent eq {}} {
199 regexp {^(\s*)} $line -> blockindent
200 }
201 set indentlen [string length $blockindent]
202 if {[string range $line 0 [expr {$indentlen - 1}]] ne $blockindent} {
203 # dedented below the block's own indentation: it has ended
204 if {$havecmd} {
205 lappend steps [cookbook_flush_step_ $cmd $curoutput]
206 set havecmd 0
207 }
208 set inliteral 0
209 continue
210 }
211 set content [string range $line $indentlen end]
212 if {[regexp {^:ps:`\$`\s?(.*)$} $content -> newcmd]} {
213 if {$havecmd} {
214 lappend steps [cookbook_flush_step_ $cmd $curoutput]
215 }
216 set cmd $newcmd
217 set curoutput {}
218 set havecmd 1
219 } elseif {$havecmd} {
220 lappend curoutput $content
221 }
222 }
223 if {$havecmd} {
224 lappend steps [cookbook_flush_step_ $cmd $curoutput]
225 }
226
227 return $steps
228 }
229
230 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: