Class: KnapsackPro::Config::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/knapsack_pro/config/env.rb

Constant Summary collapse

LOG_LEVELS =
{
  'fatal'  => ::Logger::FATAL,
  'error'  => ::Logger::ERROR,
  'warn'  => ::Logger::WARN,
  'info'  => ::Logger::INFO,
  'debug' => ::Logger::DEBUG,
}

Class Method Summary collapse

Class Method Details

.branchObject



44
45
46
# File 'lib/knapsack_pro/config/env.rb', line 44

def branch
  env_for('KNAPSACK_PRO_BRANCH', :branch)
end

.branch_encryptedObject



130
131
132
# File 'lib/knapsack_pro/config/env.rb', line 130

def branch_encrypted
  ENV['KNAPSACK_PRO_BRANCH_ENCRYPTED']
end

.branch_encrypted?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/knapsack_pro/config/env.rb', line 134

def branch_encrypted?
  branch_encrypted == 'true'
end

.ci?Boolean

Returns:

  • (Boolean)


260
261
262
263
# File 'lib/knapsack_pro/config/env.rb', line 260

def ci?
  ENV.fetch('CI', 'false').downcase == 'true' ||
    detected_ci != KnapsackPro::Config::CI::Base
end

.ci_node_build_idObject



23
24
25
26
27
# File 'lib/knapsack_pro/config/env.rb', line 23

def ci_node_build_id
  env_name = 'KNAPSACK_PRO_CI_NODE_BUILD_ID'
  env_for(env_name, :node_build_id) ||
    raise("Missing environment variable #{env_name}. Read more at #{KnapsackPro::Urls::KNAPSACK_PRO_CI_NODE_BUILD_ID}")
end

.ci_node_indexObject



19
20
21
# File 'lib/knapsack_pro/config/env.rb', line 19

def ci_node_index
  @ci_node_index ||= (env_for('KNAPSACK_PRO_CI_NODE_INDEX', :node_index) || 0).to_i
end

.ci_node_retry_countObject



29
30
31
# File 'lib/knapsack_pro/config/env.rb', line 29

def ci_node_retry_count
  (env_for('KNAPSACK_PRO_CI_NODE_RETRY_COUNT', :node_retry_count) || 0).to_i
end

.ci_node_totalObject



15
16
17
# File 'lib/knapsack_pro/config/env.rb', line 15

def ci_node_total
  @ci_node_total ||= (env_for('KNAPSACK_PRO_CI_NODE_TOTAL', :node_total) || 1).to_i
end

.ci_providerObject



240
241
242
# File 'lib/knapsack_pro/config/env.rb', line 240

def ci_provider
  detected_ci.new.ci_provider
end

.commit_hashObject



40
41
42
# File 'lib/knapsack_pro/config/env.rb', line 40

def commit_hash
  env_for('KNAPSACK_PRO_COMMIT_HASH', :commit_hash)
end

.cucumber_queue_prefixObject



177
178
179
# File 'lib/knapsack_pro/config/env.rb', line 177

def cucumber_queue_prefix
  ENV.fetch('KNAPSACK_PRO_CUCUMBER_QUEUE_PREFIX', 'bundle exec')
end

.detected_ciObject



230
231
232
233
234
235
236
237
238
# File 'lib/knapsack_pro/config/env.rb', line 230

def detected_ci
  detected = KnapsackPro::Config::CI.constants.map do |constant|
    Object.const_get("KnapsackPro::Config::CI::#{constant}").new.detected
  end
    .compact
    .first

  detected || KnapsackPro::Config::CI::Base
end

.endpointObject



142
143
144
# File 'lib/knapsack_pro/config/env.rb', line 142

def endpoint
  ENV.fetch('KNAPSACK_PRO_ENDPOINT', 'https://api.knapsackpro.com')
end

.fallback_mode_enabledObject



114
115
116
# File 'lib/knapsack_pro/config/env.rb', line 114

