cmdx-rspec 1.4.0 → 2.1.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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/CHANGELOG.md +25 -0
  4. data/README.md +337 -169
  5. data/lib/cmdx/rspec/helpers.rb +453 -291
  6. data/lib/cmdx/rspec/matchers/be_complete.rb +20 -0
  7. data/lib/cmdx/rspec/matchers/be_deprecated.rb +14 -52
  8. data/lib/cmdx/rspec/matchers/be_interrupted.rb +20 -0
  9. data/lib/cmdx/rspec/matchers/be_ko.rb +19 -0
  10. data/lib/cmdx/rspec/matchers/be_ok.rb +19 -0
  11. data/lib/cmdx/rspec/matchers/be_successful.rb +8 -20
  12. data/lib/cmdx/rspec/matchers/have_been_retried.rb +41 -0
  13. data/lib/cmdx/rspec/matchers/have_been_rolled_back.rb +23 -0
  14. data/lib/cmdx/rspec/matchers/have_callback.rb +46 -0
  15. data/lib/cmdx/rspec/matchers/have_chain_root.rb +30 -0
  16. data/lib/cmdx/rspec/matchers/have_chain_size.rb +29 -0
  17. data/lib/cmdx/rspec/matchers/have_duration.rb +30 -0
  18. data/lib/cmdx/rspec/matchers/have_empty_context.rb +4 -16
  19. data/lib/cmdx/rspec/matchers/have_empty_metadata.rb +3 -9
  20. data/lib/cmdx/rspec/matchers/have_errors_on.rb +50 -0
  21. data/lib/cmdx/rspec/matchers/have_failed.rb +7 -24
  22. data/lib/cmdx/rspec/matchers/have_input.rb +41 -0
  23. data/lib/cmdx/rspec/matchers/have_matching_context.rb +5 -20
  24. data/lib/cmdx/rspec/matchers/have_matching_metadata.rb +5 -17
  25. data/lib/cmdx/rspec/matchers/have_middleware.rb +29 -0
  26. data/lib/cmdx/rspec/matchers/have_no_errors.rb +30 -0
  27. data/lib/cmdx/rspec/matchers/have_output.rb +46 -0
  28. data/lib/cmdx/rspec/matchers/have_pipeline_tasks.rb +27 -0
  29. data/lib/cmdx/rspec/matchers/have_retry_on.rb +36 -0
  30. data/lib/cmdx/rspec/matchers/have_skipped.rb +7 -24
  31. data/lib/cmdx/rspec/matchers/have_tag.rb +30 -0
  32. data/lib/cmdx/rspec/matchers/raise_cmdx_fault.rb +74 -0
  33. data/lib/cmdx/rspec/version.rb +2 -1
  34. data/lib/cmdx/rspec.rb +22 -3
  35. metadata +25 -5
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a task class registered +middleware+. When given
4
+ # a class, matches by `is_a?`; otherwise by `==` (works for module-level
5
+ # callables like `MyMiddleware` referenced by name).
6
+ #
7
+ # @example
8
+ # expect(MyCommand).to have_middleware(LoggingMiddleware)
9
+ RSpec::Matchers.define :have_middleware do |middleware|
10
+ description { "have middleware #{middleware.inspect}" }
11
+
12
+ failure_message do |actual|
13
+ target = actual.is_a?(Class) ? actual : actual.class
14
+ "expected #{target} to register middleware #{middleware.inspect}, but had #{target.middlewares.registry.inspect}"
15
+ end
16
+
17
+ match do |actual|
18
+ target = actual.is_a?(Class) ? actual : actual.class
19
+ next false unless target.respond_to?(:middlewares)
20
+
21
+ target.middlewares.registry.any? do |entry|
22
+ m, = entry
23
+ case middleware
24
+ when Class then m.is_a?(middleware) || m == middleware
25
+ else m == middleware
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a {CMDx::Result}, task instance, or {CMDx::Errors}
4
+ # is free of errors.
5
+ #
6
+ # @example
7
+ # expect(result).to have_no_errors
8
+ RSpec::Matchers.define :have_no_errors do
9
+ description { "have no errors" }
10
+
11
+ failure_message do |actual|
12
+ errors = extract_errors(actual)
13
+ "expected #{actual.inspect} to have no errors, but had #{errors&.to_h.inspect}"
14
+ end
15
+
16
+ match do |actual|
17
+ errors = extract_errors(actual)
18
+ next false if errors.nil?
19
+
20
+ errors.empty?
21
+ end
22
+
23
+ define_method(:extract_errors) do |actual|
24
+ case actual
25
+ when CMDx::Errors then actual
26
+ when CMDx::Result, CMDx::Task then actual.errors
27
+ else actual.respond_to?(:errors) ? actual.errors : nil
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a task class declares an output named +name+.
4
+ # Optional keyword arguments are matched against the output's serialized
5
+ # {CMDx::Output#to_h} (partial match — only provided keys are checked).
6
+ #
7
+ # @example Existence check
8
+ # expect(MyCommand).to have_output(:total)
9
+ #
10
+ # @example Required output
11
+ # expect(MyCommand).to have_output(:total, required: true)
12
+ RSpec::Matchers.define :have_output do |name, **expected|
13
+ description do
14
+ expected.empty? ? "have output #{name.inspect}" : "have output #{name.inspect} matching #{expected.inspect}"
15
+ end
16
+
17
+ failure_message do |actual|
18
+ target = actual.is_a?(Class) ? actual : actual.class
19
+ output = target.outputs.registry[name.to_sym]
20
+ if output.nil?
21
+ "expected #{target} to declare output #{name.inspect}, but registry has #{target.outputs.registry.keys.inspect}"
22
+ else
23
+ "expected #{target}'s output #{name.inspect} to match #{expected.inspect}, but it was #{output.to_h.inspect}"
24
+ end
25
+ end
26
+
27
+ match do |actual|
28
+ target = actual.is_a?(Class) ? actual : actual.class
29
+ next false unless target.respond_to?(:outputs)
30
+
31
+ output = target.outputs.registry[name.to_sym]
32
+ next false if output.nil?
33
+ next true if expected.empty?
34
+
35
+ expected.all? do |k, v|
36
+ actual =
37
+ case k
38
+ when :name then output.name
39
+ when :description then output.description
40
+ else output.to_h.fetch(:options, {})[k]
41
+ end
42
+ actual = false if k == :required && actual.nil?
43
+ values_match?(v, actual)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a {CMDx::Workflow} class declares the given
4
+ # pipeline tasks. Compares the flattened, deduplicated pipeline against
5
+ # the expected task classes (order-sensitive).
6
+ #
7
+ # @example
8
+ # expect(MyWorkflow).to have_pipeline_tasks(StepA, StepB, StepC)
9
+ #
10
+ # @example Order-insensitive variant
11
+ # expect(MyWorkflow).to have_pipeline_tasks(StepA, StepC, StepB).in_any_order
12
+ RSpec::Matchers.define :have_pipeline_tasks do |*expected|
13
+ description { "have pipeline tasks #{expected.inspect}" }
14
+
15
+ failure_message do |actual|
16
+ "expected #{actual} pipeline to be #{expected.inspect}, but was #{actual.pipeline.flat_map(&:tasks).uniq.inspect}"
17
+ end
18
+
19
+ match do |actual|
20
+ raise ArgumentError, "must be a CMDx::Workflow" unless actual.is_a?(Class) && actual.include?(CMDx::Workflow)
21
+
22
+ tasks = actual.pipeline.flat_map(&:tasks).uniq
23
+ @any_order ? tasks.sort_by(&:object_id) == expected.sort_by(&:object_id) : tasks == expected
24
+ end
25
+
26
+ chain(:in_any_order) { @any_order = true }
27
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a task class is configured to retry on +exception+.
4
+ # Optional keyword arguments check the {CMDx::Retry} configuration values
5
+ # (`:limit`, `:delay`, `:max_delay`, `:jitter`).
6
+ #
7
+ # @example
8
+ # expect(MyCommand).to have_retry_on(Net::OpenTimeout)
9
+ # expect(MyCommand).to have_retry_on(Net::OpenTimeout, limit: 5, jitter: :exponential)
10
+ RSpec::Matchers.define :have_retry_on do |exception, **expected|
11
+ description do
12
+ expected.empty? ? "have retry on #{exception}" : "have retry on #{exception} matching #{expected.inspect}"
13
+ end
14
+
15
+ failure_message do |actual|
16
+ target = actual.is_a?(Class) ? actual : actual.class
17
+ retry_cfg = target.retry_on
18
+ if retry_cfg.exceptions.none?(exception)
19
+ "expected #{target} to retry on #{exception}, but only retries on #{retry_cfg.exceptions.inspect}"
20
+ else
21
+ mismatched = expected.reject { |k, v| retry_cfg.public_send(k) == v }
22
+ "expected #{target}'s retry_on to match #{expected.inspect}, but mismatched: #{mismatched.inspect}"
23
+ end
24
+ end
25
+
26
+ match do |actual|
27
+ target = actual.is_a?(Class) ? actual : actual.class
28
+ next false unless target.respond_to?(:retry_on)
29
+
30
+ retry_cfg = target.retry_on
31
+ next false unless retry_cfg.exceptions.include?(exception)
32
+ next true if expected.empty?
33
+
34
+ expected.all? { |k, v| retry_cfg.public_send(k) == v }
35
+ end
36
+ end
@@ -1,25 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Matcher to verify that a result represents a skipped execution.
3
+ # Asserts a {CMDx::Result} was skipped (`state: interrupted`,
4
+ # `status: skipped`). Extra keyword args constrain other `result.to_h`
5
+ # fields such as `:reason`, `:cause`, or `:metadata`.
4
6
  #
