response_mate 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ResponseMate::Configuration do
4
+ subject(:configuration) { ResponseMate::Configuration.new }
5
+
4
6
  describe '.initialize' do
5
- let(:subject) { ResponseMate::Configuration.new }
7
+ describe 'output_dir' do
8
+ subject { configuration.output_dir }
6
9
 
7
- it 'assigns @output_dir' do
8
- expect(subject.output_dir).to be_present
10
+ it 'has ./ as the default value' do
11
+ is_expected.to eq('./')
12
+ end
9
13
  end
10
14
 
11
15
  it 'assigns @requests_manifest' do
@@ -42,13 +46,6 @@ describe ResponseMate do
42
46
  end
43
47
  end
44
48
 
45
- after do
46
- ResponseMate.setup do |config|
47
- config.output_dir = './output/responses/'
48
- config.requests_manifest = './requests.yml.erb'
49
- end
50
- end
51
-
52
49
  it 'properly assigns ivars' do
53
50
  expect(ResponseMate.configuration.output_dir).to eq('foo')
54
51
  expect(ResponseMate.configuration.requests_manifest).to eq('bar')
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe ResponseMate::Environment do
4
+ let(:filename) { 'a_file.yml' }
5
+
6
+ subject(:environment) { described_class.new(filename) }
7
+
8
+ describe 'initialization' do
9
+ describe 'filename' do
10
+ subject { environment.filename }
11
+
12
+ context 'when it is present' do
13
+ it 'is assigned to the specified value' do
14
+ is_expected.to eq(filename)
15
+ end
16
+ end
17
+
18
+ context 'when it is nil' do
19
+ let(:filename) { nil }
20
+
21
+ it 'is assigned to the default value' do
22
+ is_expected.to eq(ResponseMate.configuration.environment)
23
+ end
24
+ end
25
+ end
26
+
27
+ describe 'env' do
28
+ subject { environment.env }
29
+
30
+ context 'when the file exists' do
31
+ around do |example|
32
+ File.open(filename, 'w') { |f| f << ':foo: bar' }
33
+ example.run
34
+ File.delete(filename)
35
+ end
36
+
37
+ it 'is assigned to the parsed YAML file' do
38
+ is_expected.to eq(foo: 'bar')
39
+ end
40
+ end
41
+
42
+ context 'when the file does not exist' do
43
+ it 'is assigned to an empty Hash' do
44
+ is_expected.to eq({})
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#exists?' do
51
+ subject { environment.exists? }
52
+
53
+ context 'when the file with filename exists' do
54
+ around do |example|
55
+ File.write filename, ''
56
+ example.run
57
+ File.delete filename
58
+ end
59
+
60
+ it { is_expected.to be(true) }
61
+ end
62
+
63
+ context 'when the file with filename exists does not exist' do
64
+ it { is_expected.to be(false) }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,175 @@
1
+ require 'spec_helper'
2
+
3
+ describe ResponseMate::Manifest do
4
+ let(:filename) { 'a_file.yml' }
5
+ let(:file_contents) { ':requests: []' }
6
+ let(:environment) { }
7
+
8
+ subject(:manifest) { described_class.new(filename, environment) }
9
+
10
+ around do |example|
11
+ File.write filename, file_contents if filename
12
+ quietly { example.run }
13
+ File.delete filename rescue Errno::ENOENT # rubocop:disable Style/RescueModifier
14
+ end
15
+
16
+ describe '#filename' do
17
+ subject { manifest.filename }
18
+
19
+ context 'when it is present' do
20
+ it 'is assigned to the specified value' do
21
+ is_expected.to eq(filename)
22
+ end
23
+ end
24
+
25
+ context 'when it is nil' do
26
+ let(:filename) { nil }
27
+
28
+ before { allow_any_instance_of(described_class).to receive(:parse) }
29
+
30
+ it 'is assigned to the default value' do
31
+ is_expected.to eq(ResponseMate.configuration.requests_manifest)
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '#environment' do
37
+ subject { manifest.environment }
38
+
39
+ before { allow(manifest).to receive(:parse) }
40
+
41
+ context 'when it is specified' do
42
+ it { is_expected.to eq(environment) }
43
+ end
44
+
45
+ context 'when it is not specified' do
46
+ it { is_expected.to be_nil }
47
+ end
48
+ end
49
+
50
+ describe '#requests_text' do
51
+ subject { manifest.requests_text }
52
+
53
+ context 'when the manifest file exists' do
54
+ let(:file_contents) { ':requests: []' }
55
+
56
+ context 'and is not a template' do
57
+ it 'returns the contents of the file' do
58
+ is_expected.to eq(file_contents)
59
+ end
60
+ end
61
+
62
+ context 'and is mustache template' do
63
+ let(:file_contents) { ':key: {{var}}' }
64
+
65
+ context 'and the environment file exists' do
66
+ let(:template_variable) { { var: 'value' } }
67
+ let(:environment) { double(exists?: true, env: template_variable) }
68
+
69
+ it 'returns the processed template' do
70
+ is_expected.to eq(":key: #{template_variable[:var]}")
71
+ end
72
+ end
73
+
74
+ context 'and the environment file does not exist' do
75
+ let(:environment) { double(exists?: false, env: {}) }
76
+
77
+ it 'returns the contents of the file' do
78
+ is_expected.to eq(':key: ')
79
+ end
80
+
81
+ it 'prints a warning to standard error' do
82
+ expect(capture(:stderr) { subject }).to match(/no environment file is found/)
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'when the manifest file does not exist' do
89
+ let(:filename) { nil }
90
+
91
+ it { expect { subject }.to raise_error(ResponseMate::ManifestMissing) }
92
+ end
93
+ end
94
+
95
+ describe '#requests' do
96
+ subject { manifest.requests }
97
+
98
+ context 'when the requests text contains requests' do
99
+ let(:file_contents) do
100
+ <<-YAML
101
+ requests:
102
+ -
103
+ key: repos_show
104
+ request:
105
+ path: 'repos/rails/rails'
106
+ YAML
107
+ end
108
+
109
+ it 'returns them' do
110
+ expect(subject.map(&:key)).to match_array(%w[repos_show])
111
+ end
112
+ end
113
+
114
+ context 'when the requests text does not contain requests' do
115
+ let(:file_contents) { 'missing_requests: true' }
116
+
117
+ it 'prints a warning to standard error' do
118
+ expect(capture(:stderr) { subject }).to match(/contains no requests/)
119
+ end
120
+
121
+ it 'returns an empty Array' do
122
+ quietly { is_expected.to be_empty }
123
+ end
124
+ end
125
+ end
126
+
127
+ describe '#name' do
128
+ let(:name) { 'github apiv3' }
129
+ let(:file_contents) { "name: #{name}" }
130
+
131
+ subject { manifest.name }
132
+
133
+ it { is_expected.to eq(name) }
134
+ end
135
+
136
+ describe '#description' do
137
+ let(:description) { 'a short description of the requests contained in the manifest' }
138
+ let(:file_contents) { "description: #{description}" }
139
+
140
+ subject { manifest.description }
141
+
142
+ it { is_expected.to eq(description) }
143
+ end
144
+
145
+ describe '#requests_for_keys' do
146
+ let(:keys) { }
147
+ subject { manifest.requests_for_keys(keys) }
148
+
149
+ context 'when the specified keys are blank' do
150
+ it { is_expected.to eq([]) }
151
+ end
152
+
153
+ context 'when a key is missing' do
154
+ let(:keys) { %w[key1 key2] }
155
+ let(:existing_requests) { [keys][1..-1].map { |key| double(key: key) } }
156
+
157
+ it 'raises ResponseMate::KeysNotFound' do
158
+ expect { subject }.to raise_error(ResponseMate::KeysNotFound)
159
+ end
160
+ end
161
+
162
+ context 'when no key is missing' do
163
+ let(:keys) { %w[key1 key2] }
164
+ let(:existing_requests) { (keys + ['spare_key']).map { |key| double(key: key) } }
165
+
166
+ before do
167
+ allow(manifest).to receive(:requests).and_return(existing_requests)
168
+ end
169
+
170
+ it 'returns only the requests for the specified keys' do
171
+ expect(subject.map(&:key)).to match_array(keys)
172
+ end
173
+ end
174
+ end
175
+ end
@@ -57,7 +57,7 @@ describe ResponseMate::Recorder do
57
57
  context 'with no keys specified' do
