check_please 0.2.0 → 0.2.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: 1c16110449be99d5ff509c720c4365b2fe6f176f23094784628db2de0e5f3bf9
4
- data.tar.gz: b45c9d1b7dea9b4405f4ca80dbce5f04d0aec14ad50e4e919802e09b469c86c8
3
+ metadata.gz: 33a26fc2e249e7ff6bba107ba2da856f0f70a9e232862b3650893b1844117cac
4
+ data.tar.gz: c8624e0cfc38a4f7bcd9db9af22215f0d2d6311ab061ec156318894fa8a01b60
5
5
  SHA512:
6
- metadata.gz: 023b9fdd6f227f8381dfba06a82d30961adab9b2ab5a2b5a0d820c8b8697d300f59d416c801808e88e7e625720776f1cec4cf0a6f57d1eec07e6cb656881d4b8
7
- data.tar.gz: eca955971798d00dd45bf5e068779aebeb33b304fe592d84534081d0068eaa981f6b765fd5c4ea9a30e7d65b2edd456ed9093c039964e3691c646e0c7d23b2b0
6
+ metadata.gz: 93c0b1d909dbdaf606a0376f38062eaaa26bd19573413ce0d81002b120aa247176d7a564752b83f6ac9a359974df32a7a5c2997f20468f95d683b28369d5548b
7
+ data.tar.gz: 78fe71950e42986ef0b9857984310fa51955d08a5b1b9b432d2b277fb9191b188b173f78575d0f88a927d4668065ac35fc32f1d74bba082894f897c0d9896267
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- check_please (0.2.0)
4
+ check_please (0.2.1)
5
5
  table_print
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -163,8 +163,6 @@ print diffs as JSON to facilitate parsing. In Ruby, pass `format: :json` to
163
163
  ## TODO
164
164
 
165
165
  * command line flags for :allthethings:!
166
- * --fail-fast
167
- * limit to first N
168
166
  * sort by path?
169
167
  * max depth (for iterative refinement?)
170
168
  * detect timestamps and compare after parsing?
data/Rakefile CHANGED
@@ -1,6 +1,21 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
- RSpec::Core::RakeTask.new(:spec)
4
+ namespace :spec do
5
+ RSpec::Core::RakeTask.new(:all)
6
+
7
+ RSpec::Core::RakeTask.new(:not_cli) do |t|
8
+ t.rspec_opts = "--tag ~cli"
9
+ end
10
+ task fast: :not_cli
11
+
12
+ # These are much slower than the rest, since they use Kernel#`
13
+ RSpec::Core::RakeTask.new(:cli) do |t|
14
+ t.rspec_opts = "--tag cli"
15
+ end
16
+ end
17
+
18
+ # By default, `rake spec` should run fast specs first, then cli if those all pass
19
+ task :spec => [ "spec:not_cli", "spec:cli" ]
5
20
 
6
21
  task :default => :spec
@@ -10,14 +10,14 @@ require_relative "check_please/cli"
10
10
  module CheckPlease
11
11
  ELEVATOR_PITCH = "Tool for parsing and diffing two JSON documents."
12
12
 
13
- def self.diff(reference, candidate)
13
+ def self.diff(reference, candidate, options = {})
14
14
  reference = maybe_parse(reference)
15
15
  candidate = maybe_parse(candidate)
16
- Comparison.perform(reference, candidate)
16
+ Comparison.perform(reference, candidate, options)
17
17
  end
18
18
 
19
19
  def self.render_diff(reference, candidate, options = {})
20
- diffs = diff(reference, candidate)
20
+ diffs = diff(reference, candidate, options)
21
21
  Printers.render(diffs, options)
22
22
  end
23
23
 
@@ -13,17 +13,33 @@ module CheckPlease
13
13
 
14
14
 
15
15
  FLAGS = []
16
- def self.flag(*args, &block)
17
- flag = Flag.new(*args, &block)
16
+ def self.flag(long:, short: nil, &block)
17
+ flag = Flag.new(short, long, &block)
18
18
  FLAGS << flag
