Permalink
Browse files

Add simple initial unit tests.

  • Loading branch information...
1 parent b5b0de3 commit cb20fc9d57b1e66b0d737ff8481e8e2ddd4bef4f @cespare cespare committed Mar 27, 2012
Showing with 135 additions and 1 deletion.
  1. +10 −0 Rakefile
  2. +4 −1 terraform.gemspec
  3. +24 −0 test/test_helper.rb
  4. +97 −0 test/unit/terraform/terraform_dsl_test.rb
View
@@ -1 +1,11 @@
require "bundler/gem_tasks"
+require "rake/testtask"
+
+task :test => ["test:units"]
+
+namespace :test do
+ Rake::TestTask.new(:units) do |task|
+ task.libs << "test"
+ task.test_files = FileList["test/unit/**/*_test.rb"]
+ end
+end
View
@@ -15,6 +15,9 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ["lib"]
+
+ s.add_development_dependency "scope", "~> 0.2.3"
+ s.add_development_dependency "rr", "~> 1.0.4"
end
View
@@ -0,0 +1,24 @@
+require "bundler/setup"
+require "minitest/autorun"
+require "scope"
+require "rr"
+require "stringio"
+
+$:.unshift(File.join(File.dirname(__FILE__), "../lib"))
+
+module Scope
+ class TestCase
+ include RR::Adapters::MiniTest
+ end
+end
+
+module Kernel
+ def capture_output
+ result = StringIO.new
+ $stdout = result
+ yield
+ result.string
+ ensure
+ $stdout = STDOUT
+ end
+end
@@ -0,0 +1,97 @@
+require File.expand_path(File.join(File.dirname(__FILE__), "../../test_helper.rb"))
+require "terraform/terraform_dsl"
+
+class DslTest < Scope::TestCase
+ include TerraformDsl
+
+ class ProcessExit < StandardError; end
+
+ # Methods to mock for met? and meet blocks
+ def do_met() end
+ def do_meet() end
+
+ setup do
+ stub(self).fail_and_exit(anything) { raise ProcessExit }
+ end
+
+ context "dep declarations" do
+ should "fail if there is no met? or meet given" do
+ assert_raises(ProcessExit) do
+ dep("no met?") { meet {} }
+ end
+ assert_raises(ProcessExit) do
+ dep("no meet") { met? {} }
+ end
+ end
+
+ should "not run meet if met? is satisfied" do
+ mock(self).do_met { true }
+ mock(self).do_meet.never
+ dep "foo" do
+ met? { do_met }
+ meet { do_meet }
+ end
+ satisfy_dependencies
+ end
+
+ should "run meet if met? is not satisfied" do
+ met_run = false
+ mock(self).do_met.twice { result = met_run; met_run = true; result }
+ mock(self).do_meet
+ dep "foo" do
+ met? { do_met }
+ meet { do_meet }
+ end
+ message = capture_output { satisfy_dependencies }
+ assert_match /Dependency foo is not met/, message
+ end
+
+ should "only meet a dep once" do
+ met_run = false
+ mock(self).do_met.twice { result = met_run; met_run = true; result }
+ mock(self).do_meet
+ 2.times do
+ dep "foo" do
+ met? { do_met }
+ meet { do_meet }
+ end
+ end
+ capture_output { satisfy_dependencies }
+ end
+
+ should "fail if met? still fails after running meet" do
+ mock(self).do_met.twice { false }
+ mock(self).do_meet
+ dep "foo" do
+ met? { do_met }
+ meet { do_meet }
+ end
+ assert_raises(ProcessExit) { capture_output { satisfy_dependencies } }
+ end
+ end
+
+ context "ensure_ppa" do
+ should "fail if the PPA name is not the expected form" do
+ assert_raises(ProcessExit) { ensure_ppa("blah") }
+ assert_raises(ProcessExit) { ensure_ppa("http://some.ppa.url") }
+ end
+
+ # TODO(caleb): Add more unit tests. Having trouble coming up with unit tests that don't feel
+ # fragile/artificial. For helpers like these, integration tests of some kind that run on a vagrant box or
+ # something may prove more useful.
+ end
+
+ context "ensure_run_once" do
+ should "run a task exactly once" do
+ mock(self).do_meet.never
+ meet_run = false
+ ensure_run_once("foo") { meet_run = true }
+ dep "run task once: foo" do
+ met? { do_met }
+ meet { do_meet }
+ end
+ capture_output { satisfy_dependencies }
+ assert meet_run
+ end
+ end
+end

0 comments on commit cb20fc9

Please sign in to comment.