aPiecek | 266ca76 | 2023-03-22 15:04:59 +0100 | [diff] [blame] | 1 | source [expr {[info exists ::env(TESTS_DIR)] ? "$env(TESTS_DIR)/common.tcl" : "../common.tcl"}] |
| 2 | |
| 3 | # namespace of internal functions |
| 4 | namespace eval ly::private { |
| 5 | namespace export * |
| 6 | } |
| 7 | |
| 8 | # Run the process with arguments. |
| 9 | # Parameter cmd is a string with arguments. |
| 10 | # Parameter wrn is a flag. Set to 1 if stderr should be ignored. |
| 11 | # Returns a pair where the first is the return code and the second is the output. |
| 12 | proc ly::private::ly_exec {cmd {wrn ""}} { |
| 13 | try { |
| 14 | set results [exec -- $::env(YANGLINT) {*}$cmd] |
| 15 | set status 0 |
| 16 | } trap CHILDSTATUS {results options} { |
| 17 | # return code is not 0 |
| 18 | set status [lindex [dict get $options -errorcode] 2] |
| 19 | } trap NONE results { |
| 20 | if { $wrn == 1 } { |
| 21 | set status 0 |
| 22 | } else { |
| 23 | error "return code is 0 but something was written to stderr:\n$results\n" |
| 24 | } |
| 25 | } trap CHILDKILLED {results options} { |
| 26 | set status [lindex [dict get $options -errorcode] 2] |
| 27 | error "process was killed: $status" |
| 28 | } |
| 29 | list $status $results |
| 30 | } |
| 31 | |
| 32 | # Internal function. |
| 33 | # Check the output with pattern. |
| 34 | # Parameter pattern is a regex or an exact string to match. |
| 35 | # Parameter msg is the output to check. |
| 36 | # Parameter 'opt' is optional. If contains '-ex', then the 'pattern' parameter is |
| 37 | # used as a simple string for exact matching of the output. |
| 38 | proc ly::private::output_check {pattern msg {opt ""}} { |
| 39 | if { $opt eq "" } { |
| 40 | expr {![regexp -- $pattern $msg]} |
| 41 | } elseif { $opt eq "-ex" } { |
| 42 | expr {![string equal "$pattern" $msg]} |
| 43 | } else { |
| 44 | global error_head |
| 45 | error "$error_head unrecognized value of parameter 'opt'" |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | # Execute yanglint with arguments and expect success. |
| 50 | # Parameter cmd is a string of arguments. |
| 51 | # Parameter pattern is a regex or an exact string to match. |
| 52 | # Parameter 'opt' is optional. If contains '-ex', then the 'pattern' parameter is |
| 53 | # used as a simple string for exact matching of the output. |
| 54 | proc ly_cmd {cmd {pattern ""} {opt ""}} { |
| 55 | namespace import ly::private::* |
| 56 | lassign [ly_exec $cmd] rc msg |
| 57 | if { $rc != 0 } { |
| 58 | error "unexpected return code $rc:\n$msg\n" |
| 59 | } |
| 60 | if { $pattern ne "" && [output_check $pattern $msg $opt] } { |
| 61 | error "unexpected output:\n$msg\n" |
| 62 | } |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | # Execute yanglint with arguments and expect error. |
| 67 | # Parameter cmd is a string of arguments. |
| 68 | # Parameter pattern is a regex. |
| 69 | proc ly_cmd_err {cmd pattern} { |
| 70 | namespace import ly::private::* |
| 71 | lassign [ly_exec $cmd] rc msg |
| 72 | if { $rc == 0 } { |
| 73 | error "unexpected return code $rc" |
| 74 | } |
| 75 | if { [output_check $pattern $msg] } { |
| 76 | error "unexpected output:\n$msg\n" |
| 77 | } |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | # Execute yanglint with arguments, expect warning in stderr but success. |
| 82 | # Parameter cmd is a string of arguments. |
| 83 | # Parameter pattern is a regex. |
| 84 | proc ly_cmd_wrn {cmd pattern} { |
| 85 | namespace import ly::private::* |
| 86 | lassign [ly_exec $cmd 1] rc msg |
| 87 | if { $rc != 0 } { |
| 88 | error "unexpected return code $rc:\n$msg\n" |
| 89 | } |
| 90 | if { [output_check $pattern $msg] } { |
| 91 | error "unexpected output:\n$msg\n" |
| 92 | } |
| 93 | return |
| 94 | } |
aPiecek | 6e5844c | 2023-05-22 11:42:09 +0200 | [diff] [blame] | 95 | |
| 96 | # Check if yanglint supports the specified option. |
| 97 | # Parameter opt is a option to be found. |
| 98 | # Return true if option is found otherwise false. |
| 99 | proc ly_opt_exists {opt} { |
| 100 | namespace import ly::private::* |
| 101 | lassign [ly_exec "--help"] rc msg |
| 102 | if { $rc != 0 } { |
| 103 | error "unexpected return code $rc:\n$msg\n" |
| 104 | } |
| 105 | if { [output_check $opt $msg] } { |
| 106 | return false |
| 107 | } else { |
| 108 | return true |
| 109 | } |
| 110 | } |