frontend-utils: Fixed tests

This commit is contained in:
Nathan Adams 2024-04-06 00:45:16 +02:00
parent e5fc56429e
commit 023ae5d14a
2 changed files with 22 additions and 14 deletions

View File

@ -67,6 +67,7 @@ mod tests {
use crate::bundle::info::{
BundleInformation, BundleInformationParseError, BUNDLE_INFORMATION_FILENAME,
};
use crate::bundle::source::BundleSourceError;
use crate::bundle::{Bundle, BundleError};
use tempfile::tempdir;
use url::Url;
@ -84,7 +85,10 @@ mod tests {
let tmp_dir = tempdir().unwrap();
let result = Bundle::from_path(tmp_dir.path());
drop(tmp_dir);
assert!(matches!(result, Err(BundleError::MissingBundleInformation)))
assert!(matches!(
result,
Err(BundleError::InvalidSource(BundleSourceError::UnknownSource))
))
}
#[test]
@ -93,7 +97,10 @@ mod tests {
let _ = std::fs::create_dir(tmp_dir.path().join(BUNDLE_INFORMATION_FILENAME));
let result = Bundle::from_path(tmp_dir.path());
drop(tmp_dir);
assert!(matches!(result, Err(BundleError::MissingBundleInformation)))
assert!(matches!(
result,
Err(BundleError::InvalidSource(BundleSourceError::UnknownSource))
))
}
#[test]

View File

@ -7,16 +7,20 @@ impl BundleSourceImpl for Path {
type Read = File;
fn read_file(&self, path: &str) -> Result<Self::Read, Error> {
let potential_path = self.join(path.strip_prefix('/').unwrap_or(path));
if !potential_path.starts_with(self) {
let potential_path = self
.join(path.strip_prefix('/').unwrap_or(path))
.canonicalize()?;
if !potential_path.starts_with(self.canonicalize()?) {
return Err(Error::from(ErrorKind::NotFound));
}
File::open(potential_path)
}
fn read_content(&self, path: &str) -> Result<Self::Read, Error> {
let root = self.join("content");
let potential_path = root.join(path.strip_prefix('/').unwrap_or(path));
let root = self.join("content").canonicalize()?;
let potential_path = root
.join(path.strip_prefix('/').unwrap_or(path))
.canonicalize()?;
if !potential_path.starts_with(root) {
return Err(Error::from(ErrorKind::NotFound));
}
@ -52,10 +56,8 @@ mod tests {
#[test]
fn read_file_invalid() {
let tmp_dir = tempdir().unwrap();
let success = matches!(
tmp_dir.path().read_file("!?\\//"),
Err(e) if e.kind() == ErrorKind::NotFound
);
// [NA] Exact error depends on OS... just check it's an error, period.
let success = tmp_dir.path().read_file("!?\\//").is_err();
drop(tmp_dir);
assert!(success)
}
@ -109,10 +111,9 @@ mod tests {
#[test]
fn read_content_invalid() {
let tmp_dir = tempdir().unwrap();
let success = matches!(
tmp_dir.path().read_content("!?\\//"),
Err(e) if e.kind() == ErrorKind::NotFound
);
// [NA] Exact error depends on OS... just check it's an error, period.
let success = tmp_dir.path().read_content("!?\\//").is_err();
drop(tmp_dir);
assert!(success)
}