Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: File downloads #22

Merged
merged 8 commits into from
Feb 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make .bundle files during integration test setup
This is so that we can test file downloads.
ev3nvy committed Feb 6, 2023
commit abbed60627480683787823177c918c291427c263
51 changes: 32 additions & 19 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ trait MakeHeader {
trait WriteFile: MakeHeader {
fn name(&self) -> &str;

fn write_file(&self, bytes: &[u8]) -> Result<(), Error> {
fn write_manifest_file(&self, bytes: &[u8]) -> Result<(), Error> {
let uncompressed_size = bytes.len();

let compressed = zstd::encode_all(bytes, 19).expect("error compressing");
@@ -57,12 +57,30 @@ trait WriteFile: MakeHeader {

let bytes = [&head[..], &compressed[..]].concat();

let path = Path::new(env!("OUT_DIR")).join(self.name());
let mut path = Path::new(env!("OUT_DIR")).join(self.name());
path.set_extension("manifest");
let mut file = fs::File::create(path)?;
file.write_all(&bytes)?;

Ok(())
}

fn write_bundle_file(&self, bytes: &[u8]) -> Result<(u32, u32), Error> {
let uncompressed_size = bytes.len();

let compressed = zstd::encode_all(bytes, 19).expect("error compressing");
let compressed_size = compressed.len();

let mut path = Path::new(env!("OUT_DIR")).join(self.name());
path.set_extension("bundle");
let mut file = fs::File::create(path)?;
file.write_all(&compressed)?;

Ok((
compressed_size.try_into().unwrap(),
uncompressed_size.try_into().unwrap(),
))
}
}

pub struct ValidManifest {
@@ -72,7 +90,7 @@ pub struct ValidManifest {
impl ValidManifest {
pub fn new() -> Self {
Self {
name: "valid.manifest".to_owned(),
name: "valid".to_owned(),
}
}

@@ -83,24 +101,19 @@ impl ValidManifest {
pub fn generate(&self) {
let mut builder = flatbuffers::FlatBufferBuilder::new();

let chunk_0 = Chunk::create(
let bundle_data = vec![b'T', b'E', b'S', b'T'];
let (compressed_size, uncompressed_size) = self.write_bundle_file(&bundle_data).unwrap();

let chunk = Chunk::create(
&mut builder,
&ChunkArgs {
id: 0,
compressed_size: 0,
uncompressed_size: 0,
},
);
let chunk_1 = Chunk::create(
&mut builder,
&ChunkArgs {
id: 1,
compressed_size: 0,
uncompressed_size: 0,
compressed_size,
uncompressed_size,
},
);

let chunks = Some(builder.create_vector(&[chunk_0, chunk_1]));
let chunks = Some(builder.create_vector(&[chunk]));

let bundle_0 = Bundle::create(&mut builder, &BundleArgs { id: 0, chunks });

@@ -141,7 +154,7 @@ impl ValidManifest {

let name = Some(builder.create_string("file.txt"));
let symlink = Some(builder.create_string(""));
let chunk_ids = Some(builder.create_vector(&[0i64, 1]));
let chunk_ids = Some(builder.create_vector(&[0i64]));
let file = File::create(
&mut builder,
&FileArgs {
@@ -182,7 +195,7 @@ impl ValidManifest {

builder.finish(manifest, None);

self.write_file(builder.finished_data()).unwrap();
self.write_manifest_file(builder.finished_data()).unwrap();
}
}

@@ -201,7 +214,7 @@ pub struct ValidEmptyManifest {
impl ValidEmptyManifest {
pub fn new() -> Self {
Self {
name: "valid_empty.manifest".to_owned(),
name: "valid_empty".to_owned(),
}
}

@@ -233,7 +246,7 @@ impl ValidEmptyManifest {

builder.finish(manifest, None);

self.write_file(builder.finished_data()).unwrap();
self.write_manifest_file(builder.finished_data()).unwrap();
}
}

24 changes: 16 additions & 8 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@ mod common;
pub fn should_parse_from_path_when_valid_manifest() {
let valid_file = common::ValidManifest::new();
valid_file.generate();
if let Err(error) = RiotManifest::from_path(valid_file.path()) {
let mut path = valid_file.path();
path.set_extension("manifest");
if let Err(error) = RiotManifest::from_path(path) {
panic!(
"there was an error when trying to parse the manifest, manifest: {:?}",
error
@@ -18,7 +20,9 @@ pub fn should_parse_from_path_when_valid_manifest() {
pub fn should_have_correct_values_when_valid_manifest() {
let valid_file = common::ValidManifest::new();
valid_file.generate();
let manifest = RiotManifest::from_path(valid_file.path()).unwrap();
let mut path = valid_file.path();
path.set_extension("manifest");
let manifest = RiotManifest::from_path(path).unwrap();

// FIXME: don't check for equality on compressed size and uncompressed size
// compressed and uncompressed size could change in the future
@@ -28,9 +32,9 @@ pub fn should_have_correct_values_when_valid_manifest() {
minor: 0,
flags: 512,
offset: 28,
compressed_size: 202,
compressed_size: 189,
manifest_id: 0,
uncompressed_size: 392,
uncompressed_size: 376,
};

assert_eq!(
@@ -44,8 +48,8 @@ pub fn should_have_correct_values_when_valid_manifest() {
);
assert_eq!(
manifest.data.bundle_entries[0].chunks.len(),
2,
"bundle entry should have 2 chunks"
1,
"bundle entry should have 1 chunk"
);
assert_eq!(
manifest.data.directory_entries.len(),
@@ -79,7 +83,9 @@ pub fn should_have_correct_values_when_valid_manifest() {
pub fn should_parse_from_path_when_valid_empty_manifest() {
let valid_empty_file = common::ValidEmptyManifest::new();
valid_empty_file.generate();
if let Err(error) = RiotManifest::from_path(valid_empty_file.path()) {
let mut path = valid_empty_file.path();
path.set_extension("manifest");
if let Err(error) = RiotManifest::from_path(path) {
panic!(
"there was an error when trying to parse the manifest, manifest: {:?}",
error
@@ -91,7 +97,9 @@ pub fn should_parse_from_path_when_valid_empty_manifest() {
pub fn should_have_correct_values_when_valid_empty_manifest() {
let valid_empty_file = common::ValidEmptyManifest::new();
valid_empty_file.generate();
let manifest = RiotManifest::from_path(valid_empty_file.path()).unwrap();
let mut path = valid_empty_file.path();
path.set_extension("manifest");
let manifest = RiotManifest::from_path(path).unwrap();

// FIXME: don't check for equality on compressed size and uncompressed size
// compressed and uncompressed size could change in the future