122 lines
3.6 KiB
Rust
122 lines
3.6 KiB
Rust
use std::{
|
|
fs::{self, File},
|
|
io::Write,
|
|
path::Path,
|
|
};
|
|
|
|
use tempfile::tempdir;
|
|
use zip::write::FileOptions;
|
|
|
|
use super::*;
|
|
|
|
fn limits() -> InputLimits {
|
|
InputLimits::default()
|
|
}
|
|
|
|
fn write_file(path: &Path, content: &str) {
|
|
let mut file = File::create(path).expect("create fixture");
|
|
file.write_all(content.as_bytes()).expect("write fixture");
|
|
}
|
|
|
|
fn create_zip(path: &Path) -> std::io::Result<()> {
|
|
let file = File::create(path)?;
|
|
let mut zip = zip::ZipWriter::new(file);
|
|
let options: FileOptions<()> = FileOptions::default()
|
|
.compression_method(zip::CompressionMethod::Stored)
|
|
.unix_permissions(0o644);
|
|
|
|
zip.start_file("foo.txt", options)?;
|
|
zip.write_all(b"hello foo")?;
|
|
zip.start_file("bar.txt", options)?;
|
|
zip.write_all(b"hello bar")?;
|
|
zip.finish()?;
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn reads_one_file() {
|
|
let directory = tempdir().expect("temp dir");
|
|
let path = directory.path().join("test.txt");
|
|
write_file(&path, "line1\nline2");
|
|
|
|
let documents = read(&path, &limits())
|
|
.expect("open input")
|
|
.collect::<Result<Vec<_>>>()
|
|
.expect("read input");
|
|
|
|
assert_eq!(documents.len(), 1);
|
|
assert_eq!(documents[0].source, SourceId::File(path));
|
|
assert_eq!(documents[0].content, "line1\nline2");
|
|
}
|
|
|
|
#[test]
|
|
fn yields_a_directory_error_without_discarding_other_files() {
|
|
let directory = tempdir().expect("temp dir");
|
|
write_file(&directory.path().join("a.txt"), "a");
|
|
fs::write(directory.path().join("b.txt"), [0xff]).expect("write invalid fixture");
|
|
write_file(&directory.path().join("c.txt"), "c");
|
|
|
|
let results = read(directory.path(), &limits())
|
|
.expect("open directory")
|
|
.collect::<Vec<_>>();
|
|
|
|
assert_eq!(results.len(), 3);
|
|
assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 2);
|
|
assert_eq!(results.iter().filter(|result| result.is_err()).count(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn streams_archive_members_with_outer_identity() {
|
|
let directory = tempdir().expect("temp dir");
|
|
let path = directory.path().join("archive.zip");
|
|
create_zip(&path).expect("create zip");
|
|
|
|
let documents = read(&path, &limits())
|
|
.expect("open archive")
|
|
.collect::<Result<Vec<_>>>()
|
|
.expect("read archive");
|
|
|
|
assert_eq!(documents.len(), 2);
|
|
assert!(documents.iter().all(|document| matches!(
|
|
&document.source,
|
|
SourceId::ArchiveMember { archive, .. } if archive == &path
|
|
)));
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_utf8_is_a_per_document_error() {
|
|
let directory = tempdir().expect("temp dir");
|
|
let path = directory.path().join("invalid.txt");
|
|
fs::write(&path, [0xff, 0xfe]).expect("write fixture");
|
|
|
|
let mut documents = read(&path, &limits()).expect("open input");
|
|
assert!(documents.next().expect("one result").is_err());
|
|
assert!(documents.next().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn missing_path_is_rejected() {
|
|
let directory = tempdir().expect("temp dir");
|
|
let path = directory.path().join("missing.txt");
|
|
|
|
let error = read(&path, &limits()).expect_err("missing input should fail");
|
|
assert!(error.to_string().contains("neither a file nor a directory"));
|
|
}
|
|
|
|
#[test]
|
|
fn enforces_document_size_limit_before_reading() {
|
|
let directory = tempdir().expect("temp dir");
|
|
let path = directory.path().join("large.txt");
|
|
write_file(&path, "too large");
|
|
let mut limits = limits();
|
|
limits.max_document_bytes = 3;
|
|
|
|
let mut documents = read(&path, &limits).expect("open input");
|
|
let error = documents
|
|
.next()
|
|
.expect("one result")
|
|
.expect_err("oversized input");
|
|
|
|
assert!(error.to_string().contains("document limit"));
|
|
}
|