【问题标题】:CasperJS pass exit code to BashCasperJS 将退出代码传递给 Bash
【发布时间】:2015-11-20 21:30:56
【问题描述】:

我在 Travis CI 上运行 CasperJS 测试时遇到问题。

只要测试失败,CasperJS 就会返回 status code 1,这将是失败测试时返回的正确状态代码。

我正在使用 bash 脚本运行我的所有测试,并且我需要 bash 脚本中测试的退出代码。我尝试了$? 运算符,但这只会返回命令是否正确执行。由于操作正确,它总是返回0

所以我的问题是:有没有办法将 CasperJs-Test 状态代码传递给我的 bash 脚本?

我需要这一切的原因是我在 Travis CI 上运行我的测试,而 Travis 总是以 status 0 退出,因为测试执行正确,我需要让 Travis 以正确的退出代码退出。

更新:

这是我的脚本:

#!/bin/sh


WIDGET_NAME=${1:-widget} # defaults to 'widget'
PORT=${2:-4001} # default port is 4001
SERVER_PORT=${3:-4002} # default port is 4002
TEST_CASES=${4:-./test/features/*/*/*-test.casper.js} # default run     all subdirectories

# bail on errors
set -e

# switch to root folder
cd `dirname $0`/..

echo "Starting feature tests ..."

echo "- start App server on port $PORT"
WIDGET_NAME_PASCAL_CASE=`node -e "console.log(require('pascal-case')    (process.argv[1]))" $WIDGET_NAME`
./node_modules/.bin/beefy app/widget.js $PORT \
  --cwd public \
  --index public/widget-test.html \
  -- \
  --standalone $WIDGET_NAME_PASCAL_CASE \
    -t [ babelify --sourceMapRelative . ] \
    -t browserify-shim \
  --exclude moment 1>/dev/null &
echo $! > /tmp/appointment-widget-tester-process1.pid
sleep 1

echo "- start Fake API server on port $SERVER_PORT"
bin/fake-api $SERVER_PORT 1>/dev/null &
echo $! > /tmp/appointment-widget-tester-process2.pid
sleep 1

echo "- run feature tests"
mocha-casperjs $TEST_CASES --viewport-width=800 --viewport-height=600 --fail-fast | grep --line-buffered -v -e '^$' | grep --line-buffered -v "Unsafe JavaScript"

echo "- stop App and Fake API server"
kill -9 `cat /tmp/appointment-widget-tester-process*.pid`
rm /tmp/appointment-widget-tester-process*.pid

echo "done."

【问题讨论】:

  • 看看这个问题的答案,他们可能会帮助stackoverflow.com/questions/17176452/…
  • 看到事情是我的 casper 实际上返回了正确的退出代码。我用casper.on('exit',function(msg) {casper.echo(msg(}) 对其进行了测试,失败时返回1,但是当我在命令后输入echo $? 时,我的bash 脚本仍然为0
  • 能否包含您的 bash 脚本(或说明问题的示例 bash 脚本)?我怀疑将 set -e 添加到 bash 脚本的顶部可能会解决问题,但也许有更好的解决方案
  • 如果casper 以非零退出状态退出,则$? 将在返回后立即包含该状态。如果您没有看到,那么可能会在两者之间运行其他东西并破坏 casper 返回状态。或者你有一些 casper 正在运行的包装器本身正在破坏它。无论哪种情况,显示您正在使用的脚本都是一个好主意。
  • 我已经添加了脚本!请善待并提供您的建议!

标签: bash casperjs travis-ci exit-code


【解决方案1】:

我发现了我的问题:

它在于| 运算符的本质!第一个操作是我的测试的开始,| 运算符之后的第二个操作是 grep 和我的 $? 对控制台上最后一个命令的引用,因此它返回的退出代码 grep 不是mocha-casperjs-runner

解决方案:Pipe output and capture exit status in Bash

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2019-12-20
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多