Skip to main content

wowlab_fs/
working_directory.rs

1//! Process working-directory discovery.
2
3use std::io;
4
5use crate::path::PathBuf;
6
7/// Failure to determine the process working directory.
8#[derive(Debug, thiserror::Error)]
9#[error("failed to determine process working directory: {source}")]
10pub struct Error {
11    #[from]
12    source: io::Error,
13}
14
15/// Return the process working directory.
16///
17/// # Errors
18///
19/// Returns an error when the operating system cannot provide the directory.
20pub fn current() -> Result<PathBuf, Error> {
21    let directory = std::env::current_dir()?;
22
23    Ok(PathBuf::from(directory))
24}
25
26#[cfg(test)]
27mod tests {
28    use googletest::prelude::*;
29
30    use super::*;
31    use crate::directory::{self, EntryKind};
32
33    #[gtest]
34    fn current_path_is_an_absolute_directory() -> Result<()> {
35        let current = current().or_fail()?;
36        let entry = directory::inspect(&current).or_fail()?.or_fail()?;
37
38        verify_true!(current.is_absolute())?;
39
40        verify_that!(entry.kind(), eq(EntryKind::Directory))
41    }
42}