require "pathname" require "timecop" require "time" def root Pathname.new(File.expand_path("../../", File.dirname(__FILE__))) end Given /^cucumber working$/ do end Given /^the following files exists:$/ do |table| table.hashes.each do |hash| full_path = root.join("tmp/#{hash["path"]}") FileUtils.mkdir_p(File.dirname(full_path)) File.open(full_path, "w") do |f| f.puts(hash["content"]) end end end Given /^the directory "([^"]*)" exists$/ do |path| FileUtils.mkdir_p(root.join("tmp/#{path}")) end When /^I call "([^"]*)"$/ do |cmd| @out = `ruby #{root}/bin/#{cmd}` end When /^I call "([^"]*)" piped through "([^"]*)"$/ do |cmd, pipe| @out = `#{pipe} | ruby #{root}/bin/#{cmd}` end Given /^I clear tmp$/ do FileUtils.rm_rf(root.join("tmp")) end Then /^I should see the following files$/ do |table| table.hashes.each do |hash| path = root.join("tmp/#{hash["path"]}").to_s Pathname.new(root.join("tmp/#{hash["symlink"]}")).realpath.to_s.should == path File.should be_exists(path) File.read(path).strip.should == hash["content"] end end Then /^I should see (\d+) files in "([^"]*)"$/ do |count, path| Dir.glob("#{root.join("#{path}/**/*")}").select { |file_path| File.file?(file_path) }.count.should == count.to_i end Given /^the following file list exists in "([^"]*)"$/ do |path, table| File.open(root.join("tmp/#{path}"), "w") do |f| table.hashes.each do |hash| f.puts root.join("tmp/#{hash["path"]}") end end end Then /^the output should look like this$/ do |table| results = [%w(index total checksummed_path)] + @out.split("\n").map do |line| hash = JSON.parse(line) [hash["index"], hash["total"], hash["checksummed_path"].gsub("#{root}/", "")].map(&:to_s) end table.diff!(results) end Given /^I freeze time to "([^"]*)"$/ do |time| Timecop.freeze(Time.parse(time)) end