check_please 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4e0e81da97eb41aade7d58c516519bde3d2527173b64e7d644e46458493450d
4
- data.tar.gz: c633c13e0a776bdbf1691b9c01d3be3cc978cb445c2fbf8d74e95449ea0b3118
3
+ metadata.gz: 41ad78ce4324c1b5d9c8ac3e336d46108850d6fd7d1d51d30c13b807632d612a
4
+ data.tar.gz: 9191e6e52c57b6d79761a8a0258a41378cf19af1364a3e8875e727ff8e501f06
5
5
  SHA512:
6
- metadata.gz: ad31ad99aef3494a2d2438b422a5fef9385c6fd05572131907d33d9e1e30a94ba0582ca4aae00c997dbe302487719da3d5ddbf715cf23422405086084f7a982c
7
- data.tar.gz: 04d8e89b72894e458394ff0428192ffbe6f3b307437ce6ee8496f6e3064236498286b2fb203a399c5245172cec391656646dac39b20f0adc8a345eff13b8f015
6
+ metadata.gz: 388b93c0277580a37b066b7fa6aade4f8feeb5f677a4ccc2221ac060d23df153c3dc16c6cdacce740448773665200855e653d74f55cfae659511e39a4391b36f
7
+ data.tar.gz: 44cb45ace2a720d90035eef279f490896a785e76c77bd4bd72c63400a828300332638442599b905dad8e4bde4b329a698fd44f4116f847699b5250c586e27c86
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- check_please (0.4.0)
4
+ check_please (0.4.1)
5
5
  table_print
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -59,10 +59,15 @@ out!
59
59
 
60
60
  ### From Ruby
61
61
 
62
+ See also: [./usage_examples.rb](usage_examples.rb).
63
+
62
64
  Create two strings, each containing a JSON or YAML document, and pass them to
63
65
  `CheckPlease.render_diff`. You'll get back a third string containing a report
64
- of all the differences CheckPlease found in the two JSON strings. (See also:
65
- [./usage_examples.rb](usage_examples.rb).)
66
+ of all the differences CheckPlease found in the two JSON strings.
67
+
68
+ Or, if you'd like to inspect the diffs in your own way, use `CheckPlease.diff`
69
+ instead. You'll get back a `CheckPlease::Diffs` custom collection that
70
+ contains `CheckPlease::Diff` instances.
66
71
 
67
72
  ### Understanding the Output
68
73
 
@@ -161,21 +166,28 @@ If you want to incorporate CheckPlease into some other toolchain, it can also
161
166
  print diffs as JSON to facilitate parsing. In Ruby, pass `format: :json` to
162
167
  `CheckPlease.render_diff`; in the CLI, use the `-f`/`--format` switch.
163
168
 
164
- ## TODO
169
+ ## TODO (maybe)
165
170
 
166
171
  * command line flags for :allthethings:!
167
172
  * sort by path?
168
173
  * detect timestamps and compare after parsing?
169
174
  * ignore sub-second precision (option / CLI flag)?
170
175
  * possibly support plugins for other folks to add custom coercions?
171
- * support expressions of specific paths to ignore
172
- * wildcards? `#` for indexes, `**` to match one or more path segments?
173
- (This could get ugly fast.)
174
176
  * display filters? (e.g., { a: 1, b: 2 } ==> "Hash#3")
175
177
  * shorter descriptions of values with different classes
176
178
  (but maybe just the existing :type_mismatch diffs?)
177
179
  * another "possibly support plugins" expansion point here
178
180
  * more output formats, maybe?
181
+ * [0xABAD1DEA] support wildcards in --select-paths and --reject-paths?
182
+ * `#` for indexes, `**` to match one or more path segments?
183
+ (This could get ugly fast.)
184
+ * [0xABAD1DEA] look for a config file in ./.check_please_config or ~/.check_please_config,
185
+ combine flags found there with those in ARGV in order of precedence:
186
+ 1) ARGV
187
+ 2) ./.check_please
188
+ 3) ~/.check_please
189
+ * but this may not actually be worth the time and complexity to implement, so
190
+ think about this first...
179
191
 
180
192
  ## Development
181
193
 
@@ -105,6 +105,7 @@ module CheckPlease
105
105
  flag.reentrant
106
106
  flag.mutually_exclusive_to :reject_paths
107
107
 
108
+ flag.cli_short = "-s PATH_EXPR"
108
109
  flag.cli_long = "--select-paths PATH_EXPR"
