Skip to content

Commit

Permalink
Revert "Merge pull request #394 from stufro/388-raise-on-invalid-file…
Browse files Browse the repository at this point in the history
…-path"

This reverts commit 18d193b, reversing
changes made to 7b8316f.
  • Loading branch information
Sija committed Dec 28, 2023
1 parent 47088b1 commit 26d9bc0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 63 deletions.
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 -c spec/fixtures/config.yml spec/fixtures/source.cr]
r = Cli.run %w[-f silent file.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[spec/fixtures/source.cr]
c = Cli.parse_args %w[file.cr]
c.rules?.should be_false
end

it "defaults skip_reading_config? flag to false" do
c = Cli.parse_args %w[spec/fixtures/source.cr]
c = Cli.parse_args %w[file.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[spec/fixtures/source.cr]
c = Cli.parse_args %w[file.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 spec/fixtures/source.cr:3:5]
c = Cli.parse_args %w[--explain src/file.cr:3:5]

location_to_explain = c.location_to_explain.should_not be_nil
location_to_explain[:file].should eq "spec/fixtures/source.cr"
location_to_explain[:file].should eq "src/file.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 spec/fixtures/source.cr:3]
Cli.parse_args %w[--explain src/file.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 spec/fixtures/source.cr:a:3]
Cli.parse_args %w[--explain src/file.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 spec/fixtures/source.cr:3:&]
Cli.parse_args %w[--explain src/file.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 spec/fixtures/source.cr]
Cli.parse_args %w[--explain src/file.cr]
end
end
end
Expand Down
21 changes: 5 additions & 16 deletions spec/ameba/config_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,10 @@ module Ameba
end

describe ".new" do
context "when config is empty" do
it "loads default globs" do
yml = YAML.parse "{}"
config = Config.new(yml)
config.globs.should eq ["**/*.cr"]
end

it "sets !lib as a default glob when there are .cr files in lib" do
File.touch "lib/shard.cr"
yml = YAML.parse "{}"
config = Config.new(yml)
config.globs.should eq ["**/*.cr", "!lib"]
ensure
File.delete "lib/shard.cr"
end
it "loads default globs when config is empty" do
yml = YAML.parse "{}"
config = Config.new(yml)
config.globs.should eq Config::DEFAULT_GLOBS
end

it "initializes globs as string" do
Expand Down Expand Up @@ -113,7 +102,7 @@ module Ameba
config = Config.load config_sample

it "holds source globs" do
config.globs.should eq ["**/*.cr"]
config.globs.should eq Config::DEFAULT_GLOBS
end

it "allows to set globs" do
Expand Down
24 changes: 0 additions & 24 deletions spec/ameba/glob_utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,6 @@ 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

it "raises an ArgumentError when given a missing file" do
expect_raises(ArgumentError, "No files found matching foo.cr") do
subject.expand(["foo.cr"])
end
end

it "raises an ArgumentError when given a missing directory" do
expect_raises(ArgumentError, "No files found matching foo/") do
subject.expand(["foo/"])
end
end

it "raises an ArgumentError when given multiple arguments, one of which is missing" do
expect_raises(ArgumentError, "No files found matching foo.cr") do
subject.expand(["**/#{current_file_basename}", "foo.cr"])
end
end
end
end
end
6 changes: 2 additions & 4 deletions spec/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
Ameba/PerfRule:
Enabled: false
Ameba/ErrorRule:
Enabled: false
Lint/ComparisonToBoolean:
Enabled: true
Empty file removed spec/fixtures/source.cr
Empty file.
13 changes: 5 additions & 8 deletions src/ameba/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class Ameba::Config
Path[XDG_CONFIG_HOME] / "ameba/config.yml",
}

SOURCES_GLOB = "**/*.cr"
DEFAULT_GLOBS = %w(
**/*.cr
!lib
)

getter rules : Array(Rule::Base)
property severity = Severity::Convention
Expand Down Expand Up @@ -92,7 +95,7 @@ class Ameba::Config
@rules = Rule.rules.map &.new(config).as(Rule::Base)
@rule_groups = @rules.group_by &.group
@excluded = load_array_section(config, "Excluded")
@globs = load_array_section(config, "Globs", default_globs)
@globs = load_array_section(config, "Globs", DEFAULT_GLOBS)

return unless formatter_name = load_formatter_name(config)
self.formatter = formatter_name
Expand Down Expand Up @@ -236,12 +239,6 @@ class Ameba::Config
end
end

private def default_globs
[SOURCES_GLOB].tap do |globs|
globs.push("!lib") unless Dir["lib/**/*.cr"].empty?
end
end

# :nodoc:
module RuleConfig
# Define rule properties
Expand Down
1 change: 0 additions & 1 deletion src/ameba/glob_utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module Ameba
def expand(globs)
globs.flat_map do |glob|
glob += "/**/*.cr" if File.directory?(glob)
raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty?
Dir[glob]
end.uniq!
end
Expand Down

0 comments on commit 26d9bc0

Please sign in to comment.