Skip to content

Commit

Permalink
cli: fixing windows paths in toml strings and adding a specific test …
Browse files Browse the repository at this point in the history
…for that
  • Loading branch information
dennybiasiolli committed Feb 22, 2024
1 parent 02e0f76 commit 8a4db85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ fn create_config_file(
let path = Config::file_path(matches);
if !path.exists() {
let source_content = include_str!("default_files/diesel.toml").to_string();
// convert the path to a valid toml string (escaping backslashes on windows)
let migrations_dir_toml_string = migrations_dir.display().to_string().replace('\\', "\\\\");
let modified_content = source_content.replace(
"dir = \"migrations\"",
&format!("dir = \"{}\"", migrations_dir.display()),
&format!("dir = \"{}\"", migrations_dir_toml_string),
);
let mut file = fs::File::create(path)?;
file.write_all(modified_content.as_bytes())?;
Expand Down
19 changes: 19 additions & 0 deletions diesel_cli/tests/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ fn setup_writes_migration_dir_by_arg_to_config_file() {
assert!(p.file_contents("diesel.toml").contains("dir = \"foo\""));
}

#[test]
#[cfg(windows)]
fn setup_writes_migration_dir_by_arg_to_config_file_win() {
let p = project("setup_writes_migration_dir_by_arg_to_config_file_win").build();

// make sure the project builder doesn't create it for us
assert!(!p.has_file("migrations"));
assert!(!p.has_file("foo"));

let result = p.command("setup").arg("--migration-dir=foo\\bar").run();

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(!p.has_file("migrations"));
assert!(p.has_file("foo"));
assert!(p
.file_contents("diesel.toml")
.contains("dir = \"foo\\\\bar\""));
}

#[test]
fn setup_works_with_migration_dir_by_env() {
let p = project("setup_works_with_migration_dir_by_env").build();
Expand Down

0 comments on commit 8a4db85

Please sign in to comment.