super_diff 0.3.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +104 -73
  3. data/lib/super_diff.rb +20 -11
  4. data/lib/super_diff/active_record.rb +21 -23
  5. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +3 -3
  6. data/lib/super_diff/active_record/differs/active_record_relation.rb +3 -5
  7. data/lib/super_diff/active_record/monkey_patches.rb +9 -0
  8. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb +32 -22
  9. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb +17 -7
  10. data/lib/super_diff/active_record/operation_tree_builders.rb +14 -0
  11. data/lib/super_diff/active_record/{operational_sequencers → operation_tree_builders}/active_record_model.rb +2 -2
  12. data/lib/super_diff/active_record/{operational_sequencers → operation_tree_builders}/active_record_relation.rb +4 -4
  13. data/lib/super_diff/active_record/{operation_sequences.rb → operation_trees.rb} +2 -2
  14. data/lib/super_diff/active_record/{operation_sequences → operation_trees}/active_record_relation.rb +2 -2
  15. data/lib/super_diff/active_support.rb +16 -19
  16. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +3 -3
  17. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +3 -5
  18. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +17 -7
  19. data/lib/super_diff/active_support/operation_tree_builders.rb +10 -0
  20. data/lib/super_diff/active_support/{operational_sequencers → operation_tree_builders}/hash_with_indifferent_access.rb +2 -2
  21. data/lib/super_diff/active_support/{operation_sequences.rb → operation_trees.rb} +2 -2
  22. data/lib/super_diff/active_support/{operation_sequences → operation_trees}/hash_with_indifferent_access.rb +2 -2
  23. data/lib/super_diff/configuration.rb +60 -0
  24. data/lib/super_diff/csi.rb +4 -0
  25. data/lib/super_diff/diff_formatters.rb +3 -3
  26. data/lib/super_diff/diff_formatters/array.rb +3 -3
  27. data/lib/super_diff/diff_formatters/base.rb +3 -2
  28. data/lib/super_diff/diff_formatters/collection.rb +2 -2
  29. data/lib/super_diff/diff_formatters/custom_object.rb +3 -3
  30. data/lib/super_diff/diff_formatters/default_object.rb +6 -8
  31. data/lib/super_diff/diff_formatters/defaults.rb +10 -0
  32. data/lib/super_diff/diff_formatters/hash.rb +3 -3
  33. data/lib/super_diff/diff_formatters/main.rb +41 -0
  34. data/lib/super_diff/diff_formatters/multiline_string.rb +3 -3
  35. data/lib/super_diff/differs.rb +4 -11
  36. data/lib/super_diff/differs/array.rb +2 -11
  37. data/lib/super_diff/differs/base.rb +20 -3
  38. data/lib/super_diff/differs/custom_object.rb +2 -11
  39. data/lib/super_diff/differs/default_object.rb +2 -8
  40. data/lib/super_diff/differs/defaults.rb +12 -0
  41. data/lib/super_diff/differs/hash.rb +2 -11
  42. data/lib/super_diff/differs/main.rb +48 -0
  43. data/lib/super_diff/differs/multiline_string.rb +2 -14
  44. data/lib/super_diff/differs/time_like.rb +15 -0
  45. data/lib/super_diff/equality_matchers.rb +3 -9
  46. data/lib/super_diff/equality_matchers/array.rb +1 -7
  47. data/lib/super_diff/equality_matchers/base.rb +1 -1
  48. data/lib/super_diff/equality_matchers/default.rb +2 -8
  49. data/lib/super_diff/equality_matchers/defaults.rb +12 -0
  50. data/lib/super_diff/equality_matchers/hash.rb +1 -7
  51. data/lib/super_diff/equality_matchers/main.rb +21 -0
  52. data/lib/super_diff/equality_matchers/multiline_string.rb +1 -7
  53. data/lib/super_diff/errors.rb +16 -0
  54. data/lib/super_diff/errors/no_diff_formatter_available_error.rb +21 -0
  55. data/lib/super_diff/errors/no_differ_available_error.rb +24 -0
  56. data/lib/super_diff/errors/no_operational_sequencer_available_error.rb +22 -0
  57. data/lib/super_diff/implementation_checks.rb +19 -0
  58. data/lib/super_diff/object_inspection.rb +1 -10
  59. data/lib/super_diff/object_inspection/inspection_tree.rb +6 -2
  60. data/lib/super_diff/object_inspection/inspectors.rb +5 -1
  61. data/lib/super_diff/object_inspection/inspectors/array.rb +20 -10
  62. data/lib/super_diff/object_inspection/inspectors/base.rb +36 -0
  63. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +24 -14
  64. data/lib/super_diff/object_inspection/inspectors/default_object.rb +44 -30
  65. data/lib/super_diff/object_inspection/inspectors/defaults.rb +15 -0
  66. data/lib/super_diff/object_inspection/inspectors/hash.rb +20 -10
  67. data/lib/super_diff/object_inspection/inspectors/main.rb +35 -0
  68. data/lib/super_diff/object_inspection/inspectors/primitive.rb +20 -5
  69. data/lib/super_diff/object_inspection/inspectors/string.rb +15 -5
  70. data/lib/super_diff/object_inspection/inspectors/time_like.rb +23 -0
  71. data/lib/super_diff/object_inspection/nodes/inspection.rb +9 -2
  72. data/lib/super_diff/operation_tree_builders.rb +18 -0
  73. data/lib/super_diff/{operational_sequencers → operation_tree_builders}/array.rb +38 -59
  74. data/lib/super_diff/operation_tree_builders/base.rb +98 -0
  75. data/lib/super_diff/{operational_sequencers → operation_tree_builders}/custom_object.rb +3 -3
  76. data/lib/super_diff/{operational_sequencers → operation_tree_builders}/default_object.rb +8 -3
  77. data/lib/super_diff/operation_tree_builders/defaults.rb +5 -0
  78. data/lib/super_diff/operation_tree_builders/hash.rb +226 -0
  79. data/lib/super_diff/operation_tree_builders/main.rb +42 -0
  80. data/lib/super_diff/{operational_sequencers → operation_tree_builders}/multiline_string.rb +3 -3
  81. data/lib/super_diff/operation_tree_builders/time_like.rb +34 -0
  82. data/lib/super_diff/operation_trees.rb +13 -0
  83. data/lib/super_diff/{operation_sequences → operation_trees}/array.rb +5 -1
  84. data/lib/super_diff/operation_trees/base.rb +31 -0
  85. data/lib/super_diff/{operation_sequences → operation_trees}/custom_object.rb +5 -1
  86. data/lib/super_diff/{operation_sequences → operation_trees}/default_object.rb +10 -8
  87. data/lib/super_diff/operation_trees/defaults.rb +5 -0
  88. data/lib/super_diff/{operation_sequences → operation_trees}/hash.rb +5 -1
  89. data/lib/super_diff/operation_trees/main.rb +35 -0
  90. data/lib/super_diff/operation_trees/multiline_string.rb +18 -0
  91. data/lib/super_diff/operations/unary_operation.rb +3 -0
  92. data/lib/super_diff/rspec.rb +54 -22
  93. data/lib/super_diff/rspec/augmented_matcher.rb +1 -1
  94. data/lib/super_diff/rspec/differ.rb +2 -17
  95. data/lib/super_diff/rspec/differs.rb +9 -3
  96. data/lib/super_diff/rspec/differs/collection_containing_exactly.rb +3 -8
  97. data/lib/super_diff/rspec/differs/collection_including.rb +18 -0
  98. data/lib/super_diff/rspec/differs/hash_including.rb +18 -0
  99. data/lib/super_diff/rspec/differs/object_having_attributes.rb +17 -0
  100. data/lib/super_diff/rspec/matcher_text_builders.rb +4 -0
  101. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +26 -7
  102. data/lib/super_diff/rspec/matcher_text_builders/have_predicate.rb +61 -0
  103. data/lib/super_diff/rspec/matcher_text_builders/match.rb +1 -1
  104. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +13 -1
  105. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +1 -1
  106. data/lib/super_diff/rspec/matcher_text_template.rb +1 -1
  107. data/lib/super_diff/rspec/monkey_patches.rb +226 -115
  108. data/lib/super_diff/rspec/object_inspection.rb +0 -1
  109. data/lib/super_diff/rspec/object_inspection/inspectors.rb +22 -6
  110. data/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb +17 -8
  111. data/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb +28 -0
  112. data/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb +31 -0
  113. data/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb +23 -0
  114. data/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb +23 -0
  115. data/lib/super_diff/rspec/object_inspection/inspectors/object_having_attributes.rb +31 -0
  116. data/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb +13 -0
  117. data/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb +29 -0
  118. data/lib/super_diff/rspec/operation_tree_builders.rb +22 -0
  119. data/lib/super_diff/rspec/{operational_sequencers → operation_tree_builders}/collection_containing_exactly.rb +6 -6
  120. data/lib/super_diff/rspec/{operational_sequencers/partial_array.rb → operation_tree_builders/collection_including.rb} +4 -3
  121. data/lib/super_diff/rspec/operation_tree_builders/hash_including.rb +25 -0
  122. data/lib/super_diff/rspec/{operational_sequencers/partial_object.rb → operation_tree_builders/object_having_attributes.rb} +5 -11
  123. data/lib/super_diff/version.rb +1 -1
  124. data/spec/examples.txt +5 -350
  125. data/spec/integration/rails/active_record_spec.rb +1 -1
  126. data/spec/integration/rails/hash_with_indifferent_access_spec.rb +1 -1
  127. data/spec/integration/rspec/be_predicate_matcher_spec.rb +111 -59
  128. data/spec/integration/rspec/eq_matcher_spec.rb +1 -1
  129. data/spec/integration/rspec/have_attributes_matcher_spec.rb +354 -227
  130. data/spec/integration/rspec/have_predicate_matcher_spec.rb +484 -0
  131. data/spec/integration/rspec/include_matcher_spec.rb +2 -2
  132. data/spec/integration/rspec/match_array_matcher_spec.rb +372 -0
  133. data/spec/integration/rspec/match_matcher_spec.rb +8 -8
  134. data/spec/integration/rspec/raise_error_matcher_spec.rb +605 -226
  135. data/spec/integration/rspec/third_party_matcher_spec.rb +241 -0
  136. data/spec/integration/rspec/unhandled_errors_spec.rb +110 -58
  137. data/spec/spec_helper.rb +18 -7
  138. data/spec/support/command_runner.rb +3 -0
  139. data/spec/support/integration/helpers.rb +14 -90
  140. data/spec/support/integration/matchers.rb +143 -0
  141. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +14 -29
  142. data/spec/support/integration/test_programs/base.rb +120 -0
  143. data/spec/support/integration/test_programs/plain.rb +13 -0
  144. data/spec/support/integration/test_programs/rspec_active_record.rb +17 -0
  145. data/spec/support/integration/test_programs/rspec_rails.rb +17 -0
  146. data/spec/support/models/active_record/person.rb +4 -11
  147. data/spec/support/models/active_record/query.rb +15 -0
  148. data/spec/support/models/active_record/shipping_address.rb +10 -14
  149. data/spec/support/object_id.rb +27 -0
  150. data/spec/support/ruby_versions.rb +4 -0
  151. data/spec/support/shared_examples/active_record.rb +71 -0
  152. data/spec/support/shared_examples/hash_with_indifferent_access.rb +724 -208
  153. data/spec/unit/{equality_matcher_spec.rb → equality_matchers/main_spec.rb} +165 -9
  154. data/spec/unit/object_inspection_spec.rb +94 -18
  155. data/spec/unit/rspec/matchers/have_predicate_spec.rb +21 -0
  156. data/spec/unit/rspec/matchers/match_array_spec.rb +11 -0
  157. data/spec/unit/rspec/matchers/raise_error_spec.rb +16 -0
  158. data/super_diff.gemspec +4 -6
  159. metadata +99 -82
  160. data/lib/super_diff/active_record/object_inspection/map_extension.rb +0 -18
  161. data/lib/super_diff/active_record/operational_sequencers.rb +0 -14
  162. data/lib/super_diff/active_support/object_inspection/map_extension.rb +0 -15
  163. data/lib/super_diff/active_support/operational_sequencers.rb +0 -10
  164. data/lib/super_diff/diff_formatter.rb +0 -32
  165. data/lib/super_diff/differ.rb +0 -51
  166. data/lib/super_diff/differs/time.rb +0 -24
  167. data/lib/super_diff/equality_matcher.rb +0 -32
  168. data/lib/super_diff/no_differ_available_error.rb +0 -22
  169. data/lib/super_diff/no_operational_sequencer_available_error.rb +0 -20
  170. data/lib/super_diff/object_inspection/inspector.rb +0 -27
  171. data/lib/super_diff/object_inspection/inspectors/time.rb +0 -13
  172. data/lib/super_diff/object_inspection/map.rb +0 -30
  173. data/lib/super_diff/operation_sequences.rb +0 -9
  174. data/lib/super_diff/operation_sequences/base.rb +0 -11
  175. data/lib/super_diff/operational_sequencer.rb +0 -48
  176. data/lib/super_diff/operational_sequencers.rb +0 -17
  177. data/lib/super_diff/operational_sequencers/base.rb +0 -89
  178. data/lib/super_diff/operational_sequencers/hash.rb +0 -85
  179. data/lib/super_diff/operational_sequencers/time_like.rb +0 -30
  180. data/lib/super_diff/rspec/configuration.rb +0 -31
  181. data/lib/super_diff/rspec/differs/partial_array.rb +0 -22
  182. data/lib/super_diff/rspec/differs/partial_hash.rb +0 -22
  183. data/lib/super_diff/rspec/differs/partial_object.rb +0 -22
  184. data/lib/super_diff/rspec/object_inspection/inspectors/partial_array.rb +0 -22
  185. data/lib/super_diff/rspec/object_inspection/inspectors/partial_hash.rb +0 -21
  186. data/lib/super_diff/rspec/object_inspection/inspectors/partial_object.rb +0 -21
  187. data/lib/super_diff/rspec/object_inspection/map_extension.rb +0 -23
  188. data/lib/super_diff/rspec/operational_sequencers.rb +0 -22
  189. data/lib/super_diff/rspec/operational_sequencers/partial_hash.rb +0 -32
