vault-test-tools 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx-2.1.1
data/Gemfile CHANGED
@@ -1,9 +1,13 @@
1
1
  source 'https://rubygems.org'
2
- ruby '1.9.3'
3
-
4
- # Specify your gem's dependencies in vault-dev-tools.gemspec
2
+ # Specify your gem's dependencies in vault-test-tools.gemspec
5
3
  gemspec
6
4
 
5
+ group :development do
6
+ gem 'rake'
7
+ gem 'yard'
8
+ end
9
+
7
10
  group :test do
8
11
  gem 'fakeweb'
12
+ gem 'scrolls', '~> 0.2.6'
9
13
  end
data/README.md CHANGED
@@ -3,25 +3,65 @@
3
3
  Test is the English phrase for テスト. Test tools for the Heroku
4
4
  Vault Team.
5
5
 
6
- ## Installation
6
+ ## Overview
7
7
 
8
- Add this line to your application's Gemfile:
8
+ ### Installation
9
+
10
+ Add this line to your application's `Gemfile`:
9
11
 
10
12
  group :test do
11
13
  gem 'vault-test-tools'
12
14
  end
13
15
 
14
- ## Usage
16
+ ### Usage
17
+
18
+ #### Rakefile
19
+
20
+ ```ruby
21
+ desc 'Run the test suite'
22
+ require 'vault-test-tools/rake_task'
23
+ ```
24
+
25
+ ##### note: we plan on renaming this to `test_task`
26
+
27
+ ### bin files
28
+
29
+ #### Run the tests with `t`
30
+
31
+ > t
32
+
33
+ Runs `bundle exec rake test`
34
+
35
+ #### Profile the tests with `pt`
36
+
37
+ > pt
38
+
39
+ #### Build the docs with `d`
40
+
41
+ > d
42
+
43
+ Runs `bundle exec yardoc`
15
44
 
16
45
  ### test/helper.rb
17
46
 
47
+ If you're using `vault-tools` and calling `Vault.setup` in
48
+ `my-lib-that-calls-vault-setup.rb` then it will automatically be
49
+ required when `RACK_ENV` is `test`.
50
+
51
+ ```ruby
52
+ ENV['RACK_ENV'] = 'test'
53
+ require 'my-lib-that-calls-vault-setup'
54
+ ```
55
+
56
+ Otherwise just require it:
57
+
18
58
  ```ruby
19
59
  require 'vault-test-tools'
20
60
  ```
21
61
 
22
- ### Test Base Classes
62
+ #### Test Base Classes
23
63
 
24
- Sublcass and go:
64
+ Subclass and go:
25
65
 
26
66
  ```ruby
27
67
  class MyTest < Vault::TestCase
@@ -31,9 +71,18 @@ class MyTest < Vault::TestCase
31
71
  end
32
72
  ```
33
73
 
34
- ### Spec Base Class
74
+ #### Spec Base Class
35
75
 
36
- Automatically installed as default. Just:
76
+ ##### in spec/helper.rb
77
+
78
+ We removed this from the gem so you have do to it in your helper file.
79
+
80
+ ```ruby
81
+ # Register our Spec class as the default.
82
+ MiniTest::Spec.register_spec_type //, Vault::Spec
83
+ ```
84
+
85
+ ##### go write some specs
37
86
 
38
87
  ```ruby
39
88
  describe 'Anything' do
@@ -43,30 +92,130 @@ describe 'Anything' do
43
92
  end
44
93
  ```
45
94
 
