Skip to content

Commit

Permalink
test_exact_output: refactor unreadable directory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rindeal authored Jun 25, 2024
1 parent c44a9d6 commit 845317c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions tests/test_exact_output.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use assert_cmd::Command;
use std::ffi::OsStr;
use std::process::Output;
use std::str;
use std::sync::Once;
use std::{fs, io, str};

static INIT: Once = Once::new();
static UNREADABLE_DIR_PATH: &str = "/tmp/unreadable_dir";

/**
* This file contains tests that verify the exact output of the command.
Expand Down Expand Up @@ -34,17 +35,26 @@ fn copy_test_data(dir: &str) {
.map_err(|err| eprintln!("Error copying directory for test setup\n{:?}", err));
}

fn create_unreadable_directory() -> io::Result<()> {
#[cfg(unix)]
{
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;
fs::create_dir_all(UNREADABLE_DIR_PATH)?;
fs::set_permissions(UNREADABLE_DIR_PATH, Permissions::from_mode(0))?;
}
Ok(())
}

fn initialize() {
INIT.call_once(|| {
copy_test_data("tests/test_dir");
copy_test_data("tests/test_dir2");
copy_test_data("tests/test_dir_unicode");

Command::new("sh")
.arg("-c")
.arg("mkdir -p /tmp/unreadable_folder && chmod 000 /tmp/unreadable_folder")
.output()
.unwrap();
if let Err(e) = create_unreadable_directory() {
panic!("Failed to create unreadable directory: {}", e);
}
});
}

Expand Down Expand Up @@ -242,7 +252,7 @@ fn apparent_size_output() -> Vec<String> {
#[cfg_attr(target_os = "windows", ignore)]
#[test]
pub fn test_permission_normal() {
let command_args = vec!["/tmp/unreadable_folder"];
let command_args = vec![UNREADABLE_DIR_PATH];
let permission_msg =
r#"Did not have permissions for all directories (add --print-errors to see errors)"#
.trim()
Expand All @@ -254,9 +264,10 @@ pub fn test_permission_normal() {
#[test]
pub fn test_permission_flag() {
// add the flag to CLI
let command_args = vec!["--print-errors", "/tmp/unreadable_folder"];
let permission_msg = r#"Did not have permissions for directories: /tmp/unreadable_folder"#
.trim()
.to_string();
let command_args = vec!["--print-errors", UNREADABLE_DIR_PATH];
let permission_msg = format!(
"Did not have permissions for directories: {}",
UNREADABLE_DIR_PATH
);
exact_stderr_test(command_args, permission_msg);
}

0 comments on commit 845317c

Please sign in to comment.