5
- # @param data [Hash] Optional hash of additional attributes to match
6
- # @option data [Symbol] :state Expected state (defaults to CMDx::Result::INTERRUPTED)
7
- # @option data [Symbol] :status Expected status (defaults to CMDx::Result::SKIPPED)
8
- # @option data [Symbol] :outcome Expected outcome (defaults to CMDx::Result::SKIPPED)
9
- # @option data [String] :reason Expected reason string
10
- # @option data [CMDx::SkipFault] :cause Expected cause fault
11
- #
12
- # @return [RSpec::Matchers::BuiltIn::BaseMatcher] The matcher instance
13
- #
14
- # @raise [ArgumentError] if the actual value is not a CMDx::Result
15
- #
16
- # @example Checking if a result is skipped
17
- # result = MyCommand.execute
18
- # expect(result).to have_skipped
19
- #
20
- # @example Checking skipped with specific reason
21
- # result = MyCommand.execute
22
- # expect(result).to have_skipped(reason: "Skipped for testing")
7
+ # @example
8
+ # expect(result).to have_skipped(reason: "out of stock")
23
9
  RSpec::Matchers.define :have_skipped do |**data|
24
10
  description { "have been skipped" }
25
11
 
@@ -27,11 +13,8 @@ RSpec::Matchers.define :have_skipped do |**data|
27
13
  raise ArgumentError, "must be a CMDx::Result" unless result.is_a?(CMDx::Result)