def fallback_mode_enabled
  ENV.fetch('KNAPSACK_PRO_FALLBACK_MODE_ENABLED', true)
end

.fallback_mode_enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/knapsack_pro/config/env.rb', line 118

def fallback_mode_enabled?
  fallback_mode_enabled.to_s == 'true'
end

.fallback_mode_error_exit_codeObject



265
266
267
# File 'lib/knapsack_pro/config/env.rb', line 265

def fallback_mode_error_exit_code
  ENV.fetch('KNAPSACK_PRO_FALLBACK_MODE_ERROR_EXIT_CODE', 1).to_i
end

.fixed_queue_splitObject



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/knapsack_pro/config/env.rb', line 154

def fixed_queue_split
  @fixed_queue_split ||= begin
    env_name = 'KNAPSACK_PRO_FIXED_QUEUE_SPLIT'
    computed = env_for(env_name, :fixed_queue_split).to_s

    if !ENV.key?(env_name)
      KnapsackPro.logger.info("#{env_name} is not set. Using default value: #{computed}. Learn more at #{KnapsackPro::Urls::FIXED_QUEUE_SPLIT}")
    end

    computed
  end
end

.fixed_queue_split?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/knapsack_pro/config/env.rb', line 173

def fixed_queue_split?
  fixed_queue_split.to_s == 'true'
end

.fixed_queue_split_?Boolean

Returns:

  • (Boolean)


167
168
169
170
171
# File 'lib/knapsack_pro/config/env.rb', line 167

def fixed_queue_split_?
  return @fixed_queue_split_ if defined?(@fixed_queue_split_)

  @fixed_queue_split_ = env_for('KNAPSACK_PRO_FIXED_QUEUE_SPLIT', :fixed_queue_split).to_s == "true"
end

.fixed_test_suite_splitObject



146
147
148
# File 'lib/knapsack_pro/config/env.rb', line 146

def fixed_test_suite_split
  ENV.fetch('KNAPSACK_PRO_FIXED_TEST_SUITE_SPLIT', true)
end

.fixed_test_suite_split?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/knapsack_pro/config/env.rb', line 150

def fixed_test_suite_split?
  fixed_test_suite_split.to_s == 'true'
end

.log_dirObject



248
249
250
# File 'lib/knapsack_pro/config/env.rb', line 248

def log_dir
  ENV['KNAPSACK_PRO_LOG_DIR']
end

.log_levelObject



244
245
246
# File 'lib/knapsack_pro/config/env.rb', line 244

def log_level
  LOG_LEVELS[ENV['KNAPSACK_PRO_LOG_LEVEL'].to_s.downcase] || ::Logger::INFO
end

.masked_user_seatObject



56
57
58
59
60
# File 'lib/knapsack_pro/config/env.rb', line 56

def masked_user_seat
  return unless user_seat

  KnapsackPro::MaskString.call(user_seat)
end

.max_request_retriesObject



33
34
35
36
37
38
# File 'lib/knapsack_pro/config/env.rb', line 33

def max_request_retries
  number = ENV['KNAPSACK_PRO_MAX_REQUEST_RETRIES']
  if number
    number.to_i
  end
end

.node_uuidObject



288
289
290
291
# File 'lib/knapsack_pro/config/env.rb', line 288

def node_uuid
  env_name = 'KNAPSACK_PRO_NODE_UUID'
  ENV[env_name] || raise("Missing environment variable #{env_name}. Please report this as a bug: #{KnapsackPro::Urls::SUPPORT}")
end

.project_dirObject



48
49
50
# File 'lib/knapsack_pro/config/env.rb', line 48

def project_dir
  env_for('KNAPSACK_PRO_PROJECT_DIR', :project_dir)
end

.queue_idObject



106
107
108
# File 'lib/knapsack_pro/config/env.rb', line 106

