From f911b4a3555820d90dad4fbf1df538f5b5e31a3f Mon Sep 17 00:00:00 2001 From: dvkt Date: Mon, 6 Jan 2020 15:13:10 -0800 Subject: [PATCH] sort_paths (failing) test + fixtures --- Cargo.lock | 7 +++ Cargo.toml | 10 ++-- src/server.rs | 55 ++++++++++++++------ tests/sort/phetch-v0.1.10-linux-armv7.tgz | 0 tests/sort/phetch-v0.1.10-linux-x86_64.tgz | 0 tests/sort/phetch-v0.1.10-macos.zip | 0 tests/sort/phetch-v0.1.11-linux-armv7.tgz | 0 tests/sort/phetch-v0.1.11-linux-x86_64.tgz | 0 tests/sort/phetch-v0.1.11-macos.zip | 0 tests/sort/phetch-v0.1.7-linux-armv7.tar.gz | 0 tests/sort/phetch-v0.1.7-linux-x86_64.tar.gz | 0 tests/sort/phetch-v0.1.7-macos.zip | 0 tests/sort/phetch-v0.1.8-linux-armv7.tar.gz | 0 tests/sort/phetch-v0.1.8-linux-x86_64.tar.gz | 0 tests/sort/phetch-v0.1.8-macos.zip | 0 tests/sort/phetch-v0.1.9-linux-armv7.tar.gz | 0 tests/sort/phetch-v0.1.9-linux-x86_64.tar.gz | 0 tests/sort/phetch-v0.1.9-macos.zip | 0 18 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 tests/sort/phetch-v0.1.10-linux-armv7.tgz create mode 100644 tests/sort/phetch-v0.1.10-linux-x86_64.tgz create mode 100644 tests/sort/phetch-v0.1.10-macos.zip create mode 100644 tests/sort/phetch-v0.1.11-linux-armv7.tgz create mode 100644 tests/sort/phetch-v0.1.11-linux-x86_64.tgz create mode 100644 tests/sort/phetch-v0.1.11-macos.zip create mode 100644 tests/sort/phetch-v0.1.7-linux-armv7.tar.gz create mode 100644 tests/sort/phetch-v0.1.7-linux-x86_64.tar.gz create mode 100644 tests/sort/phetch-v0.1.7-macos.zip create mode 100644 tests/sort/phetch-v0.1.8-linux-armv7.tar.gz create mode 100644 tests/sort/phetch-v0.1.8-linux-x86_64.tar.gz create mode 100644 tests/sort/phetch-v0.1.8-macos.zip create mode 100644 tests/sort/phetch-v0.1.9-linux-armv7.tar.gz create mode 100644 tests/sort/phetch-v0.1.9-linux-x86_64.tar.gz create mode 100644 tests/sort/phetch-v0.1.9-macos.zip diff --git a/Cargo.lock b/Cargo.lock index da7b105..d50e862 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,10 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "alphanumeric-sort" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "content_inspector" version = "0.2.4" @@ -44,6 +49,7 @@ dependencies = [ name = "phd" version = "0.1.6-dev" dependencies = [ + "alphanumeric-sort 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", "content_inspector 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "gophermap 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -58,6 +64,7 @@ dependencies = [ ] [metadata] +"checksum alphanumeric-sort 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f37ce94154d73f6961f87571a3ab7814e1608f373bd55a933e3e771b6dd59fc4" "checksum content_inspector 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b7bda66e858c683005a53a9a60c69a4aca7eeaa45d124526e389f7aec8e62f38" "checksum gophermap 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6ec2186bfad5a5dcbc9307dbc2d2444062300a836ae91b00dd80c3b71c34af3b" "checksum hermit-abi 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f629dc602392d3ec14bfc8a09b5e644d7ffd725102b48b81e59f90f2633621d7" diff --git a/Cargo.toml b/Cargo.toml index 5a23552..abffe72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,11 +18,6 @@ codegen-units = 1 panic = 'abort' opt-level = 'z' # Optimize for size. -[dependencies] -content_inspector = "0.2.4" -threadpool = "1.7.1" -gophermap = "0.1.2" - [package.metadata.release] pre-release-replacements = [ {file="README.md", search="phd-v\\d+\\.\\d+\\.\\d+-", replace="{{crate_name}}-v{{version}}-"}, @@ -30,3 +25,8 @@ pre-release-replacements = [ ] dev-version-ext = "dev" +[dependencies] +content_inspector = "0.2.4" +threadpool = "1.7.1" +gophermap = "0.1.2" +alphanumeric-sort = "1.0.11" \ No newline at end of file diff --git a/src/server.rs b/src/server.rs index ab62d4c..e51d815 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ use crate::{color, Request, Result}; use gophermap::{GopherMenu, ItemType}; use std::{ - fs, + fs::{self, DirEntry}, io::{self, prelude::*, BufReader, Read, Write}, net::{TcpListener, TcpStream}, os::unix::fs::PermissionsExt, @@ -144,20 +144,8 @@ where let mut menu = GopherMenu::with_write(w); let rel_path = req.relative_file_path(); - // sort directory entries - let mut paths: Vec<_> = fs::read_dir(&path)?.filter_map(|r| r.ok()).collect(); - let mut reverse = path.clone(); - reverse.push_str("/.reverse"); - let is_dir = |entry: &fs::DirEntry| match entry.file_type() { - Ok(t) => t.is_dir(), - _ => false, - }; - if fs_exists(&reverse) { - paths.sort_by_key(|entry| (!is_dir(&entry), std::cmp::Reverse(entry.path()))); - } else { - paths.sort_by_key(|entry| (!is_dir(&entry), entry.path())); - } - + // show directory entries + let paths = sort_paths(&path)?; for entry in paths { let file_name = entry.file_name(); let f = file_name.to_string_lossy().to_string(); @@ -332,3 +320,40 @@ fn shell(path: &str, args: &[&str]) -> Result { Ok(str::from_utf8(&output.stderr)?.to_string()) } } + +/// Sort directory paths: dirs first, files 2nd, version #s respected. +fn sort_paths(dir_path: &str) -> Result> { + let mut paths: Vec<_> = fs::read_dir(dir_path)?.filter_map(|r| r.ok()).collect(); + let mut reverse = dir_path.to_string(); + reverse.push_str("/.reverse"); + let is_dir = |entry: &fs::DirEntry| match entry.file_type() { + Ok(t) => t.is_dir(), + _ => false, + }; + if fs_exists(&reverse) { + paths.sort_by_key(|entry| (!is_dir(&entry), std::cmp::Reverse(entry.path()))); + } else { + paths.sort_by_key(|entry| (!is_dir(&entry), entry.path())); + } + Ok(paths) +} + +#[cfg(test)] +mod tests { + use super::*; + + macro_rules! str_path { + ($e:expr) => { + $e.path() + .to_str() + .unwrap() + .trim_start_matches("tests/sort/") + }; + } + + #[test] + fn test_sort_directory() { + let paths = sort_paths("tests/sort").unwrap(); + assert_eq!(str_path!(paths[0]), "phetch-v0.1.11-linux-armv7.tgz"); + } +} diff --git a/tests/sort/phetch-v0.1.10-linux-armv7.tgz b/tests/sort/phetch-v0.1.10-linux-armv7.tgz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.10-linux-x86_64.tgz b/tests/sort/phetch-v0.1.10-linux-x86_64.tgz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.10-macos.zip b/tests/sort/phetch-v0.1.10-macos.zip new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.11-linux-armv7.tgz b/tests/sort/phetch-v0.1.11-linux-armv7.tgz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.11-linux-x86_64.tgz b/tests/sort/phetch-v0.1.11-linux-x86_64.tgz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.11-macos.zip b/tests/sort/phetch-v0.1.11-macos.zip new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.7-linux-armv7.tar.gz b/tests/sort/phetch-v0.1.7-linux-armv7.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.7-linux-x86_64.tar.gz b/tests/sort/phetch-v0.1.7-linux-x86_64.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.7-macos.zip b/tests/sort/phetch-v0.1.7-macos.zip new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.8-linux-armv7.tar.gz b/tests/sort/phetch-v0.1.8-linux-armv7.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.8-linux-x86_64.tar.gz b/tests/sort/phetch-v0.1.8-linux-x86_64.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.8-macos.zip b/tests/sort/phetch-v0.1.8-macos.zip new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.9-linux-armv7.tar.gz b/tests/sort/phetch-v0.1.9-linux-armv7.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.9-linux-x86_64.tar.gz b/tests/sort/phetch-v0.1.9-linux-x86_64.tar.gz new file mode 100644 index 0000000..e69de29 diff --git a/tests/sort/phetch-v0.1.9-macos.zip b/tests/sort/phetch-v0.1.9-macos.zip new file mode 100644 index 0000000..e69de29