From 02b7fe9f5e86abf5142e4336556666eebe333205 Mon Sep 17 00:00:00 2001 From: "Francis T. O'Donovan" Date: Thu, 30 Nov 2017 21:20:58 -0500 Subject: [PATCH] Add rst-lint pre-commit hook Analyze reStructuredText files with `rst-lint`/`restructuredtext-lint`. --- config/default.yml | 7 ++++ lib/overcommit/hook/pre_commit/rst_lint.rb | 25 +++++++++++ .../hook/pre_commit/rst_lint_spec.rb | 41 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 lib/overcommit/hook/pre_commit/rst_lint.rb create mode 100644 spec/overcommit/hook/pre_commit/rst_lint_spec.rb diff --git a/config/default.yml b/config/default.yml index bac9a8a5..a65e8e65 100644 --- a/config/default.yml +++ b/config/default.yml @@ -566,6 +566,13 @@ PreCommit: - '**/Gemfile' - '**/Rakefile' + RstLint: + enabled: false + description: 'Analyze reStructuredText files with rst-lint' + required_executable: 'rst-lint' + install_command: 'pip install restructuredtext_lint' + include: '**/*.rst' + RuboCop: enabled: false description: 'Analyze with RuboCop' diff --git a/lib/overcommit/hook/pre_commit/rst_lint.rb b/lib/overcommit/hook/pre_commit/rst_lint.rb new file mode 100644 index 00000000..d85be659 --- /dev/null +++ b/lib/overcommit/hook/pre_commit/rst_lint.rb @@ -0,0 +1,25 @@ +module Overcommit::Hook::PreCommit + # Runs `rst-lint` against any modified reStructuredText files + # + # @see https://github.com/twolfson/restructuredtext-lint + class RstLint < Base + MESSAGE_REGEX = / + ^(?INFO|WARNING|ERROR|SEVERE)(?(?:\w:)?[^:]+):(?\d+)\s(?.+) + /x + + def run + result = execute(command, args: applicable_files) + output = result.stdout.chomp + + return :pass if result.success? + return [:fail, result.stderr] unless result.stderr.empty? + + # example message: + # WARNING README.rst:7 Title underline too short. + extract_messages( + output.split("\n"), + MESSAGE_REGEX + ) + end + end +end diff --git a/spec/overcommit/hook/pre_commit/rst_lint_spec.rb b/spec/overcommit/hook/pre_commit/rst_lint_spec.rb new file mode 100644 index 00000000..891247a6 --- /dev/null +++ b/spec/overcommit/hook/pre_commit/rst_lint_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe Overcommit::Hook::PreCommit::RstLint do + let(:config) { Overcommit::ConfigurationLoader.default_configuration } + let(:context) { double('context') } + subject { described_class.new(config, context) } + + let(:result) { double('result') } + + before do + result.stub(success?: success, stdout: stdout, stderr: stderr) + subject.stub(:applicable_files).and_return(%w[file1.rst file2.rst]) + subject.stub(:execute).and_return(result) + end + + context 'when rst-lint exits successfully' do + let(:success) { true } + let(:stdout) { '' } + let(:stderr) { '' } + + it { should pass } + end + + context 'when rst-lint exits unsuccessfully' do + let(:success) { false } + + context 'and it reports an error' do + let(:stdout) { 'WARNING file1.rst:7 Title underline too short.' } + let(:stderr) { '' } + + it { should fail_hook } + end + + context 'when there is an error running rst-lint' do + let(:stdout) { '' } + let(:stderr) { 'Some runtime error' } + + it { should fail_hook } + end + end +end