@@ -120,6 +120,9 @@ Output:
120
120
  @env = extract_env_from(@options)
121
121
 
122
122
  @process = ChildProcess.build(*command)
123
+ # @env.each do |key, value|
124
+ # @process.environment[key] = value
125
+ # end
123
126
  @process.io.stdout = @process.io.stderr = @writer
124
127
 
125
128
  @wrapper = -> (block) { block.call }
@@ -8,97 +8,24 @@ module SuperDiff
8
8
  end
9
9
  end
10
10
 
11
- def make_plain_test_program(test, color_enabled:)
12
- <<~PROGRAM
13
- #{set_up_with("super_diff/rspec", color_enabled: color_enabled)}
14
- #{describe_block_including(test)}
15
- PROGRAM
11
+ def make_plain_test_program(
12
+ test,
13
+ color_enabled:,
14
+ preserve_as_whole_file: false
15
+ )
16
+ TestPrograms::Plain.new(
17
+ test,
18
+ color_enabled: color_enabled,
19
+ preserve_as_whole_file: preserve_as_whole_file,
20
+ )
16
21
  end
17
22
 
18
23
  def make_rspec_active_record_program(test, color_enabled:)
19
- <<~PROGRAM
20
- #{
21
- set_up_active_record_around do
22
- set_up_with(
23
- "super_diff/rspec", "super_diff/active_record",
24
- color_enabled: color_enabled
25
- )
26
- end
27
- }
28
- #{describe_block_including(test)}
29
- PROGRAM
24
+ TestPrograms::RspecActiveRecord.new(test, color_enabled: color_enabled)
30
25
  end
