Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RSpec additions, do we need the Sandbox and SpecHelpers of buildr its…

…elf, or can we test without?

I've added them @ spec/sandbox.rb & spec/spec_helpers.rb
  • Loading branch information...
commit a5c17b05147a7d9c80c39faad548ce15eddd87b8 1 parent b39f625
@devboy authored
View
5 Gemfile
@@ -10,6 +10,7 @@ group :development do
gem "bundler", "~> 1.0.0"
gem "jeweler", "~> 1.5.2"
gem "buildr", "~> 1.4.6"
- gem "rcov", ">= 0"
- gem "rspec"
+ #gem "rcov", ">= 0"
+ gem "rspec", "~> 2.1.0"
+ gem "ci_reporter", "~> 1.6.5"
end
View
26 Rakefile
@@ -22,21 +22,25 @@ Jeweler::Tasks.new do |gem|
end
Jeweler::RubygemsDotOrgTasks.new
-require 'rake/testtask'
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
+require 'rspec/core'
+require 'rspec/core/rake_task'
+require 'ci/reporter/rake/rspec'
+
+ENV["CI_REPORTS"] ||= File.expand_path( File.join( File.dirname(__FILE__), "test", "report" ) )
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = FileList['spec/**/*_spec.rb']
end
-require 'rcov/rcovtask'
-Rcov::RcovTask.new do |test|
- test.libs << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
+task :spec => "ci:setup:rspec"
+
+RSpec::Core::RakeTask.new(:rcov) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
end
-task :default => :test
+task :test => :spec
+task :default => :spec
+
require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
View
252 spec/as3/mxml_spec.rb
@@ -0,0 +1,252 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership. The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers'))
+
+
+describe 'javac compiler' do
+ it 'should identify itself from source directories' do
+ write 'src/main/java/com/example/Test.java', 'package com.example; class Test {}'
+ define('foo').compile.compiler.should eql(:javac)
+ end
+
+ it 'should identify from source directories using custom layout' do
+ write 'src/com/example/Code.java', 'package com.example; class Code {}'
+ write 'testing/com/example/Test.java', 'package com.example; class Test {}'
+ custom = Layout.new
+ custom[:source, :main, :java] = 'src'
+ custom[:source, :test, :java] = 'testing'
+ define 'foo', :layout=>custom do
+ compile.compiler.should eql(:javac)
+ test.compile.compiler.should eql(:javac)
+ end
+ end
+
+ it 'should identify from compile source directories' do
+ write 'src/com/example/Code.java', 'package com.example; class Code {}'
+ write 'testing/com/example/Test.java', 'package com.example; class Test {}'
+ define 'foo' do
+ lambda { compile.from 'src' }.should change { compile.compiler }.to(:javac)
+ lambda { test.compile.from 'testing' }.should change { test.compile.compiler }.to(:javac)
+ end
+ end
+
+ it 'should report the language as :java' do
+ define('foo').compile.using(:javac).language.should eql(:java)
+ end
+
+ it 'should set the target directory to target/classes' do
+ define 'foo' do
+ lambda { compile.using(:javac) }.should change { compile.target.to_s }.to(File.expand_path('target/classes'))
+ end
+ end
+
+ it 'should not override existing target directory' do
+ define 'foo' do
+ compile.into('classes')
+ lambda { compile.using(:javac) }.should_not change { compile.target }
+ end
+ end
+
+ it 'should not change existing list of sources' do
+ define 'foo' do
+ compile.from('sources')
+ lambda { compile.using(:javac) }.should_not change { compile.sources }
+ end
+ end
+
+ it 'should include classpath dependencies' do
+ write 'src/dependency/Dependency.java', 'class Dependency {}'
+ define 'dependency', :version=>'1.0' do
+ compile.from('src/dependency').into('target/dependency')
+ package(:jar)
+ end
+ write 'src/test/DependencyTest.java', 'class DependencyTest { Dependency _var; }'
+ define('foo').compile.from('src/test').with(project('dependency')).invoke
+ file('target/classes/DependencyTest.class').should exist
+ end
+
+ it 'should include tools.jar dependency' do
+ write 'src/main/java/UseApt.java', <<-JAVA
+ import com.sun.mirror.apt.AnnotationProcessor;
+ public class UseApt { }
+ JAVA
+ define('foo').compile.invoke
+ file('target/classes/UseApt.class').should exist
+ end
+end
+
+
+describe 'javac compiler options' do
+ def compile_task
+ @compile_task ||= define('foo').compile.using(:javac)
+ end
+
+ def javac_args
+ compile_task.instance_eval { @compiler }.send(:javac_args)
+ end
+
+ it 'should set warnings option to false by default' do
+ compile_task.options.warnings.should be_false
+ end
+
+ it 'should set warnings option to true when running with --verbose option' do
+ verbose true
+ compile_task.options.warnings.should be_false
+ end
+
+ it 'should use -nowarn argument when warnings is false' do
+ compile_task.using(:warnings=>false)
+ javac_args.should include('-nowarn')
+ end
+
+ it 'should not use -nowarn argument when warnings is true' do
+ compile_task.using(:warnings=>true)
+ javac_args.should_not include('-nowarn')
+ end
+
+ it 'should not use -verbose argument by default' do
+ javac_args.should_not include('-verbose')
+ end
+
+ it 'should use -verbose argument when running with --trace=javac option' do
+ Buildr.application.options.trace_categories = [:javac]
+ javac_args.should include('-verbose')
+ end
+
+ it 'should set debug option to true by default' do
+ compile_task.options.debug.should be_true
+ end
+
+ it 'should set debug option to false based on Buildr.options' do
+ Buildr.options.debug = false
+ compile_task.options.debug.should be_false
+ end
+
+ it 'should set debug option to false based on debug environment variable' do
+ ENV['debug'] = 'no'
+ compile_task.options.debug.should be_false
+ end
+
+ it 'should set debug option to false based on DEBUG environment variable' do
+ ENV['DEBUG'] = 'no'
+ compile_task.options.debug.should be_false
+ end
+
+ it 'should use -g argument when debug option is true' do
+ compile_task.using(:debug=>true)
+ javac_args.should include('-g')
+ end
+
+ it 'should not use -g argument when debug option is false' do
+ compile_task.using(:debug=>false)
+ javac_args.should_not include('-g')
+ end
+
+ it 'should set deprecation option to false by default' do
+ compile_task.options.deprecation.should be_false
+ end
+
+ it 'should use -deprecation argument when deprecation is true' do
+ compile_task.using(:deprecation=>true)
+ javac_args.should include('-deprecation')
+ end
+
+ it 'should not use -deprecation argument when deprecation is false' do
+ compile_task.using(:deprecation=>false)
+ javac_args.should_not include('-deprecation')
+ end
+
+ it 'should not set source option by default' do
+ compile_task.options.source.should be_nil
+ javac_args.should_not include('-source')
+ end
+
+ it 'should not set target option by default' do
+ compile_task.options.target.should be_nil
+ javac_args.should_not include('-target')
+ end
+
+ it 'should use -source nn argument if source option set' do
+ compile_task.using(:source=>'1.5')
+ javac_args.should include('-source', '1.5')
+ end
+
+ it 'should use -target nn argument if target option set' do
+ compile_task.using(:target=>'1.5')
+ javac_args.should include('-target', '1.5')
+ end
+
+ it 'should set lint option to false by default' do
+ compile_task.options.lint.should be_false
+ end
+
+ it 'should use -lint argument if lint option is true' do
+ compile_task.using(:lint=>true)
+ javac_args.should include('-Xlint')
+ end
+
+ it 'should use -lint argument with value of option' do
+ compile_task.using(:lint=>'all')
+ javac_args.should include('-Xlint:all')
+ end
+
+ it 'should use -lint argument with value of option as array' do
+ compile_task.using(:lint=>['path', 'serial'])
+ javac_args.should include('-Xlint:path,serial')
+ end
+
+ it 'should not set other option by default' do
+ compile_task.options.other.should be_nil
+ end
+
+ it 'should pass other argument if other option is string' do
+ compile_task.using(:other=>'-Xprint')
+ javac_args.should include('-Xprint')
+ end
+
+ it 'should pass other argument if other option is array' do
+ compile_task.using(:other=>['-Xstdout', 'msgs'])
+ javac_args.should include('-Xstdout', 'msgs')
+ end
+
+ it 'should complain about options it doesn\'t know' do
+ write 'source/Test.java', 'class Test {}'
+ compile_task.using(:unknown=>'option')
+ lambda { compile_task.from('source').invoke }.should raise_error(ArgumentError, /no such option/i)
+ end
+
+ it 'should inherit options from parent' do
+ define 'foo' do
+ compile.using(:warnings=>true, :debug=>true, :deprecation=>true, :source=>'1.5', :target=>'1.4')
+ define 'bar' do
+ compile.using(:javac)
+ compile.options.warnings.should be_true
+ compile.options.debug.should be_true
+ compile.options.deprecation.should be_true
+ compile.options.source.should eql('1.5')
+ compile.options.target.should eql('1.4')
+ end
+ end
+ end
+
+ after do
+ Buildr.options.debug = nil
+ ENV.delete "debug"
+ ENV.delete "DEBUG"
+ end
+end
+
View
201 spec/sandbox.rb
@@ -0,0 +1,201 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership. The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+
+# The local repository we use for testing is void of any artifacts, which will break given
+# that the code requires several artifacts. So we establish them first using the real local
+# repository and cache these across test cases.
+Buildr.application.instance_eval { @rakefile = File.expand_path('buildfile') }
+repositories.remote << 'http://repo1.maven.org/maven2'
+repositories.remote << 'http://scala-tools.org/repo-releases'
+
+# Force Scala version for specs; don't want to rely on SCALA_HOME
+module Buildr::Scala
+ SCALA_VERSION_FOR_SPECS = ENV["SCALA_VERSION"] || "2.8.1"
+end
+Buildr.settings.build['scala.version'] = Buildr::Scala::SCALA_VERSION_FOR_SPECS
+
+# Add a 'require' here only for optional extensions, not for extensions that should be loaded by default.
+require 'buildr/clojure'
+require 'buildr/groovy'
+require 'buildr/scala'
+require 'buildr/bnd'
+require 'buildr/jaxb_xjc'
+
+Java.load # Anything added to the classpath.
+artifacts(
+ TestFramework.frameworks.map(&:dependencies).flatten,
+ JUnit.ant_taskdef,
+ Buildr::Groovy.dependencies,
+ Buildr::JaxbXjc.dependencies,
+ Buildr::Bnd.dependencies,
+ Buildr::Scala::Scalac.dependencies,
+ Buildr::Scala::Specs.dependencies,
+ Buildr::Shell::BeanShell.artifact,
+ Buildr::Clojure.dependencies
+).each do |path|
+ file(path).invoke
+end
+
+ENV['HOME'] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp', 'home'))
+mkpath ENV['HOME']
+
+# Make Scala.version resilient to sandbox reset
+module Buildr::Scala
+ DEFAULT_VERSION = SCALA_VERSION_FOR_SPECS
+
+ class << self
+ def version
+ SCALA_VERSION_FOR_SPECS
+ end
+ end
+
+ class Scalac
+ class << self
+ def use_installed?
+ false
+ end
+ end
+ end
+end
+
+# We need to run all tests inside a _sandbox, tacking a snapshot of Buildr before the test,
+# and restoring everything to its previous state after the test. Damn state changes.
+module Sandbox
+
+ class << self
+ attr_reader :tasks, :rules
+
+ def included(spec)
+ spec.before(:each) { sandbox }
+ spec.after(:each) { reset }
+ end
+
+ # Require an optional extension without letting its callbacks pollute the Project class.
+ def require_optional_extension(extension_require_path)
+ project_callbacks_without_extension = Project.class_eval { @global_callbacks }.dup
+ begin
+ require extension_require_path
+ ensure
+ Project.class_eval { @global_callbacks = project_callbacks_without_extension }
+ end
+ end
+ end
+
+ @tasks = Buildr.application.tasks.collect do |original|
+ prerequisites = original.send(:prerequisites).map(&:to_s)
+ actions = original.instance_eval { @actions }.clone
+ lambda do
+ original.class.send(:define_task, original.name=>prerequisites).tap do |task|
+ task.comment = original.comment
+ actions.each { |action| task.enhance &action }
+ end
+ end
+ end
+ @rules = Buildr.application.instance_variable_get(:@rules)
+
+ def sandbox
+ @_sandbox = {}
+
+ # Create a temporary directory where we can create files, e.g,
+ # for projects, compilation. We need a place that does not depend
+ # on the current directory.
+ @_sandbox[:original_dir] = Dir.pwd
+ @temp = File.join(File.dirname(__FILE__), '../tmp')
+ FileUtils.mkpath @temp
+ Dir.chdir @temp
+
+ ARGV.clear
+ Buildr.application = Buildr::Application.new
+ Sandbox.tasks.each { |block| block.call }
+ Buildr.application.instance_variable_set :@rules, Sandbox.rules.clone
+ Buildr.application.instance_eval { @rakefile = File.expand_path('buildfile') }
+
+ @_sandbox[:load_path] = $LOAD_PATH.clone
+
+ # clear RUBYOPT since bundler hooks into it
+ # e.g. RUBYOPT=-I/usr/lib/ruby/gems/1.8/gems/bundler-1.0.15/lib -rbundler/setup
+ # and so Buildr's own Gemfile configuration taints e.g., JRuby's environment
+ @_sandbox[:ruby_opt] = ENV["RUBYOPT"]
+ ENV["RUBYOPT"] = nil
+
+ #@_sandbox[:loaded_features] = $LOADED_FEATURES.clone
+
+ # Later on we'll want to lose all the on_define created during the test.
+ @_sandbox[:on_define] = Project.class_eval { (@on_define || []).dup }
+ @_sandbox[:extension_modules] = Project.class_eval { (@extension_modules || []).dup }
+ @_sandbox[:global_callbacks] = Project.class_eval { (@global_callbacks || []).dup }
+ @_sandbox[:layout] = Layout.default.clone
+
+ # Create a local repository we can play with. However, our local repository will be void
+ # of some essential artifacts (e.g. JUnit artifacts required by build task), so we create
+ # these first (see above) and keep them across test cases.
+ @_sandbox[:artifacts] = Artifact.class_eval { @artifacts }.clone
+ @_sandbox[:local_repository] = Buildr.repositories.local
+ ENV['HOME'] = File.expand_path('home')
+ ENV['BUILDR_ENV'] = 'development'
+
+ @_sandbox[:env_keys] = ENV.keys
+ ['DEBUG', 'TEST', 'HTTP_PROXY', 'HTTPS_PROXY', 'USER'].each { |k| ENV.delete(k) ; ENV.delete(k.downcase) }
+
+ # By default, remote repository is user's own local M2 repository
+ # since we don't want to remotely download artifacts into the sandbox over and over
+ Buildr.repositories.instance_eval do
+ @remote = ["file://" + @local]
+ @local = @release_to = nil
+ end
+ Buildr.options.proxy.http = nil
+
+ # Don't output crap to the console.
+ trace false
+ verbose false
+ end
+
+ # Call this from teardown.
+ def reset
+ # Get rid of all the projects and the on_define blocks we used.
+ Project.clear
+
+ on_define = @_sandbox[:on_define]
+ extension_modules = @_sandbox[:extension_modules]
+ global_callbacks = @_sandbox[:global_callbacks]
+
+ Project.class_eval do
+ @on_define = on_define
+ @global_callbacks = global_callbacks
+ @extension_modules = extension_modules
+ end
+
+ Layout.default = @_sandbox[:layout].clone
+
+ $LOAD_PATH.replace @_sandbox[:load_path]
+ ENV["RUBYOPT"] = @_sandbox[:ruby_opt]
+
+ FileUtils.rm_rf @temp
+ mkpath ENV['HOME']
+
+ # Get rid of all artifacts.
+ @_sandbox[:artifacts].tap { |artifacts| Artifact.class_eval { @artifacts = artifacts } }
+
+ Buildr.repositories.local = @_sandbox[:local_repository]
+
+ # Restore options.
+ Buildr.options.test = nil
+ (ENV.keys - @_sandbox[:env_keys]).each { |key| ENV.delete key }
+
+ Dir.chdir @_sandbox[:original_dir]
+ end
+
+end
View
369 spec/spec_helpers.rb
@@ -0,0 +1,369 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership. The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+
+# This file gets loaded twice when running 'spec spec/*' and not with pleasent results,
+# so ignore the second attempt to load it.
+unless defined?(SpecHelpers)
+
+ require 'rubygems'
+
+ # For testing we use the gem requirements specified on the buildr.gemspec
+ spec = Gem::Specification.load(File.expand_path('../buildr.gemspec', File.dirname(__FILE__)))
+ # Dependency.version_requirements deprecated in rubygems 1.3.6
+ spec.dependencies.select {|dep| dep.type == :runtime }.each { |dep| gem dep.name, (dep.respond_to?(:requirement) ? dep.requirement.to_s : dep.version_requirements.to_s) }
+
+ # Make sure to load from these paths first, we don't want to load any
+ # code from Gem library.
+ $LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__)),
+ File.expand_path('../addon', File.dirname(__FILE__))
+
+ # Buildr uses autoload extensively, but autoload when running specs creates
+ # a problem -- we sandbox $LOADED_FEATURES, so we endup autoloading the same
+ # module twice. This turns autoload into a require, which is not the right
+ # thing, but will do for now.
+ def autoload(symbol, path)
+ require path
+ end
+ require 'buildr'
+ # load ecj
+ require 'buildr/java/ecj'
+ #Make ecj appear as a compiler that doesn't apply:
+ class Buildr::Compiler::Ecj
+ class << self
+ def applies_to?(project, task)
+ false
+ end
+ end
+ end
+
+ # Give a chance for plugins to do a few things before requiring the sandbox.
+ include SandboxHook if defined?(SandboxHook)
+
+ require File.expand_path('sandbox', File.dirname(__FILE__))
+
+ module SpecHelpers
+
+ include Checks::Matchers
+
+ [:info, :warn, :error, :puts].each do |severity|
+ ::Object.class_eval do
+ define_method severity do |*args|
+ $messages ||= {}
+ $messages[severity] ||= []
+ $messages[severity].push(*args)
+ end
+ end
+ end
+
+ class << Buildr.application
+ alias :deprecated_without_capture :deprecated
+ def deprecated(message)
+ verbose(true) { deprecated_without_capture message }
+ end
+ end
+
+ class MessageWithSeverityMatcher
+ def initialize(severity, message)
+ @severity = severity
+ @expect = message
+ end
+
+ def matches?(target)
+ $messages = {@severity => []}
+ target.call
+ return Regexp === @expect ? $messages[@severity].join('\n') =~ @expect : $messages[@severity].include?(@expect.to_s)
+ end
+
+ def failure_message
+ "Expected #{@severity} #{@expect.inspect}, " +
+ ($messages[@severity].empty? ? "no #{@severity} issued" : "found #{$messages[@severity].inspect}")
+ end
+
+ def negative_failure_message
+ "Found unexpected #{$messages[@severity].inspect}"
+ end
+ end
+
+ # Test if an info message was shown. You can use a string or regular expression.
+ #
+ # For example:
+ # lambda { info 'ze test' }.should show_info(/ze test/)
+ def show_info(message)
+ MessageWithSeverityMatcher.new :info, message
+ end
+
+ # Test if a warning was shown. You can use a string or regular expression.
+ #
+ # For example:
+ # lambda { warn 'ze test' }.should show_warning(/ze test/)
+ def show_warning(message)
+ MessageWithSeverityMatcher.new :warn, message
+ end
+
+ # Test if an error message was shown. You can use a string or regular expression.
+ #
+ # For example:
+ # lambda { error 'ze test' }.should show_error(/ze test/)
+ def show_error(message)
+ MessageWithSeverityMatcher.new :error, message
+ end
+
+ # Test if any message was shown (puts). You can use a string or regular expression.
+ #
+ # For example:
+ # lambda { puts 'ze test' }.should show(/ze test/)
+ def show(message)
+ MessageWithSeverityMatcher.new :puts, message
+ end
+
+ # Yields a block that should try exiting the application.
+ # Accepts
+ #
+ # For example:
+ # test_exit(1) { puts "Hello" ; exit(1) }.should show("Hello")
+ #
+ def test_exit(status = nil)
+ return lambda {
+ begin
+ yield
+ raise "Exit was not called!"
+ rescue SystemExit => e
+ raise "Exit status incorrect! Expected: #{status}, got #{e.status}" if status && (e.status != status)
+ end
+ }
+ end
+
+ class ::Rake::Task
+ alias :execute_without_a_record :execute
+ def execute(args)
+ $executed ||= []
+ $executed << name
+ execute_without_a_record args
+ end
+ end
+
+ class InvokeMatcher
+ def initialize(*tasks)
+ @expecting = tasks.map { |task| [task].flatten.map(&:to_s) }
+ end
+
+ def matches?(target)
+ $executed = []
+ target.call
+ return false unless all_ran?
+ return !@but_not.any_ran? if @but_not
+ return true
+ end
+
+ def failure_message
+ return @but_not.negative_failure_message if all_ran? && @but_not
+ "Expected the tasks #{expected} to run, but #{remaining} did not run, or not in the order we expected them to." +
+ " Tasks that ran: #{$executed.inspect}"
+ end
+
+ def negative_failure_message
+ if all_ran?
+ "Expected the tasks #{expected} to not run, but they all ran."
+ else
+ "Expected the tasks #{expected} to not run, and all but #{remaining} ran."
+ end
+ end
+
+ def but_not(*tasks)
+ @but_not = InvokeMatcher.new(*tasks)
+ self
+ end
+
+ protected
+
+ def expected
+ @expecting.map { |tests| tests.join('=>') }.join(', ')
+ end
+
+ def remaining
+ @remaining.map { |tests| tests.join('=>') }.join(', ')
+ end
+
+ def all_ran?
+ @remaining ||= $executed.inject(@expecting) do |expecting, executed|
+ expecting.map { |tasks| tasks.first == executed ? tasks[1..-1] : tasks }.reject(&:empty?)
+ end
+ @remaining.empty?
+ end
+
+ def any_ran?
+ all_ran?
+ @remaining.size < @expecting.size
+ end
+
+ end
+
+ # Tests that all the tasks ran, in the order specified. Can also be used to test that some
+ # tasks and not others ran.
+ #
+ # Takes a list of arguments. Each argument can be a task name, matching only if that task ran.
+ # Each argument can be an array of task names, matching only if all these tasks ran in that order.
+ # So run_tasks('foo', 'bar') expects foo and bar to run in any order, but run_task(['foo', 'bar'])
+ # expects foo to run before bar.
+ #
+ # You can call but_not on the matchers to specify that certain tasks must not execute.
+ #
+ # For example:
+ # # Either task
+ # lambda { task('compile').invoke }.should run_tasks('compile', 'resources')
+ # # In that order
+ # lambda { task('build').invoke }.should run_tasks(['compile', 'test'])
+ # # With exclusion
+ # lambda { task('build').invoke }.should run_tasks('compile').but_not('install')
+ def run_tasks(*tasks)
+ InvokeMatcher.new *tasks
+ end
+
+ # Tests that a task ran. Similar to run_tasks, but accepts a single task name.
+ #
+ # For example:
+ # lambda { task('build').invoke }.should run_task('test')
+ def run_task(task)
+ InvokeMatcher.new [task]
+ end
+
+ class UriPathMatcher
+ def initialize(re)
+ @expression = re
+ end
+
+ def matches?(uri)
+ @uri = uri
+ uri.path =~ @expression
+ end
+
+ def description
+ "URI with path matching #{@expression}"
+ end
+ end
+
+ # Matches a parsed URI's path against the given regular expression
+ def uri(re)
+ UriPathMatcher.new(re)
+ end
+
+
+ class AbsolutePathMatcher
+ def initialize(path)
+ @expected = File.expand_path(path.to_s)
+ end
+
+ def matches?(path)
+ @provided = File.expand_path(path.to_s)
+ @provided == @expected
+ end
+
+ def failure_message
+ "Expected path #{@expected}, but found path #{@provided}"
+ end
+
+ def negative_failure_message
+ "Expected a path other than #{@expected}"
+ end
+ end
+
+ def point_to_path(path)
+ AbsolutePathMatcher.new(path)
+ end
+
+
+ # Value covered by range. For example:
+ # (1..5).should cover(3)
+ RSpec::Matchers.define :cover do |actual|
+ match do |range|
+ actual >= range.min && actual <= range.max
+ end
+ end
+
+
+ def suppress_stdout
+ stdout = $stdout
+ $stdout = StringIO.new
+ begin
+ yield
+ ensure
+ $stdout = stdout
+ end
+ end
+
+ def dryrun
+ Buildr.application.options.dryrun = true
+ begin
+ suppress_stdout { yield }
+ ensure
+ Buildr.application.options.dryrun = false
+ end
+ end
+
+ # We run tests with tracing off. Then things break. And we need to figure out what went wrong.
+ # So just use trace() as you would use verbose() to find and squash the bug.
+ def trace(value = nil)
+ old_value = Buildr.application.options.trace
+ Buildr.application.options.trace = value unless value.nil?
+ if block_given?
+ begin
+ yield
+ ensure
+ Buildr.application.options.trace = old_value
+ end
+ end
+ Buildr.application.options.trace
+ end
+
+ # Change the Buildr original directory, faking invocation from a different directory.
+ def in_original_dir(dir)
+ begin
+ original_dir = Buildr.application.original_dir
+ Buildr.application.instance_eval { @original_dir = File.expand_path(dir) }
+ yield
+ ensure
+ Buildr.application.instance_eval { @original_dir = original_dir }
+ end
+ end
+
+
+ # Buildr's define method creates a project definition but does not evaluate it
+ # (that happens once the buildfile is loaded), and we include Buildr's define in
+ # the test context so we can use it without prefixing with Buildr. This just patches
+ # define to evaluate the project definition before returning it.
+ def define(name, properties = nil, &block) #:yields:project
+ Project.define(name, properties, &block).tap { |project| project.invoke }
+ end
+
+ end
+
+
+ # Allow using matchers within the project definition.
+ class Buildr::Project
+ include ::RSpec::Matchers, SpecHelpers
+ end
+
+
+ ::RSpec.configure do |config|
+ # Make all Buildr methods accessible from test cases, and add various helper methods.
+ config.include Buildr
+ config.include SpecHelpers
+
+ # Sanbdox Buildr for each test.
+ config.include Sandbox
+ end
+
+end
View
18 test/helper.rb
@@ -1,18 +0,0 @@
-require 'rubygems'
-require 'bundler'
-begin
- Bundler.setup(:default, :development)
-rescue Bundler::BundlerError => e
- $stderr.puts e.message
- $stderr.puts "Run `bundle install` to install missing gems"
- exit e.status_code
-end
-require 'test/unit'
-require 'shoulda'
-
-$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-#require File.dirname(__FILE__) +
-
-class Test::Unit::TestCase
-end
View
7 test/test_buildr_as3.rb
@@ -1,7 +0,0 @@
-require 'helper'
-
-class TestBuildrAs3 < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- assert_equal( 1, 1, "1 should be one")
- end
-end

0 comments on commit a5c17b0

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