diff --git a/Makefile b/Makefile index c7016e634..c97bcd906 100644 --- a/Makefile +++ b/Makefile @@ -177,6 +177,7 @@ TEST_PROGS := \ fold \ hashsum \ head \ + link \ ln \ mkdir \ mv \ diff --git a/test/common/util.rs b/test/common/util.rs index 8d62fab5b..cb4a99c83 100644 --- a/test/common/util.rs +++ b/test/common/util.rs @@ -64,6 +64,11 @@ pub fn get_file_contents(name: &str) -> String { contents } +pub fn set_file_contents(name: &str, contents: &str) { + let mut f = File::open(Path::new(name)).unwrap(); + let _ = f.write(contents.as_bytes()); +} + pub fn mkdir(dir: &str) { fs::create_dir(Path::new(dir)).unwrap(); } diff --git a/test/link.rs b/test/link.rs new file mode 100644 index 000000000..44ce2c2bd --- /dev/null +++ b/test/link.rs @@ -0,0 +1,49 @@ +extern crate libc; + +use std::process::Command; +use util::*; + +static PROGNAME: &'static str = "./link"; + +#[path = "common/util.rs"] +#[macro_use] +mod util; + +#[test] +fn test_link_existing_file() { + let file = "test_link_existing_file"; + let link = "test_link_existing_file_link"; + + touch(file); + set_file_contents(file, "foobar"); + assert!(file_exists(file)); + + let result = run(Command::new(PROGNAME).args(&[file, link])); + assert_empty_stderr!(result); + assert!(result.success); + assert!(file_exists(file)); + assert!(file_exists(link)); + assert_eq!(get_file_contents(file), get_file_contents(link)); +} + +#[test] +fn test_link_no_circular() { + let link = "test_link_no_circular"; + + let result = run(Command::new(PROGNAME).args(&[link, link])); + assert_eq!(result.stderr, "link: error: No such file or directory (os error 2)\n"); + assert!(!result.success); + assert!(!file_exists(link)); +} + +#[test] +fn test_link_nonexistent_file() { + let file = "test_link_nonexistent_file"; + let link = "test_link_nonexistent_file_link"; + + let result = run(Command::new(PROGNAME).args(&[file, link])); + assert_eq!(result.stderr, "link: error: No such file or directory (os error 2)\n"); + assert!(!result.success); + assert!(!file_exists(file)); + assert!(!file_exists(link)); +}