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

Allow specifying excluded files across all checks #1438

Merged
merged 1 commit into from Aug 11, 2019
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
12 changes: 12 additions & 0 deletions nanoc-core/lib/nanoc/core/configuration-schema.json
Expand Up @@ -78,6 +78,18 @@
"checks": {
"type": "object",
"properties": {
"all": {
"type": "object",
"additionalProperties": false,
"properties": {
"exclude_files": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"internal_links": {
"type": "object",
"additionalProperties": false,
Expand Down
20 changes: 20 additions & 0 deletions nanoc-core/spec/nanoc/core/configuration_spec.rb
Expand Up @@ -647,6 +647,26 @@
end
end

context 'invalid checks (all has invalid type)' do
let(:hash) do
{ checks: { all: 123 } }
end

it 'passes' do
expect { subject }.to raise_error(JsonSchema::Error)
end
end

context 'invalid checks (all.exclude_files has invalid type)' do
let(:hash) do
{ checks: { all: { exclude_files: 'everything' } } }
end

it 'passes' do
expect { subject }.to raise_error(JsonSchema::Error)
end
end

context 'invalid checks (internal_links has invalid type)' do
let(:hash) do
{ checks: { internal_links: 123 } }
Expand Down
16 changes: 16 additions & 0 deletions nanoc/lib/nanoc/checking/check.rb
Expand Up @@ -12,6 +12,8 @@ def initialize(directory_path)
class Check < Nanoc::Core::Context
extend DDPlugin::Plugin

DDMemoize.activate(self)

attr_reader :issues

def self.define(ident, &block)
Expand Down Expand Up @@ -66,6 +68,20 @@ def add_issue(desc, subject: nil)
@issues << Issue.new(desc, subject, self.class)
end

# @private
def output_filenames
super.reject { |f| excluded_patterns.any? { |pat| pat.match?(f) } }
end

# @private
memoized def excluded_patterns
@config
.fetch(:checks, {})
.fetch(:all, {})
.fetch(:exclude_files, [])
.map { |pattern| Regexp.new(pattern) }
end

# @private
def output_html_filenames
output_filenames.select { |f| File.extname(f) =~ /\A\.x?html?\z/ }
Expand Down
116 changes: 109 additions & 7 deletions nanoc/spec/nanoc/checking/check_spec.rb
Expand Up @@ -54,11 +54,97 @@
end
end

describe '#output_filenames' do
subject { check.output_filenames }

let(:check) do
described_class.new(
output_filenames: output_filenames,
config: Nanoc::ConfigView.new(config, view_context),
)
end

let(:config) do
Nanoc::Core::Configuration.new(
dir: Dir.getwd,
hash: config_hash,
)
end

let(:config_hash) { {} }

let(:view_context) do
double(:view_context, dependency_tracker: dependency_tracker)
end

let(:dependency_tracker) do
double(:dependency_tracker).tap do |dt|
allow(dt).to receive(:bounce)
end
end

let(:output_filenames) do
[
'output/foo.html',
'output/foo.htm',
'output/foo.xhtml',
'output/foo.txt',
'output/foo.htmlx',
'output/foo.yhtml',
]
end

context 'when exclude_files is unset' do
it { is_expected.to include('output/foo.htm') }
it { is_expected.to include('output/foo.html') }
it { is_expected.to include('output/foo.htmlx') }
it { is_expected.to include('output/foo.txt') }
it { is_expected.to include('output/foo.xhtml') }
it { is_expected.to include('output/foo.yhtml') }
end

context 'when exclude_files is set' do
let(:config_hash) do
{ checks: { all: { exclude_files: ['foo.xhtml'] } } }
end

it { is_expected.to include('output/foo.htm') }
it { is_expected.to include('output/foo.html') }
it { is_expected.to include('output/foo.htmlx') }
it { is_expected.to include('output/foo.txt') }
it { is_expected.to include('output/foo.yhtml') }

it { is_expected.not_to include('output/foo.xhtml') }
end
end

describe '#output_html_filenames' do
subject { check.output_html_filenames }

let(:check) do
described_class.new(output_filenames: output_filenames)
described_class.new(
output_filenames: output_filenames,
config: Nanoc::ConfigView.new(config, view_context),
)
end

let(:config) do
Nanoc::Core::Configuration.new(
dir: Dir.getwd,
hash: config_hash,
)
end

let(:config_hash) { {} }

let(:view_context) do
double(:view_context, dependency_tracker: dependency_tracker)
end

let(:dependency_tracker) do
double(:dependency_tracker).tap do |dt|
allow(dt).to receive(:bounce)
end
end

let(:output_filenames) do
Expand All @@ -72,12 +158,28 @@
]
end

it { is_expected.to include('output/foo.html') }
it { is_expected.to include('output/foo.htm') }
it { is_expected.to include('output/foo.xhtml') }
context 'when exclude_files is unset' do
it { is_expected.to include('output/foo.html') }
it { is_expected.to include('output/foo.htm') }
it { is_expected.to include('output/foo.xhtml') }

it { is_expected.not_to include('output/foo.txt') }
it { is_expected.not_to include('output/foo.htmlx') }
it { is_expected.not_to include('output/foo.yhtml') }
it { is_expected.not_to include('output/foo.txt') }
it { is_expected.not_to include('output/foo.htmlx') }
it { is_expected.not_to include('output/foo.yhtml') }
end

context 'when exclude_files is set' do
let(:config_hash) do
{ checks: { all: { exclude_files: ['foo.xhtml'] } } }
end

it { is_expected.to include('output/foo.html') }
it { is_expected.to include('output/foo.htm') }

it { is_expected.not_to include('output/foo.xhtml') }
it { is_expected.not_to include('output/foo.txt') }
it { is_expected.not_to include('output/foo.htmlx') }
it { is_expected.not_to include('output/foo.yhtml') }
end
end
end