31
26
 
32
27
  def make_rspec_rails_test_program(test, color_enabled:)
33
- <<~PROGRAM
34
- #{
35
- set_up_active_record_around do
36
- set_up_with("super_diff/rspec-rails", color_enabled: color_enabled)
37
- end
38
- }
39
- #{describe_block_including(test)}
40
- PROGRAM
41
- end
42
-
43
- def set_up_active_record_around(&block)
44
- <<~PROGRAM
45
- require "active_record"
46
-
47
- ActiveRecord::Base.establish_connection(
48
- adapter: "sqlite3",
49
- database: ":memory:"
50
- )
51
-
52
- RSpec.configuration do |config|
53
- config.before do
54
- SuperDiff::Test::Models::ActiveRecord::Person.delete_all
55
- SuperDiff::Test::Models::ActiveRecord::ShippingAddress.delete_all
56
- end
57
- end
58
-
59
- #{block.call}
60
-
61
- Dir.glob(SUPPORT_DIR.join("models/active_record/*.rb")).each do |path|
62
- require path
63
- end
64
- PROGRAM
65
- end
66
-
67
- def set_up_with(*libraries, color_enabled:)
68
- <<~SETUP
69
- PROJECT_DIRECTORY = Pathname.new("#{PROJECT_DIRECTORY}")
70
- SUPPORT_DIR = PROJECT_DIRECTORY.join("spec/support")
71
- INSIDE_INTEGRATION_TEST = true
72
-
73
- $LOAD_PATH.unshift(PROJECT_DIRECTORY.join("lib"))
74
- #\$LOAD_PATH.unshift(PROJECT_DIRECTORY)
75
-
76
- begin
77
- require "pry-byebug"
78
- rescue LoadError
79
- require "pry-nav"
80
- end
81
-
82
- RSpec.configure do |config|
83
- config.color_mode = :#{color_enabled ? "on" : "off"}
84
- end
85
-
86
- #{libraries.map { |library| %( require "#{library}") }.join("\n")}
87
-
88
- Dir.glob(SUPPORT_DIR.join("{diff_formatters,models,operational_sequencers,operation_sequences}/*.rb")).each do |path|
89
- require path
90
- end
91
- SETUP
92
- end
93
-
94
- def describe_block_including(test)
95
- <<~PROGRAM
96
- RSpec.describe "test" do
97
- it "passes" do
98
- #{reindent(test, level: 2)}
99
- end
100
- end
101
- PROGRAM
28
+ TestPrograms::RspecRails.new(test, color_enabled: color_enabled)
102
29
  end
