rspecgithub-actions

How does a GitHub Actions workflow notice that a unit test has failed?


Finished in 0.0137 seconds (files took 0.0769 seconds to load)
5 examples, 1 failure

Failed examples:

rspec ./spec/basic_math_spec.rb:10 # Basic-Math Subtract numbers

Error: Process completed with exit code 1.

GitHub Actions marks the testing-step correctly as erroneous.

But how does GitHub Actions know that the tests have failed?

RSpec is just some command-line software, which is executed. How is it reported upwards and then reported, that something didn't work as expected?


Solution

  • But that's just some string, printed to the console. How does the actual mechanism work?

    Well, what you see is not just tests being run, but an output of another program that runs the tests. A runner, or wrapper if you will. We can write one in bash ourselves

    ls "foo" # we can another command instead of running tests
    if [ $? -ne 0 ]; then
      echo "Directory 'foo' does not exist."
    else
      echo "Directory 'foo' exists."
    fi
    

    And if you run it:

    $ ./runner.sh
    ls: cannot access 'foo': No such file or directory
    Directory 'foo' does not exist.
    

    We essentially have a program that runs another program, and inspects it's return code (bash holds the last run program's return code in $? var)

    This is a standard across operating systems for program to return a status upon it's execution. The successful runs usually return 0 (I'm not sure if it's universal rule, though).

    In Ruby (since you're asking about rspec, I'll asume this is what you're familiar with) you have https://ruby-doc.org/core-2.6/Process.html#method-c-exit to control (to an extent) what your program returns.