19
19
  end
20
20
 
21
21
  ##### Define CLI flags here #####
22
22
 
23
- flag "-f FORMAT", "--format FORMAT" do |f|
23
+ flag short: "-f FORMAT", long: "--format FORMAT" do |f|
24
24
  f.desc = "format in which to present diffs (available options: [#{CheckPlease::Printers::FORMATS.join(", ")}])"
25
25
  f.set_key :format, :to_sym
26
26
  end
27
+
28
+ flag short: "-n MAX_DIFFS", long: "--max-diffs MAX_DIFFS" do |f|
29
+ f.desc = "Stop after encountering a specified number of diffs"
30
+ f.set_key :max_diffs, :to_i
31
+ end
32
+
33
+ flag long: "--fail-fast" do |f|
34
+ f.desc = "Stop after encountering the very first diff"
35
+ f.set_key(:max_diffs) { 1 }
36
+ end
37
+
38
+ flag short: "-d MAX_DEPTH", long: "--max-depth MAX_DEPTH" do |f|
39
+ f.desc = "Limit the number of levels to descend when comparing documents (NOTE: root has depth=1)"
40
+ f.set_key :max_depth, :to_i
41
+ end
42
+
27
43
  end
28
44
 
29
45
  end
@@ -12,6 +12,7 @@ module CLI
12
12
  yield self if block_given?
13
13
 
14
14
  missing = ATTR_NAMES.select { |e| self.send(e).nil? }
15
+ missing -= %i[ short ] # short is optional!
15
16
  if missing.any?
16
17
  raise ArgumentError, "Missing attributes: #{missing.join(', ')}"
17
18
  end
@@ -3,16 +3,22 @@ module CheckPlease
3
3
  module Comparison
4
4
  extend self
5
5
 
6
- def perform(reference, candidate)
6
+ def perform(reference, candidate, options = {})
7
7
  root = CheckPlease::Path.new
8
- diffs = Diffs.new
9
- compare reference, candidate, root, diffs
8
+ diffs = Diffs.new(options: options)
9
+ catch(:max_diffs_reached) do
10
+ compare reference, candidate, root, diffs
11
+ end
10
12
  diffs
11
13
  end
12
14
 
13
15
  private
14
16
 
15
17
  def compare(ref, can, path, diffs)
18
+ if (d = diffs.options[:max_depth])
19
+ return if path.depth > d
20
+ end
21
+
16
22
  case types(ref, can)
17
23
  when [ :array, :array ] ; compare_arrays ref, can, path, diffs
18
24
  when [ :hash, :hash ] ; compare_hashes ref, can, path, diffs
@@ -5,7 +5,9 @@ module CheckPlease
5
5
  # Custom collection class for Diff instances.
6
6
  # Can retrieve members using indexes or paths.
7
7
  class Diffs
8
- def initialize(diff_list = nil)
8
+ attr_reader :options
9
+ def initialize(diff_list = nil, options: {})
10
+ @options = options
9
11
  @list = []
10
12
  @hash = {}
11
13
  Array(diff_list).each do |diff|
@@ -27,6 +29,11 @@ module CheckPlease
27
29
  end
28
30
 
29
31
  def <<(diff)
32
+ if (n = options[:max_diffs])
33
+ # It seems no one can help me now / I'm in too deep, there's no way out
34
+ throw :max_diffs_reached if length >= n
35
+ end
36
+
30
37
  @list << diff
31
38
  @hash[diff.path] = diff
32
39
  end
@@ -11,6 +11,10 @@ module CheckPlease
11
11
  self.class.new( Array(@segments) + Array(new_basename.to_s) )
12
12
  end
13
13
 
14
+ def depth
15
+ 1 + @segments.length
16
+ end
17
+
14
18
  def to_s
15
19
  SEPARATOR + @segments.join(SEPARATOR)
16
20
  end
@@ -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.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
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.2.0
4
+ version: 0.2.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-13 00:00:00.000000000 Z
11
+ date: 2020-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print