109
110
  flag.description = [
110
111
  "ONLY record diffs matching the provided PATH expression.",
@@ -117,6 +118,7 @@ module CheckPlease
117
118
  flag.reentrant
118
119
  flag.mutually_exclusive_to :select_paths
119
120
 
121
+ flag.cli_short = "-r PATH_EXPR"
120
122
  flag.cli_long = "--reject-paths PATH_EXPR"
121
123
  flag.description = [
122
124
  "DON'T record diffs matching the provided PATH expression.",
@@ -5,7 +5,7 @@ module CheckPlease
5
5
  autoload :Parser, "check_please/cli/runner"
6
6
 
7
7
  def self.run(exe_file_name)
8
- Runner.new(__FILE__).run(*ARGV.dup)
8
+ Runner.new(exe_file_name).run(*ARGV.dup)
9
9
  end
10
10
  end
11
11
 
@@ -5,7 +5,7 @@ module CLI
5
5
 
6
6
  class Parser
7
7
  def initialize(exe_file_name)
8
- @exe_file_name = exe_file_name
8
+ @exe_file_name = File.basename(exe_file_name)
9
9
  end
10
10
 
11
11
  # Unfortunately, OptionParser *really* wants to use closures. I haven't
@@ -1,30 +1,33 @@
1
1
  module CheckPlease
2
+ using Refinements
2
3
 
3
- module Comparison
4
- extend self
4
+ class Comparison
5
+ def self.perform(reference, candidate, flags = {})
6
+ new.perform(reference, candidate, flags)
7
+ end
5
8
 
6
9
  def perform(reference, candidate, flags = {})
7
- root = CheckPlease::Path.new
8
- diffs = Diffs.new(flags: flags)
10
+ @flags = Flags(flags) # whoa, it's almost like Java in here
11
+ @diffs = Diffs.new(flags: @flags)
12
+
9
13
  catch(:max_diffs_reached) do
10
- compare reference, candidate, root, diffs
14
+ compare reference, candidate, CheckPlease::Path.root
11
15
  end
12
16
  diffs
13
17
  end
14
18
 
15
19
  private
20
+ attr_reader :diffs, :flags
16
21
 
17
- def compare(ref, can, path, diffs)
18
- if (d = diffs.flags.max_depth)
19
- return if path.depth > d + 1
20
- end
22
+ def compare(ref, can, path)
23
+ return if path.excluded?(flags)
21
24
 
22
25
  case types(ref, can)
23
- when [ :array, :array ] ; compare_arrays ref, can, path, diffs
24
- when [ :hash, :hash ] ; compare_hashes ref, can, path, diffs
25
- when [ :other, :other ] ; compare_others ref, can, path, diffs
26
+ when [ :array, :array ] ; compare_arrays ref, can, path
27
+ when [ :hash, :hash ] ; compare_hashes ref, can, path
28
+ when [ :other, :other ] ; compare_others ref, can, path
26
29
  else
27
- diffs.record ref, can, path, :type_mismatch
30
+ record_diff ref, can, path, :type_mismatch
28
31
  end
29
32
  end
30
33
 
@@ -38,7 +41,7 @@ module CheckPlease
38
41
  }
39
42
  end
40
43
 
41
- def compare_arrays(ref_array, can_array, path, diffs)
44
+ def compare_arrays(ref_array, can_array, path)
42
45
  max_len = [ ref_array, can_array ].map(&:length).max
43
46
  (0...max_len).each do |i|
44
47
  n = i + 1 # count in human pls
@@ -48,45 +51,50 @@ module CheckPlease
48
51
  can = can_array[i]
49
52
 
50
53
  case
51
- when ref_array.length < n ; diffs.record ref, can, new_path, :extra
52
- when can_array.length < n ; diffs.record ref, can, new_path, :missing
54
+ when ref_array.length < n ; record_diff ref, can, new_path, :extra
55
+ when can_array.length < n ; record_diff ref, can, new_path, :missing
53
56
  else
54
- compare ref, can, new_path, diffs
57
+ compare ref, can, new_path
55
58
  end
56
59
  end
57
60
  end
58
61
 
59
- def compare_hashes(ref_hash, can_hash, path, diffs)
60
- record_missing_keys ref_hash, can_hash, path, diffs
61
- compare_common_keys ref_hash, can_hash, path, diffs
62
- record_extra_keys ref_hash, can_hash, path, diffs
62
+ def compare_hashes(ref_hash, can_hash, path)
63
+ record_missing_keys ref_hash, can_hash, path
64
+ compare_common_keys ref_hash, can_hash, path
65
+ record_extra_keys ref_hash, can_hash, path
63
66
  end
64
67
 
65
- def record_missing_keys(ref_hash, can_hash, path, diffs)
68
+ def record_missing_keys(ref_hash, can_hash, path)
66
69
  keys = ref_hash.keys - can_hash.keys
67
70
  keys.each do |k|
68
- diffs.record ref_hash[k], nil, path + k, :missing
71
+ record_diff ref_hash[k], nil, path + k, :missing
69
72
  end
70
73
  end
71
74
 
72
- def compare_common_keys(ref_hash, can_hash, path, diffs)
75
+ def compare_common_keys(ref_hash, can_hash, path)
73
76
  keys = ref_hash.keys & can_hash.keys
74
77
  keys.each do |k|
75
- compare ref_hash[k], can_hash[k], path + k, diffs
78
+ compare ref_hash[k], can_hash[k], path + k
76
79
  end
77
80
  end
78
81
 
79
- def record_extra_keys(ref_hash, can_hash, path, diffs)
82
+ def record_extra_keys(ref_hash, can_hash, path)
80
83
  keys = can_hash.keys - ref_hash.keys
81
84
  keys.each do |k|
82
- diffs.record nil, can_hash[k], path + k, :extra
85
+ record_diff nil, can_hash[k], path + k, :extra
83
86
  end
84
87
  end
85
88
 
86
- def compare_others(ref, can, path, diffs)
89
+ def compare_others(ref, can, path)
87
90
  return if ref == can
88
- diffs.record ref, can, path, :mismatch
91
+ record_diff ref, can, path, :mismatch
89
92
  end
93
+
94
+ def record_diff(ref, can, path, type)
95
+ diff = Diff.new(type, path, ref, can)
96
+ diffs << diff
97
+ end
90
98
  end
91
99
 
92
100
  end
@@ -43,11 +43,6 @@ module CheckPlease
43
43
  @hash[diff.path] = diff
44
44
  end
45
45
 
46
- def record(ref, can, path, type)
47
- return if path.excluded?(flags)
48
- self << Diff.new(type, path, ref, can)
49
- end
50
-
51
46
  def data
52
47
  @list.map(&:attributes)
53
48
  end
@@ -3,8 +3,15 @@ module CheckPlease
3
3
  class Path
4
4
  SEPARATOR = "/"
5
5
 
6
+ def self.root
7
+ new
8
+ end
9
+
10
+ attr_reader :to_s
6
11
  def initialize(segments = [])
7
12
  @segments = Array(segments)
13
+ @to_s = SEPARATOR + @segments.join(SEPARATOR)
14
+ freeze
8
15
  end
9
16
 
10
17
  def +(new_basename)
@@ -16,22 +23,42 @@ module CheckPlease
16
23
  end
17
24
 
18
25
  def excluded?(flags)
19
- s = to_s ; matches = ->(path_expr) { s.include?(path_expr) }
20
- if flags.select_paths.length > 0
21
- return flags.select_paths.none?(&matches)
22
- end
23
- if flags.reject_paths.length > 0
24
- return flags.reject_paths.any?(&matches)
25
- end
26
+ return false if root?
27
+
28
+ return true if too_deep?(flags)
29
+ return true if explicitly_excluded?(flags)
30
+ return true if implicitly_excluded?(flags)
31
+
26
32
  false
27
33
  end
28
34
 
29
- def to_s
30
- SEPARATOR + @segments.join(SEPARATOR)
35
+ def inspect
36
+ "<CheckPlease::Path '#{to_s}'>"
31
37
  end
32
38
 
33
- def inspect
34
- to_s
39
+ def root?
40
+ to_s == SEPARATOR
41
+ end
42
+
43
+ private
44
+
45
+ def explicitly_excluded?(flags)
46
+ flags.reject_paths.any?( &method(:match?) )
47
+ end
48
+
49
+ def implicitly_excluded?(flags)
50
+ return false if flags.select_paths.empty?
51
+ flags.select_paths.none?( &method(:match?) )
52
+ end
53
+
54
+ # leaving this here for a while in case it needs to grow into a public method
55
+ def match?(path_expr)
56
+ to_s.include?(path_expr)
57
+ end
58
+
59
+ def too_deep?(flags)
60
+ return false if flags.max_depth.nil?
61
+ flags.max_depth + 1 < depth
35
62
  end
36
63
  end
37
64
 
@@ -1,5 +1,5 @@
1
1
  module CheckPlease
2
2
  # NOTE: 'check_please_rspec_matcher' depends on this,
3
3
  # so try to keep them roughly in sync
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
@@ -3,6 +3,10 @@ require 'check_please'
3
3
  reference = { foo: "wibble" }
4
4
  candidate = { bar: "wibble" }
5
5
 
6
+
7
+
8
+ ##### Printing diffs #####
9
+
6
10
  puts CheckPlease.render_diff(reference, candidate)
7
11
 
8
12
  # this should print the following to stdout:
@@ -12,3 +16,15 @@ _ = <<EOF
12
16
  missing | /foo | wibble |
13
17
  extra | /bar | | wibble
14
18
  EOF
19
+
20
+
21
+
22
+ ##### Doing your own thing with diffs #####
23
+
24
+ diffs = CheckPlease.diff(reference, candidate)
25
+
26
+ # `diffs` is a custom collection (type: CheckPlease::Diffs) that contains
27
+ # individual Diff objects for you to inspect as you see fit.
28
+ #
29
+ # If you come up with a useful way to present these, feel free to submit a PR
30
+ # with a new class in `lib/check_please/printers` !
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: check_please
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Livingston-Gray
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-25 00:00:00.000000000 Z
11
+ date: 2020-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print