feat: Improve package management and testing
- Improve platform detection logic for more robust package management. - Enhance error handling and reporting in package commands. - Refactor code for better readability and maintainability. - Add comprehensive tests to cover package management functionality. - Improve test coverage for various scenarios and edge cases.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
|
||||
use regex::Regex;
|
||||
use std::fs;
|
||||
use std::io::{self, Read, Seek, SeekFrom};
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
/// Represents the type of replacement to perform.
|
||||
@@ -46,36 +45,36 @@ impl TextReplacer {
|
||||
/// Applies all configured replacement operations to the input text
|
||||
pub fn replace(&self, input: &str) -> String {
|
||||
let mut result = input.to_string();
|
||||
|
||||
|
||||
// Apply each replacement operation in sequence
|
||||
for op in &self.operations {
|
||||
result = op.apply(&result);
|
||||
}
|
||||
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
/// Reads a file, applies all replacements, and returns the result as a string
|
||||
pub fn replace_file<P: AsRef<Path>>(&self, path: P) -> io::Result<String> {
|
||||
let mut file = fs::File::open(path)?;
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content)?;
|
||||
|
||||
|
||||
Ok(self.replace(&content))
|
||||
}
|
||||
|
||||
|
||||
/// Reads a file, applies all replacements, and writes the result back to the file
|
||||
pub fn replace_file_in_place<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
|
||||
let content = self.replace_file(&path)?;
|
||||
fs::write(path, content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
/// Reads a file, applies all replacements, and writes the result to a new file
|
||||
pub fn replace_file_to<P1: AsRef<Path>, P2: AsRef<Path>>(
|
||||
&self,
|
||||
input_path: P1,
|
||||
output_path: P2
|
||||
&self,
|
||||
input_path: P1,
|
||||
output_path: P2,
|
||||
) -> io::Result<()> {
|
||||
let content = self.replace_file(&input_path)?;
|
||||
fs::write(output_path, content)?;
|
||||
@@ -111,13 +110,13 @@ impl TextReplacerBuilder {
|
||||
self.use_regex = yes;
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Sets whether the replacement should be case-insensitive
|
||||
pub fn case_insensitive(mut self, yes: bool) -> Self {
|
||||
self.case_insensitive = yes;
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Adds another replacement operation to the chain and resets the builder for a new operation
|
||||
pub fn and(mut self) -> Self {
|
||||
self.add_current_operation();
|
||||
@@ -130,20 +129,20 @@ impl TextReplacerBuilder {
|
||||
let replacement = self.replacement.take().unwrap_or_default();
|
||||
let use_regex = self.use_regex;
|
||||
let case_insensitive = self.case_insensitive;
|
||||
|
||||
|
||||
// Reset current settings
|
||||
self.use_regex = false;
|
||||
self.case_insensitive = false;
|
||||
|
||||
|
||||
// Create the replacement mode
|
||||
let mode = if use_regex {
|
||||
let mut regex_pattern = pattern;
|
||||
|
||||
|
||||
// If case insensitive, add the flag to the regex pattern
|
||||
if case_insensitive && !regex_pattern.starts_with("(?i)") {
|
||||
regex_pattern = format!("(?i){}", regex_pattern);
|
||||
}
|
||||
|
||||
|
||||
match Regex::new(®ex_pattern) {
|
||||
Ok(re) => ReplaceMode::Regex(re),
|
||||
Err(_) => return false, // Failed to compile regex
|
||||
@@ -156,12 +155,10 @@ impl TextReplacerBuilder {
|
||||
}
|
||||
ReplaceMode::Literal(pattern)
|
||||
};
|
||||
|
||||
self.operations.push(ReplacementOperation {
|
||||
mode,
|
||||
replacement,
|
||||
});
|
||||
|
||||
|
||||
self.operations
|
||||
.push(ReplacementOperation { mode, replacement });
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
@@ -172,12 +169,12 @@ impl TextReplacerBuilder {
|
||||
pub fn build(mut self) -> Result<TextReplacer, String> {
|
||||
// If there's a pending replacement operation, add it
|
||||
self.add_current_operation();
|
||||
|
||||
|
||||
// Ensure we have at least one replacement operation
|
||||
if self.operations.is_empty() {
|
||||
return Err("No replacement operations configured".to_string());
|
||||
}
|
||||
|
||||
|
||||
Ok(TextReplacer {
|
||||
operations: self.operations,
|
||||
})
|
||||
@@ -187,7 +184,7 @@ impl TextReplacerBuilder {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::io::Write;
|
||||
use std::io::{Seek, SeekFrom, Write};
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
#[test]
|
||||
@@ -219,7 +216,7 @@ mod tests {
|
||||
|
||||
assert_eq!(output, "qux bar qux baz");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_multiple_replacements() {
|
||||
let replacer = TextReplacer::builder()
|
||||
@@ -236,7 +233,7 @@ mod tests {
|
||||
|
||||
assert_eq!(output, "qux baz qux");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_case_insensitive_regex() {
|
||||
let replacer = TextReplacer::builder()
|
||||
@@ -252,44 +249,44 @@ mod tests {
|
||||
|
||||
assert_eq!(output, "bar bar bar");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_file_operations() -> io::Result<()> {
|
||||
// Create a temporary file
|
||||
let mut temp_file = NamedTempFile::new()?;
|
||||
writeln!(temp_file, "foo bar foo baz")?;
|
||||
|
||||
|
||||
// Flush the file to ensure content is written
|
||||
temp_file.as_file_mut().flush()?;
|
||||
|
||||
|
||||
let replacer = TextReplacer::builder()
|
||||
.pattern("foo")
|
||||
.replacement("qux")
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
|
||||
// Test replace_file
|
||||
let result = replacer.replace_file(temp_file.path())?;
|
||||
assert_eq!(result, "qux bar qux baz\n");
|
||||
|
||||
|
||||
// Test replace_file_in_place
|
||||
replacer.replace_file_in_place(temp_file.path())?;
|
||||
|
||||
|
||||
// Verify the file was updated - need to seek to beginning of file first
|
||||
let mut content = String::new();
|
||||
temp_file.as_file_mut().seek(SeekFrom::Start(0))?;
|
||||
temp_file.as_file_mut().read_to_string(&mut content)?;
|
||||
assert_eq!(content, "qux bar qux baz\n");
|
||||
|
||||
|
||||
// Test replace_file_to with a new temporary file
|
||||
let output_file = NamedTempFile::new()?;
|
||||
replacer.replace_file_to(temp_file.path(), output_file.path())?;
|
||||
|
||||
|
||||
// Verify the output file has the replaced content
|
||||
let mut output_content = String::new();
|
||||
fs::File::open(output_file.path())?.read_to_string(&mut output_content)?;
|
||||
assert_eq!(output_content, "qux bar qux baz\n");
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user