28
14
 
29
15
  expect(result.to_h).to include(
30
- state: CMDx::Result::INTERRUPTED,
31
- status: CMDx::Result::SKIPPED,
32
- outcome: CMDx::Result::SKIPPED,
33
- reason: CMDx::Locale.t("cmdx.faults.unspecified"),
34
- cause: be_a(CMDx::SkipFault),
16
+ state: CMDx::Signal::INTERRUPTED,
17
+ status: CMDx::Signal::SKIPPED,
35
18
  **data
36
19
  )
37
20
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a {CMDx::Result} (or task class) carries +tag+
4
+ # in its settings tags.
5
+ #
6
+ # @example
7
+ # expect(result).to have_tag(:critical)
8
+ # expect(MyCommand).to have_tag(:critical)
9
+ RSpec::Matchers.define :have_tag do |tag|
10
+ description { "have tag #{tag.inspect}" }
11
+
12
+ failure_message do |actual|
13
+ tags = extract_tags(actual)
14
+ "expected #{actual} to have tag #{tag.inspect}, but had #{tags.inspect}"
15
+ end
16
+
17
+ match do |actual|
18
+ extract_tags(actual).include?(tag)
19
+ end
20
+
21
+ define_method(:extract_tags) do |actual|
22
+ case actual
23
+ when CMDx::Result then actual.tags
24
+ when Class
25
+ actual.respond_to?(:settings) ? actual.settings.tags : []
26
+ else
27
+ actual.respond_to?(:tags) ? actual.tags : []
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Matcher to verify that a block raises a {CMDx::Fault}, optionally
4
+ # constraining which task originated the failure, the failure reason, and
5
+ # the underlying cause.
6
+ #
7
+ # @example Any CMDx fault
8
+ # expect { MyCommand.execute! }.to raise_cmdx_fault
9
+ #
10
+ # @example From a specific task class (matches `fault.task`, the leaf failure)
11
+ # expect { MyCommand.execute! }.to raise_cmdx_fault(MyCommand)
12
+ #
13
+ # @example With a specific reason
14
+ # expect { MyCommand.execute! }.to raise_cmdx_fault.with_reason("invalid")
15
+ #
16
+ # @example With a regex reason and a specific underlying cause
17
+ # expect { MyCommand.execute! }
18
+ # .to raise_cmdx_fault(MyCommand).with_reason(/invalid/).with_cause(MyError)
19
+ RSpec::Matchers.define :raise_cmdx_fault do |expected_task = nil|
20
+ description do
21
+ parts = ["raise CMDx::Fault"]
22
+ parts << "from #{expected_task}" if expected_task
23
+ parts << "with reason #{@expected_reason.inspect}" if defined?(@expected_reason)
24
+ parts << "with cause #{@expected_cause.inspect}" if defined?(@expected_cause)
25
+ parts.join(" ")
26
+ end
27
+
28
+ supports_block_expectations
29
+
30
+ match do |block|
31
+ raise ArgumentError, "block required" unless block.respond_to?(:call)
32
+
33
+ @actual_fault = nil
34
+ begin
35
+ block.call
36
+ rescue CMDx::Fault => e
37
+ @actual_fault = e
38
+ end
39
+
40
+ next false if @actual_fault.nil?
41
+ next false if expected_task && !(@actual_fault.task <= expected_task) # rubocop:disable Style/InverseMethods
42
+ next false if defined?(@expected_reason) && !reason_matches?(@actual_fault.result.reason)
43
+ next false if defined?(@expected_cause) && !cause_matches?(@actual_fault.result.cause)
44
+
45
+ true
46
+ end
47
+
48
+ failure_message do
49
+ if @actual_fault.nil?
50
+ "expected block to raise CMDx::Fault, but nothing was raised"
51
+ else
52
+ "expected #{@actual_fault.inspect} (task: #{@actual_fault.task}, reason: " \
53
+ "#{@actual_fault.result.reason.inspect}, cause: #{@actual_fault.result.cause.inspect}) " \
54
+ "to satisfy the matcher"
55
+ end
56
+ end
57
+
58
+ chain(:with_reason) { |reason| @expected_reason = reason }
59
+ chain(:with_cause) { |cause| @expected_cause = cause }
60
+
61
+ define_method(:reason_matches?) do |actual|
62
+ case @expected_reason
63
+ when Regexp then @expected_reason.match?(actual.to_s)
64
+ else @expected_reason == actual
65
+ end
66
+ end
67
+
68
+ define_method(:cause_matches?) do |actual|
69
+ case @expected_cause
70
+ when Class then actual.is_a?(@expected_cause)
71
+ else values_match?(@expected_cause, actual)
72
+ end
73
+ end
74
+ end
@@ -3,7 +3,8 @@
3
3
  module CMDx
