Skip to content

Commit

Permalink
Merge pull request #460 from crystal-ameba/fix-issue-459
Browse files Browse the repository at this point in the history
Make sure we only return files from `GlobUtils#expand` method
  • Loading branch information
Sija committed Apr 17, 2024
2 parents e6a5fa9 + f12e7f6 commit 6d03cef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 7 additions & 5 deletions spec/ameba/glob_utils_spec.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
require "../spec_helper"

module Ameba
struct GlobUtilsClass
include GlobUtils
end

subject = GlobUtilsClass.new
subject = GlobUtils
current_file_basename = File.basename(__FILE__)
current_file_path = "spec/ameba/#{current_file_basename}"

Expand Down Expand Up @@ -45,6 +41,12 @@ module Ameba
subject.expand(["**/#{current_file_basename}", "**/#{current_file_basename}"])
.should eq [current_file_path]
end

it "does not list folders" do
subject.expand(["**/*"]).each do |path|
fail "#{path.inspect} should be a file" unless File.file?(path)
end
end
end
end
end
13 changes: 9 additions & 4 deletions src/ameba/glob_utils.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Ameba
# Helper module that is utilizes helpers for working with globs.
module GlobUtils
extend self

# Returns all files that match specified globs.
# Globs can have wildcards or be rejected:
#
Expand All @@ -20,10 +22,13 @@ module Ameba
# expand(["spec/*.cr", "src"]) # => all files in src folder + first level specs
# ```
def expand(globs)
globs.flat_map do |glob|
glob += "/**/*.cr" if File.directory?(glob)
Dir[glob]
end.uniq!
globs
.flat_map do |glob|
glob += "/**/*.cr" if File.directory?(glob)
Dir[glob]
end
.uniq!
.select! { |path| File.file?(path) }
end

private def rejected_globs(globs)
Expand Down

0 comments on commit 6d03cef

Please sign in to comment.