diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0bb13ba9..31f3935f 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,18 +1,11 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2018-03-23 17:40:12 -0400 using RuboCop version 0.49.1. +# on 2018-10-01 12:52:09 +0200 using RuboCop version 0.49.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# Offense count: 3 -# Configuration parameters: Include. -# Include: **/Gemfile, **/gems.rb -Bundler/DuplicatedGem: - Exclude: - - 'Gemfile' - # Offense count: 1 # Cop supports --auto-correct. # Configuration parameters: Include, TreatCommentsAsGroupSeparators. @@ -58,7 +51,7 @@ Layout/EmptyLines: - 'lib/capistrano/datadog/v2.rb' - 'lib/dogapi/facade.rb' -# Offense count: 60 +# Offense count: 63 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles. # SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines @@ -210,21 +203,21 @@ Lint/UselessAssignment: # Offense count: 11 Metrics/AbcSize: - Max: 50 + Max: 51 -# Offense count: 15 +# Offense count: 18 # Configuration parameters: CountComments, ExcludedMethods. Metrics/BlockLength: Max: 96 -# Offense count: 1 +# Offense count: 2 Metrics/CyclomaticComplexity: - Max: 7 + Max: 8 # Offense count: 15 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 33 + Max: 34 # Offense count: 2 # Configuration parameters: CountKeywordArgs. @@ -233,7 +226,7 @@ Metrics/ParameterLists: # Offense count: 1 Metrics/PerceivedComplexity: - Max: 8 + Max: 9 # Offense count: 11 Style/AccessorMethodName: @@ -316,7 +309,7 @@ Style/ConditionalAssignment: Exclude: - 'lib/capistrano/datadog.rb' -# Offense count: 19 +# Offense count: 20 # Cop supports --auto-correct. Style/DefWithParentheses: Exclude: @@ -380,12 +373,11 @@ Style/HashSyntax: - 'lib/dogapi/v1/snapshot.rb' - 'lib/dogapi/v1/tag.rb' -# Offense count: 16 +# Offense count: 15 # Cop supports --auto-correct. # Configuration parameters: MaxLineLength. Style/IfUnlessModifier: Exclude: - - 'Gemfile' - 'lib/capistrano/datadog.rb' - 'lib/capistrano/datadog/v2.rb' - 'lib/capistrano/datadog/v3.rb' @@ -401,7 +393,7 @@ Style/LineEndConcatenation: Exclude: - 'dogapi.gemspec' -# Offense count: 14 +# Offense count: 15 # Cop supports --auto-correct. Style/MethodCallWithoutArgsParentheses: Exclude: @@ -410,13 +402,13 @@ Style/MethodCallWithoutArgsParentheses: - 'lib/dogapi/facade.rb' - 'lib/dogapi/v1/event.rb' -# Offense count: 5 +# Offense count: 4 # Cop supports --auto-correct. Style/MultilineIfThen: Exclude: - 'lib/capistrano/datadog.rb' -# Offense count: 20 +# Offense count: 21 # Cop supports --auto-correct. Style/MutableConstant: Enabled: false @@ -478,6 +470,12 @@ Style/RedundantSelf: - 'lib/dogapi/facade.rb' - 'lib/dogapi/v1/metric.rb' +# Offense count: 1 +# Cop supports --auto-correct. +Style/RescueModifier: + Exclude: + - 'lib/capistrano/datadog.rb' + # Offense count: 2 # Cop supports --auto-correct. Style/SelfAssignment: @@ -493,7 +491,7 @@ Style/SpecialGlobalVars: Exclude: - 'dogapi.gemspec' -# Offense count: 23 +# Offense count: 22 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline. # SupportedStyles: single_quotes, double_quotes @@ -520,13 +518,12 @@ Style/TrailingCommaInLiteral: - 'lib/dogapi/v1/snapshot.rb' - 'lib/dogapi/v1/user.rb' -# Offense count: 3 +# Offense count: 2 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles, MinSize, WordRegex. # SupportedStyles: percent, brackets Style/WordArray: Exclude: - - 'Rakefile' - 'examples/custom_event.rb' # Offense count: 1 diff --git a/Rakefile b/Rakefile index 8ce7c50e..a20bf448 100644 --- a/Rakefile +++ b/Rakefile @@ -24,3 +24,7 @@ end task :rubocop do sh "rubocop" end + +task :rubocop_todo do + sh "rubocop --auto-gen-config" +end diff --git a/lib/capistrano/datadog.rb b/lib/capistrano/datadog.rb index a6893462..9eff2010 100644 --- a/lib/capistrano/datadog.rb +++ b/lib/capistrano/datadog.rb @@ -13,7 +13,9 @@ def self.reporter() def self.cap_version() if @cap_version.nil? then - if Configuration.respond_to? :instance then + if !defined? Configuration + @cap_version = '' + elsif Configuration.respond_to? :instance @cap_version = :v2 else @cap_version = :v3 @@ -108,8 +110,9 @@ def report() if !task[:application].nil? and !task[:application].empty? then application = ' for ' + task[:application] end + timing = Float(task[:timing]).round(2) rescue 'n/a' title = "#{user}@#{hostname} ran #{name}#{application} on #{roles.join(', ')} "\ - "with capistrano in #{task[:timing].round(2)} secs" + "with capistrano in #{timing} secs" type = 'deploy' alert_type = 'success' source_type = 'capistrano' diff --git a/spec/unit/capistrano_spec.rb b/spec/unit/capistrano_spec.rb new file mode 100644 index 00000000..35ab9c95 --- /dev/null +++ b/spec/unit/capistrano_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' +require 'capistrano/datadog' +require 'benchmark' + +describe 'Capistrano' do + context 'Reporter' do + context 'report' do + before(:each) do + @task = { + name: 'ATask', + roles: [], + stage: 'AStage', + application: 'MyApp', + logs: [], + hosts: [] + } + @reporter = Capistrano::Datadog.reporter + end + + it 'normally works with float timing' do + @task[:timing] = 9.99999 + expected = @task[:timing].round(2) + @reporter.instance_variable_set(:@tasks, [@task]) + event = @reporter.report.first.first + expect(event.msg_title).to include("with capistrano in #{expected} secs") + end + + it 'handles non-numeric timing' do + @task[:timing] = nil + @reporter.instance_variable_set(:@tasks, [@task]) + event = @reporter.report.first.first + expect(event.msg_title).to include('with capistrano in n/a secs') + end + end + end +end