github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/functional/procs/shared_procs.tcl (about)

     1  #!/usr/bin/expect
     2  
     3  proc Feature: {name} {
     4    set ::_current_feature "$name"
     5    print_msg "FEATURE: $name" magenta
     6    add_to_report "\nFEATURE: $name"
     7    set ::_scenarios_count 0
     8    set ::_tests_errors_by_feature 0
     9    set ::_tests_failed_by_feature 0
    10    set ::_junit_scenarios_content ""
    11    set ::_time_by_feature [clock milliseconds]
    12  }
    13  
    14  proc end_feature {name} {
    15    set end [clock milliseconds]
    16    set time [expr {$end - $::_time_by_feature}]
    17    append ::_junit_features_content "<testsuite hostname='localhost' id='$name' name='$name' tests='$::_scenarios_count' time='$time' errors='$::_tests_errors_by_feature' failures='$::_tests_failed_by_feature'>"
    18    append ::_junit_features_content $::_junit_scenarios_content
    19    append ::_junit_features_content "</testsuite>"
    20    print_msg "COMPLETED FEATURE: $name in $time milliseconds" magenta
    21    add_to_report "\nCOMPLETED FEATURE: $name in $time milliseconds"
    22  }
    23  
    24  proc begin_scenario {name} {
    25    set ::_current_scenario "$name"
    26    set ::_junit_scenarios_error_content ""
    27    incr ::_tests_total 1
    28    print_msg "SCENARIO: $name" magenta
    29    add_to_report "\nSCENARIO: $name"
    30    incr ::_scenarios_count 1
    31    set ::_time_by_scenario [clock milliseconds]
    32  }
    33  
    34  proc end_scenario {name} {
    35    set end [clock milliseconds]
    36    set time [expr {$end - $::_time_by_scenario}]
    37    append ::_junit_scenarios_content "<testcase id='$name' name='$name' time='$time'>"
    38    append ::_junit_scenarios_content $::_junit_scenarios_error_content
    39    append ::_junit_scenarios_content "</testcase>"
    40  
    41    print_msg "\nCOMPLETED SCENARIO: $name in $time milliseconds" magenta
    42    add_to_report "\nCOMPLETED SCENARIO: $name in $time milliseconds"
    43  }
    44  
    45  proc Scenario: {name script {teardown ""}} {
    46    begin_scenario $name
    47  
    48    while {1} {
    49      eval $script
    50      break
    51    }
    52  
    53    if { $teardown != "" } {
    54      print_msg "Teardown in progress..."
    55      eval $teardown
    56      print_msg "Teardown complete"
    57    }
    58  
    59    end_scenario $name
    60  }
    61  
    62  # place - in front of Scenario to skip it
    63  proc -Scenario: {name script {teardown ""}} {
    64  }
    65  
    66  proc create_report {} {
    67    # cleanup/initialize test report file
    68    set report_file [open $::_test_report w]
    69    close $report_file
    70  }
    71  
    72  proc add_to_report {text} {
    73    set file [open $::_test_report a+]
    74    puts $file $text
    75    close $file
    76  }
    77  
    78  proc add_to_junit_report {text} {
    79    set file [open $::_junit_test_report w]
    80    puts $file $text
    81    close $file
    82  }
    83  
    84  proc control_c {} {
    85    send \003
    86    expect {
    87      timeout { error "^C failed" }
    88      "$::_root_dir"
    89    }
    90  }
    91  
    92  proc exit_shell {} {
    93    send "exit\r"
    94    expect {
    95      timeout { error "Failed to exit shell" }
    96      "$::_root_dir"
    97    }
    98  }
    99  
   100  proc expectation_not_met {message} {
   101    incr ::_tests_failed 1
   102    incr ::_tests_failed_by_feature 1
   103    print_msg_stderr "Expectation not met: $message"
   104    set stack [print_stack]
   105    add_to_report "Expectation Not Met Error: $message\n$stack"
   106    set timeout $::_default_timeout
   107    append ::_junit_scenarios_error_content "<failure>Expectation Not Met Error: $message\n$stack</failure>"
   108  }
   109  
   110  proc handle_timeout {{message ""}} {
   111    incr ::_tests_failed 1
   112    incr ::_tests_failed_by_feature 1
   113    print_msg_stderr "Timeout Error: $message"
   114    set stack [print_stack]
   115    add_to_report "Timeout Error: $message\n$stack"
   116    append ::_junit_scenarios_error_content "<failure>Timeout Error: $message\n$stack</failure>"
   117    set timeout $::_default_timeout
   118    control_c
   119  }
   120  
   121  proc print_msg {text {color cyan}} {
   122    puts [get_msg $text $color]
   123  }
   124  
   125  proc print_msg_stderr {text {color red}} {
   126    puts stderr [get_msg $text $color]
   127  }
   128  
   129  proc get_msg {text color} {
   130    switch $color {
   131      green { set color_code 32 }
   132      magenta { set color_code 35 }
   133      red { set color_code 31 }
   134      cyan -
   135      default { set color_code 36 }
   136    }
   137  
   138    return "\033\[01;$color_code;m$text \033\[0;m\n"
   139  }
   140  
   141  proc print_stack {} {
   142    set stack_size [info frame]
   143    set stack_payload_size [expr {$stack_size - 3}]
   144    set stack {}
   145  
   146    for { set frame_index $stack_payload_size } { $frame_index >= 1 } { incr frame_index -1 } {
   147      set frame [info frame $frame_index]
   148      set cmd [dict get $frame cmd]
   149      set file -
   150      set line -
   151      if { [dict exists $frame file] } { set file [dict get $frame file] }
   152      if { [dict exists $frame line] } { set line [dict get $frame line] }
   153  
   154      set max_string_size 120
   155      if { [string length $cmd] > $max_string_size } {
   156        set cmd "[string range $cmd 0 $max_string_size]..."
   157      }
   158  
   159      set stack_line "[file tail $file], line $line\n  $cmd"
   160  
   161      lappend stack $stack_line
   162    }
   163  
   164    puts [join $stack "\n"]
   165    return [join $stack "\n"]
   166  }
   167  
   168  proc prefixed_project {project} {
   169    return ${::project_prefix}$project
   170  }
   171  
   172  proc delete_projects {projects} {
   173    foreach project $projects {
   174      exec $::bin delete -p [prefixed_project $project] --force
   175    }
   176  }
   177  
   178  proc login {email password} {
   179    spawn $::bin login --no-browser
   180    expect {
   181      timeout {
   182        print_msg_stderr "\nLogin timed out\n"
   183        exit 1
   184      }
   185      "Your email:" {
   186        send "$email\r"
   187        expect "Now, your password:"
   188        send "$password\r"
   189        expect {
   190          timeout {
   191            print_msg_stderr "\nLogin timed out\n"
   192            exit 1
   193          }
   194          "Authentication failed" {
   195            print_msg_stderr "\nLogin failed\n"
   196            exit 1
   197          }
   198          "Type a command and press Enter to execute it."
   199        }
   200      }
   201      "Already logged in"
   202    }
   203  }
   204  
   205  proc logout {email} {
   206    send "$::bin logout\r"
   207    expect {
   208      timeout {
   209        print_msg_stderr "\nLogout timed out\n"
   210      }
   211      "You are not logged in" {}
   212      "You ($email) have been logged out"
   213    }
   214  }