Skip to content

Commit

Permalink
fix(oli): oli commands don't work properly for files in CWD (#2833)
Browse files Browse the repository at this point in the history
* Fix oli to handle files in CWD.

* Fix clippy.
  • Loading branch information
sarutak authored Aug 10, 2023
1 parent 4b33fcd commit 2c49350
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bin/oli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ impl Config {
fs_builder.root(if base.is_empty() { "/" } else { base });
filename
}
_ => s,
_ => {
fs_builder.root(".");
s
}
};

return Ok((Operator::new(fs_builder)?.finish(), filename.into()));
Expand Down
22 changes: 22 additions & 0 deletions bin/oli/tests/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,25 @@ async fn test_basic_cat() -> Result<()> {
assert_eq!(output_stdout, actual);
Ok(())
}

#[tokio::test]
async fn test_cat_for_path_in_current_dir() -> Result<()> {
let dir = tempfile::tempdir()?;
let dst_path = dir.path().join("dst.txt");
let expect = "hello";
fs::write(&dst_path, expect)?;

let mut cmd = Command::cargo_bin("oli")?;

cmd.arg("cat")
.arg("dst.txt")
.current_dir(dir.path().clone());
let actual = fs::read_to_string(&dst_path)?;
let res = cmd.assert().success();
let output = res.get_output().stdout.clone();

let output_stdout = String::from_utf8(output)?;

assert_eq!(output_stdout, actual);
Ok(())
}
21 changes: 21 additions & 0 deletions bin/oli/tests/cp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,24 @@ async fn test_basic_cp() -> Result<()> {
assert_eq!(expect, actual);
Ok(())
}

#[tokio::test]
async fn test_cp_for_path_in_current_dir() -> Result<()> {
let dir = tempfile::tempdir()?;
let src_path = dir.path().join("src.txt");
let dst_path = dir.path().join("dst.txt");
let expect = "hello";
fs::write(src_path, expect)?;

let mut cmd = Command::cargo_bin("oli")?;

cmd.arg("cp")
.arg("src.txt")
.arg("dst.txt")
.current_dir(dir.path().clone());
cmd.assert().success();

let actual = fs::read_to_string(dst_path)?;
assert_eq!(expect, actual);
Ok(())
}
16 changes: 16 additions & 0 deletions bin/oli/tests/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@ async fn test_basic_rm() -> Result<()> {
assert!(fs::read_to_string(&dst_path).is_err());
Ok(())
}

#[tokio::test]
async fn test_rm_for_path_in_current_dir() -> Result<()> {
let dir = tempfile::tempdir()?;
let dst_path = dir.path().join("dst.txt");
let expect = "hello";
fs::write(&dst_path, expect)?;

let mut cmd = Command::cargo_bin("oli")?;

cmd.arg("rm").arg("dst.txt").current_dir(dir.path().clone());
cmd.assert().success();

assert!(fs::read_to_string(&dst_path).is_err());
Ok(())
}
24 changes: 24 additions & 0 deletions bin/oli/tests/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ async fn test_basic_stat() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn test_stat_for_path_in_current_dir() -> Result<()> {
let dir = tempfile::tempdir()?;
let dst_path = dir.path().join("dst.txt");
let expect = "hello";
fs::write(dst_path, expect)?;

let mut cmd = Command::cargo_bin("oli")?;

cmd.arg("stat")
.arg("dst.txt")
.current_dir(dir.path().clone());
let res = cmd.assert().success();
let output = res.get_output().stdout.clone();

let output_stdout = String::from_utf8(output)?;
assert!(output_stdout.contains("path: dst.txt"));
assert!(output_stdout.contains("size: 5"));
assert!(output_stdout.contains("type: file"));
assert!(output_stdout.contains("last-modified: "));

Ok(())
}

0 comments on commit 2c49350

Please sign in to comment.