Skip to content

Commit 03d5a68

Browse files
EliahKaganByron
authored andcommitted
feat: Recognize GIX_TEST_CREATE_ARCHIVES_EVEN_ON_CI
When `gix-testtools` runs fixtures, it now recognizes a new environment variable, `GIX_TEST_CREATE_ARCHIVES_EVEN_ON_CI`, specifying that archives should be generated even when on CI. By default, they are still not generated when on CI. It may make sense to enable this: - If automatically testing archive creation, or - As a way to check that all intended generated arhives are committed (which is the motivating use case for this feature), or - If actually using CI to generate archives that will be uploaded as artifacts, or - In unusual non-CI environments that are mis-detected as CI (though that should usually be investigated and fixed, since some software performs destructive operations more readily without interactive checks when CI is detected). The usual reason for not generating archives on CI is that they would not typically be preserved. Thus refraining from generating them on CI remains the default behavior. Like the `GIX_TEST_IGNORE_ARCHIVES` environment variable, the new variable `GIX_TEST_CREATE_ARCHIVES_EVEN_ON_CI` is currently interpreted as "true" based solely on its presence. This is to say that is actual value is currently not examined.
1 parent 8b51b3a commit 03d5a68

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

tests/tools/src/lib.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use std::{
1212
collections::BTreeMap,
13+
env,
1314
ffi::OsString,
1415
io::Read,
1516
path::{Path, PathBuf},
@@ -150,8 +151,8 @@ fn git_version_from_bytes(bytes: &[u8]) -> Result<(u8, u8, u8)> {
150151

151152
/// Set the current working dir to `new_cwd` and return a type that returns to the previous working dir on drop.
152153
pub fn set_current_dir(new_cwd: impl AsRef<Path>) -> std::io::Result<AutoRevertToPreviousCWD> {
153-
let cwd = std::env::current_dir()?;
154-
std::env::set_current_dir(new_cwd)?;
154+
let cwd = env::current_dir()?;
155+
env::set_current_dir(new_cwd)?;
155156
Ok(AutoRevertToPreviousCWD(cwd))
156157
}
157158

@@ -166,7 +167,7 @@ pub struct AutoRevertToPreviousCWD(PathBuf);
166167

167168
impl Drop for AutoRevertToPreviousCWD {
168169
fn drop(&mut self) {
169-
std::env::set_current_dir(&self.0).unwrap();
170+
env::set_current_dir(&self.0).unwrap();
170171
}
171172
}
172173

@@ -461,7 +462,7 @@ fn scripted_fixture_read_only_with_args_inner(
461462
crc_digest.update(&std::fs::read(&script_path).unwrap_or_else(|err| {
462463
panic!(
463464
"file {script_path:?} in CWD {:?} could not be read: {err}",
464-
std::env::current_dir().expect("valid cwd"),
465+
env::current_dir().expect("valid cwd"),
465466
)
466467
}));
467468
for arg in &args {
@@ -548,7 +549,7 @@ fn scripted_fixture_read_only_with_args_inner(
548549
script_location.display()
549550
);
550551
}
551-
let script_absolute_path = std::env::current_dir()?.join(script_path);
552+
let script_absolute_path = env::current_dir()?.join(script_path);
552553
let mut cmd = std::process::Command::new(&script_absolute_path);
553554
let output = match configure_command(&mut cmd, &args, &script_result_directory).output() {
554555
Ok(out) => out,
@@ -593,7 +594,7 @@ fn configure_command<'a>(
593594
args: &[String],
594595
script_result_directory: &Path,
595596
) -> &'a mut std::process::Command {
596-
let mut msys_for_git_bash_on_windows = std::env::var_os("MSYS").unwrap_or_default();
597+
let mut msys_for_git_bash_on_windows = env::var_os("MSYS").unwrap_or_default();
597598
msys_for_git_bash_on_windows.push(" winsymlinks:nativestrict");
598599
cmd.args(args)
599600
.stdout(std::process::Stdio::piped())
@@ -632,6 +633,14 @@ fn write_failure_marker(failure_marker: &Path) {
632633
std::fs::write(failure_marker, []).ok();
633634
}
634635

636+
fn should_skip_all_archive_creation() -> bool {
637+
// On Windows, we fail to remove the meta_dir and can't do anything about it, which means tests will see more
638+
// in the directory than they should which makes them fail. It's probably a bad idea to generate archives on Windows
639+
// anyway. Either Unix is portable OR no archive is created anywhere. This also means that Windows users can't create
640+
// archives, but that's not a deal-breaker.
641+
cfg!(windows) || (is_ci::cached() && !env::var_os("GIX_TEST_CREATE_ARCHIVES_EVEN_ON_CI").is_some())
642+
}
643+
635644
fn is_lfs_pointer_file(path: &Path) -> bool {
636645
const PREFIX: &[u8] = b"version https://gix-lfs";
637646
let mut buf = [0_u8; PREFIX.len()];
@@ -645,11 +654,7 @@ fn is_lfs_pointer_file(path: &Path) -> bool {
645654
/// The `script_identity` will be baked into the soon to be created `archive` as it identifies the script
646655
/// that created the contents of `source_dir`.
647656
fn create_archive_if_we_should(source_dir: &Path, archive: &Path, script_identity: u32) -> std::io::Result<()> {
648-
// On Windows, we fail to remove the meta_dir and can't do anything about it, which means tests will see more
649-
// in the directory than they should which makes them fail. It's probably a bad idea to generate archives on Windows
650-
// anyway. Either Unix is portable OR no archive is created anywhere. This also means that Windows users can't create
651-
// archives, but that's not a deal-breaker.
652-
if cfg!(windows) || is_ci::cached() || is_excluded(archive) {
657+
if should_skip_all_archive_creation() || is_excluded(archive) {
653658
return Ok(());
654659
}
655660
if is_lfs_pointer_file(archive) {
@@ -702,7 +707,7 @@ fn is_excluded(archive: &Path) -> bool {
702707
let mut lut = EXCLUDE_LUT.lock();
703708
lut.as_mut()
704709
.and_then(|cache| {
705-
let archive = std::env::current_dir().ok()?.join(archive);
710+
let archive = env::current_dir().ok()?.join(archive);
706711
let relative_path = archive.strip_prefix(cache.base()).ok()?;
707712
cache
708713
.at_path(
@@ -746,7 +751,7 @@ fn extract_archive(
746751
let mut buf = Vec::new();
747752
#[cfg_attr(feature = "xz", allow(unused_mut))]
748753
let mut input_archive = std::fs::File::open(archive)?;
749-
if std::env::var_os("GIX_TEST_IGNORE_ARCHIVES").is_some() {
754+
if env::var_os("GIX_TEST_IGNORE_ARCHIVES").is_some() {
750755
return Err(std::io::Error::new(
751756
std::io::ErrorKind::Other,
752757
format!(
@@ -848,16 +853,16 @@ impl<'a> Env<'a> {
848853

849854
/// Set `var` to `value`.
850855
pub fn set(mut self, var: &'a str, value: impl Into<String>) -> Self {
851-
let prev = std::env::var_os(var);
852-
std::env::set_var(var, value.into());
856+
let prev = env::var_os(var);
857+
env::set_var(var, value.into());
853858
self.altered_vars.push((var, prev));
854859
self
855860
}
856861

857862
/// Unset `var`.
858863
pub fn unset(mut self, var: &'a str) -> Self {
859-
let prev = std::env::var_os(var);
860-
std::env::remove_var(var);
864+
let prev = env::var_os(var);
865+
env::remove_var(var);
861866
self.altered_vars.push((var, prev));
862867
self
863868
}
@@ -867,8 +872,8 @@ impl<'a> Drop for Env<'a> {
867872
fn drop(&mut self) {
868873
for (var, prev_value) in self.altered_vars.iter().rev() {
869874
match prev_value {
870-
Some(value) => std::env::set_var(var, value),
871-
None => std::env::remove_var(var),
875+
Some(value) => env::set_var(var, value),
876+
None => env::remove_var(var),
872877
}
873878
}
874879
}

0 commit comments

Comments
 (0)