dredd_hooks 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+
3
+ module DreddHooks
4
+ class Server
5
+
6
+ # Store JSON messages or part of them
7
+ class Buffer
8
+
9
+ def initialize(message_delimiter)
10
+ flush!
11
+ @message_delimiter = message_delimiter
12
+ end
13
+
14
+ # Empty the buffer.
15
+ #
16
+ # Returns the buffer content String.
17
+ def flush!
18
+ content = @content
19
+ @content = ""
20
+ content
21
+ end
22
+
23
+ def <<(string)
24
+ @content += string
25
+ end
26
+
27
+ def any_message?
28
+ @content.include? @message_delimiter
29
+ end
30
+
31
+ def unshift_messages
32
+ flush!.
33
+ split(@message_delimiter).
34
+ inject([]) { |messages, message|
35
+ begin
36
+ messages.push JSON.parse(message)
37
+ rescue JSON::ParserError
38
+ # If the message after the delimiter is not parseable JSON,
39
+ # then it's a chunk of the next message, and should be put back
40
+ # into the buffer.
41
+ @content += message
42
+ messages
43
+ end
44
+ }
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,33 @@
1
+ require 'dredd_hooks/definitions'
2
+ require 'dredd_hooks/errors'
3
+ require 'dredd_hooks/runner'
4
+
5
+ module DreddHooks
6
+
7
+ class Server
8
+ class EventsHandler
9
+
10
+ attr_reader :events, :runner
11
+ private :events, :runner
12
+
13
+ def initialize(events=EVENTS, runner=Runner.instance)
14
+ @events = events
15
+ @runner = runner
16
+ end
17
+
18
+ def handle(event, transaction)
19
+
20
+ events.fetch(event.to_sym, []).each do |hook_name|
21
+ begin
22
+ transaction = runner.send("run_#{hook_name}_hooks_for_transaction", transaction)
23
+ rescue NoMethodError
24
+ raise UnknownHookError.new(hook_name)
25
+ end
26
+ end
27
+
28
+ transaction
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -1,3 +1,3 @@
1
1
  module DreddHooks
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module DreddHooks
4
+ describe 'All the defined hooks', protected: true do
5
+
6
+ DEFINED_HOOKS = HOOKS_ON_SINGLE_TRANSACTIONS + HOOKS_ON_MULTIPLE_TRANSACTIONS
7
+
8
+ describe 'are runned on least one event' do
9
+
10
+ let(:hooks_in_use) { Server::EVENTS.map{ |_event, hooks| hooks }.flatten.uniq }
11
+
12
+ DEFINED_HOOKS.each do |hook_name|
13
+ it ":#{hook_name} is refered to by at least one event" do
14
+ expect(hooks_in_use).to include(hook_name)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ describe 'DreddHooks events definition', protected: true do
22
+
23
+ describe 'only refers to hooks that are already defined' do
24
+
25
+ let(:defined_hooks) { HOOKS_ON_SINGLE_TRANSACTIONS + HOOKS_ON_MULTIPLE_TRANSACTIONS }
26
+
27
+ Server::EVENTS.each do |_event, hook_names|
28
+ hook_names.each do |hook_name|
29
+ it ":#{hook_name} is defined" do
30
+ expect(defined_hooks).to include(hook_name)
31
+ end
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ require 'dredd_hooks/methods'
4
+
5
+ module DreddHooks
6
+
7
+ class DummyHooksFile
8
+ include DreddHooks::Methods
9
+ end
10
+
11
+ describe Methods do
12
+
13
+ let(:hooks_file) { DummyHooksFile.new }
14
+
15
+ describe 'exposes the Ruby hooks DSL', public: true do
16
+
17
+ it 'defines #before' do
18
+ expect(hooks_file).to respond_to :before
19
+ end
20
+
21
+ it 'defines #before_validation' do
22
+ expect(hooks_file).to respond_to :before_validation
23
+ end
24
+
25
+ it 'defines #after' do
26
+ expect(hooks_file).to respond_to :after
27
+ end
28
+
29
+ it 'defines #before_each' do
30
+ expect(hooks_file).to respond_to :before_each
31
+ end
32
+
33
+ it 'defines #before_each_validation' do
34
+ expect(hooks_file).to respond_to :before_each_validation
35
+ end
36
+
37
+ it 'defines #after_each' do
38
+ expect(hooks_file).to respond_to :after_each
39
+ end
40
+
41
+ it 'defines #before_all' do
42
+ expect(hooks_file).to respond_to :before_all
43
+ end
44
+
45
+ it 'defines #after_all' do
46
+ expect(hooks_file).to respond_to :after_all
47
+ end
48
+ end
49
+
50
+ it 'does not expose its #runner', private: true do
51
+ expect(hooks_file).not_to respond_to :runner
52
+ end
53
+
54
+ describe 'does not expose its generator methods', private: true do
55
+
56
+ it { expect(DreddHooks::Methods).not_to respond_to :define_hooks_on_multiple_transactions }
57
+ it { expect(DreddHooks::Methods).not_to respond_to :define_hooks_on_single_transactions }
58
+
59
+ end
60
+ end
61
+ end
62
+
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module DreddHooks
4
+ describe Runner do
5
+
6
+ let(:runner) { Runner.instance }
7
+
8
+ describe 'exposes an interface to run hooks', protected: true do
9
+
10
+ it { expect(runner).to respond_to :run_before_hooks_for_transaction }
11
+ it { expect(runner).to respond_to :run_before_validation_hooks_for_transaction }
12
+ it { expect(runner).to respond_to :run_after_hooks_for_transaction }
13
+ it { expect(runner).to respond_to :run_before_each_hooks_for_transaction }
14
+ it { expect(runner).to respond_to :run_before_each_validation_hooks_for_transaction }
15
+ it { expect(runner).to respond_to :run_after_each_hooks_for_transaction }
16
+ it { expect(runner).to respond_to :run_before_all_hooks_for_transaction }
17
+ it { expect(runner).to respond_to :run_after_all_hooks_for_transaction }
18
+ end
19
+
20
+ describe 'exposes an interface to register hooks', private: true do
21
+
22
+ it { expect(runner).to respond_to :register_before_hook }
23
+ it { expect(runner).to respond_to :register_before_validation_hook }
24
+ it { expect(runner).to respond_to :register_after_hook }
25
+ it { expect(runner).to respond_to :register_before_each_hook }
26
+ it { expect(runner).to respond_to :register_before_each_validation_hook }
27
+ it { expect(runner).to respond_to :register_after_each_hook }
28
+ it { expect(runner).to respond_to :register_before_all_hook }
29
+ it { expect(runner).to respond_to :register_after_all_hook }
30
+ end
31
+
32
+ describe 'does not expose its generator methods', private: true do
33
+
34
+ it { expect(runner.class).not_to respond_to :define_registration_methods_for_hooks_on_single_transactions }
35
+ it { expect(runner.class).not_to respond_to :define_registration_methods_for_hooks_on_multiple_transactions }
36
+ it { expect(runner.class).not_to respond_to :define_runners_for_hooks_on_single_transactions }
37
+ it { expect(runner.class).not_to respond_to :define_runners_for_hooks_on_multiple_transactions }
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ module DreddHooks
4
+ describe Server::Buffer, private: true do
5
+
6
+ let(:message_delimiter) { "\t" }
7
+ let(:buffer) { described_class.new(message_delimiter) }
8
+
9
+ it { expect(buffer).to respond_to :<< }
10
+ it { expect(buffer).to respond_to :flush! }
11
+ it { expect(buffer).to respond_to :unshift_messages }
12
+ it { expect(buffer).to respond_to :any_message? }
13
+
14
+ it 'is empty by default' do
15
+ expect(buffer.any_message?).to be false
16
+ end
17
+
18
+ describe '#any_message?' do
19
+
20
+ it { expect(buffer.any_message?).to be false }
21
+
22
+ context "when the buffer contains part of a message" do
23
+
24
+ it {
25
+ buffer << '{ "note": "This string contains no message delimiter..." }'
26
+ expect(buffer.any_message?).to be false
27
+ }
28
+ end
29
+
30
+ context "when the buffer contains at least one message" do
31
+
32
+ it {
33
+ buffer << "{ \"note\": \"This string does contain a message delimiter...\" }\t{ \"and\": \"some\", \"more\": \"stuff which is not even valid JSON.... aAAAah! "
34
+ expect(buffer.any_message?).to be true
35
+ }
36
+ end
37
+ end
38
+
39
+ describe '#unshift_messages' do
40
+
41
+ before(:each) do
42
+ buffer << "{ \"A\": \"JSON message\" }\t"
43
+ buffer << "{ \"another\": \"message\" }\t{ \"and a\": \"half\","
44
+ end
45
+
46
+ it 'returns an array of messages' do
47
+ expect(buffer.unshift_messages).to eq [{ 'A' => 'JSON message' }, { 'another' => 'message' }]
48
+ end
49
+
50
+ it 'keeps the uncomplete messages intact' do
51
+ buffer.unshift_messages
52
+ expect(buffer.flush!).to eq '{ "and a": "half",'
53
+ end
54
+ end
55
+
56
+ end
57
+ end
58
+
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ # Runs the 'known' hook but not the 'unknown' one.
4
+ class DummyRunner
5
+
6
+ def run_known_hooks_for_transaction(transaction)
7
+ transaction
8
+ end
9
+ end
10
+
11
+ module DreddHooks
12
+ describe Server::EventsHandler, private: true do
13
+
14
+ let(:events_handler) { described_class.new }
15
+
16
+ it { expect(events_handler).to respond_to :handle }
17
+
18
+ describe '#handle' do
19
+
20
+ it 'returns a transaction' do
21
+ event = 'doNothing'
22
+ transaction = double()
23
+ expect(events_handler.handle(event, transaction)).to eq transaction
24
+ end
25
+
26
+ context 'when attempting to handle a hook not defined by the DSL' do
27
+
28
+ let(:events_definition) {
29
+ {
30
+ beforeAll: [
31
+ :known,
32
+ ],
33
+ afterAll: [
34
+ :unknown
35
+ ]
36
+ }
37
+ }
38
+ let(:transaction) { double }
39
+ let(:runner) { DummyRunner.new }
40
+ let(:events_handler) { described_class.new(events_definition, runner) }
41
+
42
+ it 'raises a UnknownHookError' do
43
+ expect{ events_handler.handle('beforeAll', transaction) }.not_to raise_error
44
+ expect{ events_handler.handle('afterAll', transaction) }.to raise_error UnknownHookError
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,35 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'dredd_hooks'
5
+
6
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f; puts f }
7
+
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ # make sure the deprecated RSpec 2 syntax is not used
11
+ config.raise_errors_for_deprecations!
12
+
13
+ # rspec-expectations config goes here. You can use an alternate
14
+ # assertion/expectation library such as wrong or the stdlib/minitest
15
+ # assertions if you prefer.
16
+ config.expect_with :rspec do |expectations|
17
+ # This option will default to `true` in RSpec 4. It makes the `description`
18
+ # and `failure_message` of custom matchers include text for helper methods
19
+ # defined using `chain`, e.g.:
20
+ # be_bigger_than(2).and_smaller_than(4).description
21
+ # # => "be bigger than 2 and smaller than 4"
22
+ # ...rather than:
23
+ # # => "be bigger than 2"
24
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
25
+ end
26
+
27
+ # rspec-mocks config goes here. You can use an alternate test double
28
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
29
+ config.mock_with :rspec do |mocks|
30
+ # Prevents you from mocking or stubbing a method that does not exist on
31
+ # a real object. This is generally recommended, and will default to
32
+ # `true` in RSpec 4.
33
+ mocks.verify_partial_doubles = true
34
+ end
35
+ end
metadata CHANGED
@@ -1,88 +1,89 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dredd_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Kliment
8
+ - Gonzalo Bulnes Guilpain
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-07-07 00:00:00.000000000 Z
12
+ date: 2016-08-06 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
- name: bundler
15
+ name: aruba
15
16
  requirement: !ruby/object:Gem::Requirement
