1// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5interface SupportedEnvVars {
6  NO_SHUFFLE: boolean;      // Whether or not to shuffle tests.
7  STRESS: boolean;          // Stress test (slowdown CPU; multiple iterations)
8  VERBOSE: boolean;         // Log stdout from the workers.
9  THROTTLE: number;         // CPU throttle multiplier.
10  TEST_LIST: string;        // Absolute path to the test list.
11  TEST_FILE: string;        // Absolute path to the test file from the test list to run in isolation.
12  DEBUG: boolean;           // Debug mode. When enabled, has longer timeouts and runs Chrome in head mode.
13  ITERATIONS: number;       // Number of test iterations.
14  JOBS: number;             // Number of workers to use.
15  SLOWMO: number;           // Number of milliseconds between actions. Recommended value: 50.
16  CHROME_BIN: string;       // Absolute path to the Chrome binary.
17  INTERACTIVE: boolean;     // [Unused]: Placeholder for screenshot diffing.
18  TIMEOUT: number;          // The timeout in ms to wait for tests.
19  CHROME_FEATURES: string;  // --enable-features={} for the Chrome binary.
20}
21
22export function getEnvVar<Key extends keyof SupportedEnvVars>(
23    name: Key, defaultValue?: SupportedEnvVars[Key]): SupportedEnvVars[Key] {
24  const envVar = process.env[name];
25
26  if (typeof defaultValue === 'boolean') {
27    return (!!envVar) as SupportedEnvVars[Key];
28  }
29
30  if (typeof defaultValue === 'number') {
31    let value = Number(envVar);
32    if (Number.isNaN(value)) {
33      value = defaultValue;
34    }
35    return value as SupportedEnvVars[Key];
36  }
37
38  return (envVar || defaultValue) as SupportedEnvVars[Key];
39}
40