Skip to content

Commit

Permalink
Simplified and extracted config to Gitnesse::Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Stewart committed Nov 30, 2012
1 parent 54ecaa5 commit 66eb101
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 115 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -21,8 +21,8 @@ Or install it yourself as:
Create a `gitnesse.rb` file somewhere in your project, and add something like Create a `gitnesse.rb` file somewhere in your project, and add something like
the following to it: the following to it:


Gitnesse.config do Gitnesse.configure do |config|
repository_url "git@github.com:hybridgroup/gitnesse-demo.wiki" config.repository_url = "git@github.com:hybridgroup/gitnesse-demo.wiki"
end end


## rake tasks ## rake tasks
Expand Down
4 changes: 2 additions & 2 deletions bin/gitnesse
Expand Up @@ -11,7 +11,7 @@ end


def print_help def print_help
puts "Gitnesse commands:" puts "Gitnesse commands:"
puts " run: pull remote features from git-based wiki to local, and run Cucumber" puts " run: pull remote features from git-based wiki to local, and run Cucumber"
puts " push: push local features to git-based wiki" puts " push: push local features to git-based wiki"
puts " pull: pull remote features from git-based wiki to local" puts " pull: pull remote features from git-based wiki to local"
puts " info: print current Gitnesse configuration" puts " info: print current Gitnesse configuration"
Expand All @@ -25,7 +25,7 @@ when "pull"
when "run" when "run"
Gitnesse.run Gitnesse.run
when "info" when "info"
puts Gitnesse.config_to_hash.to_yaml puts Gitnesse.configuration.to_yaml
when "help" when "help"
print_help print_help
else else
Expand Down
103 changes: 16 additions & 87 deletions lib/gitnesse.rb
Expand Up @@ -2,77 +2,21 @@
require 'gollum' require 'gollum'
require 'fileutils' require 'fileutils'
require 'tmpdir' require 'tmpdir'
require 'gitnesse/configuration'
require 'gitnesse/railtie' if defined?(Rails) require 'gitnesse/railtie' if defined?(Rails)


# core module # core module
module Gitnesse module Gitnesse

class << self
extend self attr_accessor :configuration

# Public: Set url of the git-based wiki repo containing features.
#
# repository_url - A String containing your repo's url.
#
# Example:
#
# Gitnesse.config do
# repository_url "git@github.com:luishurtado/gitnesse-wiki.wiki"
# end
#
def self.repository_url(repository_url = false)
if repository_url == false
unless defined?(@@repository_url)
puts "---"
puts "No repository_url has been defined for Gitnesse."
puts "Add a gitnesse.rb file to your project and use it to configure Gitnesse."
puts "For more details, check out gitnesse.com"
auts "---"
exit 1
end
return @@repository_url
else
@@repository_url = repository_url
end
end end


# Public: Set branch of the git-based wiki repo containing features. self.configuration ||= Configuration.new
#
# branch - A String containing which branch of your repo to use.
#
# Example:
#
# Gitnesse.config do
# branch "master"
# end
#
def self.branch(branch = false)
if branch == false
@@branch ||= "master"
else
@@branch = branch
end
end


# Public: Set local directory used to sync with git-wiki stored feature stories. extend self
#
# target_directory - A String containing which directory to use.
#
# Example:
#
# Gitnesse.config do
# target_directory "features"
# end
#
def self.target_directory(target_directory = false)
if target_directory == false
@@target_directory ||= File.join(Dir.pwd, 'features')
else
@@target_directory = target_directory
end
end


def self.config(&block) def self.configure
instance_eval &block yield(configuration)
end end


# -- all methods after this are module functions -- # -- all methods after this are module functions --
Expand All @@ -81,7 +25,7 @@ def self.config(&block)
def run def run
if pull if pull
puts "Now going to run cucumber..." puts "Now going to run cucumber..."
exec("cucumber #{Gitnesse.target_directory}/*.feature") exec("cucumber #{Gitnesse.configuration.target_directory}/*.feature")
end end
end end


Expand All @@ -91,10 +35,10 @@ def pull
ensure_cucumber_available ensure_cucumber_available
ensure_repository ensure_repository


puts "Pulling features into #{Gitnesse.target_directory} from #{Gitnesse.repository_url}..." puts "Pulling features into #{Gitnesse.configuration.target_directory} from #{Gitnesse.configuration.repository_url}..."
Dir.mktmpdir do |tmp_dir| Dir.mktmpdir do |tmp_dir|
if clone_feature_repo(tmp_dir) if clone_feature_repo(tmp_dir)
FileUtils.mkdir(Gitnesse.target_directory) unless File.exists?(Gitnesse.target_directory) FileUtils.mkdir(Gitnesse.configuration.target_directory) unless File.exists?(Gitnesse.configuration.target_directory)


