Skip to content

Commit 406e5b6

Browse files
authored
Merge pull request #375 from takumin/codex/add-validation-for-self.dir-in-profile
Validate profile dir is a directory
2 parents e92ff01 + 08bdd7f commit 406e5b6

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! The configuration is typically loaded from YAML files using the
88
//! `load_profile` function.
99
10-
use anyhow::{Context, Ok, Result};
10+
use anyhow::{Context, Ok, Result, bail};
1111
use camino::{Utf8Path, Utf8PathBuf};
1212
use serde::Deserialize;
1313
use std::fs::File;
@@ -63,6 +63,9 @@ impl Bootstrap {
6363
impl Profile {
6464
/// Validate configuration semantics beyond basic deserialization.
6565
pub fn validate(&self) -> Result<()> {
66+
if self.dir.exists() && !self.dir.is_dir() {
67+
bail!("dir must be a directory: {}", self.dir);
68+
}
6669
for (index, provisioner) in self.provisioners.iter().enumerate() {
6770
provisioner
6871
.validate()

tests/config_test.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,32 @@ provisioners:
277277
Ok(())
278278
}
279279

280+
#[test]
281+
fn test_profile_validation_rejects_dir_file() -> Result<()> {
282+
let dir_file = NamedTempFile::new()?;
283+
let mut file = NamedTempFile::new()?;
284+
// editorconfig-checker-disable
285+
writeln!(
286+
file,
287+
r#"---
288+
dir: {}
289+
bootstrap:
290+
type: mmdebstrap
291+
suite: bookworm
292+
target: rootfs.tar.zst
293+
"#,
294+
dir_file.path().display()
295+
)?;
296+
// editorconfig-checker-enable
297+
298+
let path = Utf8Path::from_path(file.path()).unwrap();
299+
let profile = load_profile(path)?;
300+
301+
assert!(profile.validate().is_err());
302+
303+
Ok(())
304+
}
305+
280306
#[test]
281307
fn test_load_profile_resolves_shell_script_relative_to_profile_dir() -> Result<()> {
282308
let temp_dir = tempdir()?;

0 commit comments

Comments
 (0)