Skip to content

Commit

Permalink
Added a simple fix for cases where Saikuro results with nested inform…
Browse files Browse the repository at this point in the history
…ation

cause metrics:all to crash

Signed-off-by: Jake Scruggs <jake.scruggs@gmail.com>
  • Loading branch information
Randy Souza authored and jscruggs committed May 21, 2009
1 parent 4384319 commit d6af508
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
41 changes: 23 additions & 18 deletions lib/generators/saikuro.rb
Expand Up @@ -4,24 +4,24 @@ class Saikuro < Generator


def emit
relative_path = [File.dirname(__FILE__), '..', '..',
relative_path = [File.dirname(__FILE__), '..', '..',
'vendor', 'saikuro', 'saikuro.rb']
saikuro = File.expand_path(File.join(relative_path))

format_directories

options_string = MetricFu.saikuro.inject("") do |options, option|
options + "--#{option.join(' ')} "
options + "--#{option.join(' ')} "
end

sh %{ruby "#{saikuro}" #{options_string}} do |ok, response|
unless ok
puts "Saikuro failed with exit status: #{response.exitstatus}"
exit 1
end
end
end

def format_directories
dirs = MetricFu.saikuro[:input_directory].join(" | ")
dirs = "\"#{dirs}\""
Expand All @@ -32,9 +32,9 @@ def analyze
@files = []
saikuro_results.each do |path|
if Saikuro::SFile.is_valid_text_file?(path)
file = Saikuro::SFile.new(path)
if file
@files << file
file = Saikuro::SFile.new(path)
if file
@files << file
end
end
end
Expand All @@ -50,7 +50,7 @@ def analyze
@classes = klasses.sort_by {|k| k.complexity.to_i}
@classes.reverse!
meths = []
@files.each {|f|
@files.each {|f|
f.elements.each {|el|
el.defs.each {|defn|
defn.name = "#{el.name}##{defn.name}"
Expand All @@ -59,20 +59,20 @@ def analyze
}
meths = meths.sort_by {|meth| meth.complexity.to_i}
@meths = meths.reverse

end

def to_h
files = @files.map do |file|
my_file = file.to_h
my_file[:filename] = file.filename
my_file
my_file
end
{:saikuro => {:files => files,
{:saikuro => {:files => files,
:classes => @classes.map {|c| c.to_h},
:methods => @meths.map {|m| m.to_h}
:methods => @meths.map {|m| m.to_h}
}
}
}
end

def saikuro_results
Expand All @@ -87,7 +87,7 @@ def saikuro_results
class Saikuro::SFile

attr_reader :elements

def initialize(path)
@path = path
@file_handle = File.open(@path, "r")
Expand Down Expand Up @@ -117,11 +117,16 @@ def to_h
def get_elements
begin
while ( line = @file_handle.readline) do
element ||= nil
if line.match /START/
unless element.nil?
@elements << element
element = nil
end
line = @file_handle.readline
element = Saikuro::ParsingElement.new(line)
elsif line.match /END/
@elements << element
@elements << element unless element.nil?
element = nil
else
element << line
Expand Down Expand Up @@ -190,7 +195,7 @@ def initialize(line)
end

def <<(line)
@defs << Saikuro::ParsingElement.new(line)
@defs << Saikuro::ParsingElement.new(line)
end

def to_h
Expand Down
30 changes: 20 additions & 10 deletions spec/generators/saikuro_spec.rb
Expand Up @@ -10,44 +10,54 @@
saikuro.analyze
@output = saikuro.to_h
end

it "should find the filename of a file" do
@output[:saikuro][:files].first[:filename].should == 'users_controller.rb'
end

it "should find the name of the classes" do
@output[:saikuro][:classes].first[:name].should == "UsersController"
@output[:saikuro][:classes][1][:name].should == "SessionsController"
end

it "should put the most complex method first" do
@output[:saikuro][:methods].first[:name].should == "UsersController#create"
@output[:saikuro][:methods].first[:complexity].should == 4
end

it "should find the complexity of a method" do
@output[:saikuro][:methods].first[:complexity].should == 4
end

it "should find the lines of a method" do
@output[:saikuro][:methods].first[:lines].should == 15
end
end

describe "emit method" do
it "should format the directories" do
MetricFu::Configuration.run {}
File.stub!(:directory?).and_return(true)
saikuro = MetricFu::Saikuro.new

MetricFu.saikuro[:input_directory] = ["app", "lib"]

File.stub!(:dirname).and_return('..')
File.stub!(:expand_path)

saikuro.should_receive(:sh).with(/"app \| lib"/)

saikuro.emit
end
end

describe Saikuro::SFile do
describe "getting elements from a Saikuro result file" do
it "should parse nested START/END sections" do
path = File.join(File.dirname(__FILE__), "..", "resources", "saikuro_sfiles", "thing.rb_cyclo.html")
sfile = Saikuro::SFile.new path
sfile.elements.map { |e| e.complexity }.sort.should eql(["0","0","2"])
end
end
end
end
10 changes: 10 additions & 0 deletions spec/resources/saikuro_sfiles/thing.rb_cyclo.html
@@ -0,0 +1,10 @@
-- START Mod --
Type:Module Name:Mod Complexity:2 Lines:7
Type:Def Name:self.included Complexity:2 Lines:5
-- START --
Type:Class Name: Complexity:0 Lines:1
-- END --
-- END Mod --
-- START Thing --
Type:Class Name:Thing Complexity:0 Lines:2
-- END Thing --

0 comments on commit d6af508

Please sign in to comment.