Skip to content

Commit 69fcdeb

Browse files
committed
expect refactor progress
1 parent f0d8e92 commit 69fcdeb

14 files changed

Lines changed: 232 additions & 113 deletions

ci/expect_scripts/bytes-stdin-stdout.exp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ spawn $env(EXAMPLES_DIR)bytes-stdin-stdout
1111

1212
send -- "someinput\r"
1313

14-
expect "someinput\r\nsomeinput\r\n" {
14+
set expected_output [normalize_output {
15+
someinput
16+
someinput
17+
}]
18+
19+
expect $expected_output {
1520
expect eof {
1621
check_exit_and_segfault
1722
}
1823
}
1924

2025
puts stderr "\nExpect script failed: output was different from expected value. uncomment `exp_internal 1` to debug."
21-
exit 1
26+
exit 1

ci/expect_scripts/command-line-args.exp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ spawn $env(EXAMPLES_DIR)/command-line-args foo
1010
source ./ci/expect_scripts/shared-code.exp
1111

1212

13-
expect "received argument: foo\r\n" {
14-
expect "Unix argument, bytes: \\\[102, 111, 111\\\]\r\n" {
15-
expect "back to Arg: \"foo\"\r\n" {
16-
expect eof {
17-
check_exit_and_segfault
18-
}
19-
}
13+
set expected_output [normalize_output {
14+
received argument: foo
15+
Unix argument, bytes: \[102, 111, 111\]
16+
back to Arg: "foo"
17+
}]
18+
19+
expect $expected_output {
20+
expect eof {
21+
check_exit_and_segfault
2022
}
2123
}
2224

ci/expect_scripts/command.exp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,15 @@ source ./ci/expect_scripts/shared-code.exp
1010
spawn $env(EXAMPLES_DIR)command
1111

1212

13-
expect "Hello\r\n" {
14-
expect "Command output: Hi\r\n" {
15-
expect "Command output: BAZ=DUCK\r\n" {
16-
expect "FOO=BAR\r\n" {
17-
expect "XYZ=ABC\r\n" {
18-
expect "Yo\r\n" {
19-
expect eof {
20-
check_exit_and_segfault
21-
}
22-
}
23-
}
24-
}
25-
}
26-
}
27-
}
28-
29-
puts stderr "\nExpect script failed: output was different from expected value. uncomment `exp_internal 1` to debug."
30-
exit 1
13+
set expected_output [normalize_output {
14+
Hello
15+
Command output: Hi
16+
17+
Command output: BAZ=DUCK
18+
FOO=BAR
19+
XYZ=ABC
20+
21+
Yo
22+
}]
23+
24+
expect_with_diff $expected_output

ci/expect_scripts/env-var.exp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ set env(LETTERS) a,c,e,j
1313
spawn $env(EXAMPLES_DIR)env-var
1414

1515

16-
expect "Your favorite editor is nano!\r\n" {
17-
expect "Your favorite letters are: a c e j\r\n" {
18-
expect eof {
19-
check_exit_and_segfault
20-
}
21-
}
16+
set expected_output [normalize_output {
17+
Your favorite editor is nano!
18+
Your favorite letters are: a c e j
19+
}]
20+
21+
expect $expected_output {
22+
expect eof {
23+
check_exit_and_segfault
24+
}
2225
}
2326

2427
puts stderr "\nExpect script failed: output was different from expected value. uncomment `exp_internal 1` to debug."

ci/expect_scripts/file-permissions.exp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ source ./ci/expect_scripts/shared-code.exp
99

1010
spawn $env(EXAMPLES_DIR)file-permissions
1111

12-
expect "LICENSE file permissions:\r\n Executable: Bool.false\r\n Readable: Bool.true\r\n Writable: Bool.true\r\n" {
12+
set expected_output [normalize_output {
13+
LICENSE file permissions:
14+
Executable: Bool.false
15+
Readable: Bool.true
16+
Writable: Bool.true
17+
}]
18+
19+
expect $expected_output {
1320
expect eof {
1421
check_exit_and_segfault
1522
}

ci/expect_scripts/file-read-write.exp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55

66
set timeout 7
77

8+
source ./ci/expect_scripts/shared-code.exp
9+
810
spawn $env(EXAMPLES_DIR)file-read-write
911

10-
expect "Writing a string to out.txt"
11-
expect "I read the file back. Its contents: \"a string!\""
12+
13+
set expected_output [normalize_output {
14+
Writing a string to out.txt
15+
I read the file back. Its contents are: "a string!"
16+
}]
17+
18+
expect $expected_output {
19+
expect eof {
20+
check_exit_and_segfault
21+
}
22+
}
23+
24+
puts stderr "\nExpect script failed: output was different from expected value. uncomment `exp_internal 1` to debug."
25+
exit 1

ci/expect_scripts/locale.exp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ source ./ci/expect_scripts/shared-code.exp
99

1010
spawn $env(EXAMPLES_DIR)locale
1111

12-
expect -re {The most preferred locale for this system or application: [a-zA-Z\-]+\r\n} {
13-
expect -re {All available locales for this system or application: \[.*\]\r\n} {
12+
set expected_output [normalize_output {
13+
The most preferred locale for this system or application: [a-zA-Z\-]+
14+
All available locales for this system or application: \[.*\]
15+
}]
16+
17+
expect -re $expected_output {
1418
expect eof {
1519
check_exit_and_segfault
1620
}

ci/expect_scripts/path.exp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ source ./ci/expect_scripts/shared-code.exp
1010
cd $env(EXAMPLES_DIR)
1111
spawn ./path
1212

13-
expect "is_file: Bool.true\r\nis_dir: Bool.false\r\nis_sym_link: Bool.false\r\ntype: IsFile\r\n" {
13+
set expected_output [normalize_output {
14+
is_file: Bool.true
15+
is_dir: Bool.false
16+
is_sym_link: Bool.false
17+
type: IsFile
18+
}]
19+
20+
expect $expected_output {
1421
expect eof {
1522
check_exit_and_segfault
1623
}

ci/expect_scripts/print.exp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ source ./ci/expect_scripts/shared-code.exp
99

1010
spawn $env(EXAMPLES_DIR)print
1111

12-
expect "Hello, world!\r\nNo newline after me.Hello, error!\r\nErr with no newline after.Foo\r\nBar\r\nBaz\r\n" {
12+
set expected_output [normalize_output {
13+
Hello, world!
14+
No newline after me.Hello, error!
15+
Err with no newline after.Foo
16+
Bar
17+
Baz
18+
}]
19+
20+
expect $expected_output {
1321
expect eof {
1422
check_exit_and_segfault
1523
}

ci/expect_scripts/shared-code.exp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
1+
proc normalize_output {output} {
2+
# Remove leading newline for cleaner multiline string formatting
3+
regsub {^\n} $output "" result
4+
# Convert Unix line endings to Windows-style line endings for expect matching
5+
regsub -all {\n} $result "\r\n" result
6+
return $result
7+
}
8+
9+
proc show_diff {expected actual} {
10+
puts stderr "\n=== DIFF OUTPUT ==="
11+
puts stderr "Expected:"
12+
puts stderr [repr_string $expected]
13+
puts stderr "\nActual:"
14+
puts stderr [repr_string $actual]
15+
16+
# Line-by-line comparison
17+
set exp_lines [split $expected "\r\n"]
18+
set act_lines [split $actual "\r\n"]
19+
set max_lines [expr {max([llength $exp_lines], [llength $act_lines])}]
20+
21+
puts stderr "\nLine-by-line comparison:"
22+
for {set i 0} {$i < $max_lines} {incr i} {
23+
set exp_line [lindex $exp_lines $i]
24+
set act_line [lindex $act_lines $i]
25+
26+
if {$exp_line ne $act_line} {
27+
puts stderr "Line [expr {$i + 1}] differs:"
28+
puts stderr " Expected: [repr_string $exp_line]"
29+
puts stderr " Actual: [repr_string $act_line]"
30+
}
31+
}
32+
}
33+
34+
proc repr_string {str} {
35+
# Show invisible characters
36+
regsub -all {\r} $str "\\r" result
37+
regsub -all {\n} $result "\\n" result
38+
regsub -all {\t} $result "\\t" result
39+
return "\"$result\""
40+
}
41+
42+
proc expect_with_diff {expected_output} {
43+
expect {
44+
$expected_output {
45+
expect eof {
46+
check_exit_and_segfault
47+
}
48+
}
49+
eof {
50+
show_diff $expected_output $expect_out(buffer)
51+
puts stderr "\nExpect script failed: output was different from expected value."
52+
exit 1
53+
}
54+
timeout {
55+
puts stderr "\nExpect script failed: timeout waiting for expected output."
56+
puts stderr "Partial output received:"
57+
puts stderr [repr_string $expect_out(buffer)]
58+
exit 1
59+
}
60+
}
61+
}
62+
163
proc check_exit_and_segfault {} {
264
set status [wait]
365
set exit_code [lindex $status 2]

0 commit comments

Comments
 (0)