| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| use home::home_dir; |
| use std::path::PathBuf; |
| |
| pub fn get_os() -> String { |
| convert_macos_to_darwin(std::env::consts::OS) |
| } |
| |
| pub fn get_arch() -> String { |
| std::env::consts::ARCH.to_string() |
| } |
| |
| fn convert_macos_to_darwin(arch: &str) -> String { |
| arch.replace("macos", "Darwin") |
| } |
| |
| pub fn analytics_folder() -> String { |
| let mut metrics_path = get_home_dir(); |
| metrics_path.push(".fuchsia/metrics/"); |
| let path_str = metrics_path.to_str(); |
| match path_str { |
| Some(v) => String::from(v), |
| None => String::from("/tmp/.fuchsia/metrics/"), |
| } |
| } |
| |
| fn get_home_dir() -> PathBuf { |
| match home_dir() { |
| Some(dir) => dir, |
| None => PathBuf::from("/tmp"), |
| } |
| } |
| |
| const TEST_ENV_VAR: &'static str = "FUCHSIA_TEST_OUTDIR"; |
| const ANALYTICS_DISABLED_ENV_VAR: &'static str = "FUCHSIA_ANALYTICS_DISABLED"; |
| |
| // To detect certain bot environmments that run as USER "builder" |
| const USER: &'static str = "USER"; |
| const BUILDER: &'static str = "builder"; |
| |
| const BOT_ENV_VARS: &'static [&'static str] = &[ |
| "TF_BUILD", // Azure |
| "bamboo.buildKey", // Bamboo |
| "BUILDKITE", // BUILDKITE |
| "CIRCLECI", // Circle |
| "CIRRUS_CI", // Cirrus |
| "CODEBUILD_BUILD_ID", // Codebuild |
| "UNITTEST_ON_BORG", // Borg |
| "UNITTEST_ON_FORGE", // Forge |
| "SWARMING_BOT_ID", // Fuchsia |
| "GITHUB_ACTIONS", // GitHub Actions |
| "GITLAB_CI", // GitLab |
| "HEROKU_TEST_RUN_ID", // Heroku |
| "BUILD_ID", // Hudson & Jenkins |
| "TEAMCITY_VERSION", // Teamcity |
| "TRAVIS", // Travis |
| "BUILD_NUMBER", // android-ci |
| ]; |
| |
| pub fn is_test_env() -> bool { |
| std::env::var(TEST_ENV_VAR).is_ok() |
| } |
| |
| pub fn is_fuchsia_analytics_disabled_set() -> bool { |
| std::env::var(ANALYTICS_DISABLED_ENV_VAR).is_ok() |
| } |
| |
| pub fn is_running_in_ci_bot_env() -> bool { |
| BOT_ENV_VARS.iter().any(|env_var| std::env::var(env_var).is_ok()) |
| } |
| |
| pub fn is_user_a_bot() -> bool { |
| std::env::var(USER).is_ok_and(|v| v == BUILDER) |
| } |
| |
| pub fn is_analytics_disabled_by_env() -> bool { |
| is_test_env() |
| || is_fuchsia_analytics_disabled_set() |
| || is_running_in_ci_bot_env() |
| || is_user_a_bot() |
| } |
| |
| #[cfg(test)] |
| mod test { |
| use super::*; |
| |
| #[test] |
| // TODO(https://fxbug.dev/42148443): isolate the env test from CI env |
| #[ignore] |
| // Rust tests are run in parallel in threads, which means that this test is |
| // disruptive to other tests. There's little ROI to doing some kind of fork |
| // dance here, so the test is included, but not run by default. |
| pub fn test_is_test_env() { |
| std::env::set_var(TEST_ENV_VAR, "somepath"); |
| assert_eq!(true, is_test_env()); |
| std::env::remove_var(TEST_ENV_VAR); |
| assert_eq!(false, is_test_env()); |
| } |
| |
| #[test] |
| #[ignore] |
| // Rust tests are run in parallel in threads, which means that this test is |
| // disruptive to other tests. There's little ROI to doing some kind of fork |
| // dance here, so the test is included, but not run by default. |
| pub fn test_is_analytics_disabled_env() { |
| std::env::set_var(ANALYTICS_DISABLED_ENV_VAR, "1"); |
| assert_eq!(true, is_fuchsia_analytics_disabled_set()); |
| std::env::remove_var(ANALYTICS_DISABLED_ENV_VAR); |
| assert_eq!(false, is_fuchsia_analytics_disabled_set()); |
| } |
| |
| #[test] |
| // TODO(https://fxbug.dev/42148443): isolate the env test from CI env |
| #[ignore] |
| pub fn test_is_bot_env() { |
| std::env::set_var(&"BUILD_ID", "1"); |
| assert_eq!(true, is_running_in_ci_bot_env()); |
| std::env::remove_var(&"BUILD_ID"); |
| assert_eq!(false, is_fuchsia_analytics_disabled_set()); |
| } |
| |
| #[test] |
| pub fn test_arch_macos_conversion() { |
| assert_eq!("Darwin", convert_macos_to_darwin("macos")) |
| } |
| } |