Skip to content

Commit

Permalink
Code clean up and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
pixincreate committed Nov 3, 2023
1 parent 08fc436 commit ee99702
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
15 changes: 8 additions & 7 deletions crates/test_utils/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ fn main() {
let status = child.wait();

if runner.file_modified_flag {
let mut cmd = Command::new("git");
let output = cmd
let git_status = Command::new("git")
.args([
"checkout",
"HEAD",
Expand All @@ -27,16 +26,18 @@ fn main() {
])
.output();

match output {
match git_status {
Ok(output) => {
if output.status.success() {
let _ = String::from_utf8_lossy(&output.stdout);
let stdout_str = String::from_utf8_lossy(&output.stdout);
println!("Git command executed successfully: {}", stdout_str);
} else {
let _ = String::from_utf8_lossy(&output.stderr);
let stderr_str = String::from_utf8_lossy(&output.stderr);
eprintln!("Git command failed with error: {}", stderr_str);
}
}
Err(e) => {
println!("Error: {}", e);
eprintln!("Error running Git: {}", e);
}
}
}
Expand All @@ -52,7 +53,7 @@ fn main() {
}
}
Err(err) => {
eprintln!("Failed to wait for command execution: {err}");
eprintln!("Failed to wait for command execution: {}", err);
exit(1);
}
};
Expand Down
23 changes: 9 additions & 14 deletions crates/test_utils/src/newman_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn get_path(name: impl AsRef<str>) -> String {

// This function currently allows you to add only custom headers.
// In future, as we scale, this can be modified based on the need
fn insert_content<T>(dir: T, content_to_insert: impl AsRef<str>) -> io::Result<()>
fn insert_content<T>(dir: T, content_to_insert: T) -> io::Result<()>
where
T: AsRef<Path> + std::fmt::Debug,
{
Expand All @@ -66,13 +66,7 @@ where
.create(true)
.open(file_path)?;

// Convert the content to insert to a byte slice
let content_bytes = content_to_insert.as_ref().as_bytes();

// Write a newline character before the content
file.write_all(b"\n")?;
// Write the content to the file
file.write_all(content_bytes)?;
write!(file, "\n{:#?}", content_to_insert)?;

Ok(())
}
Expand Down Expand Up @@ -200,15 +194,16 @@ pub fn command_generate() -> ReturnArgs {
}

let mut modified = false;

if let Some(headers) = &args.custom_headers {
for header in headers {
let kv_pair: Vec<&str> = header.splitn(2, ':').collect();
if kv_pair.len() == 2 {
let key = kv_pair[0];
let value = kv_pair[1];
if let Some((key, value)) = header
.splitn(2, ':')
.collect::<Vec<_>>()
.as_slice()
.split_first()
{
let content_to_insert = format!(
"pm.request.headers.add({{key: \"{}\", value: \"{}\"}});",
"pm.request.headers.add({{key: \"{}\", value: \"{:#?}\"}});",
key, value
);
if insert_content(&collection_path, &content_to_insert).is_ok() {
Expand Down

0 comments on commit ee99702

Please sign in to comment.