Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swiftlint] add support for the --no-cache flag on autocorrect and lint #16132

Merged
merged 6 commits into from Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 28 additions & 7 deletions fastlane/lib/fastlane/actions/swiftlint.rb
Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
59 changes: 59 additions & 0 deletions fastlane/spec/actions_specs/swiftlint_spec.rb
Expand Up @@ -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"
Expand Down