<commit_before>require_relative 'spec_helper'
require 'fileutils'
require 'pathname'

def lint_regression_cookbooks
  working_dir = Pathname.new('tmp/regression')
  FileUtils.mkdir_p(working_dir)

  pinned_cookbooks('spec/regression/cookbooks.txt').each do |cbk|
    clone_cookbook(working_dir, cbk)
  end

  lint_cookbooks(working_dir)
end

def pinned_cookbooks(path)
  File.read(path).lines.map do |line|
    name, ref = line.strip.split(':')
    {:name => name, :ref => ref}
  end
end

def clone_cookbook(clone_path, cbk)
  target_path = clone_path + cbk[:name]
  unless Dir.exist?(target_path)
    %x{git clone -q git://github.com/chef-cookbooks/#{cbk[:name]}.git #{target_path}}
    raise "Unable to clone git://github.com/chef-cookbooks/#{cbk[:name]}.git" unless $?.success?
  end
  %x{cd #{target_path} && git checkout -q #{cbk[:ref]}}
  raise 'Unable to checkout revision' unless $?.success?
end

def lint_cookbooks(cookbook_path)
  result = %x{cd #{cookbook_path} && foodcritic .}
  raise 'Unable to lint' unless $?.success?
  result
end
<commit_msg>Better error logging for other failures<commit_after>require_relative 'spec_helper'
require 'fileutils'
require 'pathname'

def lint_regression_cookbooks
  working_dir = Pathname.new('tmp/regression')
  FileUtils.mkdir_p(working_dir)

  pinned_cookbooks('spec/regression/cookbooks.txt').each do |cbk|
    clone_cookbook(working_dir, cbk)
  end

  lint_cookbooks(working_dir)
end

def pinned_cookbooks(path)
  File.read(path).lines.map do |line|
    name, ref = line.strip.split(':')
    {:name => name, :ref => ref}
  end
end

def clone_cookbook(clone_path, cbk)
  target_path = clone_path + cbk[:name]
  unless Dir.exist?(target_path)
    %x{git clone -q git://github.com/chef-cookbooks/#{cbk[:name]}.git #{target_path}}
    raise "Unable to clone git://github.com/chef-cookbooks/#{cbk[:name]}.git" unless $?.success?
  end
  %x{cd #{target_path} && git checkout -q #{cbk[:ref]}}
  raise "Unable to checkout revision for #{cbk[:name]}" unless $?.success?
end

def lint_cookbooks(cookbook_path)
  result = %x{cd #{cookbook_path} && foodcritic .}
  raise "Unable to lint #{cookbook_path}" unless $?.success?
  result
end

{'fraction_non_alphanumeric': 0.1445617255621845, 'fraction_numerical': 0.0, 'mean_word_length': 4.876010781671159, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1974588', 'n_tokens_mistral': 799, 'n_tokens_neox': 785, 'n_words': 172}