From 66428d9cb7c314491d730248679f4ad8033a5a10 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Tue, 16 Apr 2024 16:19:21 +0200 Subject: Enable tests that depend on bincode The bincode crate was imported in b/330683225, so we can now enable unit tests for crates which uses it as a dev-dependency. This change includes a patch submitted upstream: https://github.com/rust-vmm/vmm-sys-util/pull/221. Bug: 333560480 Flag: TEST_ONLY Test: atest --host vmm-sys-util_test_src_lib Change-Id: Id2f7fdd8bee8a313ed09ff19028aa63e1cfde0e7 --- Android.bp | 24 +++++++++++++++++++++++- cargo_embargo.json | 3 ++- src/errno.rs | 12 ++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Android.bp b/Android.bp index b25068f..59aa814 100644 --- a/Android.bp +++ b/Android.bp @@ -1,5 +1,7 @@ // This file is generated by cargo_embargo. -// Do not modify this file as changes will be overridden on upgrade. +// Do not modify this file after the first "rust_*" or "genrule" module +// because the changes will be overridden on upgrade. +// Content before the first "rust_*" or "genrule" module is preserved. rust_library_host { name: "libvmm_sys_util", @@ -13,3 +15,23 @@ rust_library_host { "liblibc", ], } + +rust_test_host { + name: "vmm-sys-util_test_src_lib", + crate_name: "vmm_sys_util", + cargo_env_compat: true, + cargo_pkg_version: "0.12.1", + srcs: ["src/lib.rs"], + test_suites: ["general-tests"], + auto_gen_config: true, + test_options: { + unit_test: true, + }, + edition: "2021", + rustlibs: [ + "libbincode", + "libbitflags-1.3.2", + "liblibc", + "libserde_json", + ], +} diff --git a/cargo_embargo.json b/cargo_embargo.json index bc1c4ce..4ba71a8 100644 --- a/cargo_embargo.json +++ b/cargo_embargo.json @@ -7,5 +7,6 @@ "device_supported": false } }, - "run_cargo": false + "run_cargo": false, + "tests": true } diff --git a/src/errno.rs b/src/errno.rs index 27b9bcf..3478b7c 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -132,7 +132,7 @@ pub fn errno_result() -> Result { #[cfg(test)] mod tests { use super::*; - use std::env::temp_dir; + use crate::tempfile::TempFile; use std::error::Error as _; use std::fs::OpenOptions; use std::io::{self, Read}; @@ -145,14 +145,16 @@ mod tests { let expected_errno = libc::EIO; // try to read from a file without read permissions - let mut path = temp_dir(); - path.push("test"); + let temp_file = TempFile::new().unwrap(); + let path = temp_file.as_path().to_owned(); + // Drop temp_file so we can cleanly reuse path below. + std::mem::drop(temp_file); let mut file = OpenOptions::new() .read(false) .write(true) .create(true) .truncate(true) - .open(path) + .open(&path) .unwrap(); let mut buf: Vec = Vec::new(); assert!(file.read_to_end(&mut buf).is_err()); @@ -174,6 +176,8 @@ mod tests { let last_err: io::Error = last_err.into(); // Test creating a `std::io::Error` from an `Error` assert_eq!(io::Error::last_os_error().kind(), last_err.kind()); + + assert!(std::fs::remove_file(&path).is_ok()); } #[test] -- cgit v1.2.3