Skip to content

Commit

Permalink
Add Gemfile predictor strategy
Browse files Browse the repository at this point in the history
If the Gemfile (or Gemfile.lock) has changed, all tests neeed to be run
to ensure correctness.
  • Loading branch information
langsharpe committed Oct 17, 2022
1 parent be8888a commit 46c0ddc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/crystalball.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'crystalball/rspec/runner'
require 'crystalball/prediction'
require 'crystalball/predictor'
require 'crystalball/predictor/gemfile'
require 'crystalball/predictor/modified_execution_paths'
require 'crystalball/predictor/modified_specs'
require 'crystalball/predictor/modified_support_specs'
Expand Down
26 changes: 26 additions & 0 deletions lib/crystalball/predictor/gemfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'crystalball/predictor/strategy'

module Crystalball
class Predictor
# Used with `predictor.use Crystalball::Predictor::Gemfile.new`. Return every file if the Gemfile or Gemfile.lock file are changed
class Gemfile
include Strategy

# @param [Crystalball::SourceDiff] diff - the diff from which to predict
# which specs should run
# @param [Crystalball::ExampleGroupMap] map - the map with the relations of
# examples and used files
# @return [Array<String>] the spec paths associated with the changes
def call(diff, map)
super do
return [] unless diff.detect{ |file_diff| ['Gemfile', 'Gemfile.lock'].include?(file_diff.path) }
map.example_groups.keys.compact
end
end

private

attr_reader :spec_pattern
end
end
end
31 changes: 31 additions & 0 deletions spec/predictor/gemfile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'spec_helper'

describe Crystalball::Predictor::Gemfile do
subject(:predictor) { described_class.new }
let(:repository) { Git::Base.new }
let(:path1) { 'Gemfile' }
let(:file_diff1) { Crystalball::SourceDiff::FileDiff.new(Git::Diff::DiffFile.new(repository, path: path1)) }
let(:diff) { [file_diff1] }
let(:map) { instance_double('Crystalball::MapGenerator::ExecutionMap', example_groups: example_groups) }
let(:example_groups) { {'spec_file' => [], 'spec_file_2' => [] } }

describe '#call' do
subject { predictor.call(diff, map) }

it { is_expected.to eq(['./spec_file', './spec_file_2']) }

context 'when gemfile.lock is included in changed files' do
let(:path1) { 'Gemfile.lock' }

it { is_expected.to eq(['./spec_file', './spec_file_2']) }
end

context 'when neither Gemfile or Gemfile.lock are changed' do
let(:path1) { 'version.rb' }

it { is_expected.to eq([]) }
end
end
end

0 comments on commit 46c0ddc

Please sign in to comment.