wiki_pages = Gollum::Wiki.new(tmp_dir).pages wiki_pages = Gollum::Wiki.new(tmp_dir).pages
wiki_pages.each do |wiki_page| wiki_pages.each do |wiki_page|
Expand All @@ -114,7 +58,7 @@ def push
ensure_repository ensure_repository
commit_info commit_info


puts "Pushing features from #{Gitnesse.target_directory} to #{Gitnesse.repository_url}..." puts "Pushing features from #{Gitnesse.configuration.target_directory} to #{Gitnesse.configuration.repository_url}..."
Dir.mktmpdir do |tmp_dir| Dir.mktmpdir do |tmp_dir|
if clone_feature_repo(tmp_dir) if clone_feature_repo(tmp_dir)
load_feature_files_into_wiki(tmp_dir) load_feature_files_into_wiki(tmp_dir)
Expand All @@ -130,7 +74,7 @@ def push


def load_feature_files_into_wiki(tmp_dir) def load_feature_files_into_wiki(tmp_dir)
wiki = Gollum::Wiki.new(tmp_dir) wiki = Gollum::Wiki.new(tmp_dir)
feature_files = Dir.glob("#{Gitnesse.target_directory}/*.feature") feature_files = Dir.glob("#{Gitnesse.configuration.target_directory}/*.feature")


feature_files.each do |feature_file| feature_files.each do |feature_file|
feature_name = File.basename(feature_file, ".feature") feature_name = File.basename(feature_file, ".feature")
Expand Down Expand Up @@ -199,7 +143,7 @@ def extract_features(data)
end end


def clone_feature_repo(dir) def clone_feature_repo(dir)
output = `git clone #{Gitnesse.repository_url} #{dir} 2>&1` output = `git clone #{Gitnesse.configuration.repository_url} #{dir} 2>&1`
puts output puts output
$?.success? $?.success?
end end
Expand Down Expand Up @@ -244,7 +188,7 @@ def gather_features(page_features)
end end


def write_feature_file(page_name, page_features) def write_feature_file(page_name, page_features)
File.open("#{Gitnesse.target_directory}/#{page_name}.feature","w") {|f| f.write(gather_features(page_features)) } File.open("#{Gitnesse.configuration.target_directory}/#{page_name}.feature","w") {|f| f.write(gather_features(page_features)) }
end end


def ensure_git_available def ensure_git_available
Expand All @@ -256,7 +200,7 @@ def ensure_cucumber_available
end end


def ensure_repository def ensure_repository
raise "You must select a repository_url to run Gitnesse." if Gitnesse.repository_url.nil? raise "You must select a repository_url to run Gitnesse." if Gitnesse.configuration.repository_url.nil?
end end


def load_config def load_config
Expand All @@ -271,7 +215,7 @@ def load_config
false false
else else
file_content = File.read(file_name) file_content = File.read(file_name)
file_content.match("Gitnesse.config") file_content.match("Gitnesse.configure")
end end
end end


Expand All @@ -284,19 +228,4 @@ def load_config
raise "Several config files found: #{files_with_config.join(", ")}" raise "Several config files found: #{files_with_config.join(", ")}"
end end
end end

def config_to_hash
{ "repository_url" => Gitnesse.repository_url,
"branch" => Gitnesse.branch,
"target_directory" => Gitnesse.target_directory }
end

def method_missing(sym, *args, &block)
unless ["to_str", "to_ary"].contains?(sym)
raise "Invalid variable name for Gitnesse configuration.
Allowed variables are repository_url, branch, and target_directory."
else
super
end
end
end end
21 changes: 21 additions & 0 deletions lib/gitnesse/configuration.rb
@@ -0,0 +1,21 @@
module Gitnesse
class Configuration
attr_accessor :repository_url
attr_accessor :branch
attr_accessor :target_directory

def initialize
@branch = 'master'
@target_directory = File.join(Dir.pwd, 'features')
end

# Public: Returns the current Gitnesse configuration as a Hash
#
# Returns a hash containing the current Gitnesse configuration
def to_hash
{ 'repository_url' => @repository_url,
'branch' => @branch,
'target_directory' => @target_directory }
end
end
end
4 changes: 2 additions & 2 deletions lib/gitnesse/railtie.rb
Expand Up @@ -3,10 +3,10 @@


module Gitnesse module Gitnesse
class Railtie < Rails::Railtie class Railtie < Rails::Railtie
railtie_name :gitnesse railtie_name :gitnesse


