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

Fix FileLoader to skip files not matching to the given pattern #382

Merged
merged 1 commit into from
May 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions lib/steep/services/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,36 @@ def initialize(base_dir:)
end

def each_path_in_patterns(pattern, commandline_patterns = [])
pats = commandline_patterns.empty? ? pattern.patterns : commandline_patterns

pats.each do |path|
absolute_path = base_dir + path

if absolute_path.file?
yield absolute_path.relative_path_from(base_dir)
else
files = if absolute_path.directory?
Pathname.glob("#{absolute_path}/**/*#{pattern.ext}")
else
Pathname.glob(absolute_path)
end

files.sort.each do |source_path|
if source_path.file?
relative_path = source_path.relative_path_from(base_dir)
unless pattern.ignore?(relative_path)
yield relative_path
if block_given?
pats = commandline_patterns.empty? ? pattern.patterns : commandline_patterns

pats.each do |path|
absolute_path = base_dir + path

if absolute_path.file?
if pattern =~ path
yield absolute_path.relative_path_from(base_dir)
end
else
files = if absolute_path.directory?
Pathname.glob("#{absolute_path}/**/*#{pattern.ext}")
else
Pathname.glob(absolute_path)
end

files.sort.each do |source_path|
if source_path.file?
relative_path = source_path.relative_path_from(base_dir)
unless pattern.ignore?(relative_path)
yield relative_path
end
end
end
end

end
else
enum_for :each_path_in_patterns, pattern, commandline_patterns
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/cli_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ def test_check_broken
end
end

def test_check_unknown
in_tmpdir do
(current_dir + "Steepfile").write(<<-EOF)
target :app do
check "foo.rb"
end
EOF

(current_dir + "bar.rb").write("")

stdout, status = sh(*steep, "check", "bar.rb")
assert_predicate status, :success?, stdout
end
end

def test_annotations
in_tmpdir do
(current_dir + "foo.rb").write(<<-RUBY)
Expand Down
34 changes: 34 additions & 0 deletions test/file_loader_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "test_helper"

class FileLoaderTest < Minitest::Test
include Steep
include TestHelper
include ShellHelper

Pattern = Project::Pattern
FileLoader = Services::FileLoader

def dirs
@dirs ||= []
end

def test_each_path_in_patterns
in_tmpdir do
loader = FileLoader.new(base_dir: current_dir)

(current_dir + "lib").mkdir()
(current_dir + "test").mkdir()
(current_dir + "lib/foo.rb").write("")
(current_dir + "lib/parser.y").write("")
(current_dir + "test/foo_test.rb").write("")
(current_dir + "Rakefile").write("")

pat = Pattern.new(patterns: ["lib", "test"], ext: ".rb")

assert_equal [Pathname("lib/foo.rb"), Pathname("test/foo_test.rb")], loader.each_path_in_patterns(pat).to_a
assert_equal [Pathname("lib/foo.rb")], loader.each_path_in_patterns(pat, ["lib"]).to_a
assert_empty loader.each_path_in_patterns(pat, ["lib/parser.y"]).to_a
assert_empty loader.each_path_in_patterns(pat, ["Rakefile"]).to_a
end
end
end