Skip to content

Raise error when passed invalid file paths #394

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

Merged
merged 8 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 10 additions & 10 deletions spec/ameba/cli/cmd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Ameba::Cli
describe "Cmd" do
describe ".run" do
it "runs ameba" do
r = Cli.run %w(-f silent file.cr)
r = Cli.run %w(-f silent -c spec/fixtures/config.yml spec/fixtures/passing_ameba.cr)
r.should be_nil
end
end
Expand Down Expand Up @@ -43,12 +43,12 @@ module Ameba::Cli
end

it "defaults rules? flag to false" do
c = Cli.parse_args %w(file.cr)
c = Cli.parse_args %w(spec/fixtures/passing_ameba.cr)
c.rules?.should be_false
end

it "defaults skip_reading_config? flag to false" do
c = Cli.parse_args %w(file.cr)
c = Cli.parse_args %w(spec/fixtures/passing_ameba.cr)
c.skip_reading_config?.should be_false
end

Expand All @@ -58,7 +58,7 @@ module Ameba::Cli
end

it "defaults all? flag to false" do
c = Cli.parse_args %w(file.cr)
c = Cli.parse_args %w(spec/fixtures/passing_ameba.cr)
c.all?.should be_false
end

Expand Down Expand Up @@ -95,35 +95,35 @@ module Ameba::Cli

describe "-e/--explain" do
it "configures file/line/column" do
c = Cli.parse_args %w(--explain src/file.cr:3:5)
c = Cli.parse_args %w(--explain spec/fixtures/passing_ameba.cr:3:5)

location_to_explain = c.location_to_explain.should_not be_nil
location_to_explain[:file].should eq "src/file.cr"
location_to_explain[:file].should eq "spec/fixtures/passing_ameba.cr"
location_to_explain[:line].should eq 3
location_to_explain[:column].should eq 5
end

it "raises an error if location is not valid" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain src/file.cr:3)
Cli.parse_args %w(--explain spec/fixtures/passing_ameba.cr:3)
end
end

it "raises an error if line number is not valid" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain src/file.cr:a:3)
Cli.parse_args %w(--explain spec/fixtures/passing_ameba.cr:a:3)
end
end

it "raises an error if column number is not valid" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain src/file.cr:3:&)
Cli.parse_args %w(--explain spec/fixtures/passing_ameba.cr:3:&)
end
end

it "raises an error if line/column are missing" do
expect_raises(Exception, "location should have PATH:line:column") do
Cli.parse_args %w(--explain src/file.cr)
Cli.parse_args %w(--explain spec/fixtures/passing_ameba.cr)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/ameba/glob_utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ module Ameba
subject.expand(["**/#{current_file_basename}", "**/#{current_file_basename}"])
.should eq [current_file_path]
end

it "raises an ArgumentError when the glob doesn't match any files" do
expect_raises(ArgumentError, "No files found matching foo/*") do
subject.expand(["foo/*"])
end
end
end
end
end
6 changes: 4 additions & 2 deletions spec/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Lint/ComparisonToBoolean:
Enabled: true
Ameba/PerfRule:
Enabled: false
Ameba/ErrorRule:
Enabled: false
Empty file added spec/fixtures/passing_ameba.cr
Empty file.
2 changes: 2 additions & 0 deletions src/ameba/glob_utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Ameba
# ```
def expand(globs)
globs.flat_map do |glob|
raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty?

glob += "/**/*.cr" if File.directory?(glob)
Dir[glob]
end.uniq!
Expand Down