-
Notifications
You must be signed in to change notification settings - Fork 448
Create doc checker system #1568
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
Open
st0012
wants to merge
1
commit into
master
Choose a base branch
from
feature/checker-warning-system
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| ## | ||
| # RDoc::Checker collects documentation quality check warnings during RDoc | ||
| # execution. Warnings are reported at the end and cause a non-zero exit status. | ||
| # | ||
| # Example usage: | ||
| # RDoc::Checker.add("Missing reference", file: "lib/foo.rb", line: 42) | ||
| # | ||
| # At the end of rdoc execution, if any warnings were added, they will be | ||
| # printed and the process will exit with status 1. | ||
|
|
||
| class RDoc::Checker | ||
|
|
||
| ## | ||
| # Represents a single check warning with optional file and line information. | ||
|
|
||
| class Warning | ||
| attr_reader :message, :file, :line | ||
|
|
||
| def initialize(message, file: nil, line: nil) | ||
| @message = message | ||
| @file = file | ||
| @line = line | ||
| end | ||
|
|
||
| def to_s | ||
| if file && line | ||
| "#{file}:#{line}: #{message}" | ||
| elsif file | ||
| "#{file}: #{message}" | ||
| else | ||
| message | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @warnings = [] | ||
|
|
||
| class << self | ||
| ## | ||
| # Add a warning to the checker. | ||
| # | ||
| # message - The warning message | ||
| # file - Optional file path where the issue was found | ||
| # line - Optional line number where the issue was found | ||
|
|
||
| def add(message, file: nil, line: nil) | ||
| @warnings << Warning.new(message, file: file, line: line) | ||
| end | ||
|
|
||
| ## | ||
| # Returns all collected warnings. | ||
|
|
||
| def warnings | ||
| @warnings | ||
| end | ||
|
|
||
| ## | ||
| # Clear all warnings. Used between test runs. | ||
|
|
||
| def clear | ||
| @warnings = [] | ||
| end | ||
|
|
||
| ## | ||
| # Returns true if any warnings have been collected. | ||
|
|
||
| def any? | ||
| @warnings.any? | ||
| end | ||
|
|
||
| ## | ||
| # Print all collected warnings to stdout. | ||
| # Returns early if no warnings exist. | ||
|
|
||
| def report | ||
| return if @warnings.empty? | ||
|
|
||
| puts | ||
| puts "Documentation check failures:" | ||
| @warnings.each do |warning| | ||
| puts " #{warning}" | ||
| end | ||
| puts | ||
| puts "#{@warnings.size} check(s) failed" | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'helper' | ||
|
|
||
| class RDocCheckerIntegrationTest < RDoc::TestCase | ||
| def test_document_reports_checker_warnings | ||
| RDoc::Checker.add("test warning", file: "test.rb") | ||
|
|
||
| out, _err = capture_output do | ||
| temp_dir do | ||
| File.write("test.rb", "# comment\nclass Foo; end") | ||
|
|
||
| assert_raise(SystemExit) do | ||
| @rdoc.document(["test.rb"]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| assert_match(/Documentation check failures:/, out) | ||
| assert_match(/test warning/, out) | ||
| end | ||
|
|
||
| def test_document_exits_with_status_1_when_warnings | ||
| RDoc::Checker.add("test warning") | ||
|
|
||
| temp_dir do | ||
| File.write("test.rb", "# comment\nclass Foo; end") | ||
|
|
||
| error = assert_raise(SystemExit) do | ||
| capture_output do | ||
| @rdoc.document(["test.rb"]) | ||
| end | ||
| end | ||
|
|
||
| assert_equal 1, error.status | ||
| end | ||
| end | ||
|
|
||
| def test_document_does_not_exit_when_no_warnings | ||
| temp_dir do | ||
| File.write("test.rb", "# comment\nclass Foo; end") | ||
|
|
||
| # Should not raise SystemExit | ||
| capture_output do | ||
| @rdoc.document(["test.rb"]) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def test_document_coverage_report_with_warnings_exits_failure | ||
| @options.coverage_report = true | ||
| RDoc::Checker.add("test warning") | ||
|
|
||
| temp_dir do | ||
| File.write("test.rb", "# Documented class\nclass Foo; end") | ||
|
|
||
| error = assert_raise(SystemExit) do | ||
| capture_output do | ||
| @rdoc.document(["test.rb"]) | ||
| end | ||
| end | ||
|
|
||
| # Even if fully documented, warnings should cause failure | ||
| assert_equal false, error.success? | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'helper' | ||
|
|
||
| class RDocCheckerTest < RDoc::TestCase | ||
| def setup | ||
| super | ||
| RDoc::Checker.clear | ||
| end | ||
|
|
||
| def test_warning_to_s_with_file_and_line | ||
| warning = RDoc::Checker::Warning.new("test message", file: "foo.rb", line: 42) | ||
| assert_equal "foo.rb:42: test message", warning.to_s | ||
| end | ||
|
|
||
| def test_warning_to_s_with_file_only | ||
| warning = RDoc::Checker::Warning.new("test message", file: "foo.rb") | ||
| assert_equal "foo.rb: test message", warning.to_s | ||
| end | ||
|
|
||
| def test_warning_to_s_with_message_only | ||
| warning = RDoc::Checker::Warning.new("test message") | ||
| assert_equal "test message", warning.to_s | ||
| end | ||
|
|
||
| def test_add_collects_warnings | ||
| RDoc::Checker.add("warning 1") | ||
| RDoc::Checker.add("warning 2", file: "bar.rb") | ||
|
|
||
| assert_equal 2, RDoc::Checker.warnings.size | ||
| assert_equal "warning 1", RDoc::Checker.warnings[0].message | ||
| assert_equal "bar.rb", RDoc::Checker.warnings[1].file | ||
| end | ||
|
|
||
| def test_any_returns_false_when_empty | ||
| refute RDoc::Checker.any? | ||
| end | ||
|
|
||
| def test_any_returns_true_when_warnings_exist | ||
| RDoc::Checker.add("a warning") | ||
| assert RDoc::Checker.any? | ||
| end | ||
|
|
||
| def test_clear_removes_all_warnings | ||
| RDoc::Checker.add("warning") | ||
| assert RDoc::Checker.any? | ||
|
|
||
| RDoc::Checker.clear | ||
| refute RDoc::Checker.any? | ||
| end | ||
|
|
||
| def test_report_outputs_nothing_when_empty | ||
| out, _err = capture_output do | ||
| RDoc::Checker.report | ||
| end | ||
|
|
||
| assert_empty out | ||
| end | ||
|
|
||
| def test_report_outputs_warnings_grouped | ||
| RDoc::Checker.add("first warning", file: "a.rb", line: 1) | ||
| RDoc::Checker.add("second warning", file: "b.rb") | ||
|
|
||
| out, _err = capture_output do | ||
| RDoc::Checker.report | ||
| end | ||
|
|
||
| assert_match(/Documentation check failures:/, out) | ||
| assert_match(/a\.rb:1: first warning/, out) | ||
| assert_match(/b\.rb: second warning/, out) | ||
| assert_match(/2 check\(s\) failed/, out) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ def setup | |
| @rdoc.generator = Object.new | ||
|
|
||
| RDoc::Markup::PreProcess.reset | ||
| RDoc::Checker.clear | ||
| end | ||
|
|
||
| ## | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If we don't instantiate this, we don't need to use
class.moduleis better.(And I think that we can improve name of this... This doesn't check anything. This just keeps reported warnings.)