2017-02-23 19 views
2

我通过执行Cucumber::Cli::Main.new(args).execute!你怎么知道什么时候黄瓜:: Cli时:: Main.new(参数).execute执行完毕

但是Ruby移到下一行运行黄瓜脚本,并开始从读文件。但文件是空的,因为黄瓜需要时间来处理

如何使执行停止,直到Cucumber完成执行脚本并完成使用HTML填充文件。谢谢

这里是源代码的链接:https://github.com/cucumber/cucumber-ruby/blob/master/lib/cucumber/cli/main.rb

require 'cucumber' 
require 'tempfile' 
require 'securerandom' 

filename = "#{SecureRandom.urlsafe_base64}" 
file = Tempfile.new(filename) 
filepath = "#{file.path}" 
features = "features/login.feature" 
args = features.split.concat %w(--format html --out) 
args << "#{filepath}.html" 

begin 
    Cucumber::Cli::Main.new(args).execute! 
    @value = file.read 
ensure 
    file.close 
    file.unlink 
end 

编辑:

Cucumber::Cli::Main.new(args).execute!执行完毕后,它抛出一个SystemExit异常,并出现状态0

执行良好时退出代码为0

Cucumber在完成时总是抛出SystemExit异常。

这里是黄瓜源代码的链接:https://github.com/cucumber/cucumber-ruby/blob/master/lib/cucumber/cli/main.rb

如何处理在轨SystemExit例外,所以它不会跳跃执行的下一行。

def run 
    filename = "#{SecureRandom.urlsafe_base64}" 
    file = Tempfile.new(filename) 
    filepath = "#{file.path}" 
    features = "features/login.feature" 
    args = features.split.concat %w(-f html -o) 
    args << "#{filepath}.html" 
    Cucumber::Cli::Main.new(args).execute! # throws SystemExit Exception Status 0 
    @output = file.read 
    file.close 
    file.unlink 
    # More Code Below 
    # # # # # # # # # 
end 
+0

我在黄瓜里看不到任何异步的东西,所以不应该发生。 '执行!'应该阻止 – Anthony

+1

@Anthony黄瓜执行!抛出一个状态为0的SystemExit,所以发生异常并且下一行不会被执行。 –

回答

3

两个选择,我在这里看到,一个是捕获错误,然后读取该文件,如:

begin 
    Cucumber::Cli::Main.new(args).execute! 
rescue SystemExit => e 
    if e.status == 0 
    @value = file.read 
    else 
    raise e 
    end 
ensure 
    file.close 
    file.unlink 
end 

另一种选择是使从CLI继承的亚军类覆写exit_ok

class Runner < Cucumber::Cli::Main 
    def exit_ok 
    #NOOP 
    end 
end 

begin 
    Runner.new(args).execute! 
    @value = file.read 
ensure 
    file.close 
    file.unlink 
end 
+0

谢谢,真棒的东西。 @Anthony –

相关问题