58
58
  it 'records all keys in the manifest' do
59
59
  quietly { subject.record([]) }
60
- expect(output_files.call).to have_exactly(2).items
60
+ expect(output_files.call.size).to eq(2)
61
61
  end
62
62
  end
63
63
  end
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,39 @@ Coveralls.wear!
7
7
 
8
8
  Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
9
9
 
10
+ module OutputHelpers
11
+ # Deprecated in active_support due to thread-safety
12
+ # see: https://github.com/rails/rails/blob/v4.2.2/activesupport/lib/active_support/core_ext/kernel/reporting.rb#L114
13
+ # We use it only as a testing helper in a non-threaded environment
14
+ def quietly
15
+ silence_stream(STDOUT) do
16
+ silence_stream(STDERR) do
17
+ yield
18
+ end
19
+ end
20
+ end
21
+
22
+ # Deprecated in active_support due to thread-safety
23
+ # see: https://github.com/rails/rails/blob/v4.2.2/activesupport/lib/active_support/core_ext/kernel/reporting.rb#L88
24
+ # We use it only as a testing helper in a non-threaded environment
25
+ def capture(stream)
26
+ stream = stream.to_s
27
+ captured_stream = Tempfile.new(stream)
28
+ stream_io = eval("$#{stream}") # rubocop:disable Lint/Eval
29
+ origin_stream = stream_io.dup
30
+ stream_io.reopen(captured_stream)
31
+
32
+ yield
33
+
34
+ stream_io.rewind
35
+ return captured_stream.read
36
+ ensure
37
+ captured_stream.close
38
+ captured_stream.unlink
39
+ stream_io.reopen(origin_stream)
40
+ end
41
+ end
42
+
10
43
  RSpec.configure do |c|