46
- ### Uniform Module Sharing
95
+ #### Test Helpers
96
+
97
+ You just include the helper you need into the test file you're writing.
98
+ This makes tests very self-contained and also provides a way to document
99
+ what you're pulling into the test.
100
+
101
+ If you really want to share something to all the tests then include
102
+ them into `Vault::TestCase`.
103
+
104
+ ##### in `test/helper.rb`
105
+
106
+ ```ruby
107
+ class Vault::TestCase
108
+ include Vault::Test::EnvironmentHelpers
109
+ end
110
+ ```
111
+
112
+ ##### `Vault::Test::EnvironmentHelpers`
113
+
114
+ Provides a `#set_env` method that temporarily overrides an environment
115
+ variable.
116
+
117
+ ```ruby
118
+ class MyTest < Vault::TestCase
119
+ include Vault::Test::EnvironmentHelpers
120
+
121
+ def test_env_changes
122
+ assert_equal('var', ENV['FOO'])
123
+ set_env 'FOO', '42'
124
+ assert_equal('42', ENV['FOO'])
125
+ end
126
+ end
127
+ ```
128
+
129
+ ##### `Vault::Test::HTMLHelpers`
130
+
131
+ Provides `#assert_css` and `#save_and_open_page` to make assertions
132
+ against the `last_response.body` simple.
133
+
134
+ ```ruby
135
+ class Test < Vault::TestCase
136
+ include Vault::Test::HTMLHelpers
137
+
138
+ def test_env_changes
139
+ get '/my-awesome-page'
140
+ save_and_open_page
141
+ assert_css('#selector', 'text')
142
+ end
143
+ end
144
+ ```
145
+
146
+ ##### `Vault::Test::SpecHelpers`
147
+
148
+ Provides `#usage_json`, `#statement_json` and `#vault_spec` methods to
149
+ pull down and vendor latest specs from the `vault-specs` app. The
150
+ json files are stored in `test/support/<filename>.json` and are used
151
+ only when the live HTTP request against the vault-specs app fails.
47
152
 
48
- To extend your test and spec base classes uniformly, use the method
49
- `Vault::Test.include_in_all`
153
+ Yes, this means your tests are making an HTTP request. This enables
154
+ us to update specs and then run tests that immediately use the specs
155
+ on the server. However, if you're on a plane or don't have internet
156
+ we can easily fall back to the saved JSON.
50
157
 
51
158
  ```ruby
52
- module MyTestHelperClass
53
- def app; Vault::InvoiceBuilder::Web; end
159
+ class Test < Vault::TestCase
160
+ include Vault::Test::SpecHelpers
161
+
162
+ def test_env_changes
163
+ put '/statement/1', statement_json
164
+ put '/statement/1', vault_spec('statement2.json')
165
+ assert_equal(statement_json, vault_spec('statement.json'))
166
+ assert_equal(usage_json, vault_spec('usage.json'))
167
+ end
54
168
  end
169
+ ```
170
+
171
+ ##### `Vault::Test::LoggingHepers`
55
172
 
56
- Vault::Test.include_in_all Vault::MyTestHelperClass
173
+ Attaches a `StringIO` to `Scrolls.stream` to capture logs in memory.
174
+
175
+ ```ruby
176
+ class Test < Vault::TestCase
177
+ include Vault::Test::LoggingHelpers
178
+
179
+ def test_logging
180
+ Scrolls.log(message: 'A log message')
181
+ assert_equal(Scrolls.stream.string, 'A log message\n')
182
+ end
183
+ end
57
184
  ```
58
185
 
59
- Now you have an `#app` method in your `Vault::TestCase` and your `Vault::Spec`
186
+ ## Setting up a development environment
187
+
188
+ Install dependencies and setup test databases:
60
189
 
61
- ## Contributing
190
+ bundle install --binstubs vendor/bin
191
+ rbenv rehash
192
+ rake
62
193
 
63
- 1. Fork it
64
- 2. Create your feature branch (`git checkout -b my-new-feature`)
65
- 3. Commit your changes (`git commit -am 'Add some feature'`)
66
- 4. Push to the branch (`git push origin my-new-feature`)
67
- 5. Create new Pull Request
194
+ Run tests:
68
195
 
