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

fix: make tests pass on Windows #252

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ jobs:
strategy:
matrix:
crate: ${{ fromJSON(needs.changes.outputs.needs_rust_build) }}
os: [macos-latest, windows-latest, ubuntu-latest]

runs-on: macos-latest
runs-on: ${{ matrix.os }}

name: 🦀 Build ${{ matrix.crate }}
name: 🦀 Build ${{ matrix.crate }} (${{ matrix.os }})

steps:
- name: 📚 Git Checkout
Expand Down
39 changes: 25 additions & 14 deletions library/src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,29 @@ pub fn libapp_path_from_settings(original_libapp_paths: &[String]) -> Result<Pat
#[cfg(test)]
mod tests {
use std::fs::File;
use std::path::Path;
use std::path::{Path, PathBuf};
use tempdir::TempDir;
use zip::write::FileOptions;
use zip::ZipWriter;

fn assert_error_is_file_not_found(error: &anyhow::Error) {
#[cfg(not(target_os = "windows"))]
assert!(error.to_string().contains("No such file or directory"));

#[cfg(target_os = "windows")]
assert!(error
.to_string()
.contains("The system cannot find the file specified"));
}

// Takes a path to the zip to create as well as a list of file names to
// create in the zip. The files will be empty.
fn create_zip_with_empty_files(zip_path: &Path, files: Vec<&str>) {
fn create_zip_with_empty_files(zip_path: &Path, files: Vec<PathBuf>) {
let file = File::create(zip_path).unwrap();
let mut zip = ZipWriter::new(file);
for file in files {
zip.start_file(file, FileOptions::default()).unwrap();
zip.start_file(file.to_string_lossy(), FileOptions::default())
.unwrap();
}
zip.finish().unwrap();
}
Expand All @@ -230,7 +241,7 @@ mod tests {
fn find_and_open_lib_test() {
let tmp_dir = TempDir::new("example").unwrap();
let error = super::find_and_open_lib(tmp_dir.path(), "libapp.so").unwrap_err();
assert!(error.to_string().contains("No such file or directory"));
assert_error_is_file_not_found(&error);

// Write an empty file (invalid apk) to the base apk.
let base_apk_path = tmp_dir.path().join("base.apk");
Expand All @@ -253,12 +264,12 @@ mod tests {

let base_apk_path = tmp_dir.path().join("base.apk");
let arch = super::android_arch_names();
let lib_path = format!("lib/{}/libapp.so", arch.lib_dir);
create_zip_with_empty_files(&base_apk_path, vec![&lib_path]);
let lib_path = Path::new("lib").join(arch.lib_dir).join("libapp.so");
create_zip_with_empty_files(&base_apk_path, vec![lib_path.clone()]);

let zip_location = super::find_and_open_lib(tmp_dir.path(), "libapp.so").unwrap();
// Success!
assert_eq!(zip_location.internal_path, lib_path);
assert_eq!(Path::new(&zip_location.internal_path), &lib_path);
// Otherwise coverage complains that we haven't used the Debug trait
// even though the Debug trait is required for assert_eq!.
let debug_str = format!("{:?}", zip_location);
Expand All @@ -273,28 +284,28 @@ mod tests {

// Write a base.apk with the wrong arch.
let base_apk_path = tmp_dir.path().join("base.apk");
create_zip_with_empty_files(&base_apk_path, vec!["lib/wrong/libapp.so"]);
create_zip_with_empty_files(&base_apk_path, vec![PathBuf::from("lib/wrong/libapp.so")]);

// Write a split apk with the right arch.
let arch = super::android_arch_names();
let split_apk_name = format!("app-hdpi{}-release.apk", arch.apk_split);
let split_apk_path: std::path::PathBuf = tmp_dir.path().join(split_apk_name);
let lib_path = format!("lib/{}/libapp.so", arch.lib_dir);
create_zip_with_empty_files(&split_apk_path, vec![&lib_path]);
let lib_path = Path::new("lib").join(arch.lib_dir).join("libapp.so");
create_zip_with_empty_files(&split_apk_path, vec![lib_path.clone()]);

// Write another apk early in the alphabet we skip over since it isn't
// a split apk.
let split_apk_path: std::path::PathBuf = tmp_dir.path().join("aaa.apk");
create_zip_with_empty_files(&split_apk_path, vec![&lib_path]);
create_zip_with_empty_files(&split_apk_path, vec![lib_path.clone()]);

// Write an apk with our arch name but not our library.
let split_apk_name = format!("aaa{}.apk", arch.apk_split);
let split_apk_path: std::path::PathBuf = tmp_dir.path().join(split_apk_name);
let split_apk_path = tmp_dir.path().join(split_apk_name);
create_zip_with_empty_files(&split_apk_path, vec![]);

let zip_location = super::find_and_open_lib(tmp_dir.path(), "libapp.so").unwrap();
// Success!
assert_eq!(zip_location.internal_path, lib_path);
assert_eq!(Path::new(&zip_location.internal_path), lib_path);
}

#[test]
Expand All @@ -311,6 +322,6 @@ mod tests {
fn open_base_lib_test() {
let tmp_dir = TempDir::new("example").unwrap();
let error = super::open_base_lib(tmp_dir.path(), "libapp.so").unwrap_err();
assert!(error.to_string().contains("No such file or directory"));
assert_error_is_file_not_found(&error);
}
}
7 changes: 2 additions & 5 deletions library/src/cache/patch_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,8 @@ mod debug_tests {
fn patch_manager_is_debug() {
let temp_dir = TempDir::new("patch_manager").unwrap();
let patch_manager = PatchManager::new(temp_dir.path().to_owned(), Some("public_key"));
let expected_str = format!(
"PatchManager {{ root_dir: \"{}\", patches_state: PatchesState {{ last_booted_patch: None, next_boot_patch: None, currently_booting_patch: None, known_bad_patches: {{}} }}, patch_public_key: Some(\"public_key\") }}",
temp_dir.path().display()
);
assert_eq!(format!("{:?}", patch_manager), expected_str);
let actual = format!("{:?}", patch_manager);
assert!(actual.contains(r#"patches_state: PatchesState { last_booted_patch: None, next_boot_patch: None, currently_booting_patch: None, known_bad_patches: {} }, patch_public_key: Some("public_key") }"#));
}
}

Expand Down
9 changes: 7 additions & 2 deletions library/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ pub fn current_platform() -> &'static str {

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use super::set_config;
use crate::{network::NetworkHooks, testing_reset_config, AppConfig, ExternalFileProvider};
use anyhow::Result;
Expand Down Expand Up @@ -222,8 +224,11 @@ mod tests {
)?;

let config = super::with_config(|config| Ok(config.clone())).unwrap();
assert_eq!(config.storage_dir.to_str(), Some("/app_storage"));
assert_eq!(config.download_dir.to_str(), Some("/code_cache/downloads"));
assert_eq!(config.storage_dir, PathBuf::from("/app_storage"));
assert_eq!(
config.download_dir,
PathBuf::from("/").join("code_cache").join("downloads")
);
assert!(config.auto_update);
assert_eq!(config.channel, "fake_channel");
assert_eq!(config.app_id, "fake_app_id");
Expand Down
Loading