103
30
 
104
31
  def build_expected_output(
@@ -106,6 +33,7 @@ module SuperDiff
106
33
  snippet:,
107
34
  expectation:,
108
35
  newline_before_expectation: false,
36
+ indentation: 7,
109
37
  diff: nil
110
38
  )
111
39
  colored(color_enabled: color_enabled) do
@@ -122,7 +50,7 @@ module SuperDiff
122
50
  newline
123
51
  end
124
52
 
125
- indent by: 7 do
53
+ indent by: indentation do
126
54
  evaluate_block(&expectation)
127
55
 
128
56
  if diff
@@ -171,9 +99,5 @@ module SuperDiff
171
99
  def colored(color_enabled: true, &block)
172
100
  SuperDiff::Helpers.style(color_enabled: color_enabled, &block).to_s.chomp
173
101
  end
174
-
175
- def reindent(code, level: 0)
176
- code.strip.split("\n").map { |line| (" " * level) + line }.join("\n")
177
- end
178
102
  end
179
103
  end
@@ -0,0 +1,143 @@
1
+ module SuperDiff
2
+ module IntegrationTests
3
+ def fail_with_indented_multiline_failure_message
4
+ FailWithIndentedMultilineFailureMessageMatcher.new
5
+ end
6
+
7
+ def fail_with_paragraphed_failure_message
8
+ FailWithParagraphedFailureMessageMatcher.new
9
+ end
10
+
11
+ def fail_with_non_indented_multiline_failure_message
12
+ FailWithNonIndentedMultilineFailureMessageMatcher.new
13
+ end
14
+
15
+ def fail_with_singleline_failure_message
16
+ FailWithSinglelineFailureMessageMatcher.new
17
+ end
18
+
19
+ def pass_with_indented_multiline_failure_message
20
+ PassWithIndentedMultilineFailureMessageMatcher.new
21
+ end
22
+
23
+ def pass_with_paragraphed_failure_message
24
+ PassWithParagraphedFailureMessageMatcher.new
25
+ end
26
+
27
+ def pass_with_non_indented_multiline_failure_message
28
+ PassWithNonIndentedMultilineFailureMessageMatcher.new
29
+ end
30
+
31
+ def pass_with_singleline_failure_message
32
+ PassWithSinglelineFailureMessageMatcher.new
33
+ end
34
+
35
+ class FailWithIndentedMultilineFailureMessageMatcher
36
+ def matches?(_)
37
+ false
38
+ end
39
+
40
+ def failure_message
41
+ <<-MESSAGE
42
+ This is a message that spans multiple lines.
43
+ Here is the next line.
44
+ This part is indented, for whatever reason. It just kinda keeps
45
+ going until we finish saying whatever it is we want to say.
46
+ MESSAGE
47
+ end
48
+ end
49
+
50
+ class FailWithParagraphedFailureMessageMatcher
51
+ def matches?(_)
52
+ false
53
+ end
54
+
55
+ def failure_message
56
+ <<-MESSAGE
57
+ This is a message that spans multiple paragraphs.
58
+
59
+ Here is the next paragraph.
60
+ MESSAGE
61
+ end
62
+ end
63
+
64
+ class FailWithNonIndentedMultilineFailureMessageMatcher
65
+ def matches?(_)
66
+ false
67
+ end
68
+
69
+ def failure_message
70
+ <<-MESSAGE
71
+ This is a message that spans multiple lines.
72
+ Here is the next line.
73
+ MESSAGE
74
+ end
75
+ end
76
+
77
+ class FailWithSinglelineFailureMessageMatcher
78
+ def matches?(_)
79
+ false
80
+ end
81
+
82
+ def failure_message
83
+ <<-MESSAGE
84
+ This is a message that spans only one line.
85
+ MESSAGE
86
+ end
87
+ end
88
+
89
+ class PassWithIndentedMultilineFailureMessageMatcher
90
+ def does_not_match?(_)
91
+ false
92
+ end
93
+
94
+ def failure_message_when_negated
95
+ <<-MESSAGE
96
+ This is a message that spans multiple lines.
97
+ Here is the next line.
98
+ This part is indented, for whatever reason. It just kinda keeps
99
+ going until we finish saying whatever it is we want to say.
100
+ MESSAGE
101
+ end
102
+ end
103
+
104
+ class PassWithParagraphedFailureMessageMatcher
105
+ def does_not_match?(_)
106
+ false
107
+ end
108
+
109
+ def failure_message_when_negated
110
+ <<-MESSAGE
111
+ This is a message that spans multiple paragraphs.
112
+
113
+ Here is the next paragraph.
114
+ MESSAGE
115
+ end
116
+ end
117
+
118
+ class PassWithNonIndentedMultilineFailureMessageMatcher
119
+ def does_not_match?(_)
120
+ false
121
+ end
122
+
123
+ def failure_message_when_negated
124
+ <<-MESSAGE
125
+ This is a message that spans multiple lines.
126
+ Here is the next line.
127
+ MESSAGE
128
+ end
129
+ end
130
+
131
+ class PassWithSinglelineFailureMessageMatcher
132
+ def does_not_match?(_)
133
+ false
134
+ end
135
+
136
+ def failure_message_when_negated
137
+ <<-MESSAGE
138
+ This is a message that spans only one line.
139
+ MESSAGE
140
+ end
141
+ end
142
+ end
143
+ end
@@ -5,11 +5,8 @@ module SuperDiff
5
5
  end
