Michal Vasko | 832a6fa | 2022-10-24 08:31:17 +0200 | [diff] [blame] | 1 | # detect the path to the yanglint binary |
| 2 | if { [info exists ::env(YANGLINT)] } { |
| 3 | set yanglint "$env(YANGLINT)" |
| 4 | } else { |
| 5 | set yanglint "../../../../build/yanglint" |
| 6 | } |
| 7 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 8 | # detect the path to the examples |
| 9 | if { [info exists ::env(CURRENT_SOURCE_DIR)] } { |
aPiecek | 24b8fa6 | 2023-03-15 09:30:00 +0100 | [diff] [blame^] | 10 | set yang_models "$env(CURRENT_SOURCE_DIR)/tests/models" |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 11 | } else { |
aPiecek | 24b8fa6 | 2023-03-15 09:30:00 +0100 | [diff] [blame^] | 12 | set yang_models "../models" |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | # set the variable used to print the error message |
| 16 | if { ![info exists error_verbose] } { |
| 17 | set error_verbose 1 |
| 18 | } |
| 19 | |
| 20 | # prompt of yanglint |
| 21 | set prompt "> " |
| 22 | # prompt of error message |
| 23 | set error_prompt ">>>" |
| 24 | # the beginning of error message |
| 25 | set error_head "$error_prompt Check-failed" |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 26 | # set the timeout to 1 second |
| 27 | set timeout 1 |
| 28 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 29 | # detection on eof and timeout will be on every expect command |
| 30 | expect_after { |
| 31 | eof { |
| 32 | global error_head |
| 33 | send_error "\n$error_head unexpected termination.\n" |
| 34 | exit 1 |
| 35 | } timeout { |
| 36 | global error_head |
| 37 | send_error "\n$error_head timeout.\n" |
| 38 | exit 1 |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 42 | # Internal function. Print error message and exit script with an error return value. |
| 43 | proc check_failed {pattern output} { |
| 44 | global error_verbose |
| 45 | global error_prompt |
| 46 | global error_head |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 47 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 48 | set frame [info frame 1] |
| 49 | set line [dict get $frame line] |
| 50 | set file [lindex [split [dict get $frame file] /] end] |
| 51 | switch $error_verbose { |
| 52 | 0 {} |
| 53 | 1 { send_error "\n$error_head in $file on line $line\n" } |
| 54 | 2 { send_error "\n$error_head in $file on line $line, output is:\n$output\n" } |
| 55 | 3 { |
| 56 | send_error "\n$error_head in $file on line $line, expecting:\n$pattern\n" |
| 57 | send_error "$error_prompt but the output is:\n$output\n" |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 58 | } |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 59 | default { send_error "\n$error_head unrecognized entry \"$error_verbose\" in error_verbose variable.\n" } |
| 60 | } |
| 61 | close |
| 62 | wait |
| 63 | exit 1 |
| 64 | } |
| 65 | |
| 66 | # skip no dir and/or no history warnings and prompt |
| 67 | proc skip_warnings {} { |
| 68 | global prompt |
| 69 | expect -re "(YANGLINT.*)*$prompt" {} |
| 70 | } |
| 71 | |
| 72 | # Send command (cmd) to the yanglint, then check output string (pattern) and prompt. |
| 73 | # If parameter pattern is not specified, only the prompt assumed afterwards. |
| 74 | proc command {cmd {pattern ""}} { |
| 75 | global prompt |
| 76 | |
| 77 | send -- "${cmd}\r" |
| 78 | expect "${cmd}\r\n" |
| 79 | |
| 80 | if { $pattern eq "" } { |
| 81 | # command without output |
| 82 | expect $prompt |
| 83 | return |
| 84 | } |
| 85 | # check output |
| 86 | expect { |
| 87 | -re "^${pattern}\r\n${prompt}$" {} |
| 88 | -indices -re "(.*)\r\n${prompt}$" { |
| 89 | # Pattern does not match the output. Print error and exit the script. |
| 90 | check_failed $pattern $expect_out(1,string) |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 95 | # whatever is written is sent, output is ignored and then another prompt is expected |
| 96 | proc next_prompt {} { |
| 97 | global prompt |
| 98 | |
| 99 | send "\r" |
| 100 | expect -re "$prompt$" |
| 101 | } |
| 102 | |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 103 | # send a completion request and check if the anchored regex output matches |
| 104 | proc expect_completion {input output} { |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 105 | global prompt |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 106 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 107 | send -- "${input}\t" |
| 108 | # expecting echoing input, output and 10 terminal control characters |
| 109 | expect -re "^${input}\r> ${output}.*\r.*$" |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | # send a completion request and check if the anchored regex hint options match |
| 113 | proc expect_hint {input prev_input hints} { |
| 114 | set output {} |
| 115 | foreach i $hints { |
| 116 | # each element might have some number of spaces and CRLF around it |
| 117 | append output "${i} *(?:\\r\\n)?" |
| 118 | } |
| 119 | |
| 120 | send -- "${input}\t" |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 121 | # expecting the hints, previous input from which the hints were generated |
| 122 | # and some number of terminal control characters |
| 123 | expect -re "^\r\n${output}\r> ${prev_input}.*\r.*$" |
| 124 | } |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 125 | |
aPiecek | a753e48 | 2023-03-15 09:02:56 +0100 | [diff] [blame] | 126 | # send 'exit' and wait for eof |
| 127 | proc send_exit {} { |
| 128 | send "exit\r" |
| 129 | expect eof |
roman | e84f7dc | 2022-10-21 10:25:38 +0200 | [diff] [blame] | 130 | } |