rake_tasks do rake_tasks do
load File.dirname(__FILE__) + '/tasks.rake' load File.dirname(__FILE__) + '/tasks.rake'
end end
end end
end end
4 changes: 2 additions & 2 deletions lib/gitnesse/tasks.rake
Expand Up @@ -23,6 +23,6 @@ namespace :gitnesse do
desc "Dump the current config info to the console." desc "Dump the current config info to the console."
task :info => :environment do task :info => :environment do
Gitnesse.load_config Gitnesse.load_config
puts Gitnesse.config_to_hash.to_yaml puts Gitnesse.configuration.to_yaml
end end
end end
4 changes: 2 additions & 2 deletions test/lib/gitnesse/check_dependencies_test.rb
Expand Up @@ -5,12 +5,12 @@
let(:method) { lambda { Gitnesse.ensure_repository } } let(:method) { lambda { Gitnesse.ensure_repository } }


describe "when repository was defined" do describe "when repository was defined" do
before { Gitnesse.repository_url("git://github.com/hybridgroup/gitnesse-demo.wiki") } before { Gitnesse.configuration.repository_url = "git://github.com/hybridgroup/gitnesse-demo.wiki" }
it { method.call.must_be_nil } it { method.call.must_be_nil }
end end


describe "when repository was not defined" do describe "when repository was not defined" do
before { Gitnesse.repository_url(nil) } before { Gitnesse.configuration.repository_url = nil }
it { method.must_raise(RuntimeError) } it { method.must_raise(RuntimeError) }
end end
end end
Expand Down
@@ -1,13 +1,14 @@
require_relative '../../test_helper' require_relative '../../test_helper'


describe Gitnesse do describe Gitnesse::Configuration do
describe ".config_to_hash" do describe ".to_hash" do
let(:method) { lambda { Gitnesse.config_to_hash } } let(:method) { lambda { Gitnesse.configuration.to_hash } }

before do before do
Gitnesse.config do Gitnesse.configure do |config|
repository_url "git://github.com/hybridgroup/gitnesse-demo.wiki.git" config.repository_url = "git://github.com/hybridgroup/gitnesse-demo.wiki.git"
branch "wiki" config.branch = "wiki"
target_directory "feature_files" config.target_directory = "feature_files"
end end
end end


Expand Down
17 changes: 8 additions & 9 deletions test/lib/gitnesse/custom_branch_test.rb
@@ -1,25 +1,24 @@
require_relative '../../test_helper' require_relative '../../test_helper'


describe Gitnesse do describe Gitnesse::Configuration do
describe "#branch" do describe "#branch" do
describe "defaults to 'master'" do describe "defaults to 'master'" do
before { Gitnesse.branch nil } it { Gitnesse.configuration.branch.must_equal "master" }
it { Gitnesse.branch.must_equal "master" }
end end


describe "when changed" do describe "when changed" do
before { Gitnesse.branch "wiki" } before { Gitnesse.configuration.branch = "wiki" }
it { Gitnesse.branch.must_equal "wiki" } it { Gitnesse.configuration.branch.must_equal "wiki" }
end end


describe "when changed through #config" do describe "when changed through #configure" do
before do before do
Gitnesse.config do Gitnesse.configure do |config|
branch "wiki" config.branch = "wiki"
end end
end end


it { Gitnesse.branch.must_equal "wiki" } it { Gitnesse.configuration.branch.must_equal "wiki" }
end end
end end
end end
2 changes: 1 addition & 1 deletion test/lib/gitnesse/load_feature_files_into_wiki_test.rb
Expand Up @@ -26,7 +26,7 @@
file.write(feature) file.write(feature)
end end


Dir.expects(:glob).with("#{Gitnesse.target_directory}/*.feature").returns(:feature_file_dir) Dir.expects(:glob).with("#{Gitnesse.configuration.target_directory}/*.feature").returns(:feature_file_dir)
wiki.expects(:page).with("testing").returns(wiki_page) wiki.expects(:page).with("testing").returns(wiki_page)
Gitnesse.expects(:update_wiki_page).with(wiki_page, "testing", "blarg") Gitnesse.expects(:update_wiki_page).with(wiki_page, "testing", "blarg")
end end
Expand Down
2 changes: 1 addition & 1 deletion test/lib/gitnesse/write_feature_file_test.rb
Expand Up @@ -6,7 +6,7 @@
let(:file) { StringIO.new } let(:file) { StringIO.new }


before do before do
File.expects(:open).with("#{Gitnesse.target_directory}/test.feature", "w").yields(file) File.expects(:open).with("#{Gitnesse.configuration.target_directory}/test.feature", "w").yields(file)
Gitnesse.expects(:gather_features).with({ "test-feature" => "feature content" }).returns("feature content") Gitnesse.expects(:gather_features).with({ "test-feature" => "feature content" }).returns("feature content")
end end


Expand Down

0 comments on commit 66eb101

Please sign in to comment.