6
6
 
7
7
  class ProduceOutputWhenRunMatcher
8
- PROJECT_DIRECTORY = Pathname.new("../../../../../").expand_path(__FILE__)
9
- TEMP_DIRECTORY = PROJECT_DIRECTORY.join("tmp")
10
-
11
8
  def initialize(expected_output)
12
- @expected_output = expected_output.to_s.strip
9
+ @expected_output = expected_output.to_s
13
10
  @output_processor = nil
14
11
  @expect_output_to_contain_color = nil
15
12
  end
@@ -25,9 +22,7 @@ module SuperDiff
25
22
  end
26
23
 
27
24
  def matches?(program)
28
- @program = program.strip
29
-
30
- TEMP_DIRECTORY.mkpath
25
+ @program = program
31
26
 
32
27
  output_matches? && presence_of_color_matches?
33
28
  end
@@ -48,13 +43,17 @@ module SuperDiff
48
43
  "Actual output:\n\n" +
49
44
  CommandRunner::OutputHelpers.bookended(actual_output)
50
45
 
51
- ::RSpec::Matchers::ExpectedsForMultipleDiffs.
52
- from(expected_output).
53
- message_with_diff(
54
- message,
55
- ::RSpec::Expectations.differ,
56
- actual_output,
57
- )
46
+ if ["1", "true"].include?(ENV["SHOW_DIFF"])
47
+ ::RSpec::Matchers::ExpectedsForMultipleDiffs.
48
+ from(expected_output).
49
+ message_with_diff(
50
+ message,
51
+ ::RSpec::Expectations.differ,
52
+ actual_output,
53
+ )
54
+ else
55
+ message
56
+ end
58
57
  end