16
17
  requirements:
17
- - - ~>
18
+ - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: '1.6'
20
+ version: 0.6.2
20
21
  type: :development
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
- - - ~>
25
+ - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: '1.6'
27
+ version: 0.6.2
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: rake
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - ~>
32
+ - - "~>"
32
33
  - !ruby/object:Gem::Version
33
34
  version: '10.0'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - ~>
39
+ - - "~>"
39
40
  - !ruby/object:Gem::Version
40
41
  version: '10.0'
41
42
  - !ruby/object:Gem::Dependency
42
- name: aruba
43
+ name: rspec
43
44
  requirement: !ruby/object:Gem::Requirement
44
45
  requirements:
45
- - - ~>
46
+ - - "~>"
46
47
  - !ruby/object:Gem::Version
47
- version: 0.6.2
48
+ version: '3.0'
48
49
  type: :development
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
52
- - - ~>
53
+ - - "~>"
53
54
  - !ruby/object:Gem::Version
54
- version: 0.6.2
55
+ version: '3.0'
55
56
  - !ruby/object:Gem::Dependency
56
57
  name: sinatra
57
58
  requirement: !ruby/object:Gem::Requirement
58
59
  requirements:
59
- - - ~>
60
+ - - "~>"
60
61
  - !ruby/object:Gem::Version