def queue_id
  ENV['KNAPSACK_PRO_QUEUE_ID'] || raise('Missing Queue ID')
end

.queue_mode?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/knapsack_pro/config/env.rb', line 102

def queue_mode?
  ENV['KNAPSACK_PRO_QUEUE_MODE_ENABLED'] == 'true'
end

.regular_mode?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/knapsack_pro/config/env.rb', line 98

def regular_mode?
  ENV['KNAPSACK_PRO_REGULAR_MODE_ENABLED'] == 'true'
end

.repository_adapterObject



94
95
96
# File 'lib/knapsack_pro/config/env.rb', line 94

def repository_adapter
  ENV['KNAPSACK_PRO_REPOSITORY_ADAPTER']
end

.rspec_split_by_test_examples?Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/knapsack_pro/config/env.rb', line 181

def rspec_split_by_test_examples?
  return @rspec_split_by_test_examples if defined?(@rspec_split_by_test_examples)

  env = ENV['KNAPSACK_PRO_RSPEC_SPLIT_BY_TEST_EXAMPLES']

  if test_files_encrypted? && env.nil?
    KnapsackPro.logger.warn("Skipping split by test examples because test file names encryption is enabled:\n#{KnapsackPro::Urls::ENCRYPTION}\n#{KnapsackPro::Urls::SPLIT_BY_TEST_EXAMPLES}")
    return (@rspec_split_by_test_examples = false)
  end

  split = (env || true).to_s == 'true'

  if split && ci_node_total < 2
    KnapsackPro.logger.debug('Skipping split by test examples because tests are running on a single CI node (no parallelism)')
    @rspec_split_by_test_examples = false
  else
    @rspec_split_by_test_examples = split
  end
end

.rspec_test_example_detector_prefixObject



201
202
203
# File 'lib/knapsack_pro/config/env.rb', line 201

def rspec_test_example_detector_prefix
  ENV.fetch('KNAPSACK_PRO_RSPEC_TEST_EXAMPLE_DETECTOR_PREFIX', 'bundle exec')
end

.saltObject



138
139
140
# File 'lib/knapsack_pro/config/env.rb', line 138

def salt
  required_env('KNAPSACK_PRO_SALT')
end

.set_test_runner_adapter(adapter_class) ⇒ Object



256
257
258
# File 'lib/knapsack_pro/config/env.rb', line 256

def set_test_runner_adapter(adapter_class)
  ENV['KNAPSACK_PRO_TEST_RUNNER_ADAPTER'] = adapter_class.to_s.split('::').last
end

.slow_test_file_patternObject



66
67
68
# File 'lib/knapsack_pro/config/env.rb', line 66

def slow_test_file_pattern
  ENV['KNAPSACK_PRO_SLOW_TEST_FILE_PATTERN']
end

.slow_test_file_thresholdObject



70
71
72
# File 'lib/knapsack_pro/config/env.rb', line 70

def slow_test_file_threshold
  ENV.fetch('KNAPSACK_PRO_SLOW_TEST_FILE_THRESHOLD', nil)&.to_f
end

.slow_test_file_threshold?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/knapsack_pro/config/env.rb', line 74

def slow_test_file_threshold?
  !!slow_test_file_threshold
end

.subset_queue_idObject



110
111
112
# File 'lib/knapsack_pro/config/env.rb', line 110

def subset_queue_id
  ENV['KNAPSACK_PRO_SUBSET_QUEUE_ID'] || raise('Missing Subset Queue ID')
end

.test_dirObject



90
91
92
# File 'lib/knapsack_pro/config/env.rb', line 90

def test_dir
  ENV['KNAPSACK_PRO_TEST_DIR']
end

.test_file_exclude_patternObject



78
79
80
# File 'lib/knapsack_pro/config/env.rb', line 78

def test_file_exclude_pattern
  ENV['KNAPSACK_PRO_TEST_FILE_EXCLUDE_PATTERN']
