vault-test-tools 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in vault-dev-tools.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'fakeweb'
8
+ end
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Vault::Test
2
2
 
3
- Test Tools for the Heroku Vault Team.
3
+ Test is the English phrase for テスト. Test tools for the Heroku
4
+ Vault Team.
4
5
 
5
6
  ## Installation
6
7
 
data/bin/t CHANGED
@@ -1,4 +1,4 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
3
  ENV['CPUPROFILE'] = './test/.test.profile'
4
- exec('bundle exec turn -r perftools -I test -I lib test')
4
+ exec('bundle exec turn -r perftools -I test -I lib test/**/*.rb test/*.rb')
@@ -8,7 +8,9 @@ require "vault-test-tools/spec"
8
8
  require "vault-test-tools/environment_helpers"
9
9
  require "vault-test-tools/html_helpers"
10
10
  require "vault-test-tools/db_helpers"
11
+ require "vault-test-tools/spec_helpers"
11
12
 
13
+ # TODO: Deprecate
12
14
  module Vault
13
15
  module Test
14
16
  def self.include_in_all(*modules)
@@ -2,7 +2,7 @@ module Vault::Test::EnvironmentHelpers
2
2
  # Override an ENV variable for the current test. The original value will be
3
3
  # restored automatically when the test finishes.
4
4
  def set_env(key, value)
5
- overrides[key] = ENV[key] unless overrides[key]
5
+ overrides[key] = ENV[key] unless overrides.has_key?(key)
6
6
  ENV[key] = value
7
7
  end
8
8
 
@@ -0,0 +1,39 @@
1
+ # TODO: move to vault-test-tools
2
+ require 'fileutils'
3
+
4
+ module Vault::Test
5
+ module SpecHelpers
6
+ extend self
7
+
8
+ def usage_json
9
+ @usage_json ||= read_spec('usage')
10
+ end
11
+
12
+ def statement_json
13
+ @statement_json ||= read_spec('statement')
14
+ end
15
+
16
+ protected
17
+ def json_url(name)
18
+ "http://vault-specs.herokuapp.com/#{name}.json"
19
+ end
20
+
21
+ def json_file(name)
22
+ "./test/support/#{name}.json"
23
+ end
24
+
25
+ # Uses JSON at URL when it can, but will use
26
+ # the cached statement when it can't
27
+ def read_spec(name)
28
+ @statement_json ||= begin
29
+ data = Net::HTTP.get(URI.parse(json_url(name)))
30
+ FileUtils.mkdir_p(File.dirname(json_file(name)))
31
+ File.open(json_file(name), 'w') { |f| f << data }
32
+ data
33
+ rescue => e
34
+ $stderr.puts "Using cached #{name}.json"
35
+ File.read(json_file(name))
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  module Vault
2
2
  module Test
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -5,18 +5,27 @@ class EnvironmentHelpersTest < Vault::TestCase
5
5
  include Vault::Test::EnvironmentHelpers
6
6
 
7
7
  def test_set_env_ovverides_env
8
- ENV['FOO'] = 'bar'
9
- set_env('FOO', 'baz')
10
- assert_equal('baz', ENV['FOO'])
8
+ ENV['FOO_1'] = 'bar'
9
+ set_env('FOO_1', 'baz')
10
+ assert_equal('baz', ENV['FOO_1'])
11
11
  end
12
12
 
13
13
  def test_teardown_restores_env_to_original_value
14
- ENV['FOO'] = 'bar'
15
- set_env('FOO', 'baz')
16
- assert_equal('baz', ENV['FOO'])
17
- set_env('FOO', 'buzz')
18
- assert_equal('buzz', ENV['FOO'])
14
+ ENV['FOO_2'] = 'bar'
15
+ set_env('FOO_2', 'baz')
16
+ assert_equal('baz', ENV['FOO_2'])
17
+ set_env('FOO_2', 'buzz')
18
+ assert_equal('buzz', ENV['FOO_2'])
19
19
  teardown
20
- assert_equal('bar', ENV['FOO'])
20
+ assert_equal('bar', ENV['FOO_2'])
21
+ end
22
+
23
+ def test_restores_empty_env_to_empty
24
+ set_env('EMPTY', 'baz')
25
+ assert_equal('baz', ENV['EMPTY'])
26
+ set_env('EMPTY', 'buzz')
27
+ assert_equal('buzz', ENV['EMPTY'])
28
+ teardown
29
+ assert_equal(nil, ENV['EMPTY'])
21
30
  end
22
31
  end
data/test/helper.rb CHANGED
@@ -1 +1,3 @@
1
1
  require 'vault-test-tools'
