From 2b19a6213456ac6e93bee389b32ee6991ac6e31b Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 7 Mar 2020 19:23:20 -0800 Subject: [PATCH] Test creating torrents from `.` and `..` Test that torrent gets actual name of directory, and is created in the correct location. type: testing --- src/env.rs | 11 ++++--- src/subcommand/torrent/create.rs | 50 ++++++++++++++++++++++++++++++++ src/test_env.rs | 13 +++++++-- src/test_env_builder.rs | 23 ++++++++++++--- 4 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/env.rs b/src/env.rs index cbca55e..6b116e3 100644 --- a/src/env.rs +++ b/src/env.rs @@ -2,7 +2,7 @@ use crate::common::*; pub(crate) struct Env { args: Vec, - dir: Box>, + dir: PathBuf, pub(crate) err: Box, pub(crate) out: Box, err_style: Style, @@ -63,8 +63,8 @@ impl Env { args.run(self) } - pub(crate) fn new( - dir: D, + pub(crate) fn new( + dir: PathBuf, out: O, out_style: Style, out_is_term: bool, @@ -73,7 +73,6 @@ impl Env { args: I, ) -> Self where - D: AsRef + 'static, O: Write + 'static, E: Write + 'static, S: Into, @@ -81,9 +80,9 @@ impl Env { { Self { args: args.into_iter().map(Into::into).collect(), - dir: Box::new(dir), err: Box::new(err), out: Box::new(out), + dir, out_style, out_is_term, err_style, @@ -133,7 +132,7 @@ impl Env { } pub(crate) fn dir(&self) -> &Path { - self.dir.as_ref().as_ref() + &self.dir } pub(crate) fn resolve(&self, path: impl AsRef) -> PathBuf { diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index 0db619c..bbb87e0 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -381,6 +381,56 @@ mod tests { assert!(matches!(value, Value::Dict(_))); } + #[test] + fn input_dot() { + let mut env = test_env! { + args: [ + "torrent", + "create", + "--input", + ".", + "--announce", + "https://bar", + ], + cwd: "dir", + tree: { + dir: { + foo: "", + }, + } + }; + env.run().unwrap(); + let metainfo = env.load_metainfo("../dir.torrent"); + assert_eq!(metainfo.info.name, "dir"); + assert_matches!(metainfo.info.mode, Mode::Multiple{files} if files.len() == 1); + } + + #[test] + fn input_dot_dot() { + let mut env = test_env! { + args: [ + "torrent", + "create", + "--input", + "..", + "--announce", + "https://bar", + ], + cwd: "a/b", + tree: { + a: { + b: { + foo: "", + }, + }, + } + }; + env.run().unwrap(); + let metainfo = env.load_metainfo("../../a.torrent"); + assert_eq!(metainfo.info.name, "a"); + assert_matches!(metainfo.info.mode, Mode::Multiple{files} if files.len() == 1); + } + #[test] fn privacy_defaults_to_false() { let mut env = test_env! { diff --git a/src/test_env.rs b/src/test_env.rs index e23e99a..b3e33ec 100644 --- a/src/test_env.rs +++ b/src/test_env.rs @@ -3,6 +3,7 @@ use crate::common::*; macro_rules! test_env { { args: [$($arg:expr),* $(,)?], + $(cwd: $cwd:expr,)? tree: { $($tree:tt)* } $(,)? @@ -11,6 +12,7 @@ macro_rules! test_env { let tempdir = temptree! { $($tree)* }; TestEnvBuilder::new() + $(.current_dir(tempdir.path().join($cwd)))? .tempdir(tempdir) .arg("imdl") $(.arg($arg))* @@ -21,13 +23,20 @@ macro_rules! test_env { pub(crate) struct TestEnv { env: Env, + #[allow(unused)] + tempdir: TempDir, err: Capture, out: Capture, } impl TestEnv { - pub(crate) fn new(env: Env, err: Capture, out: Capture) -> TestEnv { - Self { err, env, out } + pub(crate) fn new(tempdir: TempDir, env: Env, err: Capture, out: Capture) -> TestEnv { + Self { + tempdir, + err, + env, + out, + } } pub(crate) fn err(&self) -> String { diff --git a/src/test_env_builder.rs b/src/test_env_builder.rs index 6843576..5a51ed6 100644 --- a/src/test_env_builder.rs +++ b/src/test_env_builder.rs @@ -2,18 +2,20 @@ use crate::common::*; pub(crate) struct TestEnvBuilder { args: Vec, + current_dir: Option, out_is_term: bool, - use_color: bool, tempdir: Option, + use_color: bool, } impl TestEnvBuilder { pub(crate) fn new() -> TestEnvBuilder { TestEnvBuilder { args: Vec::new(), + current_dir: None, out_is_term: false, - use_color: false, tempdir: None, + use_color: false, } } @@ -27,6 +29,11 @@ impl TestEnvBuilder { self } + pub(crate) fn current_dir(mut self, path: PathBuf) -> Self { + self.current_dir = Some(path); + self + } + pub(crate) fn arg_slice(mut self, args: &[&str]) -> Self { for arg in args.iter().cloned() { self.args.push(arg.into()); @@ -43,8 +50,16 @@ impl TestEnvBuilder { let err = Capture::new(); let out = Capture::new(); + let tempdir = self.tempdir.unwrap_or_else(|| tempfile::tempdir().unwrap()); + + let current_dir = if let Some(current_dir) = self.current_dir { + tempdir.path().join(current_dir) + } else { + tempdir.path().to_owned() + }; + let env = Env::new( - self.tempdir.unwrap_or_else(|| tempfile::tempdir().unwrap()), + current_dir, out.clone(), if self.use_color && self.out_is_term { Style::active() @@ -57,6 +72,6 @@ impl TestEnvBuilder { self.args, ); - TestEnv::new(env, err, out) + TestEnv::new(tempdir, env, err, out) } }