69
- ## Releasing
196
+ bundle exec rake test
197
+
198
+ See tasks:
199
+
200
+ rake -T
201
+
202
+ Generate API documentation:
203
+
204
+ bundle exec yard
205
+
206
+ ## Creating and shipping a change
207
+
208
+ Create a change by making a branch with the changes you want to merge.
209
+ Open a pull request and signal it to the Vault Team by creating a
210
+ Trello card in the *Needs review* list on the
211
+ [focus board](https://trello.com/b/mV7Qy3aq/vault-team-focus).
70
212
 
71
213
  Update the version in `lib/vault-test-tools/version` and run
72
214
  `bundle exec rake release`.
215
+
216
+ ## Where to get help
217
+
218
+ Ask for help in the
219
+ [Vault](https://heroku.hipchat.com/rooms/show/175790/vault) room in
220
+ HipChat or send the Vault Team an
221
+ [email](https://groups.google.com/a/heroku.com/forum/#!forum/vault).
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'vault-test-tools/rake_task'
3
+ require 'yard'
4
+
5
+ desc "Doc the things"
6
+ YARD::Rake::YardocTask.new
7
+
8
+ task default: :test
@@ -1,11 +1,20 @@
1
+ module Vault
2
+ # `Vault::Test` provides support for writing automated tests.
3
+ module Test
4
+ end
5
+ end
6
+
1
7
  require "vault-test-tools/version"
2
8
 
3
9
  require 'rack/test'
4
- require 'nokogiri'
10
+ # require 'nokogiri'
5
11
 
6
- require "vault-test-tools/test_case"
7
- require "vault-test-tools/spec"
12
+ # Test case mixins.
8
13
  require "vault-test-tools/environment_helpers"
9
14
  require "vault-test-tools/html_helpers"
10
- require "vault-test-tools/db_helpers"
15
+ require "vault-test-tools/logging_helpers"
11
16
  require "vault-test-tools/spec_helpers"
17
+
18
+ # Test case base classes.
19
+ require "vault-test-tools/test_case"
20
+ require "vault-test-tools/spec"
@@ -1,11 +1,11 @@
1
1
  module Vault::Test::EnvironmentHelpers
2
- # Override an ENV variable for the current test. The original value will be
3
- # restored automatically when the test finishes.
2
+ # Override an environment variable in the current test.
4
3
  def set_env(key, value)
5
4
  overrides[key] = ENV[key] unless overrides.has_key?(key)
6
5
  ENV[key] = value
7
6
  end
8
7
 
8
+ # Restore the environment back to its state before tests ran.
9
9
  def teardown
10
10
  overrides.each { |key, value| ENV[key] = value }
11
11
  super
@@ -1,4 +1,8 @@
1
1
  module Vault::Test::HTMLHelpers
2
+ # Save and open an HTML document in your browser.
3
+ #
4
+ # @param html [String] The page to open or nil to fetch it from
5
+ # `last_response.body` if testing a Sinatra app.
2
6
  def save_and_open_page(html = nil, name = 'page.html', i = 1)
3
7
  html ||= last_response.body
4
8
  name = "page_#{i=i+1}.html" while File.exist? name
@@ -6,26 +10,49 @@ module Vault::Test::HTMLHelpers
6
10
  system "open #{name}"
7
11
  end
8
12
 
13
+ # Parse an HTML document into a `Nokogiri::HTML` instance and store it as
14
+ # the current document.
9
15
  def set_doc(body)
10
16
  @doc = Nokogiri::HTML(body)
11
17
  end
12
18
 
19
+ # Get the current document or parse `last_response.body` into a
20
+ # `Nokogiri::HTML` instance.
21
+ #
22
+ # @return [Nokogiri::HTML] The current HTML document.
13
23
  def doc
14
24
  @doc || Nokogiri::HTML(last_response.body)
15
25
  end
16
26
 
17
- def css(string)
18
- doc.css(string)
27
+ # Get a list of text matches in the current document for a CSS selector.
28
+ #
29
+ # @param selector [String] A CSS selector to match elements in the document
30
+ # with.
31
+ # @return [Array] A list of matching elements from the current document.
32
+ def css(selector)
33
+ doc.css(selector)
19
34
  end
20
35
 
21
- def assert_includes_css(css_string)
22
- exists = doc.css(css_string).first
23
- assert exists, "Last response must include #{css_string}"
36
+ # Assert that at least one element in the current document matches the CSS
37
+ # selector.
38
+ #
39
+ # @param selector [String] A CSS selector to match elements in the document
40
+ # with.
41
+ def assert_includes_css(selector)
42
+ exists = doc.css(selector).first
43
+ assert exists, "Last response must include #{selector}"
24
44
  end
25
45
 
26
- def assert_css(css_string, content)
27
- e = css(css_string).first
28
- assert e, "Element not found: #{css_string}"
46
+ # Assert that the content in the first element matching the CSS selector
47
+ # matches the specified value.
48
+ #
49
+ # @param selector [String] A CSS selector to match elements in the document
50
+ # with.
51
+ # @param content [String] The content to match in the first matching
52
+ # element.
53
+ def assert_css(selector, content)
54
+ e = css(selector).first
55
+ assert e, "Element not found: #{selector}"
29
56
  assert_includes e.content, content
30
57
  end
31
58
  end
@@ -0,0 +1,8 @@
1
+ require 'scrolls'
2
+
3
+ module Vault::Test::LoggingHelpers
4
+ def setup
5
+ super
6
+ Scrolls.stream = StringIO.new if defined? Scrolls
7
+ end
8
+ end
@@ -1,10 +1,8 @@
1
1
  require 'rake/testtask'
2
2
 
3
- ENV['CPUPROFILE'] = './test/.test.profile'
4
3
  Rake::TestTask.new do |t|
5
4
  t.verbose = true
6
5
  t.ruby_opts << '-r turn/autorun'
7
- t.ruby_opts << '-r perftools'
8
6
  t.ruby_opts << '-I test'
9
7
  t.test_files = FileList['test/**/*_test.rb', 'test/**/*_spec.rb']
10
8
  end
@@ -13,7 +13,8 @@ module Vault::Test
13
13
  vault_spec('statement.json')
14
14
  end
15
15
 
16
- # memoizes return value so we don't keep making a request
16
+ # Memoizes return value so we don't keep making a request to download the
17
+ # spec.
17
18
  def vault_spec(filename)
18
19
  return cache[filename] if cache[filename]
19
20
  cache[filename] = read_spec(filename)
@@ -24,6 +25,7 @@ module Vault::Test
24
25
  end
25
26
 
26
27
  protected
28
+
27
29
  def cache
28
30
  @@cache ||= {}
29
31
  end
@@ -36,8 +38,8 @@ module Vault::Test
36
38
  "./test/support/#{name}"
37
39
  end
38
40
 
39
- # Uses JSON at URL when it can, but will use
40
- # the cached statement when it can't
41
+ # Uses JSON at URL when it can, but will use the cached statement when it
42
+ # can't.
41
43
  def read_spec(name)
42
44
  data = ::Net::HTTP.get(URI.parse(url(name)))
43
45
  FileUtils.mkdir_p(File.dirname(file(name)))
@@ -2,8 +2,5 @@ require 'minitest/unit'
2
2
 
3
3
  # Base class for Vault test cases.
4
4
  class Vault::TestCase < MiniTest::Unit::TestCase
5
- def setup
6
- super
7
- Scrolls.stream = StringIO.new if defined? Scrolls
8
- end
5
+ include Vault::Test::LoggingHelpers
9
6
  end
@@ -1,5 +1,5 @@
1
1
  module Vault
2
2
  module Test
3
- VERSION = "0.3.4"
3
+ VERSION = '0.3.5'
4
4
  end
5
5
  end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ class LoggingHelpersTest < Vault::TestCase
4
+ include Vault::Test::LoggingHelpers
5
+
6
+ # The LoggingHelpers mixin attaches a StringIO to Scrolls to capture log
7
+ # messages.
8
+ def test_scrolls_logs_to_stream
9
+ Scrolls.log(key: 'value')
10
+ assert_equal("key=value", Scrolls.stream.string.strip)
11
+ end
12
+ end
@@ -13,17 +13,13 @@ Gem::Specification.new do |gem|
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
- gem.executables = ['t','d','pt']
16
+ gem.executables = ['t']
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'minitest', '4.7.4'
21
21
  gem.add_dependency 'turn'
22
- gem.add_dependency 'rack-perftools_profiler'
23
22
  gem.add_dependency 'nokogiri'
24
23
  gem.add_dependency 'rack-test'
25
- gem.add_dependency 'rdoc'
26
- gem.add_dependency 'yard'
27
- gem.add_dependency 'redcarpet'
28
24
  gem.add_dependency 'rr'
29
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vault-test-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-05-17 00:00:00.000000000 Z
13
+ date: 2013-12-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -44,22 +44,6 @@ dependencies:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0'
47
- - !ruby/object:Gem::Dependency
48
- name: rack-perftools_profiler
49
- requirement: !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :runtime
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ! '>='
61
- - !ruby/object:Gem::Version
62
- version: '0'
63
47
  - !ruby/object:Gem::Dependency
64
48
  name: nokogiri
65
49
  requirement: !ruby/object:Gem::Requirement
@@ -92,54 +76,6 @@ dependencies:
92
76
  - - ! '>='
93
77
  - !ruby/object:Gem::Version
94
78
  version: '0'
95
- - !ruby/object:Gem::Dependency
96
- name: rdoc
97
- requirement: !ruby/object:Gem::Requirement
98
- none: false
99
- requirements:
100
- - - ! '>='
101
- - !ruby/object:Gem::Version
102
- version: '0'
103
- type: :runtime
104
- prerelease: false
105
- version_requirements: !ruby/object:Gem::Requirement
106
- none: false
107
- requirements:
108
- - - ! '>='
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: yard
113
- requirement: !ruby/object:Gem::Requirement
114
- none: false
115
- requirements:
116
- - - ! '>='
117
- - !ruby/object:Gem::Version
118
- version: '0'
119
- type: :runtime
120
- prerelease: false
121
- version_requirements: !ruby/object:Gem::Requirement
122
- none: false
123
- requirements:
124
- - - ! '>='
125
- - !ruby/object:Gem::Version
126
- version: '0'
127
- - !ruby/object:Gem::Dependency
128
- name: redcarpet
129
- requirement: !ruby/object:Gem::Requirement
130
- none: false
131
- requirements:
132
- - - ! '>='
133
- - !ruby/object:Gem::Version
134
- version: '0'
135
- type: :runtime
136
- prerelease: false
137
- version_requirements: !ruby/object:Gem::Requirement
138
- none: false
139
- requirements:
140
- - - ! '>='
141
- - !ruby/object:Gem::Version
142
- version: '0'
143
79
  - !ruby/object:Gem::Dependency
144
80
  name: rr
145
81
  requirement: !ruby/object:Gem::Requirement
@@ -163,24 +99,22 @@ email:
163
99
  - jkakar@heroku.com
164
100
  executables:
165
101
  - t
166
- - d
167
- - pt
168
102
  extensions: []
169
103
  extra_rdoc_files: []
170
104
  files:
171
105
  - .gitignore
172
106
  - .ruby-version
107
+ - .travis.yml
173
108
  - Gemfile
174
109
  - LICENSE.txt
175
110
  - README.md
176
111
  - Rakefile
177
- - bin/d
178
112
  - bin/pt
179
113
  - bin/t
180
114
  - lib/vault-test-tools.rb
181
- - lib/vault-test-tools/db_helpers.rb
182
115
  - lib/vault-test-tools/environment_helpers.rb
183
116
  - lib/vault-test-tools/html_helpers.rb
117
+ - lib/vault-test-tools/logging_helpers.rb
184
118
  - lib/vault-test-tools/rake_task.rb
185
119
  - lib/vault-test-tools/spec.rb
186
120
  - lib/vault-test-tools/spec_helpers.rb
@@ -188,6 +122,7 @@ files:
188
122
  - lib/vault-test-tools/version.rb
189
123
  - test/environment_helpers_test.rb
190
124
  - test/helper.rb
125
+ - test/logging_helpers_test.rb
191
126
  - test/spec_helpers_test.rb
192
127
  - test/test_spec.rb
193
128
  - vault-test-tools.gemspec
@@ -205,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
140
  version: '0'
206
141
  segments:
207
142
  - 0
208
- hash: -4588855188946910642
143
+ hash: 556099526944459047
209
144
  required_rubygems_version: !ruby/object:Gem::Requirement
210
145
  none: false
211
146
  requirements:
@@ -214,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
149
  version: '0'
215
150
  segments:
216
151
  - 0
217
- hash: -4588855188946910642
152
+ hash: 556099526944459047
218
153
  requirements: []
219
154
  rubyforge_project:
220
155
  rubygems_version: 1.8.23
@@ -224,6 +159,7 @@ summary: Test classes and stuff you want in your dev, but not prod, environment.
224
159
  test_files:
225
160
  - test/environment_helpers_test.rb
226
161
  - test/helper.rb
162
+ - test/logging_helpers_test.rb
227
163
  - test/spec_helpers_test.rb
228
164
  - test/test_spec.rb
229
165
  has_rdoc:
data/bin/d DELETED
@@ -1,3 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- system(%|bundle exec yardoc|)
@@ -1,16 +0,0 @@
1
- module Vault::Test
2
- module DB
3
- module Truncation
4
- def teardown
5
- super
6
- truncate_tables!
7
- end
8
-
9
- def truncate_tables!
10
- (db.tables - [:schema_migrations]).each do |table|
11
- db << "truncate #{table}"
12
- end
13
- end
14
- end
15
- end
16
- end