Skip to content

Commit

Permalink
Fix tests on nightly
Browse files Browse the repository at this point in the history
Nightly has richer IO error kinds, and the meaning of ErrorKind::Other
has been changed, so we cannot use that anymore.

Instead use raw OS errors.
  • Loading branch information
rbtcollins committed Mar 29, 2022
1 parent 9b164ce commit 8ccdc7c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 38 deletions.
35 changes: 19 additions & 16 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Rust

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
jobs:
build-unix:
runs-on: ${{ matrix.os }}-latest
Expand All @@ -13,10 +13,10 @@ jobs:
channel: [stable, beta, nightly]
os: [ubuntu, macos]
steps:
- uses: actions/checkout@v2
- run: rustup default ${{ matrix.channel }}
- run: cargo build --verbose --all-targets
- run: cargo test
- uses: actions/checkout@v2
- run: rustup default ${{ matrix.channel }}
- run: cargo build --verbose --all-targets
- run: cargo test
build-windows:
runs-on: windows-latest
strategy:
Expand All @@ -31,13 +31,16 @@ jobs:
- arch: i686
variant: gnu
steps:
- uses: actions/checkout@v2
- run: choco install msys2
if: matrix.variant == 'gnu'
- run: rustup default ${{ matrix.channel }}
- run: rustup target add ${{ matrix.arch }}-pc-windows-${{ matrix.variant }}
- name: Build
run: cargo build --verbose --target ${{ matrix.arch }}-pc-windows-${{ matrix.variant }}
- name: Run tests
if: matrix.arch != 'aarch64'
run: cargo test --verbose --target ${{ matrix.arch }}-pc-windows-${{ matrix.variant }}
- uses: actions/checkout@v2
- run: choco install msys2
if: matrix.variant == 'gnu'
- run: rustup default ${{ matrix.channel }}
- run: rustup target add ${{ matrix.arch }}-pc-windows-${{ matrix.variant }}
- name: Build
run: cargo build --verbose --target ${{ matrix.arch }}-pc-windows-${{ matrix.variant }}
- name: Run tests (nightly)
if: (matrix.arch != 'aarch64') && (matrix.channel == 'nightly')
run: cargo test --verbose --target ${{ matrix.arch }}-pc-windows-${{ matrix.variant }} --features nightly
- name: Run tests
if: (matrix.arch != 'aarch64') && (matrix.channel != 'nightly')
run: cargo test --verbose --target ${{ matrix.arch }}-pc-windows-${{ matrix.variant }}
20 changes: 18 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[package]
authors = ["Erin P. <xampprocky@gmail.com>", "Robert C. <robertc@robertcollins.net>"]
authors = [
"Erin P. <xampprocky@gmail.com>",
"Robert C. <robertc@robertcollins.net>",
]
categories = ["filesystem"]
description = "A safe, reliable implementation of remove_dir_all for Windows"
edition = "2018"
Expand All @@ -17,11 +20,24 @@ readme = "README.md"
repository = "https://github.com/XAMPPRocky/remove_dir_all.git"
version = "0.7.1-alpha.0"

[features]
default = []
nightly = []

[dependencies]
cfg-if = "1.0.0"

[target.'cfg(windows)'.dependencies]
log = "0.4.11"
num_cpus = "1.13"
rayon = "1.4"
winapi = {version = "0.3", features = ["std", "errhandlingapi", "winerror", "fileapi", "winbase"]}
winapi = { version = "0.3", features = [
"std",
"errhandlingapi",
"winerror",
"fileapi",
"winbase",
] }

[target.'cfg(not(windows))'.dependencies]
libc = "0.2"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! It also provides `remove_dir_contents` and `ensure_empty_dir`
//! for both Unix and Windows.

#![cfg_attr(feature = "nightly", feature(io_error_more))]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![deny(rust_2018_idioms)]
Expand Down
68 changes: 48 additions & 20 deletions src/portable.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::io;
use std::path::Path;

#[cfg(windows)]
use crate::fs::_remove_dir_contents;

