Module: Saddler::Reporter::Support

Defined in:
lib/saddler/reporter/support.rb,
lib/saddler/reporter/support/version.rb

Overview

Interface for saddler-reporter. And utilities for saddler-reporter

Constant Summary collapse

VERSION =
'0.1.1'

Instance Method Summary collapse

Instance Method Details

#file_relative_path_string(file_name) ⇒ String

Returns file’s relative path string from current.

Examples:

absolute path

Dir.pwd #=> '/foo/bar'
file_relative_path_string('/foo/bar/baz') #=> 'baz'

relative path

file_relative_path_string('./bar') #=> 'bar'

Parameters:

  • file_name (String, #to_str)

    file path

Returns:

  • (String)

    file’s relative path string from current



181
182
183
184
185
186
187
# File 'lib/saddler/reporter/support.rb', line 181

def file_relative_path_string(file_name)
  if Pathname.new(file_name).absolute?
    Pathname.new(file_name).relative_path_from(Pathname.new(Dir.pwd)).to_s
  else
    Pathname.new(file_name).relative_path_from(Pathname.new('.')).to_s
  end
end

#initialize(output) ⇒ Object

Interface for saddler-reporter

Parameters:

  • output (Object)

    output interface (defaults to: $stdout). Use StringIO for testing.



13
14
15
# File 'lib/saddler/reporter/support.rb', line 13

def initialize(output)
  @output = output
end

#parse(xml) ⇒ Object

Parser from xml to Plain Old Ruby Object with Nori

Examples:

one file no error

xml = "<?xml version='1.0' encoding='UTF-8'?>\n<checkstyle version='4.3'>\n  <file name='/home/travis/build/jser/jser.github.io/_i18n/ja/_posts/2015/2015-09-16-react-0.14-eslint-1.4-es5.md'/>\n</checkstyle>\n"

parse(xml)
#=>
{
  'checkstyle' => {
    'file' => {
      '@name' => '/home/travis/build/jser/jser.github.io/_i18n/ja/_posts/2015/2015-09-16-react-0.14-eslint-1.4-es5.md'
    },
    '@version' => '4.3'
  }
}

one foile one error

xml = "<?xml version='1.0'?>\n<checkstyle>\n  <file name='lib/example/travis_ci.rb'>\n    <error column='120' line='7' message='Line is too long. [164/120]' severity='info' source='com.puppycrawl.tools.checkstyle.Metrics/LineLength'/>\n  </file>\n</checkstyle>\n"

parse(xml)
#=>
{
  'checkstyle' => {
    'file' => {
      'error' => {
        '@column' => '120',
        '@line' => '7',
        '@message' => 'Line is too long. [164/120]',
        '@severity' => 'info',
        '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/LineLength'
      },
      '@name' => 'lib/example/travis_ci.rb'
    }
  }
}

two files multi errors

xml = "<?xml version='1.0'?>\n<checkstyle>\n  <file name='example/invalid.rb'>\n    <error line='3' column='100' severity='info' message='Line is too long. [187/100]' source='com.puppycrawl.tools.checkstyle.Metrics/LineLength'/>\n  </file>\n  <file name='lib/checkstyle_filter/git/cli.rb'>\n    <error line='14' column='6' severity='info' message='Assignment Branch Condition size for diff is too high. [38.21/15]' source='com.puppycrawl.tools.checkstyle.Metrics/AbcSize'/>\n    <error line='14' column='6' severity='info' message='Cyclomatic complexity for diff is too high. [9/6]' source='com.puppycrawl.tools.checkstyle.Metrics/CyclomaticComplexity'/>\n    <error line='14' column='6' severity='info' message='Method has too many lines. [26/10]' source='com.puppycrawl.tools.checkstyle.Metrics/MethodLength'/>\n    <error line='14' column='6' severity='info' message='Perceived complexity for diff is too high. [10/7]' source='com.puppycrawl.tools.checkstyle.Metrics/PerceivedComplexity'/>\n    <error line='65' column='8' severity='info' message='Assignment Branch Condition size for file_element_error_line_no_in_modified? is too high. [16.16/15]' source='com.puppycrawl.tools.checkstyle.Metrics/AbcSize'/>\n    <error line='65' column='8' severity='info' message='Method has too many lines. [14/10]' source='com.puppycrawl.tools.checkstyle.Metrics/MethodLength'/>\n  </file>\n</checkstyle>\n"

parse(xml)
#=>
{
  'checkstyle' => {
    'file' => [
      {
        'error' => {
          '@line' => '3',
          '@column' => '100',
          '@severity' => 'info',
          '@message' => 'Line is too long. [187/100]',
          '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/LineLength'
        },
        '@name' => 'example/invalid.rb'
      },
      {
        'error' => [
          {
            '@line' => '14',
            '@column' => '6',
            '@severity' => 'info',
            '@message' => 'Assignment Branch Condition size for diff is too high. [38.21/15]',
            '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/AbcSize'
          },
          {
            '@line' => '14',
            '@column' => '6',
            '@severity' => 'info',
            '@message' => 'Cyclomatic complexity for diff is too high. [9/6]',
            '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/CyclomaticComplexity'
          },
          {
            '@line' => '14',
            '@column' => '6',
            '@severity' => 'info',
            '@message' => 'Method has too many lines. [26/10]',
            '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/MethodLength'
          },
          {
            '@line' => '14',
            '@column' => '6',
            '@severity' => 'info',
            '@message' => 'Perceived complexity for diff is too high. [10/7]',
            '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/PerceivedComplexity'
          },
          {
            '@line' => '65',
            '@column' => '8',
            '@severity' => 'info',
            '@message' => 'Assignment Branch Condition size for file_element_error_line_no_in_modified? is too high. [16.16/15]',
            '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/AbcSize'
          },
          {
            '@line' => '65',
            '@column' => '8',
            '@severity' => 'info',
            '@message' => 'Method has too many lines. [14/10]',
            '@source' => 'com.puppycrawl.tools.checkstyle.Metrics/MethodLength'
          }
        ],
        '@name' => 'lib/checkstyle_filter/git/cli.rb'
      }
    ]
  }
}

Parameters:

  • xml (String)

    xml string

Returns:

  • (Object)

    parsed

See Also:



165
166
167
168
169
# File 'lib/saddler/reporter/support.rb', line 165

def parse(xml)
  Nori
    .new(parser: :rexml)
    .parse(xml)
end

#report(messages, options) ⇒ void

This method returns an undefined value.

Interface for saddler-reporter

Parameters:

  • messages (String)

    reporting message. checkstyle format.

  • options (Hash)

    options from saddler cli.



23
24
25
# File 'lib/saddler/reporter/support.rb', line 23

def report(messages, options) # rubocop:disable Style/UnusedMethodArgument
  @output.puts messages
end