-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run churn metric as library #182
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,61 +6,35 @@ def self.metric | |
:churn | ||
end | ||
|
||
### | ||
# options available are what can be passed to churn_calculator | ||
# https://github.com/danmayer/churn#library-options | ||
### | ||
def emit | ||
@output = generate_churn_metrics | ||
@output = run(options) | ||
end | ||
|
||
def analyze | ||
if @output.nil? || @output.match(/Churning requires.*git/) | ||
if @output.nil? || @output.size.zero? | ||
@churn = {:churn => {}} | ||
else | ||
@churn = YAML::load(@output) | ||
@churn = @output | ||
end | ||
@churn | ||
end | ||
|
||
# ensure hash only has the :churn key | ||
def to_h | ||
{:churn => @churn[:churn]} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kind of silly, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, not part of this PR... just thoughts while reading the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this because the churn output can actually have other keys. At least in the past when churn had something like { That would cause parts of metric_fu to blow up. IF having other keys in the results no longer matters this could be removed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh.. worth a comment or link or something I guess There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it still hold true and don't really have anywhere to link to. I could remove it and see if things blow up again. |
||
end | ||
|
||
private | ||
|
||
def generate_churn_metrics | ||
output = churn_code | ||
ensure_output_is_valid_yaml(output) | ||
end | ||
|
||
def churn_code | ||
run!(build_churn_options) | ||
end | ||
|
||
def ensure_output_is_valid_yaml(output) | ||
yaml_start = output.index("---") | ||
if yaml_start | ||
output[yaml_start...output.length] | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def build_churn_options | ||
opts = ["--yaml"] | ||
churn_options.each do |churn_option, command_flag| | ||
if has_option?(churn_option) | ||
opts << "#{command_flag}=#{options[churn_option]}" | ||
end | ||
end | ||
opts.join(" ") | ||
end | ||
|
||
def has_option?(churn_option) | ||
options.include?(churn_option) | ||
end | ||
def churn_options | ||
{ | ||
:minimum_churn_count => '--minimum_churn_count', | ||
:start_date => '--start_date' | ||
} | ||
|
||
# @param args [Hash] churn metric run options | ||
# @return [Hash] churn results | ||
# @example {something} | ||
def run(args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. # @param args [Hash] churn metric run options
# @return [Hash] churn results
# @example {something} |
||
# @note passing in false to report will return a hash | ||
# instead of the default String | ||
::Churn::ChurnCalculator.new(args).report(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps return an empty hash if the calculator returns nil so we don't need the nil check above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change how churn returns for a future version but need to check for current version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's discuss sometime, after this PR... My original thought was that the actual command for running the metric should be in MetricChurn (but this isn't fully-baked, right now.. except for run_external) |
||
end | ||
|
||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,12 @@ def name | |
end | ||
|
||
def default_run_options | ||
{ :start_date => %q("1 year ago"), :minimum_churn_count => 10} | ||
{ | ||
:start_date => %q("1 year ago"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this any date parseable by Chronic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that is correct. What kind of date parsing is metric_fu mostly settled on. I was considering a few fixes and moving churn to 1.0.0 and as part of that dropping chronic for a simpler more standardized date format. Mostly to reduce dependancies and complexity. If you think metric_fu is moving towards chronic like date usage, I am happy to keep it. If not it would be awesome to move towards something standard like unix date
Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whatever you decide is fine. I can see both sides. MetricFu doesn't use Chronic at this time and I haven't found a use-case for it yet. I really like Chronic. |
||
:minimum_churn_count => 10, | ||
:ignore_files => [], | ||
:data_directory => MetricFu::Io::FileSystem.scratch_directory(name) | ||
} | ||
end | ||
|
||
def has_graph? | ||
|
@@ -18,6 +23,7 @@ def enable | |
end | ||
|
||
def activate | ||
activate_library('churn/churn_calculator') | ||
super | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,43 +4,19 @@ | |
describe MetricFu::ChurnGenerator do | ||
|
||
# TODO extract yaml | ||
let(:churn_yaml) { "--- \n:churn: \n :changed_files: \n - spec/graphs/flog_grapher_spec.rb\n - spec/base/graph_spec.rb\n - lib/templates/awesome/layout.html.erb\n - lib/graphs/rcov_grapher.rb\n - lib/base/base_template.rb\n - spec/graphs/grapher_spec.rb\n - lib/templates/awesome/flog.html.erb\n - lib/templates/awesome/flay.html.erb\n - lib/graphs/roodi_grapher.rb\n - lib/graphs/reek_grapher.rb\n - HISTORY\n - spec/graphs/roodi_grapher_spec.rb\n - lib/generators/rcov.rb\n - spec/graphs/engines/gchart_spec.rb\n - spec/graphs/rcov_grapher_spec.rb\n - lib/templates/javascripts/excanvas.js\n - lib/templates/javascripts/bluff-min.js\n - spec/graphs/reek_grapher_spec.rb\n" } | ||
let(:churn_hash) { YAML::load("--- \n:churn: \n :changed_files: \n - spec/graphs/flog_grapher_spec.rb\n - spec/base/graph_spec.rb\n - lib/templates/awesome/layout.html.erb\n - lib/graphs/rcov_grapher.rb\n - lib/base/base_template.rb\n - spec/graphs/grapher_spec.rb\n - lib/templates/awesome/flog.html.erb\n - lib/templates/awesome/flay.html.erb\n - lib/graphs/roodi_grapher.rb\n - lib/graphs/reek_grapher.rb\n - HISTORY\n - spec/graphs/roodi_grapher_spec.rb\n - lib/generators/rcov.rb\n - spec/graphs/engines/gchart_spec.rb\n - spec/graphs/rcov_grapher_spec.rb\n - lib/templates/javascripts/excanvas.js\n - lib/templates/javascripts/bluff-min.js\n - spec/graphs/reek_grapher_spec.rb\n") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be great to put this yaml in a file fixture in spec/fixtures |
||
|
||
let(:config_setup) { | ||
ENV['CC_BUILD_ARTIFACTS'] = nil | ||
MetricFu.configure.reset | ||
} | ||
|
||
describe "new method" do | ||
before :each do | ||
config_setup | ||
end | ||
|
||
it "initializes with yaml option" do | ||
churn = MetricFu::ChurnGenerator.new | ||
churn.send(:build_churn_options).should == "--yaml" | ||
end | ||
|
||
it "initializes with given minimum_churn_count option" do | ||
churn = MetricFu::ChurnGenerator.new( { :minimum_churn_count => 5 }) | ||
churn.send(:build_churn_options).should == "--yaml --minimum_churn_count=5" | ||
end | ||
end | ||
|
||
|
||
describe "analyze method" do | ||
before :each do | ||
config_setup | ||
@changes = {"lib/generators/flog.rb" => 2, "lib/metric_fu.rb" => 3} | ||
end | ||
|
||
it "should be empty on error text" do | ||
churn = MetricFu::ChurnGenerator.new | ||
churn.instance_variable_set(:@output, "Churning requires a subversion or git repo") | ||
result = churn.analyze | ||
result.should == {:churn => {}} | ||
end | ||
|
||
it "should be empty on error no output captured" do | ||
churn = MetricFu::ChurnGenerator.new | ||
churn.instance_variable_set(:@output, nil) | ||
|
@@ -50,7 +26,7 @@ | |
|
||
it "should return yaml results" do | ||
churn = MetricFu::ChurnGenerator.new | ||
churn.instance_variable_set(:@output, churn_yaml) | ||
churn.instance_variable_set(:@output, churn_hash) | ||
result = churn.analyze | ||
result.should == {:churn => {:changed_files => ["spec/graphs/flog_grapher_spec.rb", "spec/base/graph_spec.rb", "lib/templates/awesome/layout.html.erb", "lib/graphs/rcov_grapher.rb", "lib/base/base_template.rb", "spec/graphs/grapher_spec.rb", "lib/templates/awesome/flog.html.erb", "lib/templates/awesome/flay.html.erb", "lib/graphs/roodi_grapher.rb", "lib/graphs/reek_grapher.rb", "HISTORY", "spec/graphs/roodi_grapher_spec.rb", "lib/generators/rcov.rb", "spec/graphs/engines/gchart_spec.rb", "spec/graphs/rcov_grapher_spec.rb", "lib/templates/javascripts/excanvas.js", "lib/templates/javascripts/bluff-min.js", "spec/graphs/reek_grapher_spec.rb"]}} | ||
end | ||
|
@@ -78,13 +54,13 @@ | |
end | ||
|
||
it "returns churn output" do | ||
@churn.stub(:churn_code).and_return(" master\n#{churn_yaml}") | ||
@churn.stub(:run).and_return(churn_hash) | ||
result = @churn.emit | ||
result.should == churn_yaml | ||
result.should == churn_hash | ||
end | ||
|
||
it "returns nil, when churn result is not yaml" do | ||
@churn.stub(:churn_code).and_return(" master\n") | ||
@churn.stub(:run).and_return(nil) | ||
result = @churn.emit | ||
result.should be nil | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,18 @@ def data_directory | |
# limited set, so we can test the basic functionality | ||
# without significantly slowing down the specs. | ||
MetricFu.configuration.configure_metrics do |metric| | ||
if metric.name == :churn | ||
if metric.name == :reek | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha ha ha! |
||
metric.enable | ||
metric.activated = true | ||
metric.should_receive(:run_external).and_return('') | ||
# so this doesn't seem to always be true | ||
# @bf4 adding that line you thought I didn't need made tests pass for me | ||
# but they failed on one of the travis builds. | ||
# swapping this line all seem to pass every run | ||
# I don't understand what is happening in run_external well enough | ||
# need some help debugging | ||
# but tests seem more stable without it. | ||
# metric.should_receive(:run_external).and_return('') | ||
metric.stub(:run_external).and_return('') | ||
else | ||
metric.enabled = false | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below re: output of run always being a hash