bengler_test_helper 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ *~
2
+ .DS_Store
3
+ /.bundle
4
+ /log
5
+ /tmp
6
+ /trash
7
+ .rvmrc
8
+ *.sublime-project
9
+ *.sublime-workspace
10
+ .rspec
11
+ /coverage
12
+ Gemfile.lock
13
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bengler_test_helper.gemspec
4
+ gemspec
@@ -0,0 +1,24 @@
1
+ This is common test stuff that can be used by Ruby apps.
2
+
3
+ Tasks
4
+ -----
5
+
6
+ To include tasks, add a Rakefile to your project containing the following:
7
+
8
+ require 'bengler_test_helper/tasks'
9
+
10
+ This adds:
11
+
12
+ * `rake db:structure:load`: Loads test database with schema from `db/development_structure.sql`.
13
+
14
+ Also, it overrides Rails' default tasks:
15
+
16
+ * `rake db:migrate`: Automatically saves schema to `db/development_structure.sql`
17
+ by invoking `db:structure:dump`.
18
+ * `rake db:test:prepare`: Instead of dumping schema from development database,
19
+ it loads bootstrap data form `db/bootstrap.sql` and uses `db:structure:load`.
20
+
21
+ For non-Rails projects, the following Rails-compatible tasks are provided:
22
+
23
+ * `rake db:structure:dump`: Saves schema to `db/development_structure.sql` like
24
+ you would expect.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "bengler_test_helper/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "bengler_test_helper"
8
+ s.version = BenglerTestHelper::VERSION
9
+ s.authors = ["Alexander Staubo"]
10
+ s.email = ["alex@origo.no"]
11
+ s.homepage = ""
12
+ s.summary = %q{This is common test stuff that can be used by Ruby apps.}
13
+ s.description = %q{This is common test stuff that can be used by Ruby apps.}
14
+ s.rubyforge_project = "bengler_test_helper"
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency 'rspec'
21
+ s.add_development_dependency 'rake'
22
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ module BenglerTestHelper
2
+ module ActiveRecord
3
+
4
+ class << self
5
+ def database_configuration(environment)
6
+ require 'active_record'
7
+ ::ActiveRecord::Base.configurations = YAML.load(File.open('config/database.yml'))
8
+ config = ::ActiveRecord::Base.configurations[environment]
9
+ raise "No database configuration for environment #{environment.inspect}." unless config
10
+ config
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ raise 'bengler_test_helper should only be loaded in the development or test envs.' unless ['development', 'test'].include?(ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development')
2
+
3
+ require 'rake' unless defined?(Rake)
4
+
5
+ Rake::TaskManager.class_eval do
6
+ unless method_defined?(:remove_task)
7
+ def remove_task(task_name)
8
+ @tasks.delete(task_name.to_s)
9
+ end
10
+ end
11
+ end
12
+
13
+ begin
14
+ # If there's no active record, we don't care.
15
+ require 'active_record'
16
+
17
+ Rake.application.remove_task :"db:test:prepare"
18
+ Rake.application.remove_task :"db:structure:dump"
19
+
20
+ require 'bengler_test_helper/active_record'
21
+ require 'bengler_test_helper/tasks/db_migrate'
22
+ require 'bengler_test_helper/tasks/db_structure_dump'
23
+ require 'bengler_test_helper/tasks/db_structure_load'
24
+ require 'bengler_test_helper/tasks/db_test_config'
25
+ require 'bengler_test_helper/tasks/db_test_create'
26
+ require 'bengler_test_helper/tasks/db_test_prepare'
27
+
28
+ rescue LoadError => e
29
+ puts "We can't load ActiveRecord. That might be fine. #{e.message}"
30
+ rescue Exception => e
31
+ puts "Bengel Tools Fail. #{e.message}."
32
+ end
@@ -0,0 +1,5 @@
1
+ namespace :db do
2
+ Rake::Task['migrate'].enhance do
3
+ Rake::Task['db:structure:dump'].invoke unless ENV['RACK_ENV'] == 'development'
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ require 'tempfile'
2
+
3
+ namespace :db do
4
+ namespace :structure do
5
+ desc 'Dump database schema to development_structure.sql'
6
+ task :dump do
7
+ config = BenglerTestHelper::ActiveRecord.database_configuration(ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development')
8
+ abort "Database configuration must use localhost." unless %w(localhost 127.0.0.1).include?(config['host'])
9
+
10
+ schema_file_name = 'db/development_structure.sql'
11
+ Tempfile.open('schema') do |tempfile|
12
+ tempfile.close
13
+ unless system("pg_dump --format=p --schema-only --no-owner --no-privileges " \
14
+ "-U '#{config['username']}' -f '#{tempfile.path}' '#{config['database']}'")
15
+ abort "Failed to dump SQL."
16
+ end
17
+ old_data = File.read(schema_file_name) rescue nil
18
+ new_data = File.read(tempfile.path)
19
+ if old_data != new_data
20
+ $stderr.puts "Dumping new schema to #{schema_file_name}."
21
+ File.open(schema_file_name, 'w') { |file| file << new_data }
22
+ else
23
+ $stderr.puts "Scheme has not changed, not updating #{schema_file_name}."
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ namespace :db do
2
+ namespace :structure do
3
+ desc 'Recreate test database from development_structure.sql'
4
+ task :load do
5
+ config = BenglerTestHelper::ActiveRecord.database_configuration('test')
6
+ abort 'Database configuration must use localhost.' unless %w(localhost 127.0.0.1).include?(config['host'])
7
+
8
+ schema_file_name = 'db/development_structure.sql'
9
+ unless system("psql -1 -q -o /dev/null --no-psqlrc -d '#{config['database']}' -f '#{schema_file_name}'")
10
+ abort "Failed to load SQL from #{schema_file_name}. Ensure that your own Postgres user is a superuser."
11
+ end
12
+
13
+ $stderr.puts "Database #{config['database']} loaded from #{schema_file_name}."
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ namespace :db do
2
+ namespace :test do
3
+ desc 'Sets up a database config file.'
4
+ task :config do
5
+ require 'bundler'
6
+ b = Bundler.setup
7
+ name = File.basename(`git config --get remote.origin.url`.strip, '.git')
8
+ File.open('config/database.yml', 'w') do |f|
9
+ f.write <<-end
10
+ test:
11
+ adapter: #{b.gems['activerecord-postgis-adapter'].empty? ? 'postgresql' : 'postgis'}
12
+ host: localhost
13
+ database: #{name}_test
14
+ username: #{name}
15
+ password: #{(0...8).map{(97+rand(26)).chr}.join}
16
+ encoding: unicode
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ namespace :db do
2
+ namespace :test do
3
+ desc 'Creates a test database.'
4
+ task :create do
5
+ config = BenglerTestHelper::ActiveRecord.database_configuration('test')
6
+ abort 'Database configuration must use localhost.' unless %w(localhost 127.0.0.1).include?(config['host'])
7
+
8
+ unless system("psql -tA postgres -c '\\du' | grep -E '^#{config['username']}\\|' >/dev/null")
9
+ unless system("psql postgres -q -c \"CREATE ROLE #{config['username']} SUPERUSER LOGIN PASSWORD '#{config['password']}'\"")
10
+ abort "Could not create database user '#{config['username']}'"
11
+ end
12
+ end
13
+
14
+ if system("psql -ltA | grep -E '^#{config['database']}\\|' >/dev/null")
15
+ unless system("dropdb '#{config['database']}'")
16
+ abort "Could not drop database '#{config['database']}'"
17
+ end
18
+ end
19
+
20
+ unless system("createdb -O '#{config['username']}' '#{config['database']}'")
21
+ abort "Could not create database '#{config['database']}'"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ namespace :db do
2
+ namespace :test do
3
+ desc 'Prepare database for running tests.'
4
+ task :prepare do
5
+ Rake::Task['db:test:config'].invoke if ENV['SEMAPHORE'] or not File.exists?'config/database.yml'
6
+ Rake::Task['db:test:create'].invoke
7
+ Rake::Task['db:structure:load'].invoke
8
+
9
+ bootstrap_file_name = File.expand_path('db/bootstrap.sql', '.')
10
+ if File.exist?(bootstrap_file_name)
11
+ config = BenglerTestHelper::ActiveRecord.database_configuration('test')
12
+ unless system("psql -q -o /dev/null --no-psqlrc -d '#{config['database']}' -f '#{bootstrap_file_name}'")
13
+ abort "Failed to load SQL from #{bootstrap_file_name}."
14
+ end
15
+ $stderr.puts "Loaded bootstrap data from #{bootstrap_file_name}."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module BenglerTestHelper
2
+ VERSION = '0.0.3'
3
+ end
@@ -0,0 +1,71 @@
1
+ require "time"
2
+ require "rspec/core/formatters/base_formatter"
3
+
4
+ class JUnitFormatter < RSpec::Core::Formatters::BaseFormatter
5
+
6
+ attr_reader :test_results
7
+
8
+ def initialize(output)
9
+ super(output)
10
+ @test_results = { :failures => [], :successes => [] }
11
+ end
12
+
13
+ def example_passed(example)
14
+ super(example)
15
+ @test_results[:successes].push(example)
16
+ end
17
+
18
+ def example_pending(example)
19
+ super(example)
20
+ # let jenkins ignore this
21
+ end
22
+
23
+ def example_failed(example)
24
+ super(example)
25
+ @test_results[:failures].push(example)
26
+ end
27
+
28
+ def extract_errors(example)
29
+ exception = example.metadata[:execution_result][:exception_encountered] || example.metadata[:execution_result][:exception]
30
+ message = ""
31
+ unless exception.nil?
32
+ message = exception.message
33
+ message += "\n"
34
+ message += format_backtrace(exception.backtrace, example).join("\n")
35
+ end
36
+ return message
37
+ end
38
+
39
+ def dump_summary(duration, example_count, failure_count, pending_count)
40
+ super(duration, example_count, failure_count, pending_count)
41
+ output.puts("<?xml version=\"1.0\" encoding=\"utf-8\" ?>")
42
+ output.puts("<testsuite errors=\"0\" failures=\"#{failure_count+pending_count}\" tests=\"#{example_count}\" time=\"#{duration}\" timestamp=\"#{Time.now.iso8601}\">")
43
+ output.puts(" <properties />")
44
+ @test_results[:successes].each do |t|
45
+ md = t.metadata
46
+ runtime = md[:execution_result][:run_time]
47
+ description = _xml_escape(md[:full_description])
48
+ file_path = _xml_escape(md[:file_path])
49
+ output.puts(" <testcase classname=\"#{file_path}\" name=\"#{description}\" time=\"#{runtime}\" />")
50
+ end
51
+ @test_results[:failures].each do |t|
52
+ md = t.metadata
53
+ description = _xml_escape(md[:full_description])
54
+ file_path = _xml_escape(md[:file_path])
55
+ runtime = md[:execution_result][:run_time]
56
+ output.puts(" <testcase classname=\"#{file_path}\" name=\"#{description}\" time=\"#{runtime}\">")
57
+ output.puts(" <failure message=\"failure\" type=\"failure\">")
58
+ output.puts("<![CDATA[ #{extract_errors(t)} ]]>")
59
+ output.puts(" </failure>")
60
+ output.puts(" </testcase>")
61
+ end
62
+ output.puts("</testsuite>")
63
+ end
64
+
65
+ def _xml_escape(x)
66
+ x.gsub("&", "&amp;").
67
+ gsub("\"", "&quot;").
68
+ gsub(">", "&gt;").
69
+ gsub("<", "&lt;")
70
+ end
71
+ end
@@ -0,0 +1,125 @@
1
+ require 'rspec/core/formatters/junit_formatter'
2
+
3
+ describe JUnitFormatter do
4
+ let(:now) { Time.now.utc }
5
+ let(:output) { StringIO.new }
6
+ let(:formatter) { JUnitFormatter.new output }
7
+
8
+ before(:each) { Time.stub(:now => now) }
9
+
10
+ it "starts with no failures and no successes" do
11
+ JUnitFormatter.new(StringIO.new).test_results.should eq({:failures=>[], :successes=>[]})
12
+ end
13
+
14
+ it "adds passing spec to the success list" do
15
+ formatter.example_passed "passing spec"
16
+ formatter.test_results[:successes].should eq ["passing spec"]
17
+ end
18
+
19
+ it "adds failing spec to failure list" do
20
+ formatter.example_failed "failing spec"
21
+ formatter.test_results[:failures].should eq ["failing spec"]
22
+ end
23
+
24
+ it "ignores pending specs" do
25
+ formatter.example_pending "pending spec"
26
+ formatter.test_results.should eq({:failures=>[], :successes=>[]})
27
+ end
28
+
29
+ describe "#extract_errors" do
30
+ let(:example) { stub(:example) }
31
+
32
+ it "is empty when there are no failures" do
33
+ metadata = {:execution_result => { :exception_encountered => nil, :exception => nil }}
34
+ example.stub(:metadata => metadata)
35
+
36
+ formatter.extract_errors(example).should eq("")
37
+ end
38
+
39
+ context "with a stacktrace" do
40
+ let(:stacktrace) { stub(:stacktrace, :message => "foobar", :backtrace => ["foo", "bar"]) }
41
+
42
+ it "extracts errors from :exception field" do
43
+ metadata = {:execution_result => {:exception_encountered => nil, :exception => stacktrace }}
44
+ example.stub(:metadata => metadata)
45
+
46
+ formatter.extract_errors(example).should eq("foobar\nfoo\nbar")
47
+ end
48
+
49
+ it "extracts errors from :exception_encountered field" do
50
+ metadata = {:execution_result => {:exception_encountered => stacktrace}}
51
+ example.stub(:metadata => metadata)
52
+ formatter.extract_errors(example).should eq("foobar\nfoo\nbar")
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "junit xml" do
58
+ let(:passing_metadata) do
59
+ {
60
+ :full_description => "a passing spec",
61
+ :file_path => "/path/to/passing_spec.rb",
62
+ :execution_result => { :run_time => 0.1 }
63
+ }
64
+ end
65
+
66
+ let(:failing_metadata) do
67
+ {
68
+ :full_description => "a failing spec",
69
+ :file_path => "/path/to/failing_spec.rb",
70
+ :execution_result => { :exception_encountered => stacktrace, :run_time => 0.1 }
71
+ }
72
+ end
73
+
74
+ let(:metadata_with_funky_characters) do
75
+ {
76
+ :full_description => "a passing spec >>> &\"& <<<",
77
+ :file_path => "/path/to/passing_spec.rb",
78
+ :execution_result => { :run_time => 0.1 }
79
+ }
80
+ end
81
+
82
+ let(:stacktrace) { stub(:stacktrace, :message => "foobar", :backtrace => ["foo", "bar"]) }
83
+ let(:passing_spec) { stub(:passing_spec) }
84
+ let(:failing_spec) { stub(:failing_spec, :metadata => failing_metadata) }
85
+
86
+ it "outputs the junit xml" do
87
+ passing_spec.stub(:metadata => passing_metadata)
88
+
89
+ formatter.example_passed passing_spec
90
+ formatter.example_failed failing_spec
91
+
92
+ formatter.dump_summary("0.1", 2, 1, 0)
93
+
94
+ expected = <<-REPORT
95
+ <?xml version="1.0" encoding="utf-8" ?>
96
+ <testsuite errors="0" failures="1" tests="2" time="0.1" timestamp="#{now.iso8601}">
97
+ <properties />
98
+ <testcase classname="/path/to/passing_spec.rb" name="a passing spec" time="0.1" />
99
+ <testcase classname="/path/to/failing_spec.rb" name="a failing spec" time="0.1">
100
+ <failure message="failure" type="failure">
101
+ <![CDATA[ foobar\nfoo\nbar ]]>
102
+ </failure>
103
+ </testcase>
104
+ </testsuite>
105
+ REPORT
106
+ output.string.should eq expected
107
+ end
108
+
109
+ it "escapes funky characters" do
110
+ passing_spec.stub(:metadata => metadata_with_funky_characters)
111
+
112
+ formatter.example_passed passing_spec
113
+ formatter.dump_summary("0.1", 2, 1, 0)
114
+
115
+ expected = <<-REPORT
116
+ <?xml version="1.0" encoding="utf-8" ?>
117
+ <testsuite errors="0" failures="1" tests="2" time="0.1" timestamp="#{now.iso8601}">
118
+ <properties />
119
+ <testcase classname="/path/to/passing_spec.rb" name="a passing spec &gt;&gt;&gt; &amp;&quot;&amp; &lt;&lt;&lt;" time="0.1" />
120
+ </testsuite>
121
+ REPORT
122
+ output.string.should eq expected
123
+ end
124
+ end
125
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bengler_test_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Staubo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: This is common test stuff that can be used by Ruby apps.
47
+ email:
48
+ - alex@origo.no
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - README.md
56
+ - Rakefile
57
+ - bengler_test_helper.gemspec
58
+ - lib/bengler_test_helper.rb
59
+ - lib/bengler_test_helper/active_record.rb
60
+ - lib/bengler_test_helper/tasks.rb
61
+ - lib/bengler_test_helper/tasks/db_migrate.rb
62
+ - lib/bengler_test_helper/tasks/db_structure_dump.rb
63
+ - lib/bengler_test_helper/tasks/db_structure_load.rb
64
+ - lib/bengler_test_helper/tasks/db_test_config.rb
65
+ - lib/bengler_test_helper/tasks/db_test_create.rb
66
+ - lib/bengler_test_helper/tasks/db_test_prepare.rb
67
+ - lib/bengler_test_helper/version.rb
68
+ - lib/rspec/core/formatters/junit_formatter.rb
69
+ - spec/junit_formatter_spec.rb
70
+ homepage: ''
71
+ licenses: []
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ segments:
83
+ - 0
84
+ hash: -4559941765150812453
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ segments:
92
+ - 0
93
+ hash: -4559941765150812453
94
+ requirements: []
95
+ rubyforge_project: bengler_test_helper
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: This is common test stuff that can be used by Ruby apps.
100
+ test_files:
101
+ - spec/junit_formatter_spec.rb