Module: Kettle::Dev::BundlerEnvGuard

Defined in:
lib/kettle/dev/bundler_env_guard.rb

Constant Summary collapse

RESET_ENV_KEYS =
%w[
  BUNDLE_BIN_PATH
  BUNDLE_FROZEN
  BUNDLE_GEMFILE
  BUNDLE_LOCKFILE
  BUNDLER_SETUP
  BUNDLER_VERSION
].freeze
RESET_ENV_PREFIXES =

Bundler uses these markers to restore the environment of the parent bundle. They are not inert when a child explicitly selects another Gemfile: Bundler can use BUNDLER_ORIG_BUNDLE_GEMFILE to reselect the parent bundle. Clear the dynamic keys along with RESET_ENV_KEYS.

%w[
  BUNDLER_ORIG_
].freeze
INERT_ENV_KEYS =
%w[
  BUNDLE_DEBUG
  BUNDLE_DISABLE_CHECKSUM_VALIDATION
  BUNDLE_QUIET
  BUNDLE_SILENCE_DEPRECATIONS
  BUNDLE_SILENCE_ROOT_WARNING
  BUNDLE_SUPPRESS_INSTALL_USING_MESSAGES
  BUNDLE_VERBOSE
  BUNDLER_DEBUG
].freeze

Class Method Summary collapse

Class Method Details

.expected_env_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/kettle/dev/bundler_env_guard.rb', line 68

def expected_env_key?(key)
  RESET_ENV_KEYS.include?(key) ||
    INERT_ENV_KEYS.include?(key) ||
    RESET_ENV_PREFIXES.any? { |prefix| key.start_with?(prefix) }
end

.reset_warning_cache!Object

rubocop:disable ThreadSafety/ClassInstanceVariable -- warning state is intentionally shared by module functions.



60
61
62
# File 'lib/kettle/dev/bundler_env_guard.rb', line 60

def reset_warning_cache!
  @warned_unexpected_env_keys = {}
end

.unbundled_envObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kettle/dev/bundler_env_guard.rb', line 36

def unbundled_env
  env = RESET_ENV_KEYS.each_with_object({}) do |key, result|
    result[key] = nil
  end
  ENV.each_key do |key|
    next unless RESET_ENV_PREFIXES.any? { |prefix| key.start_with?(prefix) }

    env[key] = nil
  end
  env
end

.unexpected_env_keysObject



64
65
66
# File 'lib/kettle/dev/bundler_env_guard.rb', line 64

def unexpected_env_keys
  ENV.to_hash.keys.grep(/\ABUNDLE(?:R)?_/).reject { |key| expected_env_key?(key) }.sort
end

.warn_unexpected_env!(stream: $stderr) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/kettle/dev/bundler_env_guard.rb', line 48

def warn_unexpected_env!(stream: $stderr)
  unexpected = unexpected_env_keys
  return if unexpected.empty?
  return if warned_unexpected_env?(unexpected)

  stream.puts(
    "[kettle-dev] Unexpected Bundler environment variable(s) present while preparing an unbundled subprocess: #{unexpected.join(", ")}. " \
      "If this variable is safe, add it to BundlerEnvGuard::INERT_ENV_KEYS; otherwise add it to RESET_ENV_KEYS."
  )
end

.warned_unexpected_env?(keys) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# File 'lib/kettle/dev/bundler_env_guard.rb', line 74

def warned_unexpected_env?(keys)
  @warned_unexpected_env_keys ||= {}
  signature = keys.join("\0")
  return true if @warned_unexpected_env_keys[signature]

  @warned_unexpected_env_keys[signature] = true
  false
end