2
+ require 'fakeweb'
3
+ require 'rr'
@@ -0,0 +1,79 @@
1
+ require 'helper'
2
+
3
+ class SpecHelpersTest < Vault::TestCase
4
+ include Vault::Test::SpecHelpers
5
+ include RR::Adapters::MiniTest
6
+ STATEMENT_FILE = './test/support/statement.json'
7
+
8
+ def setup
9
+ super
10
+ @url = 'http://vault-specs.herokuapp.com'
11
+ @statement_json = nil
12
+ FakeWeb.allow_net_connect = false
13
+ end
14
+
15
+ def teardown
16
+ super
17
+ FakeWeb.clean_registry
18
+ FakeWeb.allow_net_connect = true
19
+ # Using the filesystem - dirrrty
20
+ File.unlink(STATEMENT_FILE) if File.exists?(STATEMENT_FILE)
21
+ reset #for RR
22
+ end
23
+
24
+ # Happy path is to use the request from the service
25
+ def test_requests_spec_from_service
26
+ url = "#{@url}/statement.json"
27
+ FakeWeb.register_uri(:get, url, body: "USAGE JSON")
28
+ stub(File).open(STATEMENT_FILE, 'w')
29
+ assert_equal("USAGE JSON", statement_json)
30
+ end
31
+
32
+ # Call both methods with stubbed HTTP endpoints
33
+ def test_all_endpoints
34
+ stub(File).open
35
+ %w{usage statement}.each do |type|
36
+ url = "#{@url}/#{type}.json"
37
+ FakeWeb.register_uri(:get, url, body: "USAGE JSON")
38
+ capture_io do
39
+ assert_equal("USAGE JSON", self.send("#{type}_json"))
40
+ end
41
+ end
42
+ end
43
+
44
+ # If request blows up use cached JSON
45
+ def test_uses_json_on_request_failure
46
+ stub(URI).parse { raise RuntimeError }
47
+ mock(File).read(STATEMENT_FILE) { "USAGE JSON" }
48
+ stdout, stderr = capture_io do
49
+ assert_equal("USAGE JSON", statement_json)
50
+ end
51
+ assert_includes(stderr, "statement.json")
52
+ end
53
+
54
+ # Make sure we save it when we get it so we don't have
55
+ # to manually cache it
56
+ def test_caches_json_file_on_succsss
57
+ # successful request
58
+ url = "#{@url}/statement.json"
59
+ FakeWeb.register_uri(:get, url, body: "USAGE JSON")
60
+ assert_equal("USAGE JSON", statement_json)
61
+ @statement_json = nil
62
+ # now no network
63
+ stub(URI).parse { raise RuntimeError }
64
+ capture_io do
65
+ assert_equal("USAGE JSON", statement_json)
66
+ end
67
+ end
68
+
69
+ # This is the real error scenario
70
+ def test_bombs_if_no_cached_json
71
+ stub(URI).parse { raise RuntimeError }
72
+ stub(File).read { raise RuntimeError }
73
+ capture_io do
74
+ assert_raises RuntimeError do
75
+ statement_json
76
+ end
77
+ end
78
+ end
79
+ end
@@ -25,4 +25,5 @@ Gem::Specification.new do |gem|
25
25
  gem.add_dependency 'rdoc'
26
26
  gem.add_dependency 'yard'
27
27
  gem.add_dependency 'redcarpet'
28
+ gem.add_dependency 'rr'
28
29
  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.2.3
4
+ version: 0.3.0
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-02-13 00:00:00.000000000 Z
13
+ date: 2013-05-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: turn
@@ -140,6 +140,22 @@ dependencies:
140
140
  - - ! '>='
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
+ - !ruby/object:Gem::Dependency
144
+ name: rr
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ! '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ type: :runtime
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ! '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
143
159
  description: Basic tools for Heroku Vault's Ruby projects
144
160
  email:
145
161
  - chriscontinanza@gmail.com
@@ -166,10 +182,12 @@ files:
166
182
  - lib/vault-test-tools/html_helpers.rb
167
183
  - lib/vault-test-tools/rake_task.rb
168
184
  - lib/vault-test-tools/spec.rb
185
+ - lib/vault-test-tools/spec_helpers.rb
169
186
  - lib/vault-test-tools/test_case.rb
170
187
  - lib/vault-test-tools/version.rb
171
188
  - test/environment_helpers_test.rb
172
189
  - test/helper.rb
190
+ - test/spec_helpers_test.rb
173
191
  - test/test_spec.rb
174
192
  - vault-test-tools.gemspec
175
193
  homepage: ''
@@ -186,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
204
  version: '0'
187
205
  segments:
188
206
  - 0
189
- hash: 1086311466377128168
207
+ hash: 2085097193621949794
190
208
  required_rubygems_version: !ruby/object:Gem::Requirement
191
209
  none: false
192
210
  requirements:
@@ -195,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
213
  version: '0'
196
214
  segments:
197
215
  - 0
198
- hash: 1086311466377128168
216
+ hash: 2085097193621949794
199
217
  requirements: []
200
218
  rubyforge_project:
201
219
  rubygems_version: 1.8.23
@@ -205,5 +223,6 @@ summary: Test classes and stuff you want in your dev, but not prod, environment.
205
223
  test_files:
206
224
  - test/environment_helpers_test.rb
207
225
  - test/helper.rb
226
+ - test/spec_helpers_test.rb
208
227
  - test/test_spec.rb
209
228
  has_rdoc: