Skip to content

Commit ef06f3f

Browse files
Googlercopybara-github
authored andcommitted
test_sharding: support alternative sharding environment variables
PiperOrigin-RevId: 718988235
1 parent b8d251a commit ef06f3f

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

googletest/src/internal/test_sharding.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
/// See also <https://google.github.io/googletest/advanced.html>
99
use std::cell::OnceCell;
1010
use std::env::{var, var_os};
11-
use std::ffi::OsStr;
11+
use std::ffi::{OsStr, OsString};
1212
use std::fs::{self, File};
1313
use std::num::NonZeroU64;
1414
use std::path::{Path, PathBuf};
1515

1616
/// Environment variable specifying the total number of test shards.
17-
const TEST_TOTAL_SHARDS: &str = "GTEST_TOTAL_SHARDS";
17+
const TEST_TOTAL_SHARDS: &[&str] = &["GTEST_TOTAL_SHARDS", "TEST_TOTAL_SHARDS"];
1818

1919
/// Environment variable specifyign the index of this test shard.
20-
const TEST_SHARD_INDEX: &str = "GTEST_SHARD_INDEX";
20+
const TEST_SHARD_INDEX: &[&str] = &["GTEST_SHARD_INDEX", "TEST_SHARD_INDEX"];
2121

2222
/// Environment variable specifying the name of the file we create (or cause a
2323
/// timestamp change on) to indicate that we support the sharding protocol.
24-
const TEST_SHARD_STATUS_FILE: &str = "GTEST_SHARD_STATUS_FILE";
24+
const TEST_SHARD_STATUS_FILE: &[&str] = &["GTEST_SHARD_STATUS_FILE", "TEST_SHARD_STATUS_FILE"];
2525

2626
thread_local! {
2727
static SHARDING: OnceCell<Sharding> = const { OnceCell::new() };
@@ -44,29 +44,47 @@ pub fn test_should_run(test_case_hash: u64) -> bool {
4444
})
4545
}
4646

47+
fn get_var(keys: &[&str]) -> Option<String> {
48+
for key in keys {
49+
if let Ok(value) = var(OsStr::new(key)) {
50+
return Some(value);
51+
}
52+
}
53+
54+
None
55+
}
56+
57+
fn get_var_os(keys: &[&str]) -> Option<OsString> {
58+
for key in keys {
59+
if let Some(value) = var_os(OsStr::new(key)) {
60+
return Some(value);
61+
}
62+
}
63+
64+
None
65+
}
66+
4767
impl Sharding {
4868
fn test_should_run(&self, test_case_hash: u64) -> bool {
4969
(test_case_hash % self.total_shards.get()) == self.this_shard
5070
}
5171

5272
fn from_environment() -> Sharding {
5373
let this_shard: Option<u64> =
54-
{ var(OsStr::new(TEST_SHARD_INDEX)).ok().and_then(|value| value.parse().ok()) };
74+
{ get_var(TEST_SHARD_INDEX).and_then(|value| value.parse().ok()) };
5575
let total_shards: Option<NonZeroU64> = {
56-
var(OsStr::new(TEST_TOTAL_SHARDS))
57-
.ok()
76+
get_var(TEST_TOTAL_SHARDS)
5877
.and_then(|value| value.parse().ok())
5978
.and_then(NonZeroU64::new)
6079
};
6180

6281
match (this_shard, total_shards) {
6382
(Some(this_shard), Some(total_shards)) if this_shard < total_shards.get() => {
64-
if let Some(name) = var_os(OsStr::new(TEST_SHARD_STATUS_FILE)) {
83+
if let Some(name) = get_var_os(TEST_SHARD_STATUS_FILE) {
6584
let pathbuf = PathBuf::from(name);
6685
if let Err(e) = create_status_file(&pathbuf) {
6786
eprintln!(
68-
"failed to create {} file {}: {}",
69-
TEST_SHARD_STATUS_FILE,
87+
"failed to create $GTEST_SHARD_STATUS_FILE file {}: {}",
7088
pathbuf.display(),
7189
e
7290
);

0 commit comments

Comments
 (0)