pub mod config;
pub mod template;
use std::path::{Path, PathBuf};
pub use config::{
Architecture, BuildSection, ConfigError, ProjectConfig, ProjectSection, RunSection, Syntax,
};
#[derive(Debug, thiserror::Error)]
pub enum ProjectError {
#[error("cannot access {path}: {source}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("{path}: {source}")]
Config {
path: PathBuf,
#[source]
source: ConfigError,
},
#[error("no {} found in {start} or any parent directory", Project::FILE_NAME)]
NotFound {
start: PathBuf,
},
#[error("{path} already exists")]
AlreadyExists {
path: PathBuf,
},
#[error("{path} is outside {root}")]
OutsideRoot {
path: PathBuf,
root: PathBuf,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Project {
root: PathBuf,
config: ProjectConfig,
config_path: Option<PathBuf>,
}
impl Project {
pub const FILE_NAME: &'static str = ".ratasm.toml";
pub fn config(&self) -> &ProjectConfig {
&self.config
}
pub fn config_mut(&mut self) -> &mut ProjectConfig {
&mut self.config
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn config_path(&self) -> Option<&Path> {
self.config_path.as_deref()
}
pub fn is_explicit(&self) -> bool {
self.config_path.is_some()
}
pub fn name(&self) -> &str {
&self.config.project.name
}
pub fn discover(start: &Path) -> Result<Self, ProjectError> {
let mut directory = if start.is_dir() {
start.to_path_buf()
} else {
start.parent().unwrap_or(Path::new(".")).to_path_buf()
};
loop {
let candidate = directory.join(Self::FILE_NAME);
if candidate.is_file() {
return Self::load(&candidate);
}
if !directory.pop() {
return Err(ProjectError::NotFound {
start: start.to_path_buf(),
});
}
}
}
pub fn load(path: &Path) -> Result<Self, ProjectError> {
let text = std::fs::read_to_string(path).map_err(|source| ProjectError::Io {
path: path.to_path_buf(),
source,
})?;
let config = ProjectConfig::from_toml(&text).map_err(|source| ProjectError::Config {
path: path.to_path_buf(),
source,
})?;
config.validate().map_err(|source| ProjectError::Config {
path: path.to_path_buf(),
source,
})?;
let root = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or(Path::new("."))
.to_path_buf();
Ok(Self {
root,
config,
config_path: Some(path.to_path_buf()),
})
}
pub fn for_file(source: &Path) -> Self {
let root = source
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or(Path::new("."))
.to_path_buf();
let file_name = source
.file_name()
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("main.asm"));
let name = source
.file_stem()
.map(|stem| stem.to_string_lossy().into_owned())
.unwrap_or_else(|| "program".to_owned());
let mut config = ProjectConfig::default();
config.project.name = name;
config.project.entry = file_name;
Self {
root,
config,
config_path: None,
}
}
pub fn for_file_or_discover(source: &Path) -> Self {
Self::discover(source).unwrap_or_else(|_| Self::for_file(source))
}
pub fn open(path: &Path) -> Result<Self, ProjectError> {
match Self::discover(path) {
Err(ProjectError::NotFound { .. }) => {}
found => return found,
}
let directory = if path.is_dir() {
path.to_path_buf()
} else {
path.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or(Path::new("."))
.to_path_buf()
};
let directory = directory
.canonicalize()
.map_err(|source| ProjectError::Io {
path: directory.clone(),
source,
})?;
let root = directory
.ancestors()
.find(|candidate| {
candidate.join(".git").exists()
|| PROJECT_NAMES
.iter()
.any(|name| candidate.join(name).is_file())
})
.unwrap_or(&directory)
.to_path_buf();
let mut config = ProjectConfig::default();
config.project.name = root
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| "project".to_owned());
let mut project = Self {
root,
config,
config_path: None,
};
project.rescan();
Ok(project)
}
pub fn rescan(&mut self) {
if self.config_path.is_some() {
return;
}
let sources: Vec<PathBuf> = assembly_files_under(&self.root, &self.output_directory())
.into_iter()
.filter_map(|path| path.strip_prefix(&self.root).ok().map(Path::to_path_buf))
.collect();
let read =
|source: &PathBuf| std::fs::read_to_string(self.root.join(source)).unwrap_or_default();
let defines = |source: &PathBuf, symbol: &str| {
read(source).lines().any(|line| {
let mut words = line
.split(';')
.next()
.unwrap_or_default()
.split_whitespace();
words
.next()
.is_some_and(|word| word.eq_ignore_ascii_case("global"))
&& words.any(|word| word.trim_end_matches(',') == symbol)
})
};
let start = sources.iter().find(|source| defines(source, "_start"));
let main = sources.iter().find(|source| defines(source, "main"));
let entry = start
.or(main)
.or_else(|| sources.iter().find(|source| source.ends_with("main.asm")))
.or_else(|| sources.first())
.cloned()
.unwrap_or_else(|| PathBuf::from("main.asm"));
let build = &mut self.config.build;
if start.is_none() && main.is_some() {
build.linker = "gcc".to_owned();
build.linker_args = vec!["-no-pie".to_owned()];
} else {
build.linker = "ld".to_owned();
build.linker_args.clear();
}
self.config.project.sources = sources
.into_iter()
.filter(|source| *source != entry)
.collect();
self.config.project.entry = entry;
}
pub fn is_empty(&self) -> bool {
!self.root.join(&self.config.project.entry).is_file()
}
pub fn create(root: &Path, name: &str) -> Result<Self, ProjectError> {
Self::create_from(root, name, false)
}
pub fn create_with_libc(root: &Path, name: &str) -> Result<Self, ProjectError> {
Self::create_from(root, name, true)
}
pub fn create_with_make(root: &Path, name: &str) -> Result<Self, ProjectError> {
let makefile = root.join("Makefile");
if makefile.exists() {
return Err(ProjectError::AlreadyExists { path: makefile });
}
let name = match name.trim() {
"" => "program",
name => Path::new(name)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("program"),
};
write_new(&root.join("src").join("main.asm"), template::HELLO_WORLD)?;
write_new(&makefile, &template::makefile(name))?;
write_new(&root.join(".gitignore"), template::GITIGNORE)?;
Self::open(root)
}
fn create_from(root: &Path, name: &str, libc: bool) -> Result<Self, ProjectError> {
let config_path = root.join(Self::FILE_NAME);
if config_path.exists() {
return Err(ProjectError::AlreadyExists { path: config_path });
}
let name = if name.trim().is_empty() {
"ratasm-project"
} else {
name.trim()
};
let source_directory = root.join("src");
let (source, config) = if libc {
(template::LIBC_HELLO, template::libc_project_file(name))
} else {
(template::HELLO_WORLD, template::project_file(name))
};
write_new(&source_directory.join("main.asm"), source)?;
write_new(&config_path, &config)?;
write_new(&root.join(".gitignore"), template::GITIGNORE)?;
Self::load(&config_path)
}
pub fn save(&self) -> Result<PathBuf, ProjectError> {
let path = self
.config_path
.clone()
.unwrap_or_else(|| self.root.join(Self::FILE_NAME));
let text = self
.config
.to_toml()
.map_err(|source| ProjectError::Config {
path: path.clone(),
source,
})?;
std::fs::write(&path, text).map_err(|source| ProjectError::Io {
path: path.clone(),
source,
})?;
Ok(path)
}
pub fn resolve(&self, path: &Path) -> PathBuf {
if path.is_absolute() {
path.to_path_buf()
} else {
self.root.join(path)
}
}
pub fn entry_path(&self) -> PathBuf {
self.resolve(&self.config.project.entry)
}
pub fn for_tool(&self, path: &Path) -> PathBuf {
let root = absolute(&self.root);
let target = absolute(path);
target
.strip_prefix(&root)
.map(Path::to_path_buf)
.unwrap_or(target)
}
pub fn absolute_path(&self, path: &Path) -> PathBuf {
absolute(path)
}
pub fn relative(&self, path: &Path) -> Option<PathBuf> {
if path.is_relative() {
return Some(
path.components()
.filter(|part| !matches!(part, std::path::Component::CurDir))
.collect(),
);
}
let root = self
.root
.canonicalize()
.unwrap_or_else(|_| self.root.clone());
let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
path.strip_prefix(&root).ok().map(Path::to_path_buf)
}
pub fn builds(&self, path: &Path) -> bool {
let Some(relative) = self.relative(path) else {
return false;
};
self.config.all_sources().contains(&relative)
}
pub fn add_source(&mut self, path: &Path) -> Result<bool, ProjectError> {
let relative = self
.relative(path)
.ok_or_else(|| ProjectError::OutsideRoot {
path: path.to_path_buf(),
root: self.root.clone(),
})?;
if self.config.all_sources().contains(&relative) {
return Ok(false);
}
self.config.project.sources.push(relative);
Ok(true)
}
pub fn rename_source(&mut self, from: &Path, to: &Path) -> Result<bool, ProjectError> {
let (Some(from), Some(to)) = (self.relative(from), self.relative_target(to)) else {
return Err(ProjectError::OutsideRoot {
path: to.to_path_buf(),
root: self.root.clone(),
});
};
let project = &mut self.config.project;
let mut changed = false;
if project.entry == from {
project.entry.clone_from(&to);
changed = true;
}
for source in &mut project.sources {
if *source == from {
source.clone_from(&to);
changed = true;
}
}
Ok(changed)
}
pub fn remove_source(&mut self, path: &Path) -> bool {
let Some(relative) = self.relative(path) else {
return false;
};
let before = self.config.project.sources.len();
self.config
.project
.sources
.retain(|source| *source != relative);
before != self.config.project.sources.len()
}
fn relative_target(&self, path: &Path) -> Option<PathBuf> {
let parent = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())?;
let name = path.file_name()?;
self.relative(parent).map(|parent| parent.join(name))
}
pub fn source_paths(&self) -> Vec<PathBuf> {
self.config
.all_sources()
.iter()
.map(|source| self.resolve(source))
.collect()
}
pub fn output_directory(&self) -> PathBuf {
self.resolve(&self.config.build.output_directory)
}
pub fn object_path(&self, source: &Path) -> PathBuf {
let stem = source
.file_stem()
.map(|stem| stem.to_string_lossy().into_owned())
.unwrap_or_else(|| "output".to_owned());
let parent = source
.parent()
.and_then(Path::file_name)
.map(|name| name.to_string_lossy().into_owned());
let file_name = match parent {
Some(parent) if !parent.is_empty() && parent != "." => format!("{parent}_{stem}.o"),
_ => format!("{stem}.o"),
};
self.output_directory().join(file_name)
}
pub fn executable_path(&self) -> PathBuf {
match &self.config.build.executable {
Some(path) => self.resolve(path),
None => {
let stem = self
.config
.project
.entry
.file_stem()
.map(|stem| stem.to_string_lossy().into_owned())
.unwrap_or_else(|| self.config.project.name.clone());
self.output_directory().join(stem)
}
}
}
pub fn run_directory(&self) -> PathBuf {
match &self.config.run.working_directory {
Some(path) => self.resolve(path),
None => self.root.clone(),
}
}
}
fn write_new(path: &Path, contents: &str) -> Result<(), ProjectError> {
if path.exists() {
return Err(ProjectError::AlreadyExists {
path: path.to_path_buf(),
});
}
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|source| ProjectError::Io {
path: parent.to_path_buf(),
source,
})?;
}
std::fs::write(path, contents).map_err(|source| ProjectError::Io {
path: path.to_path_buf(),
source,
})
}
const PROJECT_EXTENSIONS: [&str; 15] = [
"asm", "inc", "s", "S", "nasm", "mac", "c", "h", "ld", "lds", "mk", "md", "txt", "toml", "gdb",
];
const PROJECT_NAMES: [&str; 3] = ["Makefile", "makefile", "GNUmakefile"];
const SKIPPED_DIRECTORIES: [&str; 3] = ["target", "node_modules", "__pycache__"];
pub fn project_files_under(root: &Path, skip: &Path) -> Vec<PathBuf> {
walk_files(root, skip, &|path| {
let name = path
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
PROJECT_NAMES.contains(&name)
|| path
.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| PROJECT_EXTENSIONS.contains(&extension))
})
}
pub fn assembly_files_under(root: &Path, skip: &Path) -> Vec<PathBuf> {
walk_files(root, skip, &|path| is_assembly(path))
}
pub fn is_assembly(path: &Path) -> bool {
matches!(
path.extension().and_then(|extension| extension.to_str()),
Some("asm" | "s" | "nasm")
)
}
fn walk_files(root: &Path, skip: &Path, keep: &dyn Fn(&Path) -> bool) -> Vec<PathBuf> {
fn walk(
directory: &Path,
skip: &Path,
depth: usize,
keep: &dyn Fn(&Path) -> bool,
found: &mut Vec<PathBuf>,
) {
let Ok(entries) = std::fs::read_dir(directory) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
let name = entry.file_name();
let name = name.to_string_lossy();
if name.starts_with('.') || same_place(&path, skip) {
continue;
}
if path.is_dir() {
if depth > 0 && !SKIPPED_DIRECTORIES.contains(&name.as_ref()) {
walk(&path, skip, depth - 1, keep, found);
}
} else if keep(&path) {
found.push(path);
}
}
}
let mut found = Vec::new();
walk(root, skip, 8, keep, &mut found);
found.sort();
found
}
fn same_place(a: &Path, b: &Path) -> bool {
a == b || matches!((a.canonicalize(), b.canonicalize()), (Ok(a), Ok(b)) if a == b)
}
fn absolute(path: &Path) -> PathBuf {
if path.is_absolute() {
return path.to_path_buf();
}
std::env::current_dir()
.map(|cwd| cwd.join(path))
.unwrap_or_else(|_| path.to_path_buf())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_folder_without_a_project_file_is_a_project() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::create_dir_all(dir.path().join("src/lib")).expect("mkdir");
std::fs::write(
dir.path().join("src/lib/util.asm"),
"global helper\nhelper: ret\n",
)
.expect("write");
std::fs::write(
dir.path().join("src/start.asm"),
" GLOBAL _start ; entry\n_start:\n",
)
.expect("write");
std::fs::write(dir.path().join("src/macros.inc"), "").expect("write");
std::fs::create_dir_all(dir.path().join("build")).expect("mkdir");
std::fs::write(dir.path().join("build/old.asm"), "").expect("write");
std::fs::create_dir(dir.path().join(".git")).expect("mkdir");
let mut project = Project::open(&dir.path().join("src/lib/util.asm")).expect("open");
assert!(!project.is_explicit());
assert_eq!(project.config().project.entry, Path::new("src/start.asm"));
assert_eq!(
project.config().project.sources,
[Path::new("src/lib/util.asm")]
);
assert_eq!(project.config().build.linker, "ld");
std::fs::write(dir.path().join("src/extra.asm"), "nop\n").expect("write");
project.rescan();
assert_eq!(project.config().project.sources.len(), 2);
let files = project_files_under(project.root(), &project.output_directory());
assert!(files.iter().any(|file| file.ends_with("macros.inc")));
assert!(!files.iter().any(|file| file.ends_with("old.asm")));
}
#[test]
fn a_folder_with_only_main_links_with_gcc() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::write(dir.path().join("prog.asm"), "global main\nmain: ret\n").expect("write");
let project = Project::open(dir.path()).expect("open");
assert_eq!(project.config().project.entry, Path::new("prog.asm"));
assert_eq!(project.config().build.linker, "gcc");
}
#[test]
fn a_project_file_above_wins() {
let dir = tempfile::tempdir().expect("temp dir");
Project::create(dir.path(), "demo").expect("create");
let project = Project::open(&dir.path().join("src")).expect("open");
assert!(project.is_explicit());
}
#[test]
fn a_dot_prefixed_source_is_still_built() {
let dir = tempfile::tempdir().expect("temp dir");
let project = Project::create(dir.path(), "demo").expect("create");
assert!(project.builds(Path::new("./src/main.asm")));
assert!(project.builds(Path::new("src/main.asm")));
}
#[test]
fn renaming_a_source_follows_it_in_the_configuration() {
let dir = tempfile::tempdir().expect("temp dir");
let mut project = Project::create(dir.path(), "demo").expect("create");
let entry = project.entry_path();
let moved = entry.with_file_name("start.asm");
assert!(project.rename_source(&entry, &moved).expect("inside"));
assert_eq!(project.config().project.entry, Path::new("src/start.asm"));
let extra = dir.path().join("src/util.asm");
std::fs::write(&extra, "").expect("write");
project.add_source(&extra).expect("add");
assert!(project.remove_source(&extra));
assert!(!project.remove_source(&moved), "the entry stays");
}
fn temp_dir() -> tempfile::TempDir {
tempfile::tempdir().expect("temp dir")
}
#[test]
fn creating_a_project_writes_a_working_hello_world() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
assert_eq!(project.name(), "hello");
assert!(project.entry_path().is_file(), "entry source must exist");
assert!(dir.path().join(".ratasm.toml").is_file());
assert!(dir.path().join(".gitignore").is_file());
let source = std::fs::read_to_string(project.entry_path()).expect("read");
assert!(source.contains("global _start"));
}
#[test]
fn creating_over_an_existing_project_is_refused() {
let dir = temp_dir();
Project::create(dir.path(), "first").expect("create");
let error = Project::create(dir.path(), "second").expect_err("must refuse");
assert!(matches!(error, ProjectError::AlreadyExists { .. }));
let project = Project::discover(dir.path()).expect("discover");
assert_eq!(project.name(), "first", "the original must survive");
}
#[test]
fn creating_refuses_to_overwrite_an_existing_source_file() {
let dir = temp_dir();
std::fs::create_dir_all(dir.path().join("src")).expect("mkdir");
std::fs::write(dir.path().join("src/main.asm"), "; my work\n").expect("write");
Project::create(dir.path(), "hello").expect_err("must refuse");
assert_eq!(
std::fs::read_to_string(dir.path().join("src/main.asm")).expect("read"),
"; my work\n"
);
}
#[test]
fn discovery_walks_up_to_the_project_root() {
let dir = temp_dir();
Project::create(dir.path(), "hello").expect("create");
let deep = dir.path().join("src/nested/deeper");
std::fs::create_dir_all(&deep).expect("mkdir");
let project = Project::discover(&deep).expect("discover");
assert_eq!(project.name(), "hello");
assert_eq!(
std::fs::canonicalize(project.root()).expect("canonicalize"),
std::fs::canonicalize(dir.path()).expect("canonicalize")
);
}
#[test]
fn discovery_starting_from_a_file_uses_its_directory() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
let found = Project::discover(&project.entry_path()).expect("discover");
assert_eq!(found.name(), "hello");
}
#[test]
fn discovery_reports_when_there_is_no_project() {
let dir = temp_dir();
let error = Project::discover(dir.path()).expect_err("must fail");
assert!(matches!(error, ProjectError::NotFound { .. }));
}
#[test]
fn an_invalid_project_file_is_reported_with_its_path() {
let dir = temp_dir();
let path = dir.path().join(Project::FILE_NAME);
std::fs::write(&path, "[project]\narchitecture = \"aarch64\"\n").expect("write");
let error = Project::discover(dir.path()).expect_err("must fail");
assert!(matches!(error, ProjectError::Config { .. }));
assert!(error.to_string().contains("not supported yet"));
}
#[test]
fn a_lone_source_file_gets_an_implicit_project() {
let dir = temp_dir();
let source = dir.path().join("scratch.asm");
std::fs::write(&source, "ret\n").expect("write");
let project = Project::for_file(&source);
assert!(!project.is_explicit());
assert_eq!(project.name(), "scratch");
assert_eq!(project.entry_path(), source);
assert_eq!(project.config().build.assembler, "nasm");
}
#[test]
fn opening_a_file_prefers_a_real_project_over_an_implicit_one() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
let found = Project::for_file_or_discover(&project.entry_path());
assert!(found.is_explicit());
assert_eq!(found.name(), "hello");
}
#[test]
fn opening_a_file_outside_any_project_falls_back_cleanly() {
let dir = temp_dir();
let source = dir.path().join("lone.asm");
std::fs::write(&source, "ret\n").expect("write");
let project = Project::for_file_or_discover(&source);
assert!(!project.is_explicit());
assert_eq!(project.name(), "lone");
}
#[test]
fn paths_resolve_against_the_project_root() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
assert_eq!(project.entry_path(), dir.path().join("src/main.asm"));
assert_eq!(project.output_directory(), dir.path().join("build"));
assert_eq!(project.executable_path(), dir.path().join("build/main"));
assert_eq!(project.run_directory(), dir.path());
}
#[test]
fn an_explicit_executable_path_overrides_the_default() {
let dir = temp_dir();
let path = dir.path().join(Project::FILE_NAME);
std::fs::write(&path, "[build]\nexecutable = \"bin/program\"\n").expect("write");
let project = Project::load(&path).expect("load");
assert_eq!(project.executable_path(), dir.path().join("bin/program"));
}
#[test]
fn object_paths_land_in_the_output_directory() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
let object = project.object_path(Path::new("src/main.asm"));
assert_eq!(object, dir.path().join("build/src_main.o"));
}
#[test]
fn sources_in_different_directories_do_not_collide() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
let first = project.object_path(Path::new("boot/main.asm"));
let second = project.object_path(Path::new("kernel/main.asm"));
assert_ne!(first, second);
}
#[test]
fn a_new_source_joins_the_ones_the_build_assembles() {
let dir = tempfile::tempdir().expect("temp dir");
let mut project = Project::create(dir.path(), "adds").expect("create");
let extra = dir.path().join("src/util.asm");
std::fs::write(&extra, "ret\n").expect("write");
assert!(!project.builds(&extra));
assert!(project.add_source(&extra).expect("add"));
assert!(project.builds(&extra));
assert!(project
.source_paths()
.iter()
.any(|path| path.ends_with("util.asm")));
assert!(!project.add_source(&extra).expect("add"), "no duplicate");
}
#[test]
fn the_entry_file_is_already_built() {
let dir = tempfile::tempdir().expect("temp dir");
let mut project = Project::create(dir.path(), "entry").expect("create");
let entry = project.entry_path();
assert!(project.builds(&entry));
assert!(!project.add_source(&entry).expect("add"));
}
#[test]
fn a_file_outside_the_root_cannot_be_a_source() {
let dir = tempfile::tempdir().expect("temp dir");
let elsewhere = tempfile::tempdir().expect("temp dir");
let mut project = Project::create(dir.path(), "outside").expect("create");
let stray = elsewhere.path().join("stray.asm");
std::fs::write(&stray, "ret\n").expect("write");
assert!(matches!(
project.add_source(&stray),
Err(ProjectError::OutsideRoot { .. })
));
}
#[test]
fn source_paths_include_every_configured_source() {
let dir = temp_dir();
let path = dir.path().join(Project::FILE_NAME);
std::fs::write(
&path,
"[project]\nentry = \"src/main.asm\"\nsources = [\"src/util.asm\"]\n",
)
.expect("write");
let project = Project::load(&path).expect("load");
assert_eq!(
project.source_paths(),
vec![
dir.path().join("src/main.asm"),
dir.path().join("src/util.asm")
]
);
}
#[test]
fn saving_round_trips_a_modified_configuration() {
let dir = temp_dir();
let mut project = Project::create(dir.path(), "hello").expect("create");
project.config_mut().run.timeout_ms = 12_345;
project.save().expect("save");
let reloaded = Project::discover(dir.path()).expect("reload");
assert_eq!(reloaded.config().run.timeout_ms, 12_345);
}
#[test]
fn saving_an_implicit_project_writes_a_project_file() {
let dir = temp_dir();
let source = dir.path().join("lone.asm");
std::fs::write(&source, "ret\n").expect("write");
let project = Project::for_file(&source);
let written = project.save().expect("save");
assert_eq!(written, dir.path().join(Project::FILE_NAME));
assert!(Project::discover(dir.path()).is_ok());
}
#[test]
fn a_created_project_builds_a_valid_configuration() {
let dir = temp_dir();
let project = Project::create(dir.path(), "hello").expect("create");
project.config().validate().expect("must be valid");
assert_eq!(project.config().project.architecture, Architecture::X86_64);
assert_eq!(project.config().project.syntax, Syntax::Nasm);
}
#[test]
fn an_empty_project_name_falls_back_to_a_default() {
let dir = temp_dir();
let project = Project::create(dir.path(), " ").expect("create");
assert_eq!(project.name(), "ratasm-project");
}
}