Module: SimpleCov::RunIdentity

Extended by:
RunIdentity
Included in:
RunIdentity
Defined in:
lib/simplecov/run_identity.rb

Overview

Identifies one test invocation and each top-level parallel worker within it. Forked subprocesses inherit both values from their parent worker.

Defined Under Namespace

Modules: Accessors

Instance Method Summary collapse

Instance Method Details

#authoritative?Boolean

Decided where the id is generated, never re-inferred from the id's shape, so an explicit SIMPLECOV_RUN_ID that happens to look like an inferred one is still trusted.

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/simplecov/run_identity.rb', line 41

def authoritative?
  materialize_current
  @authoritative
end

#currentObject



46
47
48
49
# File 'lib/simplecov/run_identity.rb', line 46

def current
  materialize_current
  @current
end

#current_worker_idObject



55
56
57
# File 'lib/simplecov/run_identity.rb', line 55

def current_worker_id
  @current_worker_id ||= worker_id
end

#generateObject

Answers the run id together with its provenance: true when the id alone proves same-run membership (explicitly configured, derived from parallel_tests' per-invocation pid file, or freshly random), false when it was inferred from the parent pid, which consecutive runs launched by the same long-lived parent process share.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simplecov/run_identity.rb', line 16

def generate
  explicit = ENV.fetch("SIMPLECOV_RUN_ID", nil)
  return [explicit, true] if explicit && !explicit.empty?

  pid_file = ENV.fetch("PARALLEL_PID_FILE", nil)
  return ["parallel-tests:#{File.basename(pid_file)}", true] if pid_file && !pid_file.empty?

  return ["parallel-parent:#{Process.ppid}", false] if ParallelAdapters.current

  [SecureRandom.uuid, true]
end

#materialize_currentObject



51
52
53
# File 'lib/simplecov/run_identity.rb', line 51

def materialize_current
  @current, @authoritative = generate unless instance_variable_defined?(:@current)
end

#prepareObject



59
60
61
62
63
# File 'lib/simplecov/run_identity.rb', line 59

def prepare
  ParallelAdapters.current
  SimpleCov.run_id
  SimpleCov.worker_id
end

#worker_idObject



28
29
30
31
32
33
34
35
36
# File 'lib/simplecov/run_identity.rb', line 28

def worker_id
  explicit = ENV.fetch("SIMPLECOV_WORKER_ID", nil)
  return explicit if explicit && !explicit.empty?

  number = ENV.fetch("TEST_ENV_NUMBER", nil)
  return number.empty? ? "1" : number if number

  Process.pid.to_s
end