cmdx-rspec 1.4.0 → 2.0.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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/README.md +274 -173
  4. data/lib/cmdx/rspec/helpers.rb +244 -276
  5. data/lib/cmdx/rspec/matchers/be_complete.rb +20 -0
  6. data/lib/cmdx/rspec/matchers/be_deprecated.rb +14 -52
  7. data/lib/cmdx/rspec/matchers/be_interrupted.rb +20 -0
  8. data/lib/cmdx/rspec/matchers/be_ko.rb +19 -0
  9. data/lib/cmdx/rspec/matchers/be_ok.rb +19 -0
  10. data/lib/cmdx/rspec/matchers/be_successful.rb +8 -20
  11. data/lib/cmdx/rspec/matchers/have_been_retried.rb +41 -0
  12. data/lib/cmdx/rspec/matchers/have_been_rolled_back.rb +23 -0
  13. data/lib/cmdx/rspec/matchers/have_callback.rb +46 -0
  14. data/lib/cmdx/rspec/matchers/have_chain_root.rb +30 -0
  15. data/lib/cmdx/rspec/matchers/have_chain_size.rb +29 -0
  16. data/lib/cmdx/rspec/matchers/have_duration.rb +30 -0
  17. data/lib/cmdx/rspec/matchers/have_empty_context.rb +4 -16
  18. data/lib/cmdx/rspec/matchers/have_empty_metadata.rb +3 -9
  19. data/lib/cmdx/rspec/matchers/have_errors_on.rb +50 -0
  20. data/lib/cmdx/rspec/matchers/have_failed.rb +7 -24
  21. data/lib/cmdx/rspec/matchers/have_input.rb +41 -0
  22. data/lib/cmdx/rspec/matchers/have_matching_context.rb +5 -20
  23. data/lib/cmdx/rspec/matchers/have_matching_metadata.rb +5 -17
  24. data/lib/cmdx/rspec/matchers/have_middleware.rb +29 -0
  25. data/lib/cmdx/rspec/matchers/have_no_errors.rb +30 -0
  26. data/lib/cmdx/rspec/matchers/have_output.rb +46 -0
  27. data/lib/cmdx/rspec/matchers/have_pipeline_tasks.rb +27 -0
  28. data/lib/cmdx/rspec/matchers/have_retry_on.rb +36 -0
  29. data/lib/cmdx/rspec/matchers/have_skipped.rb +7 -24
  30. data/lib/cmdx/rspec/matchers/have_tag.rb +30 -0
  31. data/lib/cmdx/rspec/matchers/raise_cmdx_fault.rb +74 -0
  32. data/lib/cmdx/rspec/version.rb +2 -1
  33. data/lib/cmdx/rspec.rb +22 -3
  34. metadata +24 -5
@@ -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.0.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.0.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
@@ -135,14 +135,33 @@ files:
135
135
  - Rakefile
136
136
  - lib/cmdx/rspec.rb
137
137
  - lib/cmdx/rspec/helpers.rb
138
+ - lib/cmdx/rspec/matchers/be_complete.rb
138
139
  - lib/cmdx/rspec/matchers/be_deprecated.rb
140
+ - lib/cmdx/rspec/matchers/be_interrupted.rb
141
+ - lib/cmdx/rspec/matchers/be_ko.rb
142
+ - lib/cmdx/rspec/matchers/be_ok.rb
139
143
  - lib/cmdx/rspec/matchers/be_successful.rb
144
+ - lib/cmdx/rspec/matchers/have_been_retried.rb
145
+ - lib/cmdx/rspec/matchers/have_been_rolled_back.rb
146
+ - lib/cmdx/rspec/matchers/have_callback.rb
147
+ - lib/cmdx/rspec/matchers/have_chain_root.rb
148
+ - lib/cmdx/rspec/matchers/have_chain_size.rb
149
+ - lib/cmdx/rspec/matchers/have_duration.rb
140
150
  - lib/cmdx/rspec/matchers/have_empty_context.rb
141
151
  - lib/cmdx/rspec/matchers/have_empty_metadata.rb
152
+ - lib/cmdx/rspec/matchers/have_errors_on.rb
142
153
  - lib/cmdx/rspec/matchers/have_failed.rb
154
+ - lib/cmdx/rspec/matchers/have_input.rb
143
155
  - lib/cmdx/rspec/matchers/have_matching_context.rb
144
156
  - lib/cmdx/rspec/matchers/have_matching_metadata.rb
157
+ - lib/cmdx/rspec/matchers/have_middleware.rb
158
+ - lib/cmdx/rspec/matchers/have_no_errors.rb
159
+ - lib/cmdx/rspec/matchers/have_output.rb
160
+ - lib/cmdx/rspec/matchers/have_pipeline_tasks.rb
161
+ - lib/cmdx/rspec/matchers/have_retry_on.rb
145
162
  - lib/cmdx/rspec/matchers/have_skipped.rb
163
+ - lib/cmdx/rspec/matchers/have_tag.rb
164
+ - lib/cmdx/rspec/matchers/raise_cmdx_fault.rb
146
165
  - lib/cmdx/rspec/version.rb
147
166
  homepage: https://github.com/drexed/cmdx-rspec
148
167
  licenses:
@@ -161,14 +180,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
180
  requirements:
162
181
  - - ">="
163
182
  - !ruby/object:Gem::Version
164
- version: 3.1.0
183
+ version: 3.3.0
165
184
  required_rubygems_version: !ruby/object:Gem::Requirement
166
185
  requirements:
167
186
  - - ">="
168
187
  - !ruby/object:Gem::Version
169
188
  version: '0'
170
189
  requirements: []
171
- rubygems_version: 4.0.9
190
+ rubygems_version: 4.0.11
172
191
  specification_version: 4
173
192
  summary: Simple CMDx task testing via RSpec matchers.
174
193
  test_files: []