Skip to content
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
37 changes: 36 additions & 1 deletion spec/ameba/cli/cmd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,39 @@ module Ameba::CLI
describe "Cmd" do
describe ".run" do
it "runs ameba" do
r = CLI.run %w[-f silent file.cr]
r = CLI.run ["-f", "silent", "--only", "Lint/ComparisonToBoolean", __FILE__]
r.should be_true
end

it "raises when a non-existent file is provided" do
expect_raises(Exception, "No files found matching `not_there.cr`") do
CLI.run %w[-f silent not_there.cr]
end
end

it "raises when a glob matches no files" do
expect_raises(Exception, "No files found matching `#{Path["nonexistent_dir", "**", "*.cr"].relative_to(Dir.current)}`") do
CLI.run %w[-f silent nonexistent_dir/**/*.cr]
end
end

context "with `--ignore-unmatched-paths` flag" do
it "does not raise when a non-existent file is provided" do
r = CLI.run %w[-f silent --ignore-unmatched-paths not_there.cr]
r.should be_true
end

it "does not raise when a non-matching glob is provided" do
r = CLI.run %w[-f silent --ignore-unmatched-paths nonexistent_dir/**/*.cr]
r.should be_true
end
end

it "does not raise when reading from STDIN" do
# STDIN mode should bypass glob validation
opts = CLI.parse_args %w[--stdin-filename foo.cr]
opts.stdin_filename.should_not be_nil
end
end

describe ".parse_args" do
Expand Down Expand Up @@ -91,6 +121,11 @@ module Ameba::CLI
opts.colors?.should be_false
end

it "accepts --ignore-unmatched-paths flag" do
opts = CLI.parse_args %w[--ignore-unmatched-paths]
opts.ignore_unmatched_paths?.should be_true
end

it "accepts --without-affected-code flag" do
opts = CLI.parse_args %w[--without-affected-code]
opts.without_affected_code?.should be_true
Expand Down
19 changes: 19 additions & 0 deletions src/ameba/cli/cmd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Ameba::CLI
property? all = false
property? colors = true
property? without_affected_code = false
property? ignore_unmatched_paths = false
property? autocorrect = false
end

Expand Down Expand Up @@ -69,6 +70,8 @@ module Ameba::CLI
return true
end

validate_globs(opts, config.root)

runner = Ameba.run(config)

if location_to_explain
Expand Down Expand Up @@ -186,6 +189,11 @@ module Ameba::CLI
opts.without_affected_code = true
end

parser.on("--ignore-unmatched-paths",
"Do not report unmatched path patterns") do
opts.ignore_unmatched_paths = true
end

parser.on("--no-color", "Disable colors") do
opts.colors = false
end
Expand Down Expand Up @@ -308,6 +316,17 @@ module Ameba::CLI
opts.formatter = :silent
end

private def validate_globs(opts, root) : Nil
return if opts.ignore_unmatched_paths?
return if opts.stdin_filename
return unless globs = opts.globs

globs.each do |glob|
next unless GlobUtils.expand({glob}, root).empty?
raise "No files found matching `#{Path[glob].relative_to(Dir.current)}`"
end
end

private def configure_explain_opts(loc, opts) : Nil
location_to_explain = parse_explain_location(loc)

Expand Down
Loading