forked from scullionw/dirstat-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix problem with MAX_PATH on windows + UnitTest
* Manually implemented windows File Namespace name generation * added some unit tests closes scullionw#5
- Loading branch information
1 parent
aafe068
commit bb106d2
Showing
6 changed files
with
265 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "dirstat-rs" | ||
version = "0.3.7" | ||
version = "0.3.8" | ||
authors = ["scullionw <[email protected]>"] | ||
edition = "2018" | ||
license = "MIT" | ||
|
@@ -19,8 +19,12 @@ atty = "0.2.14" | |
serde = { version = "1.0.131", features = ["derive"] } | ||
serde_json = "1.0.73" | ||
|
||
[dev-dependencies] | ||
const_format = "0.2.22" | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
winapi-util = "0.1.2" | ||
path-absolutize = "3.0.12" | ||
|
||
[target.'cfg(windows)'.dependencies.winapi] | ||
version = "0.3.7" | ||
|
@@ -35,3 +39,4 @@ incremental = false | |
bench = false | ||
path = "src/bin/main.rs" | ||
name = "ds" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use crate::{DiskItem, FileInfo}; | ||
// warn: don't remove `as &str` after macro invocation. | ||
// It breaks type checker in Intellij Rust IDE | ||
use const_format::concatcp; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::panic; | ||
use std::path::Path; | ||
|
||
// be aware that rust runs tests in parallel, so tests should use different dirs | ||
|
||
const TEST_DATA_DIR: &str = "./test-data/"; | ||
|
||
const LONG_PATH_DIR: &str = "long-path/"; | ||
//noinspection SpellCheckingInspection | ||
const PATH_1: &str = "lll1/llllllll/llllllllllllllll/llllllllllllll/lllllllllllll/oooooo\ | ||
oooooooo/oooooooooooooooo/nnnnnnnnn/nnnnnnnnnn/nnnnnnnn/nnnnnn/gggggggggg/p/a/tttt\ | ||
tttttttttt/ttttttttttt/ttttttttttttttt/ttttttttt/tttthhh/2222222222/22222222222/222222222222/\ | ||
3333333333333/33333333/33333333333/33333333333/333333333/33333333/44444444/44444444444444444/\ | ||
5555555/55555555555/55555555/5555555555/5555555/5555555/555555/555555555/66666666666666666666/\ | ||
77777777/7777777777/7777777777777/77777777777/7777777777/77777777/7777777/77777777/8888888888/\ | ||
99999999/999999/99999999/99999999999/99999999/999999999/9999999999/"; | ||
|
||
const PATH_1_FULL: &str = concatcp!(TEST_DATA_DIR, LONG_PATH_DIR, PATH_1) as &str; | ||
//noinspection SpellCheckingInspection | ||
const PATH_2: &str = "lll2/llllllll/llllllllllllllll/llllllllllllll/lllllllllllll/oooooo\ | ||
oooooooo/oooooooooooooooo/nnnnnnnnn/nnnnnnnnnn/nnnnnnnn/nnnnnn/gggggggggg/p/a/tttt\ | ||
tttttttttt/ttttttttttt/ttttttttttttttt/ttttttttt/tttthhh/2222222222/22222222222/222222222222/\ | ||
3333333333333/33333333/33333333333/33333333333/333333333/33333333/44444444/44444444444444444/\ | ||
5555555/55555555555/55555555/5555555555/5555555/5555555/555555/555555555/66666666666666666666/\ | ||
77777777/7777777777/7777777777777/77777777777/7777777777/77777777/7777777/77777777/8888888888/\ | ||
99999999/999999/99999999/99999999999/99999999/999999999/9999999999/"; | ||
|
||
const PATH_2_FULL: &str = concatcp!(TEST_DATA_DIR, LONG_PATH_DIR, PATH_2) as &str; | ||
|
||
#[test] | ||
fn test_max_path() { | ||
// do not rename it into `_` it would cause immediate destrucion after creation | ||
let _guard = CleanUpGuard { | ||
path: concatcp!(TEST_DATA_DIR, LONG_PATH_DIR) as &str, | ||
}; | ||
|
||
// Given | ||
create_dir(PATH_1_FULL); | ||
create_dir(PATH_2_FULL); | ||
create_file(&concatcp!(PATH_1_FULL, "file.bin") as &str, 4096); | ||
create_file(&concatcp!(PATH_2_FULL, "file.bin") as &str, 8192); | ||
|
||
// When | ||
let test_path = Path::new(concatcp!(TEST_DATA_DIR, LONG_PATH_DIR) as &str); | ||
let result = FileInfo::from_path(test_path, true); | ||
|
||
// Then | ||
if let Result::Ok(FileInfo::Directory { volume_id }) = result { | ||
let result = DiskItem::from_analyze(test_path, true, volume_id); | ||
let result = result.expect("Must collect data"); | ||
assert_eq!(result.disk_size, 4096 + 8192); | ||
let children = result.children.unwrap(); | ||
assert_eq!(children.len(), 2); | ||
assert_eq!(children[0].disk_size, 8192); | ||
assert_eq!(children[1].disk_size, 4096); | ||
} else { | ||
panic!("Can not get file info"); | ||
} | ||
} | ||
|
||
#[test] | ||
#[cfg(unix)] // It gives inconsistent results on windows | ||
fn test_file_size() { | ||
const DIR: &str = concatcp!(TEST_DATA_DIR, "test_file_size/") as &str; | ||
// do not rename it into `_` it would cause immediate destrucion after creation | ||
let _guard = CleanUpGuard { path: DIR }; | ||
|
||
// Given | ||
create_file(&concatcp!(DIR, "foo/file.bin") as &str, 8192); | ||
create_file(&concatcp!(DIR, "bar/file.bin") as &str, 8192 - 5); | ||
|
||
// When calculating with apparent size | ||
let test_path = Path::new(DIR); | ||
let result = FileInfo::from_path(test_path, true); | ||
|
||
// Then | ||
if let Result::Ok(FileInfo::Directory { volume_id }) = result { | ||
let result = DiskItem::from_analyze(test_path, true, volume_id); | ||
let result = result.expect("Must collect data"); | ||
assert_eq!(result.disk_size, 8192 + 8192); | ||
let children = result.children.unwrap(); | ||
assert_eq!(children.len(), 2); | ||
// Both dirs should be rounded to sector size | ||
assert_eq!(children[0].disk_size, 8192); | ||
assert_eq!(children[1].disk_size, 8192); | ||
} else { | ||
panic!("Can not get file info"); | ||
} | ||
|
||
// When calculating withOUT apparent size | ||
let result = FileInfo::from_path(test_path, false); | ||
|
||
// Then | ||
if let Result::Ok(FileInfo::Directory { volume_id }) = result { | ||
let result = DiskItem::from_analyze(test_path, false, volume_id); | ||
let result = result.expect("Must collect data"); | ||
assert_eq!(result.disk_size, 8192 + 8192 - 5); | ||
let children = result.children.unwrap(); | ||
assert_eq!(children.len(), 2); | ||
// Both dirs should be rounded to sector size | ||
assert_eq!(children[0].disk_size, 8192); | ||
assert_eq!(children[1].disk_size, 8192 - 5); | ||
} else { | ||
panic!("Can not get file info"); | ||
} | ||
} | ||
|
||
// Helper functions and cleanup code goes next | ||
|
||
fn create_dir(dir_path: &str) { | ||
std::fs::create_dir_all(dir_path).unwrap(); | ||
} | ||
|
||
fn create_file(file_path: &str, size: usize) { | ||
let content = vec![0u8; size]; | ||
let file_path = Path::new(file_path); | ||
// ensure parent | ||
std::fs::create_dir_all(file_path.parent().unwrap()).unwrap(); | ||
|
||
let mut file = File::create(file_path).unwrap(); | ||
file.write(&content).unwrap(); | ||
} | ||
|
||
/// Used to clean up test folder after test runs. | ||
struct CleanUpGuard { | ||
path: &'static str, | ||
} | ||
|
||
impl Drop for CleanUpGuard { | ||
fn drop(&mut self) { | ||
// Teardown | ||
std::fs::remove_dir_all(Path::new(self.path)).unwrap(); | ||
} | ||
} |