Skip to content

Commit

Permalink
Fix thing I forgor in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Aug 11, 2024
1 parent 7b63389 commit 34c44dc
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 42 deletions.
4 changes: 2 additions & 2 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() {

writer.add_all_segments(&document.segments).expect("");
writer
.add_all_undefined_syms(&document.symbol_assignments)
.add_all_symbol_assignments(&document.symbol_assignments)
.expect("???");

let output_path = cli
Expand All @@ -98,7 +98,7 @@ fn main() {

writer.add_all_segments(&document.segments).expect("ah?");
writer
.add_all_undefined_syms(&document.symbol_assignments)
.add_all_symbol_assignments(&document.symbol_assignments)
.expect("???");

if let Some(output_path) = cli.output {
Expand Down
16 changes: 8 additions & 8 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,21 @@ impl<'a> LinkerWriter<'a> {
Ok(())
}

pub fn add_all_undefined_syms(
pub fn add_all_symbol_assignments(
&mut self,
undefined_syms: &[SymbolAssignment],
symbol_assignments: &[SymbolAssignment],
) -> Result<(), SlinkyError> {
if undefined_syms.is_empty() {
if symbol_assignments.is_empty() {
return Ok(());
}

self.begin_undefined_syms()?;
self.begin_symbol_assignments()?;

for undefined_sym in undefined_syms {
for undefined_sym in symbol_assignments {
self.add_undefined_sym(undefined_sym)?;
}

self.end_undefined_syms()?;
self.end_symbol_assignments()?;

Ok(())
}
Expand Down Expand Up @@ -594,15 +594,15 @@ impl LinkerWriter<'_> {
Ok(())
}

pub(crate) fn begin_undefined_syms(&mut self) -> Result<(), SlinkyError> {
pub(crate) fn begin_symbol_assignments(&mut self) -> Result<(), SlinkyError> {
if !self.buffer.is_empty() {
self.buffer.write_empty_line();
}

Ok(())
}

pub(crate) fn end_undefined_syms(&mut self) -> Result<(), SlinkyError> {
pub(crate) fn end_symbol_assignments(&mut self) -> Result<(), SlinkyError> {
Ok(())
}

Expand Down
7 changes: 4 additions & 3 deletions slinky/src/partial_linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ impl<'a> PartialLinkerWriter<'a> {
Ok(())
}

pub fn add_all_undefined_syms(
pub fn add_all_symbol_assignments(
&mut self,
undefined_syms: &[SymbolAssignment],
symbol_assignments: &[SymbolAssignment],
) -> Result<(), SlinkyError> {
self.main_writer.add_all_undefined_syms(undefined_syms)
self.main_writer
.add_all_symbol_assignments(symbol_assignments)
}

pub fn export_linker_script_to_files(&self, path: &Path) -> Result<(), SlinkyError> {
Expand Down
114 changes: 85 additions & 29 deletions slinky/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,50 @@ use std::path::{Path, PathBuf};
use rstest::rstest;
use slinky::{RuntimeSettings, SlinkyError};

fn compare_multiline_strings(left: &str, right: &str) {
if left == right {
return;
}

// both strings are not the same, try to figure out where the issue is.
println!("Not equal strings :c");
println!();

let mut left_splitted = left.split("\n");
let mut right_splitted = right.split("\n");

// https://stackoverflow.com/a/38168890/6292472
loop {
match (left_splitted.next(), right_splitted.next()) {
(Some(l), Some(r)) => {
if l != r {
println!(" Different lines:");
println!(" left: {}", l);
println!(" right: {}", r);
println!();
}
}
(Some(l), None) => {
println!(" Only one line:");
println!(" left: {}", l);
println!();
}
(None, Some(r)) => {
println!(" Only one line:");
println!(" right: {}", r);
println!();
}
(None, None) => break,
}
}

println!();
println!("full inequality:");
println!();

assert_eq!(left, right);
}

fn create_runtime_settings() -> RuntimeSettings {
let mut rs = RuntimeSettings::new();

Expand All @@ -27,13 +71,14 @@ fn check_ld_generation(yaml_path: &Path, ld_path: &Path) -> Result<(), SlinkyErr

let mut writer = slinky::LinkerWriter::new(&document, &rs);
writer.add_all_segments(&document.segments)?;
writer.add_all_symbol_assignments(&document.symbol_assignments)?;

let expected_ld_contents =
fs::read_to_string(ld_path).expect("unable to read expected ld file");

assert_eq!(
expected_ld_contents,
writer.export_linker_script_to_string().unwrap()
compare_multiline_strings(
&expected_ld_contents,
&writer.export_linker_script_to_string().unwrap(),
);

Ok(())
Expand All @@ -45,15 +90,16 @@ fn check_d_generation(yaml_path: &Path, ld_path: &Path) -> Result<(), SlinkyErro

let mut writer = slinky::LinkerWriter::new(&document, &rs);
writer.add_all_segments(&document.segments)?;
writer.add_all_symbol_assignments(&document.symbol_assignments)?;

let expected_d_contents = fs::read_to_string(ld_path).expect("unable to read expected d file");

let target_path = document.settings.target_path.as_ref().unwrap();
assert_eq!(
expected_d_contents,
writer
compare_multiline_strings(
&expected_d_contents,
&writer
.export_dependencies_file_to_string(target_path)
.unwrap()
.unwrap(),
);

Ok(())
Expand All @@ -65,12 +111,13 @@ fn check_symbols_header_generation(yaml_path: &Path, ld_path: &Path) -> Result<(

let mut writer = slinky::LinkerWriter::new(&document, &rs);
writer.add_all_segments(&document.segments)?;
writer.add_all_symbol_assignments(&document.symbol_assignments)?;

let expected_h_contents = fs::read_to_string(ld_path).expect("unable to read expected h file");

assert_eq!(
expected_h_contents,
writer.export_symbol_header_to_string().unwrap()
compare_multiline_strings(
&expected_h_contents,
&writer.export_symbol_header_to_string().unwrap(),
);

Ok(())
Expand Down Expand Up @@ -114,16 +161,19 @@ fn test_partial_linking_script_generation(

let mut writer = slinky::PartialLinkerWriter::new(&document, &rs);
writer.add_all_segments(&document.segments).expect("");
writer
.add_all_symbol_assignments(&document.symbol_assignments)
.expect("");

let expected_ld_contents =
fs::read_to_string(ld_path).expect("unable to read expected ld file");

assert_eq!(
expected_ld_contents,
writer
compare_multiline_strings(
&expected_ld_contents,
&writer
.get_main_writer()
.export_linker_script_to_string()
.unwrap()
.unwrap(),
);

for (partial, name) in writer.get_partial_writers() {
Expand All @@ -139,9 +189,9 @@ fn test_partial_linking_script_generation(
let expected_partial_ld_contents =
fs::read_to_string(p).expect("unable to read expected ld file");

assert_eq!(
expected_partial_ld_contents,
partial.export_linker_script_to_string().unwrap()
compare_multiline_strings(
&expected_partial_ld_contents,
&partial.export_linker_script_to_string().unwrap(),
);
}
}
Expand All @@ -155,16 +205,19 @@ fn test_partial_linking_d_generation(#[files("../tests/partial_linking/*.d")] d_

let mut writer = slinky::PartialLinkerWriter::new(&document, &rs);
writer.add_all_segments(&document.segments).expect("");
writer
.add_all_symbol_assignments(&document.symbol_assignments)
.expect("");

let expected_d_contents = fs::read_to_string(d_path).expect("unable to read expected d file");

let target_path = document.settings.target_path.as_ref().unwrap();
assert_eq!(
expected_d_contents,
writer
compare_multiline_strings(
&expected_d_contents,
&writer
.get_main_writer()
.export_dependencies_file_to_string(target_path)
.unwrap()
.unwrap(),
);

for (partial, name) in writer.get_partial_writers() {
Expand All @@ -190,11 +243,11 @@ fn test_partial_linking_d_generation(#[files("../tests/partial_linking/*.d")] d_
.expect("Failed to escape path"),
);
partial_target.push(&format!("{}.o", name));
assert_eq!(
expected_partial_ld_contents,
partial
compare_multiline_strings(
&expected_partial_ld_contents,
&partial
.export_dependencies_file_to_string(&partial_target)
.unwrap()
.unwrap(),
);
}
}
Expand All @@ -209,14 +262,17 @@ fn test_partial_linking_symbols_header_generation(

let mut writer = slinky::PartialLinkerWriter::new(&document, &rs);
writer.add_all_segments(&document.segments).expect("");
writer
.add_all_symbol_assignments(&document.symbol_assignments)
.expect("");

let expected_h_contents = fs::read_to_string(h_path).expect("unable to read expected h file");

assert_eq!(
expected_h_contents,
writer
compare_multiline_strings(
&expected_h_contents,
&writer
.get_main_writer()
.export_symbol_header_to_string()
.unwrap()
.unwrap(),
);
}

0 comments on commit 34c44dc

Please sign in to comment.