59
58
  end
60
59
 
@@ -77,7 +76,7 @@ module SuperDiff
77
76
 
78
77
  def actual_output
79
78
  @_actual_output ||= begin
80
- output = run_command.output.strip
79
+ output = program.run.output.strip
81
80
 
82
81
  if output_processor
83
82
  output.gsub!(output_processor[0], output_processor[1])
@@ -87,20 +86,6 @@ module SuperDiff
87
86
  end
88
87
  end
89
88
 
90
- def run_command
91
- CommandRunner.run(
92
- "rspec --options /tmp/dummy-rspec-config",
93
- tempfile.to_s,
94
- env: { "DISABLE_PRY" => "true" },
95
- )
96
- end
97
-
98
- def tempfile
99
- @_tempfile =
100
- TEMP_DIRECTORY.join("integration_spec.rb").
101
- tap { |tempfile| tempfile.write(program) }
102
- end
103
-
104
89
  def presence_of_color_matches?
105
90
  @expect_output_to_contain_color.nil? ||
106
91
  output_has_color? == expect_output_to_contain_color?
@@ -0,0 +1,120 @@
1
+ module SuperDiff
2
+ module IntegrationTests
3
+ module TestPrograms
4
+ class Base
5
+ extend AttrExtras.mixin
6
+
7
+ PROJECT_DIRECTORY = Pathname.new("../../../..").expand_path(__dir__)
8
+ TEMP_DIRECTORY = PROJECT_DIRECTORY.join("tmp")
9
+
10
+ attr_private :code, :color_enabled, :preserve_as_whole_file
11
+
12
+ def initialize(code, color_enabled:, preserve_as_whole_file: false)
13
+ @code = code.strip
14
+ @color_enabled = color_enabled
15
+ @preserve_as_whole_file = preserve_as_whole_file
16
+ end
17
+
18
+ def run
19
+ result_of_command
20
+ end
21
+
22
+ protected
23
+
24
+ def test_plan_prelude
25
+ ""
26
+ end
27
+
28
+ def test_plan_command
29
+ raise NotImplementedError
30
+ end
31
+
32
+ private
33
+
34
+ attr_query :color_enabled?
35
+ attr_query :preserve_as_whole_file?
36
+
37
+ def result_of_command
38
+ @_result_of_command ||=
39
+ if zeus_running?
40
+ Bundler.with_unbundled_env { CommandRunner.run(*command) }
41
+ else
42
+ CommandRunner.run(*command)
43
+ end
44
+ end
45
+
46
+ def command
47
+ if ENV["RAILS_ENV"]
48
+ raise "RAILS_ENV is being set somehow?!"
49
+ end
50
+
51
+ if zeus_running?
52
+ ["zeus", test_plan_command, color_option, tempfile.to_s]
53
+ else
54
+ [
55
+ "rspec",
56
+ "--options",
57
+ "/tmp/dummy-rspec-config",
58
+ tempfile.to_s,
59
+ ]
60
+ end
61
+ end
62
+
63
+ def zeus_running?
64
+ PROJECT_DIRECTORY.join(".zeus.sock").exist?
65
+ end
66
+
67
+ def color_option
68
+ color_enabled ? "--color" : "--no-color"
69
+ end
70
+
71
+ def tempfile
72
+ @_tempfile ||= begin
73
+ TEMP_DIRECTORY.mkpath
74
+ TEMP_DIRECTORY.join("integration_spec.rb").tap do |file|
75
+ file.write(program)
76
+ end
77
+ end
78
+ end
79
+
80
+ def program
81
+ if zeus_running?
82
+ minimal_program
83
+ else
84
+ <<~PROGRAM
85
+ require "#{PROJECT_DIRECTORY.join("support/test_plan.rb")}"
86
+
87
+ test_plan = TestPlan.new(
88
+ using_outside_of_zeus: true,
89
+ color_enabled: #{color_enabled?}
90
+ )
91
+ test_plan.boot
92
+ #{test_plan_prelude}
93
+ test_plan.#{test_plan_command}
94
+
95
+ #{minimal_program}
96
+ PROGRAM
97
+ end
98
+ end
99
+
100
+ def minimal_program
101
+ if preserve_as_whole_file?
102
+ code
103
+ else
104
+ <<~PROGRAM
105
+ RSpec.describe "test" do
106
+ it "passes" do
107
+ #{reindent(code, level: 2)}
108
+ end
109
+ end
110
+ PROGRAM
111
+ end
112
+ end
113
+
114
+ def reindent(code, level: 0)
115
+ code.strip.split("\n").map { |line| (" " * level) + line }.join("\n")
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end