Skip to content

Commit

Permalink
Allow passing arguments to cli and bash script to regenerate tests
Browse files Browse the repository at this point in the history
Issue #12
Issue #47
  • Loading branch information
AngheloAlf committed Feb 24, 2024
1 parent 66eb40b commit c6b0a6c
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 18 deletions.
179 changes: 179 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions slinky-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
slinky = { path = "../slinky", version = "0.1.0" }
34 changes: 26 additions & 8 deletions slinky-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::path::Path;
use std::path::PathBuf;

use slinky::{Document, LinkerWriter};
use clap::Parser;

// TODO: Add program description to cli

#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Input file
input: PathBuf,

/// Output file. Print to stdout if missing
#[arg(short, long)]
output: Option<PathBuf>,
}

fn main() {
let cli = Cli::parse();

// TODO: don't use expect?
let document = Document::read_file(Path::new("tests/input_files/test_case.yaml"))
.expect("Error while parsing input file");
let document = slinky::Document::read_file(&cli.input).expect("Error while parsing input file");

// println!("settings {:#?}", document.settings);

let mut writer = LinkerWriter::new(&document.settings);
let mut writer = slinky::LinkerWriter::new(&document.settings);
writer.begin_sections();
for segment in &document.segments {
writer.add_segment(segment);
}
writer.end_sections();

writer
.save_linker_script(Path::new("tests/linker_scripts/test_case.ld"))
.expect("Error writing the linker script");
if let Some(output_path) = cli.output {
writer
.save_linker_script(&output_path)
.expect("Error writing the linker script");
} else {
println!("{}", writer.export_as_string());
}
}
1 change: 1 addition & 0 deletions slinky/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Document {
Ok(f) => f,
Err(e) => {
return Err(SlinkyError::FailedFileOpen {
path: path.to_path_buf(),
description: e.to_string(),
})
}
Expand Down
19 changes: 17 additions & 2 deletions slinky/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use std::path::PathBuf;

#[derive(Clone, Debug, PartialEq, Eq, Hash, thiserror::Error)]
pub enum SlinkyError {
#[error("Unable to open file: {description}")]
FailedFileOpen { description: String },
#[error("Unable to open file '{path}', because '{description}'")]
FailedFileOpen { path: PathBuf, description: String },

#[error("Fail while writing to file '{path}', because '{description}'.\n Contents were: '{contents}'")]
FailedFileWrite {
path: PathBuf,
description: String,
contents: String,
},

#[error("Unable to create dir '{path}', because '{description}")]
FailedDirCreate { path: PathBuf, description: String },

#[error("Unable parse yaml: {description}")]
FailedYamlParsing { description: String },

#[error("Non-nullable attribute '{name}' was null")]
NullValueOnNonNull { name: String },

#[error("The attribute '{name}' should not be empty")]
EmptyValue { name: String },
}
37 changes: 31 additions & 6 deletions slinky/src/linker_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::{
path::Path,
};

use crate::file_kind::FileKind;
use crate::segment::Segment;
use crate::settings::Settings;
use crate::{file_kind::FileKind, SlinkyError};

pub struct LinkerWriter<'a> {
pub linker_symbols: HashSet<String>,
Expand Down Expand Up @@ -110,16 +110,41 @@ impl<'a> LinkerWriter<'a> {
self.writeln("");
}

pub fn save_linker_script(&self, path: &Path) -> Result<(), std::io::Error> {
pub fn save_linker_script(&self, path: &Path) -> Result<(), SlinkyError> {
match path.parent() {
None => {}
Some(parent) => fs::create_dir_all(parent)?,
None => (),
Some(parent) => match fs::create_dir_all(parent) {
Ok(_) => (),
Err(e) => {
return Err(SlinkyError::FailedDirCreate {
path: parent.to_path_buf(),
description: e.to_string(),
})
}
},
}

let mut f = File::create(path)?;
let mut f = match File::create(path) {
Ok(f) => f,
Err(e) => {
return Err(SlinkyError::FailedFileOpen {
path: path.to_path_buf(),
description: e.to_string(),
})
}
};

for line in &self.buffer {
writeln!(f, "{}", line)?;
match writeln!(f, "{}", line) {
Ok(_) => (),
Err(e) => {
return Err(SlinkyError::FailedFileWrite {
path: path.to_path_buf(),
description: e.to_string(),
contents: line.into(),
})
}
}
}

Ok(())
Expand Down
2 changes: 0 additions & 2 deletions slinky/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* SPDX-FileCopyrightText: © 2024 decompals */
/* SPDX-License-Identifier: MIT */

use slinky;

use std::fs;
use std::path::{Path, PathBuf};

Expand Down
20 changes: 20 additions & 0 deletions tests/regen_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# SPDX-FileCopyrightText: © 2024 decompals
# SPDX-License-Identifier: MIT

set -e
set -o pipefail

# This file should be run from the root of the repo

for filepath in tests/input_files/*.yaml; do
filename=$(basename -- "$filepath")
stem="${filename%.*}"
output=tests/linker_scripts/$stem.ld
echo Generating $output
cargo run -- $filepath -o $output
#for ((i=0; i<=3; i++)); do
# ./MyProgram.exe "$filename" "Logs/$(basename "$filename" .txt)_Log$i.txt"
#done
done

0 comments on commit c6b0a6c

Please sign in to comment.