<commit_before>When /^I run "([^"]*)" with the following arguments:$/ do |script, table|
  @arguments = []
  table.hashes.each do |attrs|
    @arguments << attrs["argument"]
  end

  command = "bin/#{script} #{@arguments.join(' ')}"

  @output = `#{command}`
  $?.exitstatus.should == 0
end

Then /^I should see a batch of checks in the output$/ do
  @data["batches"].should_not be_nil
  @data["batches"].size.should > 0

  @data["batches"].each do |batch|
    batch["checks"].should_not be_nil
    batch["checks"].size.should > 0

    batch["checks"].each do |check|
      check["id"].should_not be_nil
      check["command"].should_not be_nil
    end
  end
end

Then /^I should see a batch of checks with relationships in the output$/ do
  @data["batches"].should_not be_nil
  @data["batches"].size.should > 0

  @data["batches"].each do |batch|
    batch["checks"].should_not be_nil
    batch["checks"].size.should > 0

    batch["checks"].each do |check|
      check.key?("parent_id").should be_true
      check.key?("child_id").should be_true
    end
  end
end

Given /^no file exists at "([^"]*)"$/ do |filename|
  FileUtils.rm_f(filename).should be_true
end

Then /^I should see valid JSON in "([^"]*)"$/ do |filename|
  lambda {
    file = File.new(filename, 'r')
    parser = Yajl::Parser.new
    @data = parser.parse(file)
  }.should_not raise_error
end

Given /^I run the importer$/ do
  pending # express the regexp above with the code you wish you had
end

Given /^the necessary checks and relationships are created$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^the latest batch of checks should be in the work queue$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^I should see "([^"]*)" in the output$/ do |regex|
  @output.should =~ /#{regex}/i
end

Given /^beanstalkd is running$/ do
  system("which beanstalkd > /dev/null 2>&1").should be_true

  @beanstalk = IO.popen("beanstalkd")

  # So beanstalkd has a moment to catch its breath.
  sleep 0.5

  at_exit do
    Process.kill("KILL", @beanstalk.pid)
  end
end

After do |scenario|
  Process.kill("KILL", @beanstalk.pid)
end

Given /^there are no jobs on the "([^"]*)" beanstalkd queue$/ do |queue_name|
  @queue = Beanstalk::Connection.new('localhost:11300', queue_name)
  100.times { @queue.put('foo') }
  until @queue.stats["current-jobs-ready"] == 0
    job = @queue.reserve
    job.delete<commit_msg>Refactor After kill_lingering_daemons step into env.rb<commit_after>When /^I run "([^"]*)" with the following arguments:$/ do |script, table|
  @arguments = []
  table.hashes.each do |attrs|
    @arguments << attrs["argument"]
  end

  command = "bin/#{script} #{@arguments.join(' ')}"

  @output = `#{command}`
  $?.exitstatus.should == 0
end

Then /^I should see a batch of checks in the output$/ do
  @data["batches"].should_not be_nil
  @data["batches"].size.should > 0

  @data["batches"].each do |batch|
    batch["checks"].should_not be_nil
    batch["checks"].size.should > 0

    batch["checks"].each do |check|
      check["id"].should_not be_nil
      check["command"].should_not be_nil
    end
  end
end

Then /^I should see a batch of checks with relationships in the output$/ do
  @data["batches"].should_not be_nil
  @data["batches"].size.should > 0

  @data["batches"].each do |batch|
    batch["checks"].should_not be_nil
    batch["checks"].size.should > 0

    batch["checks"].each do |check|
      check.key?("parent_id").should be_true
      check.key?("child_id").should be_true
    end
  end
end

Given /^no file exists at "([^"]*)"$/ do |filename|
  FileUtils.rm_f(filename).should be_true
end

Then /^I should see valid JSON in "([^"]*)"$/ do |filename|
  lambda {
    file = File.new(filename, 'r')
    parser = Yajl::Parser.new
    @data = parser.parse(file)
  }.should_not raise_error
end

Given /^I run the importer$/ do
  pending # express the regexp above with the code you wish you had
end

Given /^the necessary checks and relationships are created$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^the latest batch of checks should be in the work queue$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^I should see "([^"]*)" in the output$/ do |regex|
  @output.should =~ /#{regex}/i
end

Given /^beanstalkd is running$/ do
  system("which beanstalkd > /dev/null 2>&1").should be_true

  @beanstalk = IO.popen("beanstalkd")

  # So beanstalkd has a moment to catch its breath.
  sleep 0.5

  at_exit do
    Process.kill("KILL", @beanstalk.pid)
  end
end

Given /^there are no jobs on the "([^"]*)" beanstalkd queue$/ do |queue_name|
  @queue = Beanstalk::Connection.new('localhost:11300', queue_name)
  100.times { @queue.put('foo') }
  until @queue.stats["current-jobs-ready"] == 0
    job = @queue.reserve
    job.delete
{'fraction_non_alphanumeric': 0.14619643223293008, 'fraction_numerical': 0.007381587041213861, 'mean_word_length': 3.593220338983051, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6258926', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1612, 'n_words': 516}