将bash脚本错误传递给rails中的控制台

时间:2011-06-09 14:15:15

标签: ruby-on-rails ruby bash shell

我有一个shell脚本,它为我正在处理的应用程序运行一些验收测试。脚本运行测试,检查是否有错误,然后以0(成功)或1(失败)退出。

我有一个调用shell脚本的rake任务,然后获取结果。我遇到的问题是,如何将该结果传递给rails控制台,以便在echo $?时它与shell脚本返回的值相等?

我目前的代码如下:

def acceptance_tests
    system("./run_tests.sh");
    error_code = $?.success? ? 0 : 1

    result = error_code == 0 ? 'passed' : 'failed'
    puts ("The acceptance tests have #{result}.")

    SystemExit.new(error_code)
end

测试在我运行时按预期通过/失败,但在完成后,我运行echo $?并且它总是等于0

关于我做错了什么的想法?

2 个答案:

答案 0 :(得分:0)

SystemExit是一个例外,所以提高它:

$ echo "raise SystemExit.new(5)" | ruby; echo $?
5

答案 1 :(得分:0)

最后,将SystemExit.new()更改为exit()对我有用。

def acceptance_tests
    system("./run_tests.sh");
    error_code = $?.success? ? 0 : 1

    result = error_code == 0 ? 'passed' : 'failed'
    puts ("The acceptance tests have #{result}.")

    exit(error_code)
end