From 54b21136531c582e55911b275c2cbca805b2a739 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 4 May 2010 11:48:59 -0400 Subject: [PATCH] more updates, starting code coverage --- .gitignore | 2 + Rakefile | 10 +++++ lib/apache.rb | 4 +- lib/apache/config.rb | 70 +++++++++++++++++++++++++++++---- lib/apache/master.rb | 88 ++---------------------------------------- lib/apache/modules.rb | 24 ++++++++++++ lib/apache/quoteize.rb | 14 +++++++ spec/config_spec.rb | 44 +++++++++++++++++++++ spec/master_spec.rb | 3 -- spec/spec_helper.rb | 0 test/config/httpd.rb | 2 + 11 files changed, 163 insertions(+), 98 deletions(-) create mode 100644 lib/apache/modules.rb create mode 100644 lib/apache/quoteize.rb create mode 100644 spec/config_spec.rb delete mode 100644 spec/master_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 7b3b46b..1a53548 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .loadpath .project +coverage/* + diff --git a/Rakefile b/Rakefile index 953edfe..fc574b7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,7 @@ $LOAD_PATH << 'lib' require 'apache' +require 'spec/rake/spectask' namespace :apache do desc "Generate the configs" @@ -10,3 +11,12 @@ namespace :apache do end end end + +namespace :spec do + desc "Run RCov tests" + Spec::Rake::SpecTask.new('rcov') do |t| + t.spec_files = FileList['spec/*.rb'] + t.rcov = true + t.rcov_opts = ['--exclude', 'spec'] + end +end diff --git a/lib/apache.rb b/lib/apache.rb index 6ebbffa..d49c9b4 100644 --- a/lib/apache.rb +++ b/lib/apache.rb @@ -1,3 +1 @@ -Dir[File.join(File.dirname(__FILE__), '**', '*.rb')].each do |file| - require file -end +require 'apache/config' diff --git a/lib/apache/config.rb b/lib/apache/config.rb index 983155e..e610045 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -1,31 +1,87 @@ -require 'apache/master' -require 'apache/permissions' +Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f } module Apache class Config class << self + attr_accessor :line_indent, :config + include Apache::Master include Apache::Quoteize include Apache::Permissions def build(target, &block) - @config = [] - @indent = 0 + reset! self.instance_eval(&block) puts @config * "\n" - - #File.open(target, 'w') { |f| f.puts @config * "\n" } end + # Reset the current settings + def reset! + @config = [] + @line_indent = 0 + end + + # Indent the string by the current @line_indent level def indent(string) - " " * (@indent * 2) + string + " " * (@line_indent * 2) + string end + # Add the string to the current config def <<(string) @config << indent(string) end + + # Apachify a string + # + # Split the provided name on underscores and capitalize the individual parts + def apachify(name) + name.to_s.split("_").collect(&:capitalize).join + end + + # Handle options that aren't specially handled + def method_missing(method, *args) + if method.to_s[-1..-1] == "!" + method = method.to_s[0..-2].to_sym + else + args = *quoteize(*args) + end + + self << [ apachify(method), *args ] * ' ' + end + + # Handle creating block methods + def block_methods(*methods) + methods.each do |method| + self.class.class_eval <<-EOT + def #{method}(*name, &block) + blockify(apachify("#{method}"), name, &block) + end + EOT + end + end + + # Handle the blockification of a provided block + def blockify(tag_name, name, &block) + start = [ tag_name ] + + case name + when String + start << quoteize(name).first if name + when Array + start << (quoteize(*name) * " ") if name + end + + start = start.join(' ') + + self << "" if (@indent == 0) + self << "<#{start}>" + @line_indent += 1 + self.instance_eval(&block) + @line_indent -= 1 + self << "" + end end block_methods :if_module, :directory, :virtual_host diff --git a/lib/apache/master.rb b/lib/apache/master.rb index 0e2fc60..e04fe0e 100644 --- a/lib/apache/master.rb +++ b/lib/apache/master.rb @@ -1,69 +1,18 @@ module Apache - module Quoteize - def quoteize(*args) - args.collect do |arg| - case arg - when Symbol - arg.to_s.gsub('_', ' ') - else - %{"#{arg}"} - end - end - end - end - module Master def modules(*modules, &block) @config << Modules.build(*modules, &block) end - def block_methods(*methods) - methods.each do |method| - self.class.class_eval <<-EOT - def #{method}(*name, &block) - blockify(apachify("#{method}"), name, &block) - end - EOT - end - end - - def blockify(tag_name, name, &block) - start = [ tag_name ] - - case name - when String - start << quoteize(name).first if name - when Array - start << (quoteize(*name) * " ") if name - end - - start = start.uniq.join(' ') - - self << "" if (@indent == 0) - self << "<" + start + ">" - @indent += 1 - self.instance_eval(&block) - @indent -= 1 - self << "" - end - - def method_missing(method, *args) - if method.to_s[-1..-1] == "!" - method = method.to_s[0..-2].to_sym - else - args = *quoteize(*args) - end - - self << [ apachify(method), *args ] * ' ' - end - def runner(user, group = nil) user! user group! group if group end def passenger(ruby_root, ruby_version, passenger_version) - + load_module 'passenger_module', "#{ruby_root}/lib/ruby/gems/#{ruby_version}/gems/passenger-#{passenger_version}/ext/apache2/mod_passenger.so" + passenger_root "#{ruby_root}/lib/ruby/gems/#{ruby_version}/gems/passenger-#{passenger_version}" + passenger_ruby "#{ruby_root}/bin/ruby" end def order(*args) @@ -71,36 +20,5 @@ module Apache end alias :order! :order - - private - def apachify(name) - name = name.to_s - case name - when true - else - name.split("_").collect(&:capitalize).join - end - end - end - - class Modules - class << self - include Apache::Quoteize - - def build(*modules, &block) - @modules = [] - - modules.each { |m| self.send(m) } - self.instance_eval(&block) if block - - @modules - end - - def method_missing(method, *args) - module_name = "#{method}_module" - module_path = args[0] || "modules/mod_#{method}.so" - @modules << [ 'LoadModule', *quoteize(module_name, module_path) ] * " " - end - end end end diff --git a/lib/apache/modules.rb b/lib/apache/modules.rb new file mode 100644 index 0000000..5a63441 --- /dev/null +++ b/lib/apache/modules.rb @@ -0,0 +1,24 @@ +require 'apache/quoteize' + +module Apache + class Modules + class << self + include Apache::Quoteize + + def build(*modules, &block) + @modules = [] + + modules.each { |m| self.send(m) } + self.instance_eval(&block) if block + + @modules + end + + def method_missing(method, *args) + module_name = "#{method}_module" + module_path = args[0] || "modules/mod_#{method}.so" + @modules << [ 'LoadModule', *quoteize(module_name, module_path) ] * " " + end + end + end +end diff --git a/lib/apache/quoteize.rb b/lib/apache/quoteize.rb new file mode 100644 index 0000000..21b0f65 --- /dev/null +++ b/lib/apache/quoteize.rb @@ -0,0 +1,14 @@ +module Apache + module Quoteize + def quoteize(*args) + args.collect do |arg| + case arg + when Symbol + arg.to_s.gsub('_', ' ') + else + %{"#{arg}"} + end + end + end + end +end diff --git a/spec/config_spec.rb b/spec/config_spec.rb new file mode 100644 index 0000000..ae5654a --- /dev/null +++ b/spec/config_spec.rb @@ -0,0 +1,44 @@ +require 'apache/config' + +describe Apache::Config, "should handle the basics of Apache config" do + before do + Apache::Config.reset! + end + + it "should handle indent" do + Apache::Config.line_indent = 1 + + Apache::Config.indent("hello").should == " hello" + end + + it "should add a line to the config" do + Apache::Config << "hello" + Apache::Config.config.should == [ 'hello' ] + end + + it "should handle method_missing" do + Apache::Config.test "test2", "test3" + Apache::Config.test_again! "test2", "test3" + + Apache::Config.config.should == [ + 'Test "test2" "test3"', + 'TestAgain test2 test3' + ] + end + + it "should Apachify the name" do + Apache::Config.apachify("test").should == "Test" + Apache::Config.apachify("test_full_name").should == "TestFullName" + end + + it "should quoteize properly" do + Apache::Config.quoteize("test", "test2").should == %w{"test" "test2"} + Apache::Config.quoteize(:test, :test2).should == %w{test test2} + end + + it "should blockify a block" do + Apache::Config.blockify("Tag", [ 'part', 'part2' ]) do + something "goes here" + end.should == ['', ' Something "goes here"', ''] + end +end diff --git a/spec/master_spec.rb b/spec/master_spec.rb deleted file mode 100644 index 066f5aa..0000000 --- a/spec/master_spec.rb +++ /dev/null @@ -1,3 +0,0 @@ -if __FILE__ == $0 - # TODO Generated stub -end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..e69de29 diff --git a/test/config/httpd.rb b/test/config/httpd.rb index 1b909ea..b4e16c5 100644 --- a/test/config/httpd.rb +++ b/test/config/httpd.rb @@ -23,5 +23,7 @@ Apache::Config.build('httpd.conf') do directory '/' do allow_from_all end + + end end