Skip to content
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

Resolve #389: Add line numbers to recog_verify output #390

Merged
merged 4 commits into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion Rakefile
Expand Up @@ -14,8 +14,12 @@ end
require 'cucumber'
require 'cucumber/rake/task'

def jruby?
defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
end

Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format pretty"
t.cucumber_opts = "features --format pretty" + (jruby? ? " --tags 'not @jruby-disabled'" : "")
end

task :default => [ :tests, :yard ]
Expand Down
18 changes: 12 additions & 6 deletions features/verify.feature
Expand Up @@ -15,13 +15,16 @@ Feature: Verify
successful_tests.xml: SUMMARY: Test completed with 4 successful, 0 warnings, and 0 failures
"""

# This test is diabled in JRuby because JRuby does not number lines correctly, whereas
# CRuby does. See https://github.com/sparklemotion/nokogiri/issues/2380
@no-clobber
@jruby-disabled
Scenario: Tests with warnings, warnings enabled
When I run `recog_verify tests_with_warnings.xml`
Then it should fail with:
"""
tests_with_warnings.xml: WARN: 'Pure-FTPd' has no test cases
tests_with_warnings.xml: WARN: 'Pure-FTPd' is missing an example that checks for parameter 'pureftpd.config' which is derived from a capture group
tests_with_warnings.xml:10: WARN: 'Pure-FTPd' has no test cases
tests_with_warnings.xml:10: WARN: 'Pure-FTPd' is missing an example that checks for parameter 'pureftpd.config' which is derived from a capture group
tests_with_warnings.xml: SUMMARY: Test completed with 1 successful, 2 warnings, and 0 failures
"""
And the exit status should be 2
Expand All @@ -34,15 +37,18 @@ Feature: Verify
tests_with_warnings.xml: SUMMARY: Test completed with 1 successful, 0 warnings, and 0 failures
"""

# This test is diabled in JRuby because JRuby does not number lines correctly, whereas
# CRuby does. See https://github.com/sparklemotion/nokogiri/issues/2380
@no-clobber
@jruby-disabled
Scenario: Tests with failures
When I run `recog_verify tests_with_failures.xml`
Then it should fail with:
"""
tests_with_failures.xml: FAIL: 'foo test' failed to match "bar" with (?-mix:^foo$)'
tests_with_failures.xml: FAIL: '' failed to match "This almost matches" with (?-mix:^This matches$)'
tests_with_failures.xml: FAIL: 'bar test's os.name is a non-zero pos but specifies a value of 'Bar'
tests_with_failures.xml: FAIL: 'bar test' failed to find expected capture group os.version '5.0'. Result was 1.0
tests_with_failures.xml:3: FAIL: 'foo test' failed to match "bar" with (?-mix:^foo$)'
tests_with_failures.xml:8: FAIL: '' failed to match "This almost matches" with (?-mix:^This matches$)'
tests_with_failures.xml:13: FAIL: 'bar test's os.name is a non-zero pos but specifies a value of 'Bar'
tests_with_failures.xml:13: FAIL: 'bar test' failed to find expected capture group os.version '5.0'. Result was 1.0
tests_with_failures.xml: SUMMARY: Test completed with 0 successful, 0 warnings, and 4 failures
"""
And the exit status should be 4
7 changes: 7 additions & 0 deletions lib/recog/fingerprint.rb
Expand Up @@ -28,6 +28,12 @@ class Fingerprint
# @return (see #parse_examples)
attr_reader :tests

# The line number of the XML entity in the source file for this
# fingerprint.
#
# @return [Integer] The line number of this entity.
attr_reader :line

# @param xml [Nokogiri::XML::Element]
# @param match_key [String] See Recog::DB
# @param protocol [String] Protocol such as ftp, mssql, http, etc.
Expand All @@ -37,6 +43,7 @@ def initialize(xml, match_key=nil, protocol=nil, filepath=nil)
@protocol = protocol
@name = parse_description(xml)
@regex = create_regexp(xml)
@line = xml.line
@params = {}
@tests = []

Expand Down
8 changes: 4 additions & 4 deletions lib/recog/verifier.rb
Expand Up @@ -15,19 +15,19 @@ def verify
fp.verify_params do |status, message|
case status
when :warn
reporter.warning "WARN: #{message}"
reporter.warning "WARN: #{message}", fp.line
when :fail
reporter.failure "FAIL: #{message}"
reporter.failure "FAIL: #{message}", fp.line
when :success
reporter.success(message)
end
end
fp.verify_tests do |status, message|
case status
when :warn
reporter.warning "WARN: #{message}"
reporter.warning "WARN: #{message}", fp.line
when :fail
reporter.failure "FAIL: #{message}"
reporter.failure "FAIL: #{message}", fp.line
when :success
reporter.success(message)
end
Expand Down
13 changes: 7 additions & 6 deletions lib/recog/verify_reporter.rb
Expand Up @@ -24,15 +24,15 @@ def success(text)
formatter.success_message("#{padding}#{text}") if detail?
end

def warning(text)
def warning(text, line=nil)
return unless @options.warnings
@warning_count += 1
formatter.warning_message("#{path_label}#{padding}#{text}")
formatter.warning_message("#{path_label(line)}#{padding}#{text}")
end

def failure(text)
def failure(text, line=nil)
@failure_count += 1
formatter.failure_message("#{path_label}#{padding}#{text}")
formatter.failure_message("#{path_label(line)}#{padding}#{text}")
end

def print_name(fingerprint)
Expand Down Expand Up @@ -65,9 +65,10 @@ def detail?
@options.detail
end

def path_label
def path_label(line=nil)
unless detail?
@path.to_s.empty? ? "" : "#{@path}: "
line_label = line ? line.to_s + ":" : ""
@path.to_s.empty? ? "" : "#{@path}:#{line_label} "
end
end

Expand Down