Skip to content
Browse files

Annotate wiki with Cucumber test results

  • Loading branch information...
1 parent 0b4dbe3 commit 7db9d2865dc36600383ed2dbb23a7c8ce2a611ff @stewart stewart committed Dec 4, 2012
Showing with 111 additions and 1 deletion.
  1. +1 −1 bin/gitnesse
  2. +3 −0 lib/gitnesse.rb
  3. +50 −0 lib/gitnesse/hooks.rb
  4. +11 −0 lib/gitnesse/support/hook.rb
  5. +46 −0 lib/gitnesse/wiki.rb
View
2 bin/gitnesse
@@ -1,4 +1,4 @@
-e!/usr/bin/env ruby
+#!/usr/bin/env ruby
require 'rubygems'
require 'gitnesse'
View
3 lib/gitnesse.rb
@@ -6,6 +6,7 @@
require 'gitnesse/git_config'
require 'gitnesse/dependencies'
require 'gitnesse/features'
+require 'gitnesse/hooks'
require 'gitnesse/wiki'
require 'gitnesse/railtie' if defined?(Rails)
@@ -29,6 +30,7 @@ def self.configure
def run
if pull
+ Hooks.create
puts "Now going to run cucumber..."
exec("cucumber #{Gitnesse.configuration.target_directory}/*.feature")
end
@@ -53,6 +55,7 @@ def pull
end
end
puts " Done pulling features."
+ true
end
# push features back up to git wiki from features directory
View
50 lib/gitnesse/hooks.rb
@@ -0,0 +1,50 @@
+module Gitnesse
+ class Hooks
+ @dir = File.join(Dir.home, ".gitnesse")
+
+ # Public: Sets up ~/.gitnesse for appending scenario results
+ #
+ # Returns nothing
+ def self.setup
+ Gitnesse::Configuration.load_using_search
+ FileUtils.rm_rf(@dir) if File.directory?(@dir)
+ Dir.mkdir(@dir)
+ `git clone #{Gitnesse.configuration.repository_url} #{@dir}`
+ Wiki.new(@dir).remove_past_results
+ end
+
+ # Public: Removes ~/.gitnesse after all cukes are run
+ #
+ # Returns nothing
+ def self.teardown
+ Dir.chdir(@dir) do
+ `git push origin master`
+ end
+
+ FileUtils.rm_rf(@dir)
+ FileUtils.rm(File.absolute_path("#{Gitnesse.configuration.target_directory}/support/gitnesse_hooks.rb"))
+ end
+
+ # Public: Adds hooks into Cucumber
+ #
+ # Returns nothing
+ def self.create
+ hook_file = File.join(File.dirname(__FILE__), "support/hook.rb")
+ support_dir = File.absolute_path("#{Gitnesse.configuration.target_directory}/support")
+ target_file = File.join(support_dir, "gitnesse_hooks.rb")
+
+ Dir.mkdir(support_dir) unless File.directory?(support_dir)
+
+ File.write(target_file, File.read(hook_file))
+ end
+
+ # Public: Appends scenario results to relevant page in wiki
+ #
+ # scenario - the scenario results from Cucumber
+ #
+ # Returns nothing
+ def self.append_to_wiki(scenario)
+ Wiki.new(@dir).append_results(scenario)
+ end
+ end
+end
View
11 lib/gitnesse/support/hook.rb
@@ -0,0 +1,11 @@
+require 'gitnesse'
+
+Gitnesse::Hooks.setup
+
+After do |scenario|
+ Gitnesse::Hooks.append_to_wiki(scenario)
+end
+
+at_exit do
+ Gitnesse::Hooks.teardown
+end
View
46 lib/gitnesse/wiki.rb
@@ -81,6 +81,52 @@ def build_page_content(feature_content, old_page_content = nil)
old_page_content.sub(old_feature_content, feature_content)
end
+ # Public: Removes past Cucumber results from feature files
+ #
+ # Returns nothing
+ def remove_past_results
+ @commit_info[:message] = Gitnesse.configuration.info
+ features = Dir.glob("#{Gitnesse.configuration.target_directory}/*.feature")
+
+ features.each do |feature|
+ feature_name = File.basename(feature, ".feature")
+ page = @wiki.page(feature_name) || @wiki.page("#{feature_name}.feature")
+
+ if page
+ content = page.raw_data
+ if content.match(/\u0060{3}gherkin.*\u0060{3}(.*)/m)[1]
+ [ "FAILED", "PASSED", "PENDING", "UNDEFINED" ].each do |type|
+ content.gsub!(/#{type}: .*\n/, '')
+ end
+ @wiki.update_page(page, page.name, :markdown, content, @commit_info)
+ end
+ end
+ end
+ end
+
+ # Public: Appends results of cucumber scenario to wiki
+ #
+ # scenario - Cucumber scenario from After hook
+ #
+ # Returns nothing
+ def append_results(scenario)
+ pages = @wiki.pages
+ filename = File.basename(scenario.feature.file, ".feature")
+ @commit_info[:message] = Gitnesse.configuration.info
+
+ pages.each do |page|
+ if page.name == filename || page.name == "#{filename}.feature"
+ if page.text_data.include? scenario.name
+ content = page.raw_data
+ string = "\n#{scenario.status.to_s.upcase}: #{scenario.name}\n"
+ content.gsub(string, '')
+ content << string
+ @wiki.update_page(page, page.name, :markdown, content, @commit_info)
+ end
+ end
+ end
+ end
+
private
# Private: Creates a new wiki page for the provided name and content

0 comments on commit 7db9d28

Please sign in to comment.
Something went wrong with that request. Please try again.