|
| 1 | +//! Deployment configuration functions used across test cases. |
| 2 | +//! Use via helpers in `mod.rs` rather than directly. |
| 3 | +//! |
| 4 | +use super::helpers::get_path_from_project_root; |
| 5 | +use serde_json::Value; |
| 6 | +use std::fs; |
| 7 | + |
| 8 | +/// Load deployment at `main_deployment_path` |
| 9 | +/// replace url with `new_postgres_url` |
| 10 | +/// save at `new_deployment_path` |
| 11 | +pub fn copy_deployment_with_new_postgres_url( |
| 12 | + main_deployment_path: &str, |
| 13 | + new_postgres_url: &str, |
| 14 | + new_deployment_path: &str, |
| 15 | +) { |
| 16 | + let full_path = get_path_from_project_root(main_deployment_path); |
| 17 | + |
| 18 | + // load and decode deployment |
| 19 | + let deployment: Value = serde_json::from_str(&fs::read_to_string(full_path).unwrap()).unwrap(); |
| 20 | + |
| 21 | + let new_json = match deployment { |
| 22 | + Value::Object(mut map) => { |
| 23 | + map.insert( |
| 24 | + "postgres_database_url".to_string(), |
| 25 | + Value::String(new_postgres_url.to_string()), |
| 26 | + ); |
| 27 | + Value::Object(map) |
| 28 | + } |
| 29 | + other => other, |
| 30 | + }; |
| 31 | + |
| 32 | + let new_absolute_deployment_path = get_path_from_project_root(new_deployment_path); |
| 33 | + |
| 34 | + let new_deployment = new_json.to_string(); |
| 35 | + |
| 36 | + fs::write(new_absolute_deployment_path, new_deployment).unwrap() |
| 37 | +} |
| 38 | + |
| 39 | +/// Erase test deployment file created at `deployment_path` |
| 40 | +pub fn delete_deployment(deployment_path: &str) { |
| 41 | + let absolute_path = get_path_from_project_root(deployment_path); |
| 42 | + |
| 43 | + fs::remove_file(absolute_path).unwrap() |
| 44 | +} |
0 commit comments