From 3b9c442e0922522a52e890345dc29ec82ec57655 Mon Sep 17 00:00:00 2001 From: Stuart Frost Date: Wed, 26 Jul 2023 14:11:07 +0100 Subject: [PATCH] Raise error when passed invalid file paths --- spec/ameba/cli/cmd_spec.cr | 20 ++++++++++---------- spec/ameba/glob_utils_spec.cr | 6 ++++++ spec/fixtures/config.yml | 4 ++++ spec/fixtures/passing_ameba.cr | 0 src/ameba/glob_utils.cr | 2 ++ 5 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 spec/fixtures/config.yml create mode 100644 spec/fixtures/passing_ameba.cr diff --git a/spec/ameba/cli/cmd_spec.cr b/spec/ameba/cli/cmd_spec.cr index a412c194..2265a892 100644 --- a/spec/ameba/cli/cmd_spec.cr +++ b/spec/ameba/cli/cmd_spec.cr @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/ameba/glob_utils_spec.cr b/spec/ameba/glob_utils_spec.cr index 41309195..8481e157 100644 --- a/spec/ameba/glob_utils_spec.cr +++ b/spec/ameba/glob_utils_spec.cr @@ -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 diff --git a/spec/fixtures/config.yml b/spec/fixtures/config.yml new file mode 100644 index 00000000..4cc49722 --- /dev/null +++ b/spec/fixtures/config.yml @@ -0,0 +1,4 @@ +Ameba/PerfRule: + Enabled: false +Ameba/ErrorRule: + Enabled: false diff --git a/spec/fixtures/passing_ameba.cr b/spec/fixtures/passing_ameba.cr new file mode 100644 index 00000000..e69de29b diff --git a/src/ameba/glob_utils.cr b/src/ameba/glob_utils.cr index 71cd0cb5..5a0e5c72 100644 --- a/src/ameba/glob_utils.cr +++ b/src/ameba/glob_utils.cr @@ -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!