Class: Packs::Rails::Integrations::RSpec

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/packs/rails/integrations/rspec.rb

Instance Method Summary collapse

Constructor Details

#initializeRSpec

Returns a new instance of RSpec.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/packs/rails/integrations/rspec.rb', line 9

def initialize
  # This is the list of directories RSpec was told to run.
  to_run = ::RSpec.configuration.instance_variable_get(:@files_or_directories_to_run)
  default_path = ::RSpec.configuration.default_path

  if to_run == [default_path]
    # This is the default case when you run `rspec`. We want to add all the pack's spec paths
    # to the collection of directories to run.

    pack_paths = Packs.all.map do |pack|
      next if pack.is_gem?
      spec_path = pack.relative_path.join(default_path)
      spec_path.to_s if spec_path.exist?
    end

    to_run.concat(pack_paths)
  else
    # This is when `rspec` is run with a list of directories or files. We scan this list to see
    # if any of them matches a pack's directory. If it does, we concat the `default_path` to the
    # end of it.
    #
    # packs/my_pack => packs/my_pack/spec
    # 
    # If it doesn't match a pack path, we leave it alone.

    to_run.map! do |path|
      if pack = Packs.find(path)
        [
          pack,
          *nested_packs_for(pack)
        ].map do |pack|
          spec_path = pack.relative_path.join(default_path)
          spec_path.to_s if spec_path.exist?
        end
      else
        path
      end
    end
  end

  ::RSpec.configuration.files_or_directories_to_run = to_run.flatten.compact.uniq
end

Instance Method Details

#nested_packs_for(parent_pack) ⇒ Object



53
54
55
56
57
# File 'lib/packs/rails/integrations/rspec.rb', line 53

def nested_packs_for(parent_pack)
  Packs.all.select do |pack|
    pack.name != parent_pack.name && pack.name.include?(parent_pack.name)
  end
end