#[cfg(not(windows))]
use crate::unix::_remove_dir_contents;
cfg_if::cfg_if! {
if #[cfg(windows)] {
use crate::fs::_remove_dir_contents;
} else {
use crate::unix::_remove_dir_contents;
}
}

/// Deletes the contents of `path`, but not the directory iteself.
///
Expand All @@ -28,26 +30,47 @@ pub fn remove_dir_contents<P: AsRef<Path>>(path: P) -> io::Result<()> {
/// a symlink to one).
pub fn ensure_empty_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
match std::fs::create_dir(&path) {
Err(e) if e.kind() == io::ErrorKind::AlreadyExists
=> remove_dir_contents(path),
Err(e) if e.kind() == io::ErrorKind::AlreadyExists => remove_dir_contents(path),
otherwise => otherwise,
}
}

#[cfg(test)]
mod test {
use std::fs::{self, File};
use std::io;
use std::path::PathBuf;

use tempfile::TempDir;

use crate::ensure_empty_dir;
use crate::remove_dir_all;
use crate::remove_dir_contents;
use crate::ensure_empty_dir;
use std::fs::{self, File};
use std::path::PathBuf;
use std::io;

fn expect_failure<T>(k: io::ErrorKind, r: io::Result<T>) -> io::Result<()> {
cfg_if::cfg_if! {
if #[cfg(windows)] {
const ENOTDIR:i32 = winapi::shared::winerror::ERROR_DIRECTORY as i32;
const ENOENT:i32 = winapi::shared::winerror::ERROR_FILE_NOT_FOUND as i32;
} else {
const ENOTDIR:i32 = libc::ENOTDIR;
const ENOENT:i32 = libc::ENOENT;
}
}

/// Expect a particular sort of failure
fn expect_failure<T>(n: &[i32], r: io::Result<T>) -> io::Result<()> {
match r {
Err(e) if e.kind() == k => Ok(()),
Err(e) => Err(e),
Err(e)
if n.iter()
.map(|n| Option::Some(*n))
.any(|n| n == e.raw_os_error()) =>
{
Ok(())
}
Err(e) => {
println!("{e} {:?}, {:?}, {:?}", e.raw_os_error(), e.kind(), n);
Err(e)
}
Ok(_) => Err(io::Error::new(
io::ErrorKind::Other,
"unexpected success".to_string(),
Expand All @@ -61,39 +84,44 @@ mod test {
file: PathBuf,
}

/// Create test setup: t.mkdir/file all in a tempdir.
fn prep() -> Result<Prep, io::Error> {
let tmp = TempDir::new()?;
let ours = tmp.path().join("t.mkdir");
let file = ours.join("file");
fs::create_dir(&ours)?;
File::create(&file)?;
File::open(&file)?;
Ok(Prep { _tmp: tmp, ours, file })
Ok(Prep {
_tmp: tmp,
ours,
file,
})
}

#[test]
fn mkdir_rm() -> Result<(), io::Error> {
let p = prep()?;

expect_failure(io::ErrorKind::Other, remove_dir_contents(&p.file))?;
expect_failure(&[ENOTDIR], remove_dir_contents(&p.file))?;

remove_dir_contents(&p.ours)?;
expect_failure(io::ErrorKind::NotFound, File::open(&p.file))?;
expect_failure(&[ENOENT], File::open(&p.file))?;

remove_dir_contents(&p.ours)?;
remove_dir_all(&p.ours)?;
expect_failure(io::ErrorKind::NotFound, remove_dir_contents(&p.ours))?;
expect_failure(&[ENOENT], remove_dir_contents(&p.ours))?;
Ok(())
}

#[test]
fn ensure_rm() -> Result<(), io::Error> {
let p = prep()?;

expect_failure(io::ErrorKind::Other, ensure_empty_dir(&p.file))?;
expect_failure(&[ENOTDIR], ensure_empty_dir(&p.file))?;

ensure_empty_dir(&p.ours)?;
expect_failure(io::ErrorKind::NotFound, File::open(&p.file))?;
expect_failure(&[ENOENT], File::open(&p.file))?;
ensure_empty_dir(&p.ours)?;

remove_dir_all(&p.ours)?;
Expand Down

0 comments on commit 8ccdc7c

Please sign in to comment.