Module: RSpecTracer::Tracker::EnvMatcher Private

Defined in:
lib/rspec_tracer/tracker/env_matcher.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Wildcard env matching helper.

Lives outside Configuration so configure’s alias loop does not leak its private helpers as public _name DSL surface (memory: feedback_configure_dsl_private_leak). Pure utility module; def self.x style for mutant observability (memory: feedback_mutation_friendly_modules). ASCII-only source (memory: feedback_mutant_non_ascii_source).

Patterns accepted:

- Literal env name              "AUTH_TOKEN"
- Single trailing wildcard      "RAILS_*"
- Single leading wildcard       "*_TOKEN"
- Bare wildcard (matches all)   "*"

Patterns rejected (raise ArgumentError):

- Multi-segment / embedded *    "RAILS_*_ENV"
- Multiple *                    "RAILS_*_*"
- Character classes             "RAILS_[A-Z]*"
- Negation / glob escape        "RAILS_!ENV", "RAILS_\X"
- Question mark                 "RAILS_?ENV"
- Empty / nil

Constant Summary collapse

WILDCARD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Internal constant.

'*'
DISALLOWED_CHARS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Internal constant.

%w[? [ ] ! \\].freeze

Class Method Summary collapse

Class Method Details

.expand(patterns, env: ::ENV) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Expand a list of patterns against env keys. Literals pass through; wildcards are anchored + grepped against env.keys. Returns a deduped Array<String> in input order.

‘env` is injectable for testability; defaults to ::ENV. Reads only env.keys, never values.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rspec_tracer/tracker/env_matcher.rb', line 48

def self.expand(patterns, env: ::ENV)
  result = []
  patterns.each do |pattern|
    str = pattern.to_s
    validate!(str)
    if wildcard?(str)
      re = glob_to_regex(str)
      env.each_key { |k| result << k if re.match?(k) }
    else
      result << str
    end
  end
  result.uniq
end

.match_glob?(pattern, name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Boolean: does ‘name` match `pattern`? Single-pattern variant of expand for ad-hoc checks (specs, future call sites). Named `match_glob?` (predicate suffix) per Naming/PredicateMethod.

Returns:

  • (Boolean)


66
67
68
69
70
71
72
# File 'lib/rspec_tracer/tracker/env_matcher.rb', line 66

def self.match_glob?(pattern, name)
  str = pattern.to_s
  validate!(str)
  return str == name.to_s unless wildcard?(str)

  glob_to_regex(str).match?(name.to_s)
end

.validate!(pattern) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raise ArgumentError on unsupported pattern syntax. Called before any regex build so users see a clear message at config-load time (Engine#setup) or at RunnerHook Pass 1 (per-example metadata) rather than a regex parse crash. rubocop:disable Metrics/PerceivedComplexity

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rspec_tracer/tracker/env_matcher.rb', line 79

def self.validate!(pattern)
  if pattern.nil? || pattern.empty?
    raise ArgumentError,
          "track_env pattern must be a non-empty String (got #{pattern.inspect})"
  end

  DISALLOWED_CHARS.each do |c|
    next unless pattern.include?(c)

    raise ArgumentError,
          "track_env pattern #{pattern.inspect} contains unsupported character " \
          "#{c.inspect} (allowed: literals, single trailing/leading *)"
  end

  return if pattern == WILDCARD

  stars = pattern.count(WILDCARD)
  return if stars.zero?

  if stars > 1
    raise ArgumentError,
          "track_env pattern #{pattern.inspect} contains multiple wildcards " \
          '(only one * is allowed, at the start or end)'
  end

  return if pattern.start_with?(WILDCARD) || pattern.end_with?(WILDCARD)

  raise ArgumentError,
        "track_env pattern #{pattern.inspect} has an embedded wildcard " \
        '(only single trailing/leading * is supported, e.g. PREFIX_* or *_SUFFIX)'
end

.wildcard?(pattern) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Boolean: does the pattern contain at least one wildcard?

Returns:

  • (Boolean)


38
39
40
# File 'lib/rspec_tracer/tracker/env_matcher.rb', line 38

def self.wildcard?(pattern)
  pattern.to_s.include?(WILDCARD)
end