From 8f8990c14e0d53cab8f2f1044cf683af7c971449 Mon Sep 17 00:00:00 2001 From: Victor Galan Date: Sun, 3 Sep 2017 14:05:38 +0200 Subject: [PATCH 1/3] Ensure only files in the selected directory are linted --- lib/danger_plugin.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/danger_plugin.rb b/lib/danger_plugin.rb index fa121ee..d1660ee 100755 --- a/lib/danger_plugin.rb +++ b/lib/danger_plugin.rb @@ -47,19 +47,21 @@ def lint_files(files=nil, inline_mode: false) else nil end + + dir_selected = directory ? File.expand_path(directory) : Dir.pwd # Extract excluded paths excluded_paths = excluded_files_from_config(config) # Extract swift files (ignoring excluded ones) - files = find_swift_files(files, excluded_paths) + files = find_swift_files(files, excluded_paths, dir_selected) # Prepare swiftlint options options = { config: config, reporter: 'json', quiet: true, - pwd: directory ? File.expand_path(directory) : Dir.pwd + pwd: dir_selected } # Lint each file and collect the results @@ -100,7 +102,7 @@ def run_swiftlint(files, options) # If files are not provided it will use git modifield and added files # # @return [Array] swift files - def find_swift_files(files=nil, excluded_paths=[]) + def find_swift_files(files=nil, excluded_paths=[], dir_selected) # Assign files to lint files = files ? Dir.glob(files) : (git.modified_files - git.deleted_files) + git.added_files @@ -113,6 +115,8 @@ def find_swift_files(files=nil, excluded_paths=[]) # Remove dups uniq. map { |file| File.expand_path(file) }. + # Ensure only files in the selected directory + select { |file| file.start_with?(dir_selected)}. # Reject files excluded on configuration reject { |file| excluded_paths.any? { |excluded_path| From 8e498341da99c81189e2cbe7b139a9f23bd4cd55 Mon Sep 17 00:00:00 2001 From: Victor Galan Date: Sun, 3 Sep 2017 14:06:28 +0200 Subject: [PATCH 2/3] Add test for custom directory --- spec/danger_plugin_spec.rb | 22 ++++++++++++++++++++-- spec/fixtures/some_dir/SwiftFile.swift | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100755 spec/fixtures/some_dir/SwiftFile.swift diff --git a/spec/danger_plugin_spec.rb b/spec/danger_plugin_spec.rb index fcdac4b..b259b67 100755 --- a/spec/danger_plugin_spec.rb +++ b/spec/danger_plugin_spec.rb @@ -68,18 +68,36 @@ module Danger end it 'uses a custom directory' do - @swiftlint.directory = 'some_dir' + @swiftlint.directory = 'spec/fixtures/some_dir' allow_any_instance_of(Swiftlint).to receive(:lint) .with(hash_including(:pwd => File.expand_path(@swiftlint.directory))) .and_return(@swiftlint_response) - @swiftlint.lint_files("spec/fixtures/*.swift") + @swiftlint.lint_files(["spec/fixtures/some_dir/SwiftFile.swift"]) output = @swiftlint.status_report[:markdowns].first.to_s expect(output).to_not be_empty + end + it 'only lint files specified in custom dir' do + @swiftlint.directory = 'spec/fixtures/some_dir' + + allow(@swiftlint.git).to receive(:added_files).and_return([]) + allow(@swiftlint.git).to receive(:modified_files).and_return([ + 'spec/fixtures/some_dir/SwiftFile.swift', + 'spec/fixtures/SwiftFile.swift' + ]) + + expect_any_instance_of(Swiftlint).to receive(:lint) + .with(hash_including(:path => File.expand_path('spec/fixtures/some_dir/SwiftFile.swift'))) + .once + .and_return(@swiftlint_response) + + @swiftlint.lint_files + end + it 'does not crash if JSON reporter returns an empty string rather than an object' do # This can occurr if for some reson there is no file at the give path. # In such a case SwiftLint will write to stderr: diff --git a/spec/fixtures/some_dir/SwiftFile.swift b/spec/fixtures/some_dir/SwiftFile.swift new file mode 100755 index 0000000..a12c611 --- /dev/null +++ b/spec/fixtures/some_dir/SwiftFile.swift @@ -0,0 +1 @@ +// This file intentional left blank-ish. From d1bf4263de9cf0070ecddd79cb5a69f20a0831f1 Mon Sep 17 00:00:00 2001 From: Victor Galan Date: Sun, 3 Sep 2017 16:08:24 +0200 Subject: [PATCH 3/3] Fix indentation --- lib/danger_plugin.rb | 2 +- spec/danger_plugin_spec.rb | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/danger_plugin.rb b/lib/danger_plugin.rb index d1660ee..7ead533 100755 --- a/lib/danger_plugin.rb +++ b/lib/danger_plugin.rb @@ -116,7 +116,7 @@ def find_swift_files(files=nil, excluded_paths=[], dir_selected) uniq. map { |file| File.expand_path(file) }. # Ensure only files in the selected directory - select { |file| file.start_with?(dir_selected)}. + select { |file| file.start_with?(dir_selected) }. # Reject files excluded on configuration reject { |file| excluded_paths.any? { |excluded_path| diff --git a/spec/danger_plugin_spec.rb b/spec/danger_plugin_spec.rb index b259b67..aafd016 100755 --- a/spec/danger_plugin_spec.rb +++ b/spec/danger_plugin_spec.rb @@ -84,19 +84,19 @@ module Danger it 'only lint files specified in custom dir' do @swiftlint.directory = 'spec/fixtures/some_dir' - allow(@swiftlint.git).to receive(:added_files).and_return([]) - allow(@swiftlint.git).to receive(:modified_files).and_return([ + allow(@swiftlint.git).to receive(:added_files).and_return([]) + allow(@swiftlint.git).to receive(:modified_files).and_return([ 'spec/fixtures/some_dir/SwiftFile.swift', 'spec/fixtures/SwiftFile.swift' - ]) + ]) - expect_any_instance_of(Swiftlint).to receive(:lint) - .with(hash_including(:path => File.expand_path('spec/fixtures/some_dir/SwiftFile.swift'))) - .once - .and_return(@swiftlint_response) + expect_any_instance_of(Swiftlint).to receive(:lint) + .with(hash_including(:path => File.expand_path('spec/fixtures/some_dir/SwiftFile.swift'))) + .once + .and_return(@swiftlint_response) - @swiftlint.lint_files - end + @swiftlint.lint_files + end it 'does not crash if JSON reporter returns an empty string rather than an object' do # This can occurr if for some reson there is no file at the give path.