end

.test_file_listObject



82
83
84
# File 'lib/knapsack_pro/config/env.rb', line 82

def test_file_list
  ENV['KNAPSACK_PRO_TEST_FILE_LIST']
end

.test_file_list_source_fileObject



86
87
88
# File 'lib/knapsack_pro/config/env.rb', line 86

def test_file_list_source_file
  ENV['KNAPSACK_PRO_TEST_FILE_LIST_SOURCE_FILE']
end

.test_file_patternObject



62
63
64
# File 'lib/knapsack_pro/config/env.rb', line 62

def test_file_pattern
  ENV['KNAPSACK_PRO_TEST_FILE_PATTERN']
end

.test_files_encryptedObject



122
123
124
# File 'lib/knapsack_pro/config/env.rb', line 122

def test_files_encrypted
  ENV['KNAPSACK_PRO_TEST_FILES_ENCRYPTED']
end

.test_files_encrypted?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/knapsack_pro/config/env.rb', line 126

def test_files_encrypted?
  test_files_encrypted == 'true'
end

.test_queue_idObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/knapsack_pro/config/env.rb', line 269

def test_queue_id
  knapsack_env_name = 'KNAPSACK_PRO_TEST_QUEUE_ID'
  knapsack_env_value = ENV[knapsack_env_name]

  ci_env_value = detected_ci.new.test_queue_id

  if !knapsack_env_value.nil? && !ci_env_value.nil? && knapsack_env_value != ci_env_value.to_s
    warn("You have set the environment variable #{knapsack_env_name} to #{knapsack_env_value} which could be automatically determined from the CI environment as #{ci_env_value}.")
  end

  id = knapsack_env_value != nil ? knapsack_env_value : ci_env_value
  return id unless id.nil?

  triplet = [ci_node_total, branch, commit_hash]
  return triplet.join('-') if triplet.all?

  raise("Missing test_queue_id. See: #{KnapsackPro::Urls::KNAPSACK_PRO_TEST_QUEUE_ID}")
end

.test_runner_adapterObject



252
253
254
# File 'lib/knapsack_pro/config/env.rb', line 252

def test_runner_adapter
  ENV['KNAPSACK_PRO_TEST_RUNNER_ADAPTER']
end

.test_suite_tokenObject



205
206
207
208
# File 'lib/knapsack_pro/config/env.rb', line 205

def test_suite_token
  env_name = 'KNAPSACK_PRO_TEST_SUITE_TOKEN'
  ENV[env_name] || raise("Missing environment variable #{env_name}. You should set environment variable like #{env_name}_RSPEC (note there is suffix _RSPEC at the end). knapsack_pro gem will set #{env_name} based on #{env_name}_RSPEC value. If you use other test runner than RSpec then use proper suffix.")
end

.test_suite_token_cucumberObject



222
223
224
# File 'lib/knapsack_pro/config/env.rb', line 222

def test_suite_token_cucumber
  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_CUCUMBER']
end

.test_suite_token_minitestObject



214
215
216
# File 'lib/knapsack_pro/config/env.rb', line 214

def test_suite_token_minitest
  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_MINITEST']
end

.test_suite_token_rspecObject



210
211
212
# File 'lib/knapsack_pro/config/env.rb', line 210

def test_suite_token_rspec
  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_RSPEC']
end

.test_suite_token_spinachObject



226
227
228
# File 'lib/knapsack_pro/config/env.rb', line 226

def test_suite_token_spinach
  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_SPINACH']
end

.test_suite_token_test_unitObject



218
219
220
# File 'lib/knapsack_pro/config/env.rb', line 218

def test_suite_token_test_unit
  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN_TEST_UNIT']
end

.user_seatObject



52
53
54
# File 'lib/knapsack_pro/config/env.rb', line 52

def user_seat
  env_for('KNAPSACK_PRO_USER_SEAT', :user_seat)
end