From 33cfa2c60e2e5b294462734b5e36c06be6100cfa Mon Sep 17 00:00:00 2001 From: angie Date: Sat, 24 Feb 2024 12:15:16 -0300 Subject: [PATCH] Setup tests Issue #47 --- Cargo.lock | 229 ++++++++++++++++++ slinky-cli/src/main.rs | 6 +- slinky/Cargo.toml | 3 + slinky/src/lib.rs | 50 ++++ slinky/src/linker_writer.rs | 11 + .../input_files/test_case.yaml | 0 .../linker_scripts/test_case.ld | 0 7 files changed, 296 insertions(+), 3 deletions(-) rename test_case.yaml => tests/input_files/test_case.yaml (100%) rename test_case.ld => tests/linker_scripts/test_case.ld (100%) diff --git a/Cargo.lock b/Cargo.lock index b947962..0414014 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,134 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-timer" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "hashbrown" version = "0.14.3" @@ -30,6 +152,24 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "proc-macro2" version = "1.0.78" @@ -48,12 +188,91 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + +[[package]] +name = "relative-path" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e898588f33fdd5b9420719948f9f2a32c922a246964576f71ba7f24f80610fbc" + +[[package]] +name = "rstest" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97eeab2f3c0a199bc4be135c36c924b6590b88c377d416494288c14f2db30199" +dependencies = [ + "futures", + "futures-timer", + "rstest_macros", + "rustc_version", +] + +[[package]] +name = "rstest_macros" +version = "0.18.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d428f8247852f894ee1be110b375111b586d4fa431f6c46e64ba5a0dcccbe605" +dependencies = [ + "cfg-if", + "glob", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn", + "unicode-ident", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "ryu" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +[[package]] +name = "semver" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" + [[package]] name = "serde" version = "1.0.197" @@ -87,10 +306,20 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "slinky" version = "0.1.0" dependencies = [ + "rstest", "serde", "serde_yaml", "thiserror", diff --git a/slinky-cli/src/main.rs b/slinky-cli/src/main.rs index bb5b5f6..077a853 100644 --- a/slinky-cli/src/main.rs +++ b/slinky-cli/src/main.rs @@ -7,8 +7,8 @@ use slinky::{Document, LinkerWriter}; fn main() { // TODO: don't use expect? - let document = - Document::read_file(Path::new("test_case.yaml")).expect("Error while parsing input file"); + let document = Document::read_file(Path::new("tests/input_files/test_case.yaml")) + .expect("Error while parsing input file"); // println!("settings {:#?}", document.settings); @@ -20,6 +20,6 @@ fn main() { writer.end_sections(); writer - .save_linker_script(Path::new("test_case.ld")) + .save_linker_script(Path::new("tests/linker_scripts/test_case.ld")) .expect("Error writing the linker script"); } diff --git a/slinky/Cargo.toml b/slinky/Cargo.toml index e5f745b..23b9736 100644 --- a/slinky/Cargo.toml +++ b/slinky/Cargo.toml @@ -12,3 +12,6 @@ edition = "2021" serde = {version="1.0.197", features = ["derive"]} serde_yaml = "0.9.32" thiserror = "1.0.57" + +[dev-dependencies] +rstest = "0.18.2" diff --git a/slinky/src/lib.rs b/slinky/src/lib.rs index 97e344b..0785f16 100644 --- a/slinky/src/lib.rs +++ b/slinky/src/lib.rs @@ -27,3 +27,53 @@ pub use segment::Segment; pub use document::Document; pub use linker_writer::LinkerWriter; + +#[cfg(test)] +mod tests { + use std::fs; + use std::path::{Path, PathBuf}; + + use rstest::rstest; + + use crate::Document; + use crate::LinkerWriter; + + // Is there a better way to do this? + // Converts "tests/input_files/test_case.yaml" to "tests/`folder_name`/test_case.`extension`" + fn get_target_path(original: &Path, folder_name: &str, extension: &str) -> PathBuf { + let ancestors: Vec<_> = original.ancestors().collect(); + let mut test_path = ancestors[2].to_path_buf(); + + let extension_temp = original.with_extension(extension); + let ld_name = extension_temp + .file_name() + .unwrap() + .to_os_string() + .into_string() + .unwrap(); + + test_path.extend(Path::new(folder_name)); + test_path.extend(Path::new(&ld_name)); + + test_path + } + + #[rstest] + fn test_simple_linker_script_generation(#[files("../tests/input_files/*.yaml")] path: PathBuf) { + let ld_path = get_target_path(&path, "linker_scripts", "ld"); + + let document = Document::read_file(&path).expect("unable to read original file"); + + let mut writer = LinkerWriter::new(&document.settings); + writer.begin_sections(); + for segment in &document.segments { + writer.add_segment(segment); + } + writer.end_sections(); + + let expected_ld_contents = + fs::read_to_string(ld_path).expect("unable to read expected ld file"); + + assert_eq!(expected_ld_contents, writer.export_as_string()); + } +} diff --git a/slinky/src/linker_writer.rs b/slinky/src/linker_writer.rs index dc3c8da..1e54bd9 100644 --- a/slinky/src/linker_writer.rs +++ b/slinky/src/linker_writer.rs @@ -124,6 +124,17 @@ impl<'a> LinkerWriter<'a> { Ok(()) } + + #[must_use] + pub fn export_as_string(&self) -> String { + let mut ret = String::new(); + + for line in &self.buffer { + ret += &format!("{}\n", line); + } + + ret + } } // internal functions diff --git a/test_case.yaml b/tests/input_files/test_case.yaml similarity index 100% rename from test_case.yaml rename to tests/input_files/test_case.yaml diff --git a/test_case.ld b/tests/linker_scripts/test_case.ld similarity index 100% rename from test_case.ld rename to tests/linker_scripts/test_case.ld