Permalink
Browse files

generation of reports by rake tasks report or cucumber/spec

  • Loading branch information...
1 parent 2a5ec49 commit 82bad56d88ab1272585ab9af83afc631dcf14067 @thoferon committed May 7, 2012
View
@@ -3,6 +3,7 @@ PATH
specs:
git_farm (0.0.1)
grit
+ rake
GEM
remote: http://rubygems.org/
@@ -32,14 +33,15 @@ GEM
json (1.7.0)
mime-types (1.18)
posix-spawn (0.3.6)
- rspec (2.9.0)
- rspec-core (~> 2.9.0)
- rspec-expectations (~> 2.9.0)
- rspec-mocks (~> 2.9.0)
- rspec-core (2.9.0)
- rspec-expectations (2.9.1)
+ rake (0.9.2.2)
+ rspec (2.10.0)
+ rspec-core (~> 2.10.0)
+ rspec-expectations (~> 2.10.0)
+ rspec-mocks (~> 2.10.0)
+ rspec-core (2.10.0)
+ rspec-expectations (2.10.0)
diff-lcs (~> 1.1.3)
- rspec-mocks (2.9.0)
+ rspec-mocks (2.10.1)
term-ansicolor (1.0.7)
PLATFORMS
View
@@ -4,11 +4,12 @@ require "rspec/core/rake_task"
$:.unshift File.expand_path('lib', __FILE__)
Cucumber::Rake::Task.new(:cucumber) do |task|
+ task.cucumber_opts = %w{--exclude features/resources}
end
namespace :cucumber do
Cucumber::Rake::Task.new(:wip) do |task|
- task.cucumber_opts = %w{--tags @wip}
+ task.cucumber_opts = %w{--tags @wip --exclude features/resources}
end
end
@@ -0,0 +1,23 @@
+Feature: Generation of reports
+ In order to see the development progress of my projects
+ As a developer or clients
+ I want to generate reports
+
+ Background:
+ Given a farm
+ And a repository "test_repo" from resources
+
+ Scenario: Without a rake task "report"
+ Given a rake task "spec"
+ And a rake task "cucumber"
+ When I generate reports for "test_repo"
+ Then the rake task "cucumber" has been called
+ And the rake task "spec" has been called
+
+ Scenario: With a rake task "report"
+ Given a rake task "report"
+ And a rake task "cucumber"
+ And a rake task "spec"
+ When I generate reports for "test_repo"
+ Then the rake task "report" has been called
+ And the rake task "cucumber" has not been called
@@ -0,0 +1,5 @@
+Feature: Returning hello world
+
+ Scenario: calling the method 'hello'
+ When I call "hello"
+ Then it returns "Hello, world !"
@@ -0,0 +1,9 @@
+require File.expand_path("../../lib/hello", __FILE__)
+
+When /^I call "hello"$/ do
+ @result = hello
+end
+
+Then /^it returns "(.*)"$/ do |result|
+ @result.should == result
+end
@@ -0,0 +1,3 @@
+def hello
+ "Hello, world !"
+end
@@ -12,7 +12,7 @@
end
Given /^a directory "(.*)"$/ do |dir_name|
- create_dir dir_name
+ create_dir File.join(@farm.path, dir_name)
end
When /^I create the repository "(.*)"$/ do |repo_name|
@@ -0,0 +1,22 @@
+Given /^a repository "(.*)" from resources$/ do |repo_name|
+ path = cp_resource(repo_name, @farm.path)
+ repo = GitFarm::Repository.init(path)
+ repo.add_recursive(path)
+ repo.commit_all("initial commit")
+end
+
+Given /^a rake task "(.*)"$/ do |task_name|
+ fake_rake_app.add_task(task_name)
+end
+
+When /^I generate reports for "(.*)"$/ do |repo_name|
+ @farm.generate_reports(repo_name)
+end
+
+Then /^the rake task "(.*)" has been called$/ do |task_name|
+ fake_rake_app.tasks_called.should include(task_name)
+end
+
+Then /^the rake task "(.*)" has not been called$/ do |task_name|
+ fake_rake_app.tasks_called.should_not include(task_name)
+end
View
@@ -0,0 +1,46 @@
+require "git_farm/rake_app"
+require "cucumber/rspec/doubles"
+
+Before do
+ fake_rake_app.clean
+end
+
+module RakeHelper
+ def fake_rake_app
+ GitFarm::RakeApp
+ end
+end
+
+World(RakeHelper)
+
+class GitFarm::RakeApp
+ class << self
+ def tasks_called
+ @@tasks_called
+ end
+
+ def clean
+ @@tasks_called = []
+ @@tasks = []
+ end
+
+ def add_task(name)
+ @@tasks << name
+ end
+ end
+
+ def initialize
+ end
+
+ def invoke_task!(name)
+ @@tasks_called << name.to_s
+ raise GitFarm::RakeApp::TaskFailed unless @@tasks.include?(name.to_s)
+ end
+
+ def handle_options; end
+ def collect_tasks; end
+
+ def have_rakefile
+ @rakefile = File.absolute_path("Rakefile")
+ end
+end
@@ -0,0 +1,21 @@
+module Resources
+ def cp_resource(name, path)
+ resource_path = File.join(File.dirname(__FILE__), "..", "resources", name)
+ copy_without_git(resource_path, path)
+ File.join(path, name)
+ end
+
+ def copy_without_git(source, target)
+ if File.directory?(source)
+ path = File.join(target, File.basename(source))
+ Dir.mkdir path
+ Dir.entries(source).each do |e|
+ copy_without_git(File.join(source, e), path) unless e =~ /[\.|\.\.|\.git]$/
+ end
+ else
+ FileUtils.cp(source, target)
+ end
+ end
+end
+
+World(Resources)
View
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.add_runtime_dependency "grit"
+ s.add_runtime_dependency "rake"
s.add_development_dependency "cucumber"
s.add_development_dependency "rspec"
View
@@ -0,0 +1,4 @@
+require "git_farm/constants"
+require "git_farm/farm"
+require "git_farm/repository"
+require "git_farm/rake_app"
View
@@ -16,6 +16,13 @@ def open(path)
end
end
+ def [](name)
+ name = name.to_s
+ in_farm_dir do
+ return GitFarm::Repository.new(name) if has_repository?(name)
+ end
+ end
+
def initialize(options = {})
@path = File.expand_path(options[:path] || '.')
end
@@ -45,6 +52,10 @@ def rename_repository(source, target)
delete_repository(source)
end
+ def generate_reports(name)
+ self[name].generate_reports
+ end
+
def has_repository?(name)
in_farm_dir do
Dir.exists? File.join(name, '.git')
View
@@ -0,0 +1,27 @@
+require "rake"
+
+module GitFarm
+ class RakeApp < Rake::Application
+ class TaskFailed < GitFarm::Error; end
+
+ def initialize(*args)
+ super(*args)
+ init("rake")
+ load_rakefile
+ end
+
+ def invoke_task!(name)
+ name = name.to_s
+ puts "rake_app[:#{name}] = #{self[name]}"
+ raise GitFarm::RakeApp::TaskFailed if self[name]
+ invoke_task(name)
+ end
+
+ private
+ def [](task_name)
+ tasks.each do |t|
+ return t if t.name == task_name
+ end
+ end
+ end
+end
View
@@ -1,9 +1,52 @@
+require "git_farm/rake_app"
require "grit"
module GitFarm
class Repository < Grit::Repo
+ def working_directory
+ @path.match(/(.*)(\/\.git)$/)[1]
+ end
+
+ def initialize(*args)
+ super(*args)
+ in_repo_dir { @rake_app = GitFarm::RakeApp.new }
+ end
+
def name
- @path.scan(/\/([^\/]*)\/\.git$/).first.first
+ @path.match(/\/([^\/]*)\/\.git$/)[1]
+ end
+
+ def add_recursive(path)
+ return if File.basename(path) =~ /^\.\.?$/
+
+ add(path)
+ if File.directory? path
+ Dir.entries(path).each { |e| add_recursive(File.join(path, e)) }
+ end
+ end
+
+ def generate_reports
+ in_repo_dir do
+ begin
+ @rake_app.invoke_task!(:report)
+ rescue GitFarm::RakeApp::TaskFailed
+ @rake_app.invoke_task!(:cucumber) rescue nil
+ @rake_app.invoke_task!(:spec) rescue nil
+ end
+ end
end
+
+ def inspect
+ %Q{#<GitFarm::Repository "#{@path}">}
+ end
+
+ private
+ def in_repo_dir
+ current_dir = Dir.pwd
+ Dir.chdir working_directory
+ result = yield
+ Dir.chdir current_dir
+ result
+ end
end
end
@@ -15,6 +15,31 @@
end
end
+ describe "#[]" do
+ before do
+ Dir.stub(:entries).and_return(["repo"])
+ GitFarm::Repository.stub_chain(:new, :kind_of?).and_return(GitFarm::Repository)
+ end
+
+ context "when the repository exists" do
+ before { subject.stub(:has_repository?).with("repo").and_return(true) }
+
+ it "should return a GitFarm::Repository instance" do
+ subject["repo"].should be_a(GitFarm::Repository)
+ end
+
+ it "should also work wih symbols" do
+ subject[:repo].should be_a(GitFarm::Repository)
+ end
+ end
+
+ context "when the repository doesn't exist" do
+ it "should return nil if the repository doesn't exist" do
+ subject["repo"].should == nil
+ end
+ end
+ end
+
describe "#create_repository" do
it "should return an instance of GitFarm::Repository" do
subject.stub(:has_repository?).and_return(false)
@@ -1,4 +1,5 @@
require "git_farm/repository"
+require "rake"
describe GitFarm::Repository do
let(:repository) { GitFarm::Repository.new(".") }
@@ -19,4 +20,35 @@
end
end
end
+
+ describe "#add_recursive" do
+ before do
+ Dir.stub(:entries).with(/path$/).and_return(["some_file"])
+ File.stub(:directory?).with(/path$/).and_return(true)
+ File.stub(:directory?).with(/some_file$/).and_return(false)
+ end
+
+ it "should raise GitFarm::Repository::PathOutsideRepository if so"
+
+ it "should add files recursively" do
+ repository.should_receive(:add).with("path")
+ repository.should_receive(:add).with(File.join("path", "some_file"))
+ repository.add_recursive("path")
+ end
+ end
+
+ describe "#generate_reports" do
+ it "should invoke a rake task" do
+ fake_rake_app = GitFarm::RakeApp.new
+ GitFarm::RakeApp.should_receive(:new).and_return(fake_rake_app)
+ fake_rake_app.should_receive(:invoke_task!)
+
+ repository.generate_reports
+ end
+ end
+
+ describe "#working_directory" do
+ subject { repository.working_directory }
+ it { should_not =~ /\/\.git$/ }
+ end
end

0 comments on commit 82bad56

Please sign in to comment.