61
62
  version: 1.4.5
62
63
  type: :development
63
64
  prerelease: false
64
65
  version_requirements: !ruby/object:Gem::Requirement
65
66
  requirements:
66
- - - ~>
67
+ - - "~>"
67
68
  - !ruby/object:Gem::Version
68
69
  version: 1.4.5
69
70
  description: Write Dredd hooks in Ruby to glue together API Blueprint with your Ruby
70
- project
71
+ project.
71
72
  email:
72
73
  - adam@apiary.io
74
+ - gon.bulnes@gmail.com
73
75
  executables:
74
76
  - dredd-hooks-ruby
75
77
  extensions: []
76
78
  extra_rdoc_files: []
77
79
  files:
78
- - .gitignore
79
- - .travis.yml
80
+ - CHANGELOG.md
80
81
  - Gemfile
81
82
  - LICENSE.txt
82
83
  - README.md
83
84
  - Rakefile
84
85
  - bin/dredd-hooks-ruby
85
- - dredd_hooks.gemspec
86
+ - doc/README.md
86
87
  - features/execution_order.feature
87
88
  - features/failing_transaction.feature
88
89
  - features/hook_handlers.feature
@@ -90,13 +91,23 @@ files:
90
91
  - features/step_definitions/dredd_steps.rb
