Skip to content

Commit

Permalink
fix(lib): Fix root escaping check in lib::vcs
Browse files Browse the repository at this point in the history
Canonicalize the root in vcs to ensure that `Path::strip_prefix` is applied
correctly.
  • Loading branch information
tingerrr committed Jul 23, 2024
1 parent 6673433 commit 93e73c6
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions crates/typst-test-lib/src/store/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ pub struct Git {

impl Git {
/// Creates a new git vcs abstraction with the given git root directory.
pub fn new<P: Into<PathBuf>>(root: P) -> Self {
Self { root: root.into() }
pub fn new<P: Into<PathBuf>>(root: P) -> io::Result<Self> {
Ok(Self {
root: root.into().canonicalize()?,
})
}

pub fn ensure_no_escape(&self, in_root: &Path) -> io::Result<()> {
Expand Down Expand Up @@ -182,7 +184,7 @@ mod tests {
_dev::fs::TempEnv::run(
|root| root,
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
assert_eq!(vcs.ignore(root).unwrap_err().kind(), io::ErrorKind::Other);
},
|root| root,
Expand All @@ -194,7 +196,7 @@ mod tests {
_dev::fs::TempEnv::run(
|root| root.setup_dir("fancy/out"),
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.ignore(&root.join("fancy/out")).unwrap();
},
|root| {
Expand All @@ -212,7 +214,7 @@ mod tests {
.setup_file("fancy/out/.gitignore", "ref.pdf")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.ignore(&root.join("fancy/out")).unwrap();
},
|root| {
Expand All @@ -230,7 +232,7 @@ mod tests {
.setup_file("fancy/out/.gitignore", "**")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.ignore(&root.join("fancy/out")).unwrap();
},
|root| {
Expand All @@ -245,7 +247,7 @@ mod tests {
_dev::fs::TempEnv::run(
|root| root.setup_file_empty("fancy/out.txt"),
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.ignore(&root.join("fancy/out.txt")).unwrap();
},
|root| {
Expand All @@ -263,7 +265,7 @@ mod tests {
.setup_file("fancy/.gitignore", "ref.pdf")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.ignore(&root.join("fancy/out.txt")).unwrap();
},
|root| {
Expand All @@ -281,7 +283,7 @@ mod tests {
.setup_file("fancy/.gitignore", "out.txt")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.ignore(&root.join("fancy/out.txt")).unwrap();
},
|root| {
Expand All @@ -299,7 +301,7 @@ mod tests {
.setup_file("fancy/out/.gitignore", "ref.pdf\n**")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out")).unwrap();
},
|root| {
Expand All @@ -317,7 +319,7 @@ mod tests {
.setup_file("fancy/out/.gitignore", "**")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out")).unwrap();
},
|root| root.expect_dir("fancy/out"),
Expand All @@ -332,7 +334,7 @@ mod tests {
.setup_file("fancy/out/.gitignore", "ref.pdf\n")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out")).unwrap();
},
|root| {
Expand All @@ -347,7 +349,7 @@ mod tests {
_dev::fs::TempEnv::run(
|root| root.setup_dir("fancy/out"),
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out")).unwrap();
},
|root| root.expect_dir("fancy/out"),
Expand All @@ -362,7 +364,7 @@ mod tests {
.setup_file_empty("fancy/out.txt")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out.txt")).unwrap();
},
|root| {
Expand All @@ -380,7 +382,7 @@ mod tests {
.setup_file_empty("fancy/out.txt")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out.txt")).unwrap();
},
|root| root.expect_file_empty("fancy/out.txt"),
Expand All @@ -395,7 +397,7 @@ mod tests {
.setup_file("fancy/.gitignore", "ref.pdf\n")
},
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out.txt")).unwrap();
},
|root| {
Expand All @@ -410,7 +412,7 @@ mod tests {
_dev::fs::TempEnv::run(
|root| root.setup_file_empty("fancy/out.txt"),
|root| {
let vcs = Git::new(root);
let vcs = Git::new(root).unwrap();
vcs.unignore(&root.join("fancy/out.txt")).unwrap();
},
|root| root.expect_dir("fancy/out.txt"),
Expand Down

0 comments on commit 93e73c6

Please sign in to comment.