4
4
  module RSpec
5
5
 
6
- VERSION = "1.4.0"
6
+ # Gem version. Bumped on release; mirrored in the gemspec.
7
+ VERSION = "2.1.0"
7
8
 
8
9
  end
9
10
  end
data/lib/cmdx/rspec.rb CHANGED
@@ -5,11 +5,30 @@ require "rspec"
5
5
 
6
6
  require_relative "rspec/helpers"
7
7
 
8
+ require_relative "rspec/matchers/be_complete"
8
9
  require_relative "rspec/matchers/be_deprecated"
10
+ require_relative "rspec/matchers/be_interrupted"
11
+ require_relative "rspec/matchers/be_ko"
12
+ require_relative "rspec/matchers/be_ok"
9
13
  require_relative "rspec/matchers/be_successful"
10
- require_relative "rspec/matchers/have_skipped"
11
- require_relative "rspec/matchers/have_failed"
14
+ require_relative "rspec/matchers/have_been_retried"
15
+ require_relative "rspec/matchers/have_been_rolled_back"
16
+ require_relative "rspec/matchers/have_callback"
17
+ require_relative "rspec/matchers/have_chain_root"
18
+ require_relative "rspec/matchers/have_chain_size"
19
+ require_relative "rspec/matchers/have_duration"
12
20
  require_relative "rspec/matchers/have_empty_context"
13
- require_relative "rspec/matchers/have_matching_context"
14
21
  require_relative "rspec/matchers/have_empty_metadata"