11
- c.treat_symbols_as_metadata_keys_with_true_values = true
44
+ c.include OutputHelpers
12
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: response_mate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitris Zorbas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-07 00:00:00.000000000 Z
11
+ date: 2015-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,19 +95,19 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: colored
98
+ name: colorize
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - ">"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: 0.7.5
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - ">"
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: 0.7.5
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: faraday
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -191,6 +191,8 @@ files:
191
191
  - ".rspec"
192
192
  - ".rubocop.yml"
193
193
  - ".travis.yml"
194
+ - CHANGELOG.md
195
+ - CONTRIBUTING.md
194
196
  - Gemfile
195
197
  - Guardfile
196
198
  - LICENSE
@@ -222,14 +224,15 @@ files:
222
224
  - lib/response_mate/version.rb
223
225
  - requests.yml.sample
224
226
  - response_mate.gemspec
225
- - roadmap.md
226
227
  - spec/fixtures/two_keys.yml.erb
227
228
  - spec/lib/response_mate/cli_spec.rb
228
229
  - spec/lib/response_mate/commands/inspect_spec.rb
229
230
  - spec/lib/response_mate/commands/list_spec.rb
230
231
  - spec/lib/response_mate/commands/record_spec.rb
231
232
  - spec/lib/response_mate/core_spec.rb
233
+ - spec/lib/response_mate/environment_spec.rb
232
234
  - spec/lib/response_mate/exporters/postman/collection_spec.rb
235
+ - spec/lib/response_mate/manifest_spec.rb
233
236
  - spec/lib/response_mate/recorder_spec.rb
234
237
  - spec/lib/response_mate/tape_spec.rb
235
238
  - spec/lib/response_mate_spec.rb
@@ -271,7 +274,9 @@ test_files:
271
274
  - spec/lib/response_mate/commands/list_spec.rb
272
275
  - spec/lib/response_mate/commands/record_spec.rb
273
276
  - spec/lib/response_mate/core_spec.rb
277
+ - spec/lib/response_mate/environment_spec.rb
274
278
  - spec/lib/response_mate/exporters/postman/collection_spec.rb
279
+ - spec/lib/response_mate/manifest_spec.rb
275
280
  - spec/lib/response_mate/recorder_spec.rb
276
281
  - spec/lib/response_mate/tape_spec.rb
277
282
  - spec/lib/response_mate_spec.rb
data/roadmap.md DELETED
@@ -1,24 +0,0 @@
1
- ## Milestone 0.1.0
2
- - Make the list command prompt you to perform any of the listed requests ✓
3
- - Recording is optional, you may just perform requests to inspect their
4
- - output. [Done]
5
- - Support mustache templates in favor of erb ✓
6
- - Extract Manifest to a separate class ✓
7
- - Add usage instructions in README.md [Done]
8
- - Remove support for .yml.erb request manifests [Done]
9
- - Export Connection as a separate class [Done]
10
- - Export Tape as a separate class [Done]
11
-
12
- ## Milestone 0.2.0
13
- - Request helpers (random ids, uuids).
14
- - Paraller requests.
15
- - Expectations, a expectations.yml file will contain a list of keys and
16
- - status codes to match against the recordings.
17
- - response_mate server command that serves the recorded responses when
18
- - requests matching requests.yml are replayed.
19
- - Curl exporter.
20
- - Export environment.yml to postman format
21
- - Accept extra params/headers from cli
22
- - Support request types: [:raw, urlencoded]
23
- - read manifest file from url
24
- - Allow multiline input for inspect