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

[action][ensure_git_status_clean] new ignore_files option for explicitly ignoring files #21283

Merged
merged 1 commit into from
May 17, 2023
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
34 changes: 31 additions & 3 deletions fastlane/lib/fastlane/actions/ensure_git_status_clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,35 @@ module SharedValues
# Raises an exception and stop the lane execution if the repo is not in a clean state
class EnsureGitStatusCleanAction < Action
def self.run(params)
# Build command
if params[:ignored]
ignored_mode = params[:ignored]
ignored_mode = 'no' if ignored_mode == 'none'
repo_status = Actions.sh("git status --porcelain --ignored='#{ignored_mode}'")
command = "git status --porcelain --ignored='#{ignored_mode}'"
else
repo_status = Actions.sh("git status --porcelain")
command = "git status --porcelain"
end

# Don't log if manually ignoring files as it will emulate output later
print_output = params[:ignore_files].nil?
repo_status = Actions.sh(command, log: print_output)

# Manual post processing trying to ignore certain file paths
if (ignore_files = params[:ignore_files])
repo_status = repo_status.lines.reject do |line|
path = line.split(' ').last
was_found = ignore_files.include?(path)

UI.message("Ignoring '#{path}'") if was_found

was_found
end.join("")

# Emulate the output format of `git status --porcelain`
UI.command(command)
repo_status.lines.each do |line|
UI.message("▸ " + line.chomp.magenta)
end
end

repo_clean = repo_status.empty?
Expand Down Expand Up @@ -86,7 +109,12 @@ def self.available_options
modes = %w(traditional none matching)

UI.user_error!("Unsupported mode, must be: #{modes}") unless modes.include?(mode)
end)
end),
FastlaneCore::ConfigItem.new(key: :ignore_files,
env_name: "FL_ENSURE_GIT_STATUS_CLEAN_IGNORE_FILES",
description: "Array of files to ignore",
optional: true,
type: Array)
]
end

Expand Down
31 changes: 26 additions & 5 deletions fastlane/spec/actions_specs/ensure_git_status_clean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

context "when git status is clean" do
before :each do
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain").and_return("")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain", log: true).and_return("")
end

it "outputs success message" do
Expand All @@ -21,13 +21,34 @@

context "when git status is not clean" do
before :each do
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain").and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain --ignored='traditional'").and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb\n!! .DS_Store\n!! fastlane/")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain --ignored='no'").and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain --ignored='matching'").and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb\n!! .DS_Store\n!! fastlane/.DS_Store")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain", log: true).and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain", log: false).and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain --ignored='traditional'", log: true).and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb\n!! .DS_Store\n!! fastlane/")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain --ignored='no'", log: true).and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb")
allow(Fastlane::Actions).to receive(:sh).with("git status --porcelain --ignored='matching'", log: true).and_return("M fastlane/lib/fastlane/actions/ensure_git_status_clean.rb\n!! .DS_Store\n!! fastlane/.DS_Store")
allow(Fastlane::Actions).to receive(:sh).with("git diff").and_return("+ \"this is a new line\"")
end

context "with ignore_files" do
it "outputs success message" do
expect(FastlaneCore::UI).to receive(:success).with("Git status is clean, all good! 💪")
Fastlane::FastFile.new.parse("lane :test do
ensure_git_status_clean(
ignore_files: ['fastlane/lib/fastlane/actions/ensure_git_status_clean.rb']
)
end").runner.execute(:test)
end

it "outputs rich error message" do
expect(FastlaneCore::UI).to receive(:user_error!).with("Git repository is dirty! Please ensure the repo is in a clean state by committing/stashing/discarding all changes first.")
Fastlane::FastFile.new.parse("lane :test do
ensure_git_status_clean(
ignore_files: ['.DS_Store']
)
end").runner.execute(:test)
end
end

context "with show_uncommitted_changes flag" do
context "true" do
it "outputs rich error message" do
Expand Down