blob: eac35a763e8cc486daf08ff150f6806763076601 [file] [log] [blame]
Michal Vasko832a6fa2022-10-24 08:31:17 +02001# detect the path to the yanglint binary
2if { [info exists ::env(YANGLINT)] } {
3 set yanglint "$env(YANGLINT)"
4} else {
5 set yanglint "../../../../build/yanglint"
6}
7
aPieceka753e482023-03-15 09:02:56 +01008# detect the path to the examples
9if { [info exists ::env(CURRENT_SOURCE_DIR)] } {
10 set yang_dir "$env(CURRENT_SOURCE_DIR)/examples"
11} else {
12 set yang_dir "../../examples"
13}
14
15# set the variable used to print the error message
16if { ![info exists error_verbose] } {
17 set error_verbose 1
18}
19
20# prompt of yanglint
21set prompt "> "
22# prompt of error message
23set error_prompt ">>>"
24# the beginning of error message
25set error_head "$error_prompt Check-failed"
romane84f7dc2022-10-21 10:25:38 +020026# set the timeout to 1 second
27set timeout 1
28
aPieceka753e482023-03-15 09:02:56 +010029# detection on eof and timeout will be on every expect command
30expect_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
romane84f7dc2022-10-21 10:25:38 +020039 }
40}
41
aPieceka753e482023-03-15 09:02:56 +010042# Internal function. Print error message and exit script with an error return value.
43proc check_failed {pattern output} {
44 global error_verbose
45 global error_prompt
46 global error_head
romane84f7dc2022-10-21 10:25:38 +020047
aPieceka753e482023-03-15 09:02:56 +010048 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"
romane84f7dc2022-10-21 10:25:38 +020058 }
aPieceka753e482023-03-15 09:02:56 +010059 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
67proc 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.
74proc 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)
romane84f7dc2022-10-21 10:25:38 +020091 }
92 }
93}
94
aPieceka753e482023-03-15 09:02:56 +010095# whatever is written is sent, output is ignored and then another prompt is expected
96proc next_prompt {} {
97 global prompt
98
99 send "\r"
100 expect -re "$prompt$"
101}
102
romane84f7dc2022-10-21 10:25:38 +0200103# send a completion request and check if the anchored regex output matches
104proc expect_completion {input output} {
aPieceka753e482023-03-15 09:02:56 +0100105 global prompt
romane84f7dc2022-10-21 10:25:38 +0200106
aPieceka753e482023-03-15 09:02:56 +0100107 send -- "${input}\t"
108 # expecting echoing input, output and 10 terminal control characters
109 expect -re "^${input}\r> ${output}.*\r.*$"
romane84f7dc2022-10-21 10:25:38 +0200110}
111
112# send a completion request and check if the anchored regex hint options match
113proc 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"
aPieceka753e482023-03-15 09:02:56 +0100121 # 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}
romane84f7dc2022-10-21 10:25:38 +0200125
aPieceka753e482023-03-15 09:02:56 +0100126# send 'exit' and wait for eof
127proc send_exit {} {
128 send "exit\r"
129 expect eof
romane84f7dc2022-10-21 10:25:38 +0200130}