Skip to content

Commit

Permalink
Merge pull request #13237 from apainintheneck/tests-changed-option
Browse files Browse the repository at this point in the history
'brew tests --changed' supports changed test files
  • Loading branch information
MikeMcQuaid committed May 4, 2022
2 parents dc4d504 + 489d383 commit 6d0bf18
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Library/Homebrew/dev-cmd/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ def run_buildpulse
"--repository-id", ENV["HOMEBREW_BUILDPULSE_REPOSITORY_ID"]
end

def changed_test_files
changed_files = Utils.popen_read("git", "diff", "--name-only", "master")

raise UsageError, "No files have been changed from the master branch!" if changed_files.blank?

filestub_regex = %r{Library/Homebrew/([\w/-]+).rb}
changed_files.scan(filestub_regex).map(&:last).map do |filestub|
if filestub.start_with?("test/")
# Only run tests on *_spec.rb files in test/ folder
filestub.end_with?("_spec") ? Pathname("#{filestub}.rb") : nil
else
# For all other changed .rb files guess the associated test file name
Pathname("test/#{filestub}_spec.rb")
end
end.compact.select(&:exist?)
end

def tests
args = tests_args.parse

Expand Down Expand Up @@ -129,14 +146,7 @@ def tests
["test/#{test_name}_spec.rb:#{line}"]
end
elsif args.changed?
changed_files = Utils.popen_read("git", "diff", "--name-only", "master")

raise UsageError, "No files have been changed from the master branch!" if changed_files.blank?

filestub_regex = %r{Library/Homebrew/([\w/-]+).rb}
changed_files.scan(filestub_regex)
.map { |filestub| Pathname("test/#{filestub.last}_spec.rb") }
.select(&:exist?)
changed_test_files
else
Dir.glob("test/**/*_spec.rb")
end
Expand Down

0 comments on commit 6d0bf18

Please sign in to comment.