From 512ae4816188dd760a419f7c28c5c8dfd5f411a9 Mon Sep 17 00:00:00 2001 From: Nick Juszczak Date: Wed, 1 Jan 2014 02:08:27 -0600 Subject: [PATCH 1/8] mkdir init --- mkdir/mkdir.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 mkdir/mkdir.rs diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs new file mode 100644 index 000000000..044b73848 --- /dev/null +++ b/mkdir/mkdir.rs @@ -0,0 +1,69 @@ +#[crate_id(name="mkdir", vers="1.0.0", author="Nicholas Juszczak")]; + +/* + * This file is part of the uutils coreutils package. + * + * (c) Nicholas Juszczak + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +extern mod extra; + +use std::os; +use std::io::stderr; +use extra::getopts::groups; +use std::io::fs::mkdir; +use std::path; + +static VERSION: &'static str = "1.0.0"; + +fn print_help(opts: &[groups::OptGroup]) { + println!("mkdir v{} - make a new directory with the given path", VERSION); + println(""); + println("Usage:"); + print(groups::usage("Create the given DIRECTORY(ies)" + + " if they do not exist", opts)); +} + +fn main() { + let args: ~[~str] = os::args(); + let program: ~str = args[0].clone(); + + let opts: ~[groups::OptGroup] = ~[ + //groups::optflag("m", "mode", "set file mode"), + groups::optflag("p", "parents", "make parent directories as needed"), + groups::optflag("v", "verbose", + "print a message for each printed directory"), + groups::optflag("", "help", "display this help"), + groups::optflag("", "version", "display this version") + ]; + + let matches = match groups::getopts(args.tail(), opts) { + Ok(m) => m, + Err(f) => { + writeln!(&mut stderr() as &mut Writer, + "Invalid options\n{}", f.to_err_msg()); + os::set_exit_status(1); + return; + } + }; + + if matches.opt_present("help") { + print_help(opts); + return; + } + if matches.opt_present("version") { + println("mkdir v" + VERSION); + return; + } + + let parents: bool = matches.opt_present("parents"); + mkdir(parents); +} + +fn mkdir(mk_parents: bool) { + +} + From d0c2896b16674f35803971f274bd764190bdfedb Mon Sep 17 00:00:00 2001 From: Nick Juszczak Date: Wed, 1 Jan 2014 17:32:21 -0600 Subject: [PATCH 2/8] add to makefile, basic mkdir functionality done --- Makefile | 1 + mkdir/mkdir.rs | 54 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 2886fdb7b..b0368d529 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ PROGS := \ echo \ env \ false \ + mkdir \ printenv \ pwd \ rmdir \ diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index 044b73848..da5e3f1a8 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -14,25 +14,18 @@ extern mod extra; use std::os; use std::io::stderr; use extra::getopts::groups; -use std::io::fs::mkdir; -use std::path; +use std::io::fs; static VERSION: &'static str = "1.0.0"; -fn print_help(opts: &[groups::OptGroup]) { - println!("mkdir v{} - make a new directory with the given path", VERSION); - println(""); - println("Usage:"); - print(groups::usage("Create the given DIRECTORY(ies)" + - " if they do not exist", opts)); -} - fn main() { let args: ~[~str] = os::args(); - let program: ~str = args[0].clone(); let opts: ~[groups::OptGroup] = ~[ - //groups::optflag("m", "mode", "set file mode"), + // Linux-specific options + // groups::optflag("m", "mode", "set file mode"), + // groups::optflag("Z", "context", "set SELinux secutiry context" + + // " of each created directory to CTX"), groups::optflag("p", "parents", "make parent directories as needed"), groups::optflag("v", "verbose", "print a message for each printed directory"), @@ -59,11 +52,40 @@ fn main() { return; } - let parents: bool = matches.opt_present("parents"); - mkdir(parents); + let mk_parents: bool = matches.opt_present("parents"); + let dirs: ~[~str] = matches.free; + mkdir(dirs, mk_parents); } -fn mkdir(mk_parents: bool) { - +fn print_help(opts: &[groups::OptGroup]) { + println!("mkdir v{} - make a new directory with the given path", VERSION); + println(""); + println("Usage:"); + print(groups::usage("Create the given DIRECTORY(ies)" + + " if they do not exist", opts)); } +/* std::libc currently does not provide bindings for any bits +besides user bits, so might as well use octal for now. +See 'std::io::FilePermission', +'std::libc::consts::os::posix88' for future updates */ +fn mkdir(dirs: ~[~str], mk_parents: bool) { + let default: u32 = 0o755; + + for dir in dirs.iter() { + let path = Path::new((*dir).clone()); + // Recursively create parent dirs as needed + if mk_parents { + match path.dirname_str() { + Some(p) => if p != "." { + mkdir(~[p.into_owned()], mk_parents) + }, + None => () + } + } + if !path.exists() { + println(*dir); + fs::mkdir(&path, default); + } + } +} From fc1da5a691dd0257a70ef17b799a432c33eda99d Mon Sep 17 00:00:00 2001 From: Nick Juszczak Date: Fri, 3 Jan 2014 17:32:39 -0600 Subject: [PATCH 3/8] remove cat warning, update makefile --- Makefile | 1 + cat/cat.rs | 1 + mkdir/mkdir.rs | 120 ++++++++++++++++++++++++++++++++++++++++--------- mkdir/test.rs | 3 ++ 4 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 mkdir/test.rs diff --git a/Makefile b/Makefile index b0368d529..d6ddf8589 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ EXES := \ # Programs with usable tests TEST_PROGS := \ cat \ + mkdir \ TEST ?= $(TEST_PROGS) diff --git a/cat/cat.rs b/cat/cat.rs index 1a20937a0..170f16ce2 100644 --- a/cat/cat.rs +++ b/cat/cat.rs @@ -86,6 +86,7 @@ pub enum NumberingMode { } static TAB: u8 = '\t' as u8; +#[allow(dead_code)] static CR: u8 = '\r' as u8; static LF: u8 = '\n' as u8; diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index da5e3f1a8..e0f0476ab 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -1,6 +1,6 @@ #[crate_id(name="mkdir", vers="1.0.0", author="Nicholas Juszczak")]; -/* +/** * This file is part of the uutils coreutils package. * * (c) Nicholas Juszczak @@ -12,26 +12,29 @@ extern mod extra; use std::os; -use std::io::stderr; +use std::io::{fs, result, stderr}; +use std::num::strconv; use extra::getopts::groups; -use std::io::fs; static VERSION: &'static str = "1.0.0"; +/** + * Handles option parsing + */ fn main() { let args: ~[~str] = os::args(); let opts: ~[groups::OptGroup] = ~[ - // Linux-specific options - // groups::optflag("m", "mode", "set file mode"), + // Linux-specific options, not implemented // groups::optflag("Z", "context", "set SELinux secutiry context" + // " of each created directory to CTX"), + groups::optopt("m", "mode", "set file mode", "755"), groups::optflag("p", "parents", "make parent directories as needed"), groups::optflag("v", "verbose", "print a message for each printed directory"), groups::optflag("", "help", "display this help"), groups::optflag("", "version", "display this version") - ]; + ]; let matches = match groups::getopts(args.tail(), opts) { Ok(m) => m, @@ -43,7 +46,7 @@ fn main() { } }; - if matches.opt_present("help") { + if args.len() == 1 || matches.opt_present("help") { print_help(opts); return; } @@ -51,10 +54,35 @@ fn main() { println("mkdir v" + VERSION); return; } + let mut verbose_flag: bool = false; + if matches.opt_present("verbose") { + verbose_flag = true; + } let mk_parents: bool = matches.opt_present("parents"); + + // Translate a ~str in octal form to u32, default to 755 + // Not tested on Windows + let mode_match = matches.opts_str(&[~"mode"]); + let mode: u32 = if mode_match.is_some() { + let m: ~str = mode_match.unwrap(); + let res = strconv::from_str_common(m, 8, false, false, false, + strconv::ExpNone, + false, false); + if res.is_some() { + res.unwrap() + } else { + writeln!(&mut stderr() as &mut Writer, + "Error: no mode given"); + os::set_exit_status(1); + return; + } + } else { + 0o755 + }; + let dirs: ~[~str] = matches.free; - mkdir(dirs, mk_parents); + exec(dirs, mk_parents, mode, verbose_flag); } fn print_help(opts: &[groups::OptGroup]) { @@ -65,27 +93,79 @@ fn print_help(opts: &[groups::OptGroup]) { " if they do not exist", opts)); } -/* std::libc currently does not provide bindings for any bits -besides user bits, so might as well use octal for now. -See 'std::io::FilePermission', -'std::libc::consts::os::posix88' for future updates */ -fn mkdir(dirs: ~[~str], mk_parents: bool) { - let default: u32 = 0o755; - +/** + * Create the list of new directories + */ +fn exec(dirs: ~[~str], mk_parents: bool, mode: u32, verbose: bool) { + let mut parent_dirs: ~[~str] = ~[]; for dir in dirs.iter() { let path = Path::new((*dir).clone()); - // Recursively create parent dirs as needed + // Build list of parent dirs which need to be created if mk_parents { match path.dirname_str() { Some(p) => if p != "." { - mkdir(~[p.into_owned()], mk_parents) + parent_dirs.push(p.into_owned()) }, None => () } } - if !path.exists() { - println(*dir); - fs::mkdir(&path, default); + } + // Recursively build parent dirs that are needed + if !parent_dirs.is_empty() { + exec(parent_dirs, mk_parents, mode, verbose); + } + + for dir in dirs.iter() { + let path = Path::new((*dir).clone()); + // Determine if parent directory to the one to + // be created exists + let parent: &str = match path.dirname_str() { + Some(p) => p, + None => "" + }; + let parent_exists:bool = Path::new(parent).exists(); + if parent_exists && !path.exists() { + // if mkdir failed return + if !mkdir(&path, mode) {return;} + if verbose {println(*dir);} + } else { + let mut error_msg: ~str = ~""; + if !parent_exists { + error_msg.push_str("Error: parent directory '"); + error_msg.push_str(parent); + error_msg.push_str("' does not exist"); + } else { + error_msg.push_str("Error: directory '"); + error_msg.push_str(*dir); + error_msg.push_str("' already exists"); + } + writeln!(&mut stderr() as &mut Writer, + "{}", error_msg); } } } + +/** + * Wrapper to catch errors, return false if failed + */ +fn mkdir(path: &Path, mode: u32) -> bool { + match result(|| fs::mkdir(path, mode)) { + Ok(_) => true, + Err(e) => { + writeln!(&mut stderr() as &mut Writer, + "mkdir: test {}", e.to_str()); + os::set_exit_status(1); + false + } + } +} + +// #[test] +// fn create_dir() { +// let test_dir = "mkdir_test_dir"; +// let path: Path = Path::new(test_dir); +// let mode: u32 = 0x755; +// let result = mkdir(&path, mode); +// fs::rmdir(&path); +// assert_eq!(true, result); +// } diff --git a/mkdir/test.rs b/mkdir/test.rs new file mode 100644 index 000000000..58dfaaa8a --- /dev/null +++ b/mkdir/test.rs @@ -0,0 +1,3 @@ +fn main() { + +} From aac0a46b39abcaaf6dcf5102e36071c8b5fa2552 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 3 Jan 2014 22:33:05 -0600 Subject: [PATCH 4/8] Fix test for cat, add mkdir test --- Makefile | 4 +-- cat/test.rs | 10 +++++--- mkdir/mkdir.rs | 11 +------- mkdir/test.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index d6ddf8589..611f04310 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,8 @@ EXES := \ # Programs with usable tests TEST_PROGS := \ - cat \ mkdir \ + cat \ TEST ?= $(TEST_PROGS) @@ -54,7 +54,7 @@ test_$(1): tmp/$(1)_test build build/$(1) $(call command,tmp/$(1)_test) tmp/$(1)_test: $(1)/test.rs - $(call command,$(RUSTC) $(RUSTCFLAGS) --test -o tmp/$(1)_test $(1)/test.rs) + $(call command,$(RUSTC) $(RUSTCFLAGS) -o tmp/$(1)_test $(1)/test.rs) endef # Main rules diff --git a/cat/test.rs b/cat/test.rs index 080e4eeee..3c17b6c31 100644 --- a/cat/test.rs +++ b/cat/test.rs @@ -10,14 +10,16 @@ fn test_output_multi_files_print_all_chars() { let prog = run::process_output("build/cat", [~"cat/fixtures/alpha.txt", ~"cat/fixtures/256.txt", ~"-A", ~"-n"]); - let out = str::from_utf8_owned(prog.output); + let out = str::from_utf8_owned(prog.unwrap().output); assert_eq!(out, ~" 1\tabcde$\n 2\tfghij$\n 3\tklmno$\n 4\tpqrst$\n 5\tuvwxyz$\n 6\t^@^A^B^C^D^E^F^G^H^I$\n 7\t^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\\^]^^^_ !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\\M-^]M-^^M-^_M- M-!M-\"M-#M-$M-%M-&M-\'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?"); } fn test_stdin_squeeze() { - let mut prog = run::Process::new("build/cat", [~"-A"], run::ProcessOptions::new()); + let program = run::Process::new("build/cat", [~"-A"], run::ProcessOptions::new()); + if program.is_none() { fail!("cat: error - process not started"); } + let mut prog = program.unwrap(); prog.input().write(bytes!("\x00\x01\x02")); prog.close_input(); @@ -26,8 +28,10 @@ fn test_stdin_squeeze() { } fn test_stdin_number_non_blank() { - let mut prog = run::Process::new("build/cat", [~"-b", ~"-"], run::ProcessOptions::new()); + let program = run::Process::new("build/cat", [~"-b", ~"-"], run::ProcessOptions::new()); + if program.is_none() { fail!("cat: error - process not started"); } + let mut prog = program.unwrap(); prog.input().write(bytes!("\na\nb\n\n\nc")); prog.close_input(); diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index e0f0476ab..0a112e595 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -141,6 +141,7 @@ fn exec(dirs: ~[~str], mk_parents: bool, mode: u32, verbose: bool) { } writeln!(&mut stderr() as &mut Writer, "{}", error_msg); + os::set_exit_status(1); } } } @@ -159,13 +160,3 @@ fn mkdir(path: &Path, mode: u32) -> bool { } } } - -// #[test] -// fn create_dir() { -// let test_dir = "mkdir_test_dir"; -// let path: Path = Path::new(test_dir); -// let mode: u32 = 0x755; -// let result = mkdir(&path, mode); -// fs::rmdir(&path); -// assert_eq!(true, result); -// } diff --git a/mkdir/test.rs b/mkdir/test.rs index 58dfaaa8a..299550e51 100644 --- a/mkdir/test.rs +++ b/mkdir/test.rs @@ -1,3 +1,70 @@ +use std::{run}; +use std::io::fs::rmdir; + +static exe: &'static str = "build/mkdir"; +static test_dir1: &'static str = "mkdir_test1"; +static test_dir2: &'static str = "mkdir_test1/mkdir_test2"; + fn main() { - + test_mkdir_mkdir(); + test_mkdir_dup_dir(); + test_mkdir_mode(); + test_mkdir_parent(); + test_mkdir_no_parent(); +} + +fn cleanup() { + let dirs = [test_dir2, test_dir1]; + for d in dirs.iter() { + let p = Path::new(d.into_owned()); + if p.exists() { + rmdir(&p); + } + } +} + +fn test_mkdir_mkdir() { + cleanup(); + let prog = run::process_status(exe.into_owned(), [test_dir1.into_owned()]); + let exit_success = prog.unwrap().success(); + cleanup(); + assert_eq!(exit_success, true); +} + +fn test_mkdir_dup_dir() { + cleanup(); + let prog = run::process_status(exe.into_owned(), [test_dir1.into_owned()]); + let exit_success = prog.unwrap().success(); + if !exit_success { + cleanup(); + fail!(); + } + let prog2 = run::process_status(exe.into_owned(), [test_dir1.into_owned()]); + let exit_success2 = prog2.unwrap().success(); + cleanup(); + assert_eq!(exit_success2, false); +} + +fn test_mkdir_mode() { + cleanup(); + let prog = run::process_status(exe.into_owned(), [~"-m", ~"755", test_dir1.into_owned()]); + let exit_success = prog.unwrap().success(); + cleanup(); + assert_eq!(exit_success, true); +} + +fn test_mkdir_parent() { + cleanup(); + let prog = run::process_status(exe.into_owned(), [~"-p", test_dir2.into_owned()]); + let exit_success = prog.unwrap().success(); + cleanup(); + assert_eq!(exit_success, true); +} + +fn test_mkdir_no_parent() { + cleanup(); + let prog = run::process_status(exe.into_owned(), [test_dir2.into_owned()]); + let exit_success = prog.unwrap().success(); + cleanup(); + assert_eq!(exit_success, false); } From 233c49292d993ca37ca15ba55229bb9fde76f730 Mon Sep 17 00:00:00 2001 From: Nick Date: Fri, 3 Jan 2014 22:39:46 -0600 Subject: [PATCH 5/8] Update readme, add success message at end of tests --- README.md | 1 - cat/test.rs | 1 + mkdir/test.rs | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bc4adc52b..d17a257b2 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,6 @@ To do - ls - make-prime-list - md5sum -- mkdir - mkfifo - mknod - mktemp diff --git a/cat/test.rs b/cat/test.rs index 3c17b6c31..1cfddfddf 100644 --- a/cat/test.rs +++ b/cat/test.rs @@ -4,6 +4,7 @@ fn main() { test_output_multi_files_print_all_chars(); test_stdin_squeeze(); test_stdin_number_non_blank(); + println("cat tests completed successfully!\n"); } fn test_output_multi_files_print_all_chars() { diff --git a/mkdir/test.rs b/mkdir/test.rs index 299550e51..5bcff8e0a 100644 --- a/mkdir/test.rs +++ b/mkdir/test.rs @@ -11,6 +11,7 @@ fn main() { test_mkdir_mode(); test_mkdir_parent(); test_mkdir_no_parent(); + println("mkdir tests completed successfully!\n"); } fn cleanup() { From 3a63b509562597fe8c15cc2c54d61602857e31b3 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 5 Jan 2014 16:03:13 -0600 Subject: [PATCH 6/8] move parent opt check, update tests --- Makefile | 2 +- mkdir/mkdir.rs | 17 ++++++++------ mkdir/test.rs | 63 ++++++++++++++++++++++++-------------------------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 611f04310..14a0063c0 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ test_$(1): tmp/$(1)_test build build/$(1) $(call command,tmp/$(1)_test) tmp/$(1)_test: $(1)/test.rs - $(call command,$(RUSTC) $(RUSTCFLAGS) -o tmp/$(1)_test $(1)/test.rs) + $(call command,$(RUSTC) $(RUSTCFLAGS) --test -o tmp/$(1)_test $(1)/test.rs) endef # Main rules diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index 0a112e595..f8e4d92c4 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -98,13 +98,16 @@ fn print_help(opts: &[groups::OptGroup]) { */ fn exec(dirs: ~[~str], mk_parents: bool, mode: u32, verbose: bool) { let mut parent_dirs: ~[~str] = ~[]; - for dir in dirs.iter() { - let path = Path::new((*dir).clone()); - // Build list of parent dirs which need to be created - if mk_parents { - match path.dirname_str() { - Some(p) => if p != "." { - parent_dirs.push(p.into_owned()) + if mk_parents { + for dir in dirs.iter() { + let path = Path::new((*dir).clone()); + // Build list of parent dirs which need to be created + let parent = path.dirname_str(); + match parent { + Some(p) => { + if !Path::new(p).exists() { + parent_dirs.push(p.into_owned()) + } }, None => () } diff --git a/mkdir/test.rs b/mkdir/test.rs index 5bcff8e0a..25a51a464 100644 --- a/mkdir/test.rs +++ b/mkdir/test.rs @@ -3,69 +3,66 @@ use std::io::fs::rmdir; static exe: &'static str = "build/mkdir"; static test_dir1: &'static str = "mkdir_test1"; -static test_dir2: &'static str = "mkdir_test1/mkdir_test2"; +static test_dir2: &'static str = "mkdir_test2"; +static test_dir3: &'static str = "mkdir_test3"; +static test_dir4: &'static str = "mkdir_test4/mkdir_test4_1"; +static test_dir5: &'static str = "mkdir_test5/mkdir_test5_1"; -fn main() { - test_mkdir_mkdir(); - test_mkdir_dup_dir(); - test_mkdir_mode(); - test_mkdir_parent(); - test_mkdir_no_parent(); - println("mkdir tests completed successfully!\n"); -} - -fn cleanup() { - let dirs = [test_dir2, test_dir1]; - for d in dirs.iter() { - let p = Path::new(d.into_owned()); - if p.exists() { - rmdir(&p); - } +fn cleanup(dir: &'static str) { + let d = dir.into_owned(); + let p = Path::new(d.into_owned()); + if p.exists() { + rmdir(&p); } } +#[test] fn test_mkdir_mkdir() { - cleanup(); + cleanup(test_dir1); let prog = run::process_status(exe.into_owned(), [test_dir1.into_owned()]); let exit_success = prog.unwrap().success(); - cleanup(); + cleanup(test_dir1); assert_eq!(exit_success, true); } +#[test] fn test_mkdir_dup_dir() { - cleanup(); - let prog = run::process_status(exe.into_owned(), [test_dir1.into_owned()]); + cleanup(test_dir2); + let prog = run::process_status(exe.into_owned(), [test_dir2.into_owned()]); let exit_success = prog.unwrap().success(); if !exit_success { - cleanup(); + cleanup(test_dir2); fail!(); } - let prog2 = run::process_status(exe.into_owned(), [test_dir1.into_owned()]); + let prog2 = run::process_status(exe.into_owned(), [test_dir2.into_owned()]); let exit_success2 = prog2.unwrap().success(); - cleanup(); + cleanup(test_dir2); assert_eq!(exit_success2, false); } +#[test] fn test_mkdir_mode() { - cleanup(); - let prog = run::process_status(exe.into_owned(), [~"-m", ~"755", test_dir1.into_owned()]); + cleanup(test_dir3); + let prog = run::process_status(exe.into_owned(), [~"-m", ~"755", test_dir3.into_owned()]); let exit_success = prog.unwrap().success(); - cleanup(); + cleanup(test_dir3); assert_eq!(exit_success, true); } +#[test] fn test_mkdir_parent() { - cleanup(); - let prog = run::process_status(exe.into_owned(), [~"-p", test_dir2.into_owned()]); + cleanup(test_dir4); + let prog = run::process_status(exe.into_owned(), [~"-p", test_dir4.into_owned()]); let exit_success = prog.unwrap().success(); - cleanup(); + cleanup(test_dir4); assert_eq!(exit_success, true); } +#[test] fn test_mkdir_no_parent() { - cleanup(); - let prog = run::process_status(exe.into_owned(), [test_dir2.into_owned()]); + cleanup(test_dir5); + let prog = run::process_status(exe.into_owned(), [test_dir5.into_owned()]); let exit_success = prog.unwrap().success(); - cleanup(); + cleanup(test_dir5); assert_eq!(exit_success, false); } From 9cd301f653e7855c085e3269ccffeb56cd471176 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 5 Jan 2014 16:22:04 -0600 Subject: [PATCH 7/8] remove unneeded typing, swap cat and mkdir --- Makefile | 2 +- mkdir/mkdir.rs | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 14a0063c0..d6ddf8589 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,8 @@ EXES := \ # Programs with usable tests TEST_PROGS := \ - mkdir \ cat \ + mkdir \ TEST ?= $(TEST_PROGS) diff --git a/mkdir/mkdir.rs b/mkdir/mkdir.rs index f8e4d92c4..3dfd8488c 100644 --- a/mkdir/mkdir.rs +++ b/mkdir/mkdir.rs @@ -22,9 +22,9 @@ static VERSION: &'static str = "1.0.0"; * Handles option parsing */ fn main() { - let args: ~[~str] = os::args(); + let args = os::args(); - let opts: ~[groups::OptGroup] = ~[ + let opts = ~[ // Linux-specific options, not implemented // groups::optflag("Z", "context", "set SELinux secutiry context" + // " of each created directory to CTX"), @@ -32,9 +32,9 @@ fn main() { groups::optflag("p", "parents", "make parent directories as needed"), groups::optflag("v", "verbose", "print a message for each printed directory"), - groups::optflag("", "help", "display this help"), + groups::optflag("h", "help", "display this help"), groups::optflag("", "version", "display this version") - ]; + ]; let matches = match groups::getopts(args.tail(), opts) { Ok(m) => m, @@ -54,18 +54,14 @@ fn main() { println("mkdir v" + VERSION); return; } - let mut verbose_flag: bool = false; - if matches.opt_present("verbose") { - verbose_flag = true; - } - - let mk_parents: bool = matches.opt_present("parents"); + let verbose_flag = matches.opt_present("verbose"); + let mk_parents = matches.opt_present("parents"); // Translate a ~str in octal form to u32, default to 755 // Not tested on Windows let mode_match = matches.opts_str(&[~"mode"]); let mode: u32 = if mode_match.is_some() { - let m: ~str = mode_match.unwrap(); + let m = mode_match.unwrap(); let res = strconv::from_str_common(m, 8, false, false, false, strconv::ExpNone, false, false); @@ -81,7 +77,7 @@ fn main() { 0o755 }; - let dirs: ~[~str] = matches.free; + let dirs = matches.free; exec(dirs, mk_parents, mode, verbose_flag); } @@ -122,17 +118,17 @@ fn exec(dirs: ~[~str], mk_parents: bool, mode: u32, verbose: bool) { let path = Path::new((*dir).clone()); // Determine if parent directory to the one to // be created exists - let parent: &str = match path.dirname_str() { + let parent = match path.dirname_str() { Some(p) => p, None => "" }; - let parent_exists:bool = Path::new(parent).exists(); + let parent_exists = Path::new(parent).exists(); if parent_exists && !path.exists() { // if mkdir failed return if !mkdir(&path, mode) {return;} if verbose {println(*dir);} } else { - let mut error_msg: ~str = ~""; + let mut error_msg = ~""; if !parent_exists { error_msg.push_str("Error: parent directory '"); error_msg.push_str(parent); From 73a19a0818b643e8d6325ec769541a68dce522e5 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 5 Jan 2014 16:27:35 -0600 Subject: [PATCH 8/8] update test dir paths into tmp dir --- mkdir/test.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mkdir/test.rs b/mkdir/test.rs index 25a51a464..c3cf9c9ac 100644 --- a/mkdir/test.rs +++ b/mkdir/test.rs @@ -2,11 +2,11 @@ use std::{run}; use std::io::fs::rmdir; static exe: &'static str = "build/mkdir"; -static test_dir1: &'static str = "mkdir_test1"; -static test_dir2: &'static str = "mkdir_test2"; -static test_dir3: &'static str = "mkdir_test3"; -static test_dir4: &'static str = "mkdir_test4/mkdir_test4_1"; -static test_dir5: &'static str = "mkdir_test5/mkdir_test5_1"; +static test_dir1: &'static str = "tmp/mkdir_test1"; +static test_dir2: &'static str = "tmp/mkdir_test2"; +static test_dir3: &'static str = "tmp/mkdir_test3"; +static test_dir4: &'static str = "tmp/mkdir_test4/mkdir_test4_1"; +static test_dir5: &'static str = "tmp/mkdir_test5/mkdir_test5_1"; fn cleanup(dir: &'static str) { let d = dir.into_owned();