diff --git a/fastlane/lib/fastlane/actions/swiftlint.rb b/fastlane/lib/fastlane/actions/swiftlint.rb index 8ab2fd82aaa..d0a70270738 100644 --- a/fastlane/lib/fastlane/actions/swiftlint.rb +++ b/fastlane/lib/fastlane/actions/swiftlint.rb @@ -13,13 +13,7 @@ def self.run(params) command = (params[:executable] || "swiftlint").dup command << " #{params[:mode]}" - command << " --path #{params[:path].shellescape}" if params[:path] - command << supported_option_switch(params, :strict, "0.9.2", true) - command << " --config #{params[:config_file].shellescape}" if params[:config_file] - command << " --reporter #{params[:reporter]}" if params[:reporter] - command << supported_option_switch(params, :quiet, "0.9.0", true) - command << supported_option_switch(params, :format, "0.11.0", true) if params[:mode] == :autocorrect - command << " --compiler-log-path #{params[:compiler_log_path].shellescape}" if params[:compiler_log_path] + command << optional_flags(params) if params[:files] if version < Gem::Version.new('0.5.1') @@ -41,12 +35,33 @@ def self.run(params) end end + def self.optional_flags(params) + command = "" + command << " --path #{params[:path].shellescape}" if params[:path] + command << supported_option_switch(params, :strict, "0.9.2", true) + command << " --config #{params[:config_file].shellescape}" if params[:config_file] + command << " --reporter #{params[:reporter]}" if params[:reporter] + command << supported_option_switch(params, :quiet, "0.9.0", true) + command << supported_option_switch(params, :format, "0.11.0", true) if params[:mode] == :autocorrect + command << supported_no_cache_option(params) if params[:no_cache] + command << " --compiler-log-path #{params[:compiler_log_path].shellescape}" if params[:compiler_log_path] + return command + end + # Get current SwiftLint version def self.swiftlint_version(executable: nil) binary = executable || 'swiftlint' Gem::Version.new(`#{binary} version`.chomp) end + def self.supported_no_cache_option(params) + if params[:mode] == :autocorrect || params[:mode] == :lint + return " --no-cache" + else + return "" + end + end + # Return "--option" switch if option is on and current SwiftLint version is greater or equal than min version. # Return "" otherwise. def self.supported_option_switch(params, option, min_version, can_ignore = false) @@ -139,6 +154,12 @@ def self.available_options is_string: false, type: Boolean, optional: true), + FastlaneCore::ConfigItem.new(key: :no_cache, + description: "Ignore the cache when mode is :autocorrect or :lint", + default_value: false, + is_string: false, + type: Boolean, + optional: true), FastlaneCore::ConfigItem.new(key: :compiler_log_path, description: "Compiler log path when mode is :analyze", is_string: true, diff --git a/fastlane/spec/actions_specs/swiftlint_spec.rb b/fastlane/spec/actions_specs/swiftlint_spec.rb index fc3902cdfeb..717a56f0346 100644 --- a/fastlane/spec/actions_specs/swiftlint_spec.rb +++ b/fastlane/spec/actions_specs/swiftlint_spec.rb @@ -353,6 +353,65 @@ end end + context "when specify no-cache option" do + it "adds no-cache option if mode is :autocorrect" do + result = Fastlane::FastFile.new.parse("lane :test do + swiftlint( + mode: :autocorrect, + no_cache: true + ) + end").runner.execute(:test) + + expect(result).to eq("swiftlint autocorrect --no-cache") + end + + it "adds no-cache option if mode is :lint" do + result = Fastlane::FastFile.new.parse("lane :test do + swiftlint( + mode: :lint, + no_cache: true + ) + end").runner.execute(:test) + + expect(result).to eq("swiftlint lint --no-cache") + end + + it "omits format option if mode is :analyze" do + result = Fastlane::FastFile.new.parse("lane :test do + swiftlint( + mode: :analyze, + no_cache: true + ) + end").runner.execute(:test) + + expect(result).to eq("swiftlint analyze") + end + end + + context "when specify false for no-cache option" do + it "doesn't add no-cache option if mode is :autocorrect" do + result = Fastlane::FastFile.new.parse("lane :test do + swiftlint( + mode: :autocorrect, + no_cache: false + ) + end").runner.execute(:test) + + expect(result).to eq("swiftlint autocorrect") + end + + it "doesn't add no-cache option if mode is :lint" do + result = Fastlane::FastFile.new.parse("lane :test do + swiftlint( + mode: :lint, + no_cache: false + ) + end").runner.execute(:test) + + expect(result).to eq("swiftlint lint") + end + end + context "when using analyzer mode" do it "adds compiler-log-path option" do path = "./spec/fixtures"