forked from sds/haml-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract file finding into separate helper class
Extract file finding/filtering logic into a helper class so it can be more-easily tested. In the process, this adds support for linting files without a `.haml` extension, though files without `.haml` will still be ignored when only a directory is given. Change-Id: Ie34745374f0453a55efc03dddd1e2c0abccf5925 Reviewed-on: http://gerrit.causes.com/46309 Tested-by: jenkins <[email protected]> Reviewed-by: Shane da Silva <[email protected]>
- Loading branch information
Showing
7 changed files
with
201 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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,69 @@ | ||
require 'find' | ||
|
||
module HamlLint | ||
# Finds HAML files that should be linted given a specified list of paths, glob | ||
# patterns, and configuration. | ||
class FileFinder | ||
# List of extensions of files to include under a directory when a directory | ||
# is specified instead of a file. | ||
VALID_EXTENSIONS = %w[.haml] | ||
|
||
# @param config [HamlLint::Configuration] | ||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
# Return list of files to lint given the specified set of paths and glob | ||
# patterns. | ||
# @param patterns [Array<String>] | ||
# @param excluded_patterns [Array<String>] | ||
# @raise [HamlLint::Exceptions::InvalidFilePath] | ||
# @return [Array<String>] list of actual files | ||
def find(patterns, excluded_patterns) | ||
extract_files_from(patterns).reject do |file| | ||
excluded_patterns.any? do |exclusion_glob| | ||
::File.fnmatch?(exclusion_glob, file, | ||
::File::FNM_PATHNAME | # Wildcards don't match path separators | ||
::File::FNM_DOTMATCH) # `*` wildcard matches dotfiles | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def extract_files_from(patterns) # rubocop:disable MethodLength | ||
files = [] | ||
|
||
patterns.each do |pattern| | ||
if File.file?(pattern) | ||
files << pattern | ||
else | ||
begin | ||
::Find.find(pattern) do |file| | ||
files << file if haml_file?(file) | ||
end | ||
rescue ::Errno::ENOENT | ||
# File didn't exist; it might be a file glob pattern | ||
matches = ::Dir.glob(pattern) | ||
if matches.any? | ||
files += matches | ||
else | ||
# One of the paths specified does not exist; raise a more | ||
# descriptive exception so we know which one | ||
raise HamlLint::Exceptions::InvalidFilePath, | ||
"File path '#{pattern}' does not exist" | ||
end | ||
end | ||
end | ||
end | ||
|
||
files.uniq | ||
end | ||
|
||
def haml_file?(file) | ||
return false unless ::FileTest.file?(file) | ||
|
||
VALID_EXTENSIONS.include?(::File.extname(file)) | ||
end | ||
end | ||
end |
This file contains 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 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 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,101 @@ | ||
require 'spec_helper' | ||
|
||
describe HamlLint::FileFinder do | ||
let(:config) { double } | ||
let(:excluded_patterns) { [] } | ||
|
||
subject { described_class.new(config) } | ||
|
||
describe '#find' do | ||
include_context 'isolated environment' | ||
|
||
subject { super().find(patterns, excluded_patterns) } | ||
|
||
context 'when no patterns are given' do | ||
let(:patterns) { [] } | ||
|
||
context 'and there are no HAML files under the current directory' do | ||
it { should == [] } | ||
end | ||
|
||
context 'and there are HAML files under the current directory' do | ||
before do | ||
`touch blah.haml` | ||
`mkdir -p more` | ||
`touch more/more.haml` | ||
end | ||
|
||
it { should == [] } | ||
end | ||
end | ||
|
||
context 'when files without a valid extension are given' do | ||
let(:patterns) { ['test.txt'] } | ||
|
||
context 'and those files exist' do | ||
before do | ||
`touch test.txt` | ||
end | ||
|
||
it { should == ['test.txt'] } | ||
end | ||
|
||
context 'and those files do not exist' do | ||
it 'raises an error' do | ||
expect { subject }.to raise_error HamlLint::Exceptions::InvalidFilePath | ||
end | ||
end | ||
end | ||
|
||
context 'when directories are given' do | ||
let(:patterns) { ['some-dir'] } | ||
|
||
context 'and those directories exist' do | ||
before do | ||
`mkdir -p some-dir` | ||
end | ||
|
||
context 'and they contain HAML files' do | ||
before do | ||
`touch some-dir/test.haml` | ||
end | ||
|
||
it { should == ['some-dir/test.haml'] } | ||
end | ||
|
||
context 'and they contain more directories with files with recognized extensions' do | ||
before do | ||
`mkdir -p some-dir/more-dir` | ||
`touch some-dir/more-dir/test.haml` | ||
end | ||
|
||
it { should == ['some-dir/more-dir/test.haml'] } | ||
end | ||
|
||
context 'and they contain files with some other extension' do | ||
before do | ||
`touch some-dir/test.txt` | ||
end | ||
|
||
it { should == [] } | ||
end | ||
end | ||
|
||
context 'and those directories do not exist' do | ||
it 'raises an error' do | ||
expect { subject }.to raise_error HamlLint::Exceptions::InvalidFilePath | ||
end | ||
end | ||
end | ||
|
||
context 'when the same file is specified multiple times' do | ||
let(:patterns) { ['test.haml'] * 3 } | ||
|
||
before do | ||
`touch test.haml` | ||
end | ||
|
||
it { should == ['test.haml'] } | ||
end | ||
end | ||
end |
This file contains 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,25 @@ | ||
require 'fileutils' | ||
require 'tmpdir' | ||
|
||
shared_context 'isolated environment' do | ||
around do |example| | ||
Dir.mktmpdir do |tmpdir| | ||
original_home = ENV['HOME'] | ||
|
||
begin | ||
virtual_home = File.expand_path(File.join(tmpdir, 'home')) | ||
Dir.mkdir(virtual_home) | ||
ENV['HOME'] = virtual_home | ||
|
||
working_dir = File.join(tmpdir, 'work') | ||
Dir.mkdir(working_dir) | ||
|
||
Dir.chdir(working_dir) do | ||
example.run | ||
end | ||
ensure | ||
ENV['HOME'] = original_home | ||
end | ||
end | ||
end | ||
end |