From c1fe9b0be61616100052bd2f1fc25ae59b53ae37 Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Wed, 19 Apr 2017 15:05:08 +0200 Subject: [PATCH] Ensure excluded config exists This ensures we don't attempt to call map on nil when swiftlint is configured with an empty excluded key. --- lib/danger_plugin.rb | 4 +++- spec/danger_plugin_spec.rb | 15 +++++++++++++++ spec/fixtures/empty_excluded.yml | 7 +++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/empty_excluded.yml diff --git a/lib/danger_plugin.rb b/lib/danger_plugin.rb index fed7749..81f3979 100755 --- a/lib/danger_plugin.rb +++ b/lib/danger_plugin.rb @@ -119,8 +119,10 @@ def excluded_files_from_config(filepath) {"excluded" => []} end + excluded_paths = config['excluded'] || [] + # Extract excluded paths - return config['excluded']. + return excluded_paths. map { |path| File.join(File.dirname(config_file), path) }. map { |path| File.expand_path(path) }. select { |path| File.exists?(path) || Dir.exists?(path) } diff --git a/spec/danger_plugin_spec.rb b/spec/danger_plugin_spec.rb index cf26d2a..4e6fd72 100755 --- a/spec/danger_plugin_spec.rb +++ b/spec/danger_plugin_spec.rb @@ -103,6 +103,21 @@ module Danger @swiftlint.lint_files end + it 'does not crash when excluded is nil' do + allow(@swiftlint.git).to receive(:added_files).and_return([]) + allow(@swiftlint.git).to receive(:modified_files).and_return([ + 'spec/fixtures/SwiftFile.swift', + ]) + + expect(Swiftlint).to receive(:lint) + .with(hash_including(:path => File.expand_path('spec/fixtures/SwiftFile.swift'))) + .and_return(@swiftlint_response) + .once + + @swiftlint.config_file = 'spec/fixtures/empty_excluded.yml' + @swiftlint.lint_files + end + it 'does not lint deleted files paths' do # Danger (4.3.0 at the time of writing) returns deleted files in the # modified fiels array, which kinda makes sense. diff --git a/spec/fixtures/empty_excluded.yml b/spec/fixtures/empty_excluded.yml new file mode 100644 index 0000000..237d0c1 --- /dev/null +++ b/spec/fixtures/empty_excluded.yml @@ -0,0 +1,7 @@ +disabled_rules: + - todo + +included: + - an/included/folder + +excluded: