-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File.Replace_all (#26)
- Loading branch information
Showing
3 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,58 @@ | ||
use anyhow::Result; | ||
use std::fs::{write,read_to_string}; | ||
use regex::{Regex,NoExpand}; | ||
|
||
pub fn replace_all(path: String, pattern: String, value: String) -> Result<()> { | ||
let file_contents = read_to_string(path.clone()).unwrap(); | ||
let re = Regex::new(&pattern).unwrap(); | ||
let result = re.replace_all(&file_contents, NoExpand(&value)); | ||
write(path, String::from(result))?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use tempfile::{NamedTempFile}; | ||
|
||
#[test] | ||
fn test_replace_all_multiline() -> anyhow::Result<()>{ | ||
let tmp_file_new = NamedTempFile::new()?; | ||
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); | ||
let _ = write(path_new.clone(),"Match User anoncvs\nMatch User anoncvs\nMatch User anoncvs\n"); | ||
|
||
// Run our code | ||
replace_all(path_new.clone(), String::from("Match"), String::from("Not Match"))?; | ||
|
||
let res = read_to_string(path_new)?; | ||
assert_eq!(res, "Not Match User anoncvs\n".repeat(3)); | ||
Ok(()) | ||
} | ||
#[test] | ||
fn test_replace_all_regex_simple() -> anyhow::Result<()>{ | ||
let tmp_file_new = NamedTempFile::new()?; | ||
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); | ||
let _ = write(path_new.clone(),"Match User anoncvs\nMatch User anoncvs\nMatch User anoncvs\n"); | ||
|
||
// Run our code | ||
replace_all(path_new.clone(), String::from(r".*Match.*"), String::from("Not Match"))?; | ||
|
||
let res = read_to_string(path_new)?; | ||
assert_eq!(res, "Not Match\n".repeat(3)); | ||
Ok(()) | ||
} | ||
#[test] | ||
fn test_replace_all_regex_complex() -> anyhow::Result<()>{ | ||
let tmp_file_new = NamedTempFile::new()?; | ||
let path_new = String::from(tmp_file_new.path().to_str().unwrap()).clone(); | ||
let _ = write(path_new.clone(),"MaxStartups 10:30:100\nListenAddress 0.0.0.0\nMaxAuthTries 6\nMatch User anoncvs\n"); | ||
|
||
// Run our code | ||
replace_all(path_new.clone(), String::from(r"\d\.\d\.\d\.\d"), String::from("127.0.0.1"))?; | ||
|
||
let res = read_to_string(path_new)?; | ||
assert_eq!(res, "MaxStartups 10:30:100\nListenAddress 127.0.0.1\nMaxAuthTries 6\nMatch User anoncvs\n"); | ||
Ok(()) | ||
} | ||
|
||
pub fn replace_all(_path: String, _pattern: String, _value: String) -> Result<()> { | ||
unimplemented!("Method unimplemented") | ||
} |