mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Merge pull request #394 from stufro/388-raise-on-invalid-file-path
Raise error when passed invalid file paths
This commit is contained in:
		
						commit
						18d193bd08
					
				
					 7 changed files with 63 additions and 22 deletions
				
			
		|  | @ -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/source.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/source.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/source.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/source.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/source.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/source.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/source.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/source.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/source.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/source.cr) | ||||||
|           end |           end | ||||||
|         end |         end | ||||||
|       end |       end | ||||||
|  |  | ||||||
|  | @ -9,10 +9,21 @@ module Ameba | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     describe ".new" do |     describe ".new" do | ||||||
|       it "loads default globs when config is empty" do |       context "when config is empty" do | ||||||
|  |         it "loads default globs" do | ||||||
|           yml = YAML.parse "{}" |           yml = YAML.parse "{}" | ||||||
|           config = Config.new(yml) |           config = Config.new(yml) | ||||||
|         config.globs.should eq Config::DEFAULT_GLOBS |           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 | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       it "initializes globs as string" do |       it "initializes globs as string" do | ||||||
|  | @ -102,7 +113,7 @@ module Ameba | ||||||
|       config = Config.load config_sample |       config = Config.load config_sample | ||||||
| 
 | 
 | ||||||
|       it "holds source globs" do |       it "holds source globs" do | ||||||
|         config.globs.should eq Config::DEFAULT_GLOBS |         config.globs.should eq ["**/*.cr"] | ||||||
|       end |       end | ||||||
| 
 | 
 | ||||||
|       it "allows to set globs" do |       it "allows to set globs" do | ||||||
|  |  | ||||||
|  | @ -45,6 +45,30 @@ 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 | ||||||
|  | 
 | ||||||
|  |       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 |   end | ||||||
| end | end | ||||||
|  |  | ||||||
							
								
								
									
										6
									
								
								spec/fixtures/config.yml
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								spec/fixtures/config.yml
									
										
									
									
										vendored
									
									
								
							|  | @ -1,2 +1,4 @@ | ||||||
| Lint/ComparisonToBoolean: | Ameba/PerfRule: | ||||||
|   Enabled: true |   Enabled: false | ||||||
|  | Ameba/ErrorRule: | ||||||
|  |   Enabled: false | ||||||
|  |  | ||||||
							
								
								
									
										0
									
								
								spec/fixtures/source.cr
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								spec/fixtures/source.cr
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -55,10 +55,7 @@ class Ameba::Config | ||||||
|     Path[XDG_CONFIG_HOME] / "ameba/config.yml", |     Path[XDG_CONFIG_HOME] / "ameba/config.yml", | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   DEFAULT_GLOBS = %w( |   SOURCES_GLOB = "**/*.cr" | ||||||
|     **/*.cr |  | ||||||
|     !lib |  | ||||||
|   ) |  | ||||||
| 
 | 
 | ||||||
|   getter rules : Array(Rule::Base) |   getter rules : Array(Rule::Base) | ||||||
|   property severity = Severity::Convention |   property severity = Severity::Convention | ||||||
|  | @ -95,7 +92,7 @@ class Ameba::Config | ||||||
|     @rules = Rule.rules.map &.new(config).as(Rule::Base) |     @rules = Rule.rules.map &.new(config).as(Rule::Base) | ||||||
|     @rule_groups = @rules.group_by &.group |     @rule_groups = @rules.group_by &.group | ||||||
|     @excluded = load_array_section(config, "Excluded") |     @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) |     return unless formatter_name = load_formatter_name(config) | ||||||
|     self.formatter = formatter_name |     self.formatter = formatter_name | ||||||
|  | @ -239,6 +236,12 @@ class Ameba::Config | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  |   private def default_globs | ||||||
|  |     [SOURCES_GLOB].tap do |globs| | ||||||
|  |       globs.push("!lib") unless Dir["lib/**/*.cr"].empty? | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|   # :nodoc: |   # :nodoc: | ||||||
|   module RuleConfig |   module RuleConfig | ||||||
|     # Define rule properties |     # Define rule properties | ||||||
|  |  | ||||||
|  | @ -22,6 +22,7 @@ module Ameba | ||||||
|     def expand(globs) |     def expand(globs) | ||||||
|       globs.flat_map do |glob| |       globs.flat_map do |glob| | ||||||
|         glob += "/**/*.cr" if File.directory?(glob) |         glob += "/**/*.cr" if File.directory?(glob) | ||||||
|  |         raise ArgumentError.new("No files found matching #{glob}") if Dir[glob].empty? | ||||||
|         Dir[glob] |         Dir[glob] | ||||||
|       end.uniq! |       end.uniq! | ||||||
|     end |     end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue