more updates, starting code coverage

This commit is contained in:
John Bintz 2010-05-04 11:48:59 -04:00
parent 1ba0016f9f
commit 54b2113653
11 changed files with 163 additions and 98 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
.loadpath .loadpath
.project .project
coverage/*

View File

@ -1,6 +1,7 @@
$LOAD_PATH << 'lib' $LOAD_PATH << 'lib'
require 'apache' require 'apache'
require 'spec/rake/spectask'
namespace :apache do namespace :apache do
desc "Generate the configs" desc "Generate the configs"
@ -10,3 +11,12 @@ namespace :apache do
end end
end 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

View File

@ -1,3 +1 @@
Dir[File.join(File.dirname(__FILE__), '**', '*.rb')].each do |file| require 'apache/config'
require file
end

View File

@ -1,31 +1,87 @@
require 'apache/master' Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f }
require 'apache/permissions'
module Apache module Apache
class Config class Config
class << self class << self
attr_accessor :line_indent, :config
include Apache::Master include Apache::Master
include Apache::Quoteize include Apache::Quoteize
include Apache::Permissions include Apache::Permissions
def build(target, &block) def build(target, &block)
@config = [] reset!
@indent = 0
self.instance_eval(&block) self.instance_eval(&block)
puts @config * "\n" puts @config * "\n"
#File.open(target, 'w') { |f| f.puts @config * "\n" }
end end
# Reset the current settings
def reset!
@config = []
@line_indent = 0
end
# Indent the string by the current @line_indent level
def indent(string) def indent(string)
" " * (@indent * 2) + string " " * (@line_indent * 2) + string
end end
# Add the string to the current config
def <<(string) def <<(string)
@config << indent(string) @config << indent(string)
end 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 << "</#{tag_name}>"
end
end end
block_methods :if_module, :directory, :virtual_host block_methods :if_module, :directory, :virtual_host

View File

@ -1,69 +1,18 @@
module Apache 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 module Master
def modules(*modules, &block) def modules(*modules, &block)
@config << Modules.build(*modules, &block) @config << Modules.build(*modules, &block)
end 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 << "</" + tag_name + ">"
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) def runner(user, group = nil)
user! user user! user
group! group if group group! group if group
end end
def passenger(ruby_root, ruby_version, passenger_version) 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 end
def order(*args) def order(*args)
@ -71,36 +20,5 @@ module Apache
end end
alias :order! :order 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
end end

24
lib/apache/modules.rb Normal file
View File

@ -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

14
lib/apache/quoteize.rb Normal file
View File

@ -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

44
spec/config_spec.rb Normal file
View File

@ -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 == ['<Tag "part" "part2">', ' Something "goes here"', '</Tag>']
end
end

View File

@ -1,3 +0,0 @@
if __FILE__ == $0
# TODO Generated stub
end

0
spec/spec_helper.rb Normal file
View File

View File

@ -23,5 +23,7 @@ Apache::Config.build('httpd.conf') do
directory '/' do directory '/' do
allow_from_all allow_from_all
end end
end end
end end