Assertions for CLI Commands in CI/CD environment



  • I am working with a Cypress testing framework in a CI/CD environment. There are several Cypress smoke tests that run each time my team pushes a build. However, I would like to add tests that make assertions based on command line responses. Specifically, I want to ensure that there are no merge conflicts in the code base by creating a test that checks that "git grep --ignore-case "<<<< HEAD" results in 'not found'.

    What options do I have to implement such a test?



  • If you want to roll your own bash script you can model it on mine at

    https://durrantm.wordpress.com/2015/04/02/tdd-for-sh/
    See below.

    So you could do something like:

    # Define the function
    $ check_git () { git grep '>>>>> HEAD' && return 1; return 0; }
    
    # Call the function
    $ check_git
    # do something based on return code - $?
    # "To check the exit code we can simply print the $? special variable in bash.
    # This variable will print the exit code of the last run command"
    

    The code below is the full detail of my code which essentially lets you use

    do_test some_bash_function expected_result other_param1 other_param2 ...
    

    Code:

    #!/usr/bin/env bash
    . colors_for_text.sh 2> /dev/null
    
    do_test () {
      if [[ $# -eq 0 ]]; then
        printf "$fail_color Error - do_test: No parameters provided - Usage is do_test [function_name] \$LINENO [expected result] [optional params]\n"
        exit 1
      elif [[ $# -eq 1 ]]; then
        printf "$fail_color Error - do_test: Two parameters missing - \$LINENO and expected result (other params as needed)\n"
        exit 1
      fi
      declare -ig test_runs test_passes test_fails
      local -r function="$1";shift
      local -r line_number="$1";shift
      local failfast="False"
      local debug="False"
      local verbose="False"
      local test_description=
      local test_matcher=
      local expected=
      local pass_on_params=
      local command_not_found=False
      type "setup" &> /dev/null && setup
      # TODO Replace with for loop (initial attempts failed)
      [[ "$1" == "failfast" ]] && failfast="True" && shift
      [[ "$1" == "debug" ]] && debug="True" && shift
      [[ "$1" == "verbose" ]] && verbose="True" && shift
      [[ "$1" == "failfast" ]] && failfast="True" && shift
      [[ "$1" == "debug" ]] && debug="True" && shift
      [[ "$1" == "failfast" ]] && failfast="True" && shift
      if [[ "$1" =~ ^description= ]]; then
        [[ `echo $1 | grep =` ]] && test_description=`echo $1 | sed 's/.*=//'`' ' &&
        shift
      fi
      if [[ "$1" =~ ^matcher= ]]; then
        [[ `echo "$1" | grep =` ]] && test_matcher="`echo "$1" | sed 's/matcher=//'`" &&
        shift
      fi
      if [[ $# -eq 0 ]]; then
        printf "$fail_color Error - Function: $function, Line: $line_number \n
    do_test: Third parameter missing - expected result\n"
        exit 1
      fi
      expected="$1";shift
      pass_on_params="$@"
      $function ${pass_on_params[@]} 2> $TMPDIR"/output$$-"$test_runs".txt"
      cat $TMPDIR"/output$$-"$test_runs".txt" | grep -i "command not found" &&
        command_not_found=True
      debug_printf "(debug)"
      if [[ $command_not_found == "True" ]]; then
        record_script_failure "$function"
      elif [[ "$test_matcher" == "==" ]]; then
        [[ "$result" == $expected ]] &&
        record_test_success "$function" ||
        record_test_failure "$function";
      elif [[ "$expected" =~ ^-?[0-9]+$ ]]; then
        [[ "$result" -eq $expected ]] &&
        record_test_success "$function" ||
        record_test_failure "$function";
      else
        [[ "$result" == $expected ]] &&
        record_test_success "$function" ||
        record_test_failure "$function";
      fi
      test_runs+=1
    }
    record_test_success () {
      [[ $# -eq 0 ]] && no_param_quit $FUNCNAME
      local -r function="$1"
      printf "$pass_color"."$color_end"
      [[ "$verbose" == "True" ]] && printf "$function"
      test_passes+=1
    }
    record_test_failure () {
      [[ $# -eq 0 ]] && no_param_quit $FUNCNAME
      local -r function="$1"
      test_fails+=1
      printf "$fail_color"F"$color_end"
      function_exists? "$function" &&
        error_messages=$error_messages"$fail_color""$test_description""Line: $line_number - \
    '$function ${pass_on_params[@]}' failed:\n\
    Expected: $expected\n\
    Received: $result $color_end\n\n" ||
        error_messages=$error_messages"$fail_color""Line: $line_number: \
    Function '$function' is undefined\n"
      [[ "$failfast" == "True" ]] && failfast "Test" $error_messages
    }
    record_script_failure () {
      [[ $# -eq 0 ]] && no_param_quit $FUNCNAME
      local -r function="$1"
      test_fails+=1
      err=`cat $TMPDIR"/output$$-"$test_runs".txt"`
      printf $err
      error_messages=$error_messages"$fail_color""Line: $line_number: \
    Error within function '$function', getting \"command not found\" in $err \n"
      [[ "$failfast" == "True" ]] && failfast "Script" $error_messages
    }
    failfast () {
      printf "\n$1 error & failfast set to exit immediately.\n$2\n" && exit 1
    }
    
    no_param_quit () {
      printf "\nError - 1 parameter required for: $1 - exiting\n" && exit 1
    }
    function_exists?() {
        declare -f -F $1 > /dev/null
        return $?
    }
    debug_echo () {
      [[ "$debug" == "True" ]] && echo "$1"
    }
    debug_printf () {
      [[ "$debug" == "True" ]] && printf "$1"
    }
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2