Permalink
Browse files

Add --query and --no-modified. Diff now shows modified objects and al…

…lows queries
  • Loading branch information...
1 parent 2493419 commit 0b7f5def088a6553d61bd3948e3fdb95e6685991 @lsegal committed Jul 9, 2012
Showing with 120 additions and 45 deletions.
  1. +25 −9 lib/yard/cli/diff.rb
  2. +95 −36 spec/cli/diff_spec.rb
View
@@ -13,6 +13,8 @@ def initialize
@list_all = false
@use_git = false
@compact = false
+ @modified = true
+ @verifier = Verifier.new
@old_git_commit = nil
@old_path = Dir.pwd
log.show_backtraces = true
@@ -26,11 +28,11 @@ def run(*args)
registry = optparse(*args).map do |gemfile|
if @use_git
load_git_commit(gemfile)
- Registry.all.map {|o| o.path }
+ all_objects
else
if load_gem_data(gemfile)
log.info "Found #{gemfile}"
- Registry.all.map {|o| o.path }
+ all_objects
else
log.error "Cannot find gem #{gemfile}"
nil
@@ -41,14 +43,16 @@ def run(*args)
return if registry.size != 2
first_object = nil
- [ ["Added objects", "A", registry[1] - registry[0]],
- ["Removed objects", "D", registry[0] - registry[1]]].each do |name, short, objects|
+ [ ["Added objects", "A", added_objects(*registry)],
+ ["Modified objects", "M", modified_objects(*registry)],
+ ["Removed objects", "D", removed_objects(*registry)]].each do |name, short, objects|
+ next if short == "M" && @modified == false
next if objects.empty?
last_object = nil
all_objects_notice = false
puts name + ":" unless @compact
- objects.sort.each do |object|
- if !@list_all && last_object && object =~ /#{Regexp.quote last_object}(::|\.|#)/
+ objects.sort_by {|o| o.path }.each do |object|
+ if !@list_all && last_object && object.parent == last_object
print " (...)" unless all_objects_notice
all_objects_notice = true
next
@@ -58,7 +62,8 @@ def run(*args)
puts
end
all_objects_notice = false
- print (@compact ? "#{short} " : " ") + object
+ print (@compact ? "#{short} " : " ") +
+ object.path + " (#{object.file}:#{object.line})"
last_object = object
first_object = true
end
@@ -71,8 +76,13 @@ def run(*args)
private
+ def all_objects
+ return Registry.all if @verifier.expressions.empty?
+ @verifier.run(Registry.all)
+ end
+
def added_objects(registry1, registry2)
- registry2 - registry1
+ registry2.reject {|o| registry1.find {|o2| o2.path == o.path } }
end
def modified_objects(registry1, registry2)
@@ -87,7 +97,7 @@ def modified_objects(registry1, registry2)
end
def removed_objects(registry1, registry2)
- registry1 - registry2
+ registry1.reject {|o| registry2.find {|o2| o2.path == o.path } }
end
def load_git_commit(commit)
@@ -215,6 +225,12 @@ def optparse(*args)
opts.on('--git', 'Compare versions from two git commit/branches') do
@use_git = true
end
+ opts.on('--query QUERY', 'Only diff filtered objects') do |query|
+ @verifier.add_expressions(query)
+ end
+ opts.on('--no-modified', 'Ignore modified objects') do
+ @modified = false
+ end
common_options(opts)
parse_options(opts, args)
unless args.size == 2
View
@@ -29,10 +29,34 @@ def run(*args)
@objects1 ||= %w( C#fooey C#baz D.bar )
@objects2 ||= %w( A A::B A::B::C A.foo A#foo B C.foo C.bar C#baz )
@objects = [@objects1, @objects2]
- @diff.should_receive(:load_gem_data).ordered.with('gem1').and_return(true)
- @diff.should_receive(:load_gem_data).ordered.with('gem2').and_return(true)
- Registry.should_receive(:all).twice.and_return do
- @objects[@all_call += 1].map {|o| P(o) }
+ @diff.should_receive(:load_gem_data).ordered.with('gem1') do
+ Registry.clear
+ YARD.parse_string <<-eof
+ class C
+ def fooey; end
+ def baz; FOO end
+ end
+ class D
+ def self.bar; end
+ end
+ eof
+ end
+ @diff.should_receive(:load_gem_data).ordered.with('gem2') do
+ Registry.clear
+ YARD.parse_string <<-eof
+ module A
+ module B
+ class C; end
+ end
+ def self.foo; end
+ def foo; end
+ end
+ class C
+ def self.foo; end
+ def self.bar; end
+ def baz; BAR end
+ end
+ eof
end
@diff.stub!(:print) {|data| @data << data }
@diff.stub!(:puts) {|*args| @data << args.join("\n"); @data << "\n" }
@@ -44,28 +68,33 @@ def run(*args)
@data.string.should == <<-eof
Added objects:
- A (...)
- B
- C.bar
- C.foo
+ A ((stdin):1) (...)
+ A::B::C ((stdin):3)
+ C.bar ((stdin):10)
+ C.foo ((stdin):9)
+
+Modified objects:
+
+ C#baz ((stdin):3)
Removed objects:
- C#fooey
- D.bar
+ C#fooey ((stdin):2)
+ D ((stdin):5) (...)
eof
end
it "should accept --compact" do
run('--compact')
@data.string.should == <<-eof
-A A (...)
-A B
-A C.bar
-A C.foo
-D C#fooey
-D D.bar
+A A ((stdin):1) (...)
+A A::B::C ((stdin):3)
+A C.bar ((stdin):10)
+A C.foo ((stdin):9)
+M C#baz ((stdin):3)
+D C#fooey ((stdin):2)
+D D ((stdin):5) (...)
eof
end
@@ -75,19 +104,23 @@ def run(*args)
@data.string.should == <<-eof
Added objects:
- A
- A#foo
- A.foo
- A::B
- A::B::C
- B
- C.bar
- C.foo
+ A ((stdin):1)
+ A#foo ((stdin):6)
+ A.foo ((stdin):5)
+ A::B ((stdin):2)
+ A::B::C ((stdin):3)
+ C.bar ((stdin):10)
+ C.foo ((stdin):9)
+
+Modified objects:
+
+ C#baz ((stdin):3)
Removed objects:
- C#fooey
- D.bar
+ C#fooey ((stdin):2)
+ D ((stdin):5)
+ D.bar ((stdin):6)
eof
end
@@ -96,16 +129,42 @@ def run(*args)
it "should accept --compact and --all" do
run('--compact', '--all')
@data.string.should == <<-eof
-A A
-A A#foo
-A A.foo
-A A::B
-A A::B::C
-A B
-A C.bar
-A C.foo
-D C#fooey
-D D.bar
+A A ((stdin):1)
+A A#foo ((stdin):6)
+A A.foo ((stdin):5)
+A A::B ((stdin):2)
+A A::B::C ((stdin):3)
+A C.bar ((stdin):10)
+A C.foo ((stdin):9)
+M C#baz ((stdin):3)
+D C#fooey ((stdin):2)
+D D ((stdin):5)
+D D.bar ((stdin):6)
+eof
+ end
+
+ it "should accept --no-modified" do
+ run('--compact', '--no-modified')
+ @data.string.should == <<-eof
+A A ((stdin):1) (...)
+A A::B::C ((stdin):3)
+A C.bar ((stdin):10)
+A C.foo ((stdin):9)
+D C#fooey ((stdin):2)
+D D ((stdin):5) (...)
+eof
+ end
+
+ it "should accept --query" do
+ run('--compact', '--query', 'type == :method')
+ @data.string.should == <<-eof
+A A#foo ((stdin):6)
+A A.foo ((stdin):5)
+A C.bar ((stdin):10)
+A C.foo ((stdin):9)
+M C#baz ((stdin):3)
+D C#fooey ((stdin):2)
+D D.bar ((stdin):6)
eof
end
end

0 comments on commit 0b7f5de

Please sign in to comment.