Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ concurrency:
cancel-in-progress: true

env:
MSRV: "1.76"
MSRV: "1.88"
RUST_BACKTRACE: "1"
RUSTFLAGS: "-Dwarnings"
RUSTDOCFLAGS: "-Dwarnings"
Expand All @@ -20,11 +20,11 @@ jobs:
lints:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: mozilla-actions/sccache-action@v0.0.6
- uses: mozilla-actions/sccache-action@v0.0.9
- run: cargo fmt --check
- run: cargo clippy --no-deps
- run: cargo doc --no-deps --document-private-items
Expand All @@ -36,30 +36,30 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@stable
- uses: mozilla-actions/sccache-action@v0.0.6
- uses: mozilla-actions/sccache-action@v0.0.9
- uses: taiki-e/install-action@nextest
- run: cargo test
- run: cargo nextest run
minimal-crates:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@nightly
- uses: mozilla-actions/sccache-action@v0.0.6
- uses: mozilla-actions/sccache-action@v0.0.9
- name: cargo check
run: |
rm -f Cargo.lock
cargo +nightly check -Z minimal-versions
msrv:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.MSRV }}
- uses: mozilla-actions/sccache-action@v0.0.6
- uses: mozilla-actions/sccache-action@v0.0.9
- name: Check MSRV
run: |
cargo +$MSRV check
13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ keywords = ["test", "temp", "temporary", "directory"]
categories = ["development-tools::testing", "filesystem"]

# Sadly this also needs to be updated in .github/workflows/ci.yml
rust-version = "1.76"
rust-version = "1.88"

[dependencies]
anyhow = "1.0.20"
backtrace = "0.3.55"
cargo_metadata = "0.14.0"
once_cell = "1.5"
sysinfo = { version = "0.26", default-features = false }
whoami = "1"

# Force old version of cargo-platform before they bumped the MSRV
cargo-platform = ">=0.1.2, <0.1.9"
cargo-platform = "0.3"
cargo_metadata = "0.23.0"
sysinfo = { version = "0.38", default-features = false, features = ["system"] }
whoami = "2"

