Skip to content

Commit

Permalink
Automatically create RYE_HOME in config (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko authored Mar 6, 2024
1 parent 39f2c04 commit bab0144
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions rye/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ impl Config {

/// Saves changes back.
pub fn save(&self) -> Result<(), Error> {
// try to make the parent folder if it does not exist. ignore the error though.
if let Some(parent) = self.path.parent() {
fs::create_dir_all(parent).ok();
}
fs::write(&self.path, self.doc.to_string())
.path_context(&self.path, "failed to save config")?;
Ok(())
Expand Down
25 changes: 25 additions & 0 deletions rye/tests/test_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::fs;

use insta::assert_snapshot;

use crate::common::{rye_cmd_snapshot, Space};

mod common;
Expand Down Expand Up @@ -109,3 +113,24 @@ fn test_config_show_path_and_any_action() {
error: an argument cannot be used with one or more of the other specified arguments
"###);
}

#[test]
fn test_config_save_missing_folder() {
let space = Space::new();
let fake_home = space.project_path().join("missing-thing");
rye_cmd_snapshot!(space.rye_cmd()
.arg("config")
.arg("--set")
.arg("[email protected]")
.env("RYE_HOME", fake_home.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
"###);
assert_snapshot!(fs::read_to_string(fake_home.join("config.toml")).unwrap(), @r###"
[default]
toolchain = "[email protected]"
"###);
}

0 comments on commit bab0144

Please sign in to comment.