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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +18 -6
- data/lib/check_please.rb +2 -0
- data/lib/check_please/cli.rb +1 -1
- data/lib/check_please/cli/parser.rb +1 -1
- data/lib/check_please/comparison.rb +37 -29
- data/lib/check_please/diffs.rb +0 -5
- data/lib/check_please/path.rb +38 -11
- data/lib/check_please/version.rb +1 -1
- data/usage_examples.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 41ad78ce4324c1b5d9c8ac3e336d46108850d6fd7d1d51d30c13b807632d612a
|
4
|
+
data.tar.gz: 9191e6e52c57b6d79761a8a0258a41378cf19af1364a3e8875e727ff8e501f06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 388b93c0277580a37b066b7fa6aade4f8feeb5f677a4ccc2221ac060d23df153c3dc16c6cdacce740448773665200855e653d74f55cfae659511e39a4391b36f
|
7
|
+
data.tar.gz: 44cb45ace2a720d90035eef279f490896a785e76c77bd4bd72c63400a828300332638442599b905dad8e4bde4b329a698fd44f4116f847699b5250c586e27c86
|
data/Gemfile.lock
CHANGED
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.
|
65
|
-
|
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
|
|
data/lib/check_please.rb
CHANGED
@@ -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.",
|
data/lib/check_please/cli.rb
CHANGED
@@ -1,30 +1,33 @@
|
|
1
1
|
module CheckPlease
|
2
|
+
using Refinements
|
2
3
|
|
3
|
-
|
4
|
-
|
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
|
-
|
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
|
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
|
18
|
-
if (
|
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
|
24
|
-
when [ :hash, :hash ] ; compare_hashes ref, can, path
|
25
|
-
when [ :other, :other ] ; compare_others ref, can, path
|
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
|
-
|
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
|
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 ;
|
52
|
-
when can_array.length < n ;
|
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
|
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
|
60
|
-
record_missing_keys ref_hash, can_hash, path
|
61
|
-
compare_common_keys ref_hash, can_hash, path
|
62
|
-
record_extra_keys ref_hash, can_hash, path
|
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
|
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
|
-
|
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
|
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
|
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
|
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
|
-
|
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
|
89
|
+
def compare_others(ref, can, path)
|
87
90
|
return if ref == can
|
88
|
-
|
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
|
data/lib/check_please/diffs.rb
CHANGED
data/lib/check_please/path.rb
CHANGED
@@ -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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
if flags
|
24
|
-
|
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
|
30
|
-
|
35
|
+
def inspect
|
36
|
+
"<CheckPlease::Path '#{to_s}'>"
|
31
37
|
end
|
32
38
|
|
33
|
-
def
|
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
|
|
data/lib/check_please/version.rb
CHANGED
data/usage_examples.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2020-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: table_print
|