[dev-dependencies]
tempfile = "3"
12 changes: 10 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ impl NumberedDirBuilder {
if base.contains('/') || base.contains('\\') {
panic!("base must not contain path separators");
}
let root = format!("{}-of-{}", ROOT_DEFAULT, whoami::username());
let root = format!(
"{}-of-{}",
ROOT_DEFAULT,
whoami::username().unwrap_or("unknown".to_string())
);
Self {
parent: std::env::temp_dir().join(root),
base,
Expand Down Expand Up @@ -110,7 +114,11 @@ impl NumberedDirBuilder {
/// temporary directory location as the parent direcotry for the [`NumberedDir`].
/// However it suffixes the username to the given `prefix` to use as *root*.
pub fn user_root(&mut self, prefix: &str) -> &mut Self {
let root = format!("{}{}", prefix, whoami::username());
let root = format!(
"{}{}",
prefix,
whoami::username().unwrap_or("unknown".to_string())
);
self.parent.set_file_name(root);
self
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@
#![warn(missing_docs, missing_debug_implementations, clippy::all)]

use std::num::NonZeroU8;

use once_cell::sync::OnceCell;
use std::sync::OnceLock;

mod builder;
mod macros;
Expand All @@ -77,7 +76,7 @@ pub const KEEP_DEFAULT: Option<NonZeroU8> = NonZeroU8::new(8);
///
/// Do not use this directly, use [`init_testdir!`] to initialise this.
#[doc(hidden)]
pub static TESTDIR: OnceCell<NumberedDir> = OnceCell::new();
pub static TESTDIR: OnceLock<NumberedDir> = OnceLock::new();

/// Executes a function passing the global [`NumberedDir`] instance.
///
Expand Down
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
/// ```
/// These constructs can also be used in a doctest.
///
/// The module path is valid in any scope, so can be used together with [once_cell] (or
/// [lazy_static]) to share a common directory between different tests.
/// The module path is valid in any scope, so can be used together with
/// e.g. [`std::sync::LazyLock`] to share a common directory between different tests.
/// ```no_run
/// use std::sync::LazyLock;
/// use std::path::PathBuf;
/// use once_cell::sync::Lazy;
/// use testdir::testdir;
///
/// static TDIR: Lazy<PathBuf> = Lazy::new(|| testdir!(ModuleScope));
/// static TDIR: LazyLock<PathBuf> = LazyLock::new(|| testdir!(ModuleScope));
///
/// #[test]
/// fn test_module_scope() {
Expand Down
2 changes: 1 addition & 1 deletion src/numbered_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ fn remove_obsolete_dirs(dir: impl AsRef<Path>, base: &str, current: u16, keep: u
Err(err) if err.kind() == ErrorKind::NotFound => (),
Err(err) => {
return Err(err)
.with_context(|| format!("Failed to remove {}", numdir.path().display()))
.with_context(|| format!("Failed to remove {}", numdir.path().display()));
}
}
}
Expand Down
35 changes: 20 additions & 15 deletions src/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
use std::sync::LazyLock;

use once_cell::sync::Lazy;
use sysinfo::{Pid, ProcessExt, SystemExt};
use sysinfo::Pid;

pub use cargo_metadata;

/// The filename in which we store the Cargo PID: `cargo-pid`.
const CARGO_PID_FILE_NAME: &str = "cargo-pid";

/// Whether we are a cargo sub-process.
static CARGO_PID: Lazy<Option<Pid>> = Lazy::new(cargo_pid);
static CARGO_PID: LazyLock<Option<Pid>> = LazyLock::new(cargo_pid);

#[cfg(target_family = "unix")]
const CARGO_NAME: &str = "cargo";
Expand All @@ -45,25 +45,30 @@ const NEXTEST_NAME: &str = "cargo-nextest.exe";
// assert!(pidfile.is_file());
// ```
fn cargo_pid() -> Option<Pid> {
let mut sys = sysinfo::System::new();
let pid = sysinfo::get_current_pid().ok()?;
let what = sysinfo::ProcessRefreshKind::new();
sys.refresh_process_specifics(pid, what);

let mut sys = sysinfo::System::new();
let what = sysinfo::ProcessRefreshKind::nothing().with_exe(sysinfo::UpdateKind::OnlyIfNotSet);
sys.refresh_processes_specifics(sysinfo::ProcessesToUpdate::Some(&[pid]), false, what);
let current = sys.process(pid)?;
let ppid = current.parent()?;
sys.refresh_process_specifics(ppid, what);
sys.refresh_processes_specifics(sysinfo::ProcessesToUpdate::Some(&[ppid]), false, what);
let parent = sys.process(ppid)?;
let parent_exe = parent.exe();
let parent_file_name = parent_exe.file_name()?;
if parent_file_name == OsStr::new(CARGO_NAME) || parent_file_name == OsStr::new(NEXTEST_NAME) {
let parent_name = parent
.exe()
.and_then(|exe| exe.file_name())
.unwrap_or_else(|| parent.name());
if parent_name == OsStr::new(CARGO_NAME) || parent_name == OsStr::new(NEXTEST_NAME) {
Some(parent.pid())
} else if parent_file_name == OsStr::new("rustdoc") {
} else if parent_name == OsStr::new("rustdoc") {
let ppid = parent.parent()?;
sys.refresh_process_specifics(ppid, what);
sys.refresh_processes_specifics(sysinfo::ProcessesToUpdate::Some(&[ppid]), false, what);
let parent = sys.process(ppid)?;
let parent_exe = parent.exe();
let parent_file_name = parent_exe.file_name()?;
if parent_file_name == OsStr::new("cargo") {
let parent_name = parent
.exe()
.and_then(|exe| exe.file_name())
.unwrap_or_else(|| parent.name());
if parent_name == OsStr::new("cargo") {
Some(parent.pid())
} else {
None
Expand Down
6 changes: 3 additions & 3 deletions tests/macro.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

use once_cell::sync::Lazy;
use testdir::testdir;

static MOD_LEVEL: Lazy<PathBuf> = Lazy::new(|| testdir!(ModuleScope));
static MOD_LEVEL: LazyLock<PathBuf> = LazyLock::new(|| testdir!(ModuleScope));

#[test]
fn test_macro() {
Expand Down Expand Up @@ -74,7 +74,7 @@ fn test_cargo_pid_created() {
mod submodule {
use super::*;

static SUB_MOD: Lazy<PathBuf> = Lazy::new(|| testdir!(ModuleScope));
static SUB_MOD: LazyLock<PathBuf> = LazyLock::new(|| testdir!(ModuleScope));

#[test]
fn test_test_scope() {
Expand Down
Loading