22
+ require_relative "rspec/matchers/have_errors_on"
23
+ require_relative "rspec/matchers/have_failed"
24
+ require_relative "rspec/matchers/have_input"
25
+ require_relative "rspec/matchers/have_matching_context"
15
26
  require_relative "rspec/matchers/have_matching_metadata"
27
+ require_relative "rspec/matchers/have_middleware"
28
+ require_relative "rspec/matchers/have_no_errors"
29
+ require_relative "rspec/matchers/have_output"
30
+ require_relative "rspec/matchers/have_pipeline_tasks"
31
+ require_relative "rspec/matchers/have_retry_on"
32
+ require_relative "rspec/matchers/have_skipped"
33
+ require_relative "rspec/matchers/have_tag"
34
+ require_relative "rspec/matchers/raise_cmdx_fault"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdx-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 1.5.0
18
+ version: 2.0.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: 1.5.0
25
+ version: 2.0.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -128,6 +128,7 @@ executables: []
128
128
  extensions: []
129
129
  extra_rdoc_files: []
130
130
  files:
131
+ - ".DS_Store"
131
132
  - CHANGELOG.md
132
133
  - CODE_OF_CONDUCT.md
133
134
  - LICENSE.txt
@@ -135,14 +136,33 @@ files:
135
136
  - Rakefile
136
137
  - lib/cmdx/rspec.rb
137
138
  - lib/cmdx/rspec/helpers.rb
139
+ - lib/cmdx/rspec/matchers/be_complete.rb
138
140
  - lib/cmdx/rspec/matchers/be_deprecated.rb
141
+ - lib/cmdx/rspec/matchers/be_interrupted.rb
142
+ - lib/cmdx/rspec/matchers/be_ko.rb
143
+ - lib/cmdx/rspec/matchers/be_ok.rb
139
144
  - lib/cmdx/rspec/matchers/be_successful.rb
145
+ - lib/cmdx/rspec/matchers/have_been_retried.rb
146
+ - lib/cmdx/rspec/matchers/have_been_rolled_back.rb
147
+ - lib/cmdx/rspec/matchers/have_callback.rb
148
+ - lib/cmdx/rspec/matchers/have_chain_root.rb
149
+ - lib/cmdx/rspec/matchers/have_chain_size.rb
150
+ - lib/cmdx/rspec/matchers/have_duration.rb
140
151
  - lib/cmdx/rspec/matchers/have_empty_context.rb
141
152
  - lib/cmdx/rspec/matchers/have_empty_metadata.rb
153
+ - lib/cmdx/rspec/matchers/have_errors_on.rb
142
154
  - lib/cmdx/rspec/matchers/have_failed.rb
155
+ - lib/cmdx/rspec/matchers/have_input.rb
143
156
  - lib/cmdx/rspec/matchers/have_matching_context.rb
144
157
  - lib/cmdx/rspec/matchers/have_matching_metadata.rb
158
+ - lib/cmdx/rspec/matchers/have_middleware.rb
159
+ - lib/cmdx/rspec/matchers/have_no_errors.rb
160
+ - lib/cmdx/rspec/matchers/have_output.rb
161
+ - lib/cmdx/rspec/matchers/have_pipeline_tasks.rb
162
+ - lib/cmdx/rspec/matchers/have_retry_on.rb
145
163
  - lib/cmdx/rspec/matchers/have_skipped.rb
164
+ - lib/cmdx/rspec/matchers/have_tag.rb
165
+ - lib/cmdx/rspec/matchers/raise_cmdx_fault.rb
146
166
  - lib/cmdx/rspec/version.rb
147
167
  homepage: https://github.com/drexed/cmdx-rspec
148
168
  licenses:
@@ -161,14 +181,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
181
  requirements:
162
182
  - - ">="
163
183
  - !ruby/object:Gem::Version
164
- version: 3.1.0
184
+ version: 3.3.0
165
185
  required_rubygems_version: !ruby/object:Gem::Requirement
166
186
  requirements:
167
187
  - - ">="
168
188
  - !ruby/object:Gem::Version
169
189
  version: '0'
170
190
  requirements: []
171
- rubygems_version: 4.0.9
191
+ rubygems_version: 4.0.21
172
192
  specification_version: 4
173
193
  summary: Simple CMDx task testing via RSpec matchers.
174
194
  test_files: []