tests: fix input tests with expect, not covering false negatives (#19518)

This commit is contained in:
Turiiya 2023-10-10 19:37:21 +02:00 committed by GitHub
parent a1c16a76be
commit c4ee940047
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 124 additions and 43 deletions

View file

@ -99,13 +99,9 @@ jobs:
./v3 version ./v3 version
./v3 -o tetris -usecache examples/tetris/tetris.v ./v3 -o tetris -usecache examples/tetris/tetris.v
- name: Test password input - name: Test password input
run: | run: ./v test examples/password/
./v examples/password
./v examples/password/password_ci.vsh
- name: Test readline - name: Test readline
run: | run: ./v test examples/readline/
./v examples/readline/readline_ci.v
./v examples/readline/readline.vsh
ubuntu-tcc-boehm-gc: ubuntu-tcc-boehm-gc:
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04

View file

@ -113,10 +113,6 @@ jobs:
run: | run: |
./v -o v2 -parallel-cc cmd/v ./v -o v2 -parallel-cc cmd/v
- name: Test password input - name: Test password input
run: | run: ./v test examples/password/
./v examples/password
./v examples/password/password_ci.vsh
- name: Test readline - name: Test readline
run: | run: ./v test examples/readline/
./v examples/readline/readline_ci.v
./v examples/readline/readline.vsh

View file

@ -1,8 +0,0 @@
#!/usr/bin/expect
spawn ./password
expect "Enter your password : "
send "Sample\r"
expect "Confirm password : "
send "Sample\r"
expect "Password confirmed! You entered: Sample ."
expect eof

View file

@ -1,8 +0,0 @@
#!/usr/bin/expect
spawn ./password
expect "Enter your password : "
send "Sample123\r"
expect "Confirm password : "
send "Sample234\r"
expect "Passwords do not match ."
expect eof

View file

@ -1,3 +0,0 @@
chdir('examples/password')!
assert execute('./correct.expect').exit_code == 0
assert execute('./incorrect.expect').exit_code == 0

View file

@ -0,0 +1,27 @@
import os
const (
// Expect has to be installed for the test.
expect_exe = os.find_abs_path_of_executable('expect') or {
eprintln('skipping test, since expect is missing')
exit(0)
}
// Directory that contains the Expect scripts used in the test.
expect_tests_path = os.join_path(@VMODROOT, 'examples', 'password', 'tests')
)
fn test_password_input() {
correct := os.execute(os.join_path(expect_tests_path, 'correct.expect'))
assert correct.exit_code == 0, correct.output
incorrect := os.execute(os.join_path(expect_tests_path, 'incorrect.expect'))
assert incorrect.exit_code == 0, incorrect.output
expected_out := 'Enter your password : '
mut res := os.execute('${os.join_path(expect_tests_path, 'output_from_expect_arg.expect')} "${expected_out}"')
assert res.exit_code == 0, res.output
not_exptectd_out := 'Enter your passwords : '
res = os.execute('${os.join_path(expect_tests_path, 'output_from_expect_arg.expect')} "${not_exptectd_out}"')
assert res.exit_code == 1, res.output
}

View file

@ -0,0 +1,12 @@
#!/usr/bin/expect
set timeout 3
set v_root [exec sh -c "git rev-parse --show-toplevel"]
spawn $v_root/v run $v_root/examples/password/password.v
expect "Enter your password : " { send "Sample\r" } timeout { exit 1 }
expect "Confirm password : " { send "Sample\r" } timeout { exit 1 }
expect "Password confirmed! You entered: Sample ." {} timeout { exit 1 }
expect eof

View file

@ -0,0 +1,12 @@
#!/usr/bin/expect
set timeout 3
set v_root [exec sh -c "git rev-parse --show-toplevel"]
spawn $v_root/v run $v_root/examples/password/password.v
expect "Enter your password : " { send "Sample123\r" } timeout { exit 1 }
expect "Confirm password : " { send "Sample234\r" } timeout { exit 1 }
expect "Passwords do not match ." {} timeout { exit 1 }
expect eof

View file

@ -0,0 +1,12 @@
#!/usr/bin/expect
set timeout 3
set v_root [exec sh -c "git rev-parse --show-toplevel"]
# Send expected output as arg to re-use the script for testing incorrect values.
set expect_ [lindex $argv 0]
spawn $v_root/v run $v_root/examples/password/password.v
expect $expect_ {} timeout { exit 1 }
expect eof

View file

@ -1,9 +0,0 @@
#!/usr/bin/expect
spawn ./readline_ci
send "a"
expect "got 97"
send "1"
expect "got 49"
send "q"
expect "Goodbye."
expect eof

View file

@ -8,9 +8,10 @@ fn main() {
fn run() ! { fn run() ! {
$if windows { $if windows {
eprintln('skipping test on windows for now') eprintln('skipping on Windows, since raw mode and read_char are not yet implemented.')
return return
} $else { } $else {
// Explicit comptime block for other OSes than Windows is required to not break compilation on Windows.
mut r := readline.Readline{} mut r := readline.Readline{}
r.enable_raw_mode_nosig() r.enable_raw_mode_nosig()
defer { defer {

View file

@ -1,2 +0,0 @@
chdir('examples/readline')!
assert execute('./correct.expect').exit_code == 0

View file

@ -0,0 +1,26 @@
import os
const (
// Expect has to be installed for the test.
expect_exe = os.find_abs_path_of_executable('expect') or {
eprintln('skipping test, since expect is missing')
exit(0)
}
// Directory that contains the Expect scripts used in the test.
expect_tests_path = os.join_path(@VMODROOT, 'examples', 'readline', 'tests')
)
fn test_password_input() {
correct := os.execute(os.join_path(expect_tests_path, 'readline.expect'))
assert correct.exit_code == 0, correct.output
send_a := 'a'
expect_a := 'got 97' // readline output for `a`
a_res := os.execute('${os.join_path(expect_tests_path, 'readline_from_expect_arg.expect')} ${send_a} "${expect_a}"')
assert a_res.exit_code == 0, a_res.output
send_b := 'b'
b_res := os.execute('${os.join_path(expect_tests_path, 'readline_from_expect_arg.expect')} ${send_b} "${expect_a}"')
assert b_res.exit_code == 1, b_res.output
assert b_res.output.contains('got 98')
}

View file

@ -0,0 +1,15 @@
#!/usr/bin/expect
set timeout 3
set v_root [exec sh -c "git rev-parse --show-toplevel"]
spawn $v_root/v run $v_root/examples/readline/readline.v
send "a"
expect "got 97" {} timeout { exit 1 }
send "1"
expect "got 49" {} timeout { exit 1 }
send "q"
expect "Goodbye." {} timeout { exit 1 }
expect eof

View file

@ -0,0 +1,14 @@
#!/usr/bin/expect
set timeout 3
set v_root [exec sh -c "git rev-parse --show-toplevel"]
# Use input arguments for send and expect.
set send_ [lindex $argv 0]
set expect_ [lindex $argv 1]
spawn $v_root/v run $v_root/examples/readline/readline.v
send $send_
expect $expect_ {} timeout { exit 1 }
expect eof