Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically create RYE_HOME in config #844

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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("default.toolchain=cpython@3.12")
.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 = "cpython@3.12"
"###);
}