Module: CodeTeams::Testing

Extended by:
T::Sig
Defined in:
lib/code_teams/testing.rb,
lib/code_teams/testing/rspec_helpers.rb

Overview

Utilities for tests that need a controlled set of teams without writing YML files to disk.

Opt-in by requiring code_teams/testing.

Defined Under Namespace

Modules: CodeTeamsExtension, RSpecHelpers

Constant Summary collapse

THREAD_KEY =
T.let(:__code_teams_collection, Symbol)

Class Method Summary collapse

Class Method Details

.code_teamsObject



58
59
60
61
62
63
64
# File 'lib/code_teams/testing.rb', line 58

def self.code_teams
  existing = Thread.current[THREAD_KEY]
  return existing if existing.is_a?(Array)

  Thread.current[THREAD_KEY] = []
  T.cast(Thread.current[THREAD_KEY], T::Array[CodeTeams::Team])
end

.create_code_team(attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/code_teams/testing.rb', line 44

def self.create_code_team(attributes)
  attributes = attributes.dup
  attributes[:name] ||= "Fake Team #{SecureRandom.hex(4)}"

  code_team = CodeTeams::Team.new(
    config_yml: 'tmp/fake_config.yml',
    raw_hash: Utils.deep_stringify_keys(attributes)
  )

  code_teams << code_team
  code_team
end

.enable!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/code_teams/testing.rb', line 21

def self.enable!
  return if @enabled

  CodeTeams.prepend(CodeTeamsExtension)
  @enabled = true

  return unless defined?(RSpec)

  T.unsafe(RSpec).configure do |config|
    config.include CodeTeams::Testing::RSpecHelpers

    config.around do |example|
      example.run
      # Bust caches because plugins may hang onto stale data between examples.
      if CodeTeams::Testing.code_teams.any?
        CodeTeams.bust_caches!
        CodeTeams::Testing.reset!
      end
    end
  end
end

.reset!Object



67
68
69
# File 'lib/code_teams/testing.rb', line 67

def self.reset!
  Thread.current[THREAD_KEY] = []
end