Skip to content

Instantly share code, notes, and snippets.

@pedrocarmona
Created June 19, 2020 15:56
Show Gist options
  • Save pedrocarmona/637f1ce2da0046a7b6cd06ab24231aa1 to your computer and use it in GitHub Desktop.
Save pedrocarmona/637f1ce2da0046a7b6cd06ab24231aa1 to your computer and use it in GitHub Desktop.
Displays tests added in the new PR branch
# frozen_string_literal: true
require "rspec/core/formatters/base_text_formatter"
class CustomJsonFormatter
# This registers the notifications this formatter supports, and tells
# us that this was written against the RSpec 3.x formatter API.
RSpec::Core::Formatters.register self, :example_started
def initialize(output)
@output = output
end
def example_started(notification)
@output << {
full_description: notification.example.full_description,
location: notification.example.location
}.to_json
@output << ",\n"
end
end
#!/usr/bin/env ruby
# frozen_string_literal: true
require "json"
results = `git diff --name-status master | grep _spec.rb`
files_by_git_action = { A: [], M: [], D: [] }
results.split("\n").each do |result_line|
git_action, file = result_line.split("\t")
files_by_git_action[git_action.to_sym] << file
end
`git checkout master`
puts "Running specs for files changed or deleted"
master_output = `bundle exec rspec #{(files_by_git_action[:M] + files_by_git_action[:D]).join(" ")} --require ./spec/support/custom_json_formatter.rb --format CustomJsonFormatter`
`git checkout -`
puts "Running specs for files changed or created"
branch_output = `bundle exec rspec #{(files_by_git_action[:M] + files_by_git_action[:A]).join(" ")} --require ./spec/support/custom_json_formatter.rb --format CustomJsonFormatter`
master_rows = JSON.parse("[#{master_output[0..-3]}]")
master_full_descriptions = master_rows.map{ |row| row["full_description"] }
branch_rows = JSON.parse("[#{branch_output[0..-3]}]")
branch_full_descriptions = branch_rows.map{ |row| row["full_description"] }
new_examples = branch_full_descriptions.uniq - (master_full_descriptions.uniq & branch_full_descriptions.uniq)
locations = branch_rows.select{ |row| new_examples.include?(row["full_description"]) }.map{ |row| row["location"] }
pr_changed_tests = "bundle exec rspec #{locations.sort.join(" ")} --format documentation"
puts pr_changed_tests
puts "Running specs...\n"
puts `#{pr_changed_tests}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment