Test creating torrents from . and ..

Test that torrent gets actual name of directory, and is created in the
correct location.

type: testing
This commit is contained in:
Casey Rodarmor 2020-03-07 19:23:20 -08:00
parent fa6d4e6ad0
commit 2b19a62134
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
4 changed files with 85 additions and 12 deletions

View File

@ -2,7 +2,7 @@ use crate::common::*;
pub(crate) struct Env { pub(crate) struct Env {
args: Vec<OsString>, args: Vec<OsString>,
dir: Box<dyn AsRef<Path>>, dir: PathBuf,
pub(crate) err: Box<dyn Write>, pub(crate) err: Box<dyn Write>,
pub(crate) out: Box<dyn Write>, pub(crate) out: Box<dyn Write>,
err_style: Style, err_style: Style,
@ -63,8 +63,8 @@ impl Env {
args.run(self) args.run(self)
} }
pub(crate) fn new<D, O, E, S, I>( pub(crate) fn new<O, E, S, I>(
dir: D, dir: PathBuf,
out: O, out: O,
out_style: Style, out_style: Style,
out_is_term: bool, out_is_term: bool,
@ -73,7 +73,6 @@ impl Env {
args: I, args: I,
) -> Self ) -> Self
where where
D: AsRef<Path> + 'static,
O: Write + 'static, O: Write + 'static,
E: Write + 'static, E: Write + 'static,
S: Into<OsString>, S: Into<OsString>,
@ -81,9 +80,9 @@ impl Env {
{ {
Self { Self {
args: args.into_iter().map(Into::into).collect(), args: args.into_iter().map(Into::into).collect(),
dir: Box::new(dir),
err: Box::new(err), err: Box::new(err),
out: Box::new(out), out: Box::new(out),
dir,
out_style, out_style,
out_is_term, out_is_term,
err_style, err_style,
@ -133,7 +132,7 @@ impl Env {
} }
pub(crate) fn dir(&self) -> &Path { pub(crate) fn dir(&self) -> &Path {
self.dir.as_ref().as_ref() &self.dir
} }
pub(crate) fn resolve(&self, path: impl AsRef<Path>) -> PathBuf { pub(crate) fn resolve(&self, path: impl AsRef<Path>) -> PathBuf {

View File

@ -381,6 +381,56 @@ mod tests {
assert!(matches!(value, Value::Dict(_))); 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] #[test]
fn privacy_defaults_to_false() { fn privacy_defaults_to_false() {
let mut env = test_env! { let mut env = test_env! {

View File

@ -3,6 +3,7 @@ use crate::common::*;
macro_rules! test_env { macro_rules! test_env {
{ {
args: [$($arg:expr),* $(,)?], args: [$($arg:expr),* $(,)?],
$(cwd: $cwd:expr,)?
tree: { tree: {
$($tree:tt)* $($tree:tt)*
} $(,)? } $(,)?
@ -11,6 +12,7 @@ macro_rules! test_env {
let tempdir = temptree! { $($tree)* }; let tempdir = temptree! { $($tree)* };
TestEnvBuilder::new() TestEnvBuilder::new()
$(.current_dir(tempdir.path().join($cwd)))?
.tempdir(tempdir) .tempdir(tempdir)
.arg("imdl") .arg("imdl")
$(.arg($arg))* $(.arg($arg))*
@ -21,13 +23,20 @@ macro_rules! test_env {
pub(crate) struct TestEnv { pub(crate) struct TestEnv {
env: Env, env: Env,
#[allow(unused)]
tempdir: TempDir,
err: Capture, err: Capture,
out: Capture, out: Capture,
} }
impl TestEnv { impl TestEnv {
pub(crate) fn new(env: Env, err: Capture, out: Capture) -> TestEnv { pub(crate) fn new(tempdir: TempDir, env: Env, err: Capture, out: Capture) -> TestEnv {
Self { err, env, out } Self {
tempdir,
err,
env,
out,
}
} }
pub(crate) fn err(&self) -> String { pub(crate) fn err(&self) -> String {

View File

@ -2,18 +2,20 @@ use crate::common::*;
pub(crate) struct TestEnvBuilder { pub(crate) struct TestEnvBuilder {
args: Vec<OsString>, args: Vec<OsString>,
current_dir: Option<PathBuf>,
out_is_term: bool, out_is_term: bool,
use_color: bool,
tempdir: Option<TempDir>, tempdir: Option<TempDir>,
use_color: bool,
} }
impl TestEnvBuilder { impl TestEnvBuilder {
pub(crate) fn new() -> TestEnvBuilder { pub(crate) fn new() -> TestEnvBuilder {
TestEnvBuilder { TestEnvBuilder {
args: Vec::new(), args: Vec::new(),
current_dir: None,
out_is_term: false, out_is_term: false,
use_color: false,
tempdir: None, tempdir: None,
use_color: false,
} }
} }
@ -27,6 +29,11 @@ impl TestEnvBuilder {
self 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 { pub(crate) fn arg_slice(mut self, args: &[&str]) -> Self {
for arg in args.iter().cloned() { for arg in args.iter().cloned() {
self.args.push(arg.into()); self.args.push(arg.into());
@ -43,8 +50,16 @@ impl TestEnvBuilder {
let err = Capture::new(); let err = Capture::new();
let out = 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( let env = Env::new(
self.tempdir.unwrap_or_else(|| tempfile::tempdir().unwrap()), current_dir,
out.clone(), out.clone(),
if self.use_color && self.out_is_term { if self.use_color && self.out_is_term {
Style::active() Style::active()
@ -57,6 +72,6 @@ impl TestEnvBuilder {
self.args, self.args,
); );
TestEnv::new(env, err, out) TestEnv::new(tempdir, env, err, out)
} }
} }