Skip to content

Instantly share code, notes, and snippets.

@bgschiller
Created April 20, 2018 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgschiller/397ad1ca123aa15f331f3f3ec9717e5f to your computer and use it in GitHub Desktop.
Save bgschiller/397ad1ca123aa15f331f3f3ec9717e5f to your computer and use it in GitHub Desktop.
Workaround for "cypress ci hangs on codeship" bug

Workaround for "Cypress build hangs on codeship"

cypress-io/cypress#328

The plan: Wrap cypress in a script that watches its output, kills the process when cypress is done.

We'll need to capture the line Failures: 0 or Failures: 5 in order for the wrapper script to produce an appropriate exit code. That is, it should sys.exit(0) if all tests passed, and sys.exit(1) if any failed.

Here we run into a bit of a problem: The ANSI escape codes that Cypress uses to color it's output include numbers to specify the color. For example, green text is prepended with \e[30m. Even though these color codes aren't visible in the terminal, pexpect can see them, and thinks that they are the number of failed tests. So we need to strip out all color codes prior to pexpect seeing the output. That's the perl one-liner in the script.

Make sure you pip install pexpect on your CI server.

import sys
import pexpect
# This line represents workarounds for two bugs:
# 1. https://github.com/pexpect/pexpect/issues/373
# pexpect fails on MacOS without giving output. The solution seems to be wrapping the command in 'bash -c'
# 2. cypress gives output with ANSI color codes. These codes look like
# \e[30mFailures\e[39m \e[30m0
# This turns the text green, but also introduces new characters to the stream. Since some of these are numbers,
# pexpect was catching those and saying "lookie here, 3 failed tests!" because there is a 3 in the color code.
# Since there's no option for turning this off, we pipe to a perl one-liner that strips these color codes.
run_tests_without_colors = """bash -c './node_modules/.bin/cypress run --browser chrome | perl -pe "s/\\e\\[?.*?[\\@-~]//g" ' """
cypress = pexpect.spawn(
run_tests_without_colors,
# your tests will likely take more than 30 seconds, so disable the timeout.
timeout=None,
# Ask pexpect to copy output from the subprocess to stdout (so that we can see it in the CI logs)
logfile=sys.stdout,
)
cypress.expect('Tests Finished')
cypress.expect('Failures')
# code is 0 if the first pattern matched, 1 if the second did.
code = cypress.expect(['0', '\d'])
cypress.expect('All Done')
# Kill cypress, since it hangs indefinitely on codeship (https://github.com/cypress-io/cypress/issues/328)
cypress.terminate(force=True)
# terminate with an exit code corresponding to the number of failed tests
sys.exit(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment