【问题标题】:Is it possible to send arguments to `rake cucumber` without using environment variables?是否可以在不使用环境变量的情况下向“rake cucumber”发送参数?
【发布时间】:2012-10-23 07:02:18
【问题描述】:

我发现可以将参数传递给 rake 任务:

task :task_name, :arg_name do |t, args|

我想做的是将参数传递给黄瓜耙任务:

Cucumber::Rake::Task.new({:tags => 'db:test:prepare'}) do |t, args|
  t.cucumber_opts = ['--tags', #args?]
end

这种事情可能吗?这样我可以做到:

rake cucumber:tags['tag1', 'tag2', ...]

让它只运行那些标签。大多数消息来源都说要使用环境变量,我已经这样做了,但我更愿意以“正确”的方式提供参数。

【问题讨论】:

  • 不,不是。此问题特定于 Cucumber,您不能像在该问题的答案中那样为 Cucumber rake 任务添加参数。
  • 明白了。是否可以编写自己的 rake 任务来包装黄瓜?

标签: ruby-on-rails cucumber rake


【解决方案1】:

您可以通过执行以下操作来使其工作:

task :task_name, :arg1 do |t, args|
  Cucumber::Rake::Task.new(:run) do |t|
    t.cucumber_opts = "--format pretty --tags @#{args[:arg1]}"
  end
  Rake::Task[:run].invoke()
end

【讨论】:

    【解决方案2】:

    这样做的一种方法是通过像这样的环境变量:rake cucumber:tags TAGS=tag1,tag2,然后在您的 rake 任务中解析 ARGV

    tags = ARGV[1].gsub(/.+=/, '')
    
    Cucumber::Rake::Task.new({:tags => 'db:test:prepare'}) do |t, args|
      t.cucumber_opts = ['--tags', tags]
    end
    

    你也可以像这样让它更灵活

    tags_index = ARGV.index {|a| a.start_with?('TAGS') }
    tags = ARGV[tags_index].gsub(/.+=/, '')
    

    但您可能会更好地使用 OptionParser 之类的东西。

    【讨论】:

      猜你喜欢
      • 2011-07-24
      • 1970-01-01
      • 2017-11-11
      • 2017-02-07
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多