Raise error when passed invalid file paths

This commit is contained in:
Stuart Frost 2023-07-26 14:11:07 +01:00
parent 8c9d234d0b
commit 3b9c442e09
5 changed files with 22 additions and 10 deletions

View file

@ -5,7 +5,7 @@ module Ameba::Cli
describe "Cmd" do describe "Cmd" do
describe ".run" do describe ".run" do
it "runs ameba" 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 r.should be_nil
end end
end end
@ -43,12 +43,12 @@ module Ameba::Cli
end end
it "defaults rules? flag to false" do 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 c.rules?.should be_false
end end
it "defaults skip_reading_config? flag to false" do 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 c.skip_reading_config?.should be_false
end end
@ -58,7 +58,7 @@ module Ameba::Cli
end end
it "defaults all? flag to false" do 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 c.all?.should be_false
end end
@ -95,35 +95,35 @@ module Ameba::Cli
describe "-e/--explain" do describe "-e/--explain" do
it "configures file/line/column" 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 = 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[:line].should eq 3
location_to_explain[:column].should eq 5 location_to_explain[:column].should eq 5
end end
it "raises an error if location is not valid" do it "raises an error if location is not valid" do
expect_raises(Exception, "location should have PATH:line:column") 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
end end
it "raises an error if line number is not valid" do it "raises an error if line number is not valid" do
expect_raises(Exception, "location should have PATH:line:column") 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
end end
it "raises an error if column number is not valid" do it "raises an error if column number is not valid" do
expect_raises(Exception, "location should have PATH:line:column") 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
end end
it "raises an error if line/column are missing" do it "raises an error if line/column are missing" do
expect_raises(Exception, "location should have PATH:line:column") 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 end
end end

View file

@ -45,6 +45,12 @@ module Ameba
subject.expand(["**/#{current_file_basename}", "**/#{current_file_basename}"]) subject.expand(["**/#{current_file_basename}", "**/#{current_file_basename}"])
.should eq [current_file_path] .should eq [current_file_path]
end 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 end
end end

4
spec/fixtures/config.yml vendored Normal file
View file

@ -0,0 +1,4 @@
Ameba/PerfRule:
Enabled: false
Ameba/ErrorRule:
Enabled: false

0
spec/fixtures/passing_ameba.cr vendored Normal file
View file

View file

@ -21,6 +21,8 @@ module Ameba
# ``` # ```
def expand(globs) def expand(globs)
globs.flat_map do |glob| globs.flat_map do |glob|
raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty?
glob += "/**/*.cr" if File.directory?(glob) glob += "/**/*.cr" if File.directory?(glob)
Dir[glob] Dir[glob]
end.uniq! end.uniq!