91
92
  - features/support/env.rb
92
93
  - features/support/server.rb
93
- - features/tcp_server.feature
94
94
  - lib/dredd_hooks.rb
95
+ - lib/dredd_hooks/cli.rb
96
+ - lib/dredd_hooks/definitions.rb
97
+ - lib/dredd_hooks/errors.rb
95
98
  - lib/dredd_hooks/file_loader.rb
96
99
  - lib/dredd_hooks/methods.rb
97
100
  - lib/dredd_hooks/runner.rb
98
101
  - lib/dredd_hooks/server.rb
102
+ - lib/dredd_hooks/server/buffer.rb
103
+ - lib/dredd_hooks/server/events_handler.rb
99
104
  - lib/dredd_hooks/version.rb
105
+ - spec/integration/definitions_consistency_spec.rb
106
+ - spec/lib/dredd_hooks/methods_spec.rb
107
+ - spec/lib/dredd_hooks/runner_spec.rb
108
+ - spec/lib/dredd_hooks/server/buffer_spec.rb
109
+ - spec/lib/dredd_hooks/server/events_handler_spec.rb
110
+ - spec/spec_helper.rb
100
111
  homepage: https://github.com/apiaryio/dredd-hooks-ruby
101
112
  licenses:
102
113
  - MIT
@@ -107,17 +118,17 @@ require_paths:
107
118
  - lib
108
119
  required_ruby_version: !ruby/object:Gem::Requirement
109
120
  requirements:
110
- - - '>='
121
+ - - ">="
111
122
  - !ruby/object:Gem::Version
112
123
  version: '0'
113
124
  required_rubygems_version: !ruby/object:Gem::Requirement
114
125
  requirements:
115
- - - '>='
126
+ - - ">="
116
127
  - !ruby/object:Gem::Version
117
128
  version: '0'
118
129
  requirements: []
119
130
  rubyforge_project:
120
- rubygems_version: 2.4.1
131
+ rubygems_version: 2.6.1
121
132
  signing_key:
122
133
  specification_version: 4
123
134
  summary: Ruby Hooks Handler for Dredd API Testing Framework
@@ -129,4 +140,9 @@ test_files:
129
140
  - features/step_definitions/dredd_steps.rb
130
141
  - features/support/env.rb
131
142
  - features/support/server.rb
132
- - features/tcp_server.feature
143
+ - spec/integration/definitions_consistency_spec.rb
144
+ - spec/lib/dredd_hooks/methods_spec.rb
145
+ - spec/lib/dredd_hooks/runner_spec.rb
146
+ - spec/lib/dredd_hooks/server/buffer_spec.rb
147
+ - spec/lib/dredd_hooks/server/events_handler_spec.rb
148
+ - spec/spec_helper.rb