Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: off by one error env file without new line #62

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 58 additions & 23 deletions linkup-cli/src/env_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,27 @@ pub fn clear_env_file(service: &str, env_path: &PathBuf) -> Result<()> {
)
})?;

let start_idx = file_content.find(LINKUP_ENV_SEPARATOR);
let end_idx = file_content.rfind(LINKUP_ENV_SEPARATOR);

if let (Some(mut start), Some(mut end)) = (start_idx, end_idx) {
if start < end {
let new_line_above_start =
start > 0 && file_content.chars().nth(start - 1) == Some('\n');
let new_line_bellow_end = file_content.chars().nth(end + 1) == Some('\n');

if new_line_above_start {
start -= 1;
}

if new_line_bellow_end {
end += 1;
}
if let (Some(mut linkup_block_start), Some(mut linkup_block_end)) = (
file_content.find(LINKUP_ENV_SEPARATOR),
file_content.rfind(LINKUP_ENV_SEPARATOR),
) {
if linkup_block_start > 0 && file_content.chars().nth(linkup_block_start - 1) == Some('\n')
{
linkup_block_start -= 1;
}

file_content.drain(start..=end + LINKUP_ENV_SEPARATOR.len());
linkup_block_end += LINKUP_ENV_SEPARATOR.len() - 1;
if file_content.chars().nth(linkup_block_end + 1) == Some('\n') {
linkup_block_end += 1;
}

if file_content.ends_with('\n') {
file_content.pop();
if linkup_block_start < linkup_block_end {
file_content.drain(linkup_block_start..=linkup_block_end);
}

file_content = file_content.trim_end_matches('\n').to_string();
file_content.push('\n');

// Write the updated content back to the file
let mut file = OpenOptions::new()
.write(true)
Expand Down Expand Up @@ -170,7 +167,7 @@ mod test {
clear_env_file("service_1", &env_file.path).unwrap();

let file_content = fs::read_to_string(&env_file.path).unwrap();
assert_eq!("", file_content);
assert_eq!("\n", file_content);
}

#[test]
Expand All @@ -188,7 +185,7 @@ mod test {
clear_env_file("service_1", &env_file.path).unwrap();

let file_content = fs::read_to_string(&env_file.path).unwrap();
let expected_content = format!("{}\n{}", "EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2",);
let expected_content = format!("{}\n{}\n", "EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2",);
assert_eq!(expected_content, file_content);
}

Expand All @@ -210,12 +207,50 @@ mod test {

let file_content = fs::read_to_string(&env_file.path).unwrap();
let expected_content = format!(
"{}\n{}\n{}\n{}",
"{}\n{}\n{}\n{}\n",
"EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2", "EXISTING_3=VALUE_3", "EXISTING_4=VALUE_4",
);
assert_eq!(expected_content, file_content);
}

#[test]
fn clear_env_file_no_new_line_after_linkup() {
let content = format!(
"{}\n{}\n{}\n\n{}\n{}",
"EXISTING_1=VALUE_1",
"EXISTING_2=VALUE_2",
"##### Linkup environment - DO NOT EDIT #####",
"SOURCE_1=VALUE_1",
"##### Linkup environment - DO NOT EDIT #####",
);
let env_file = TestFile::create(&content);

clear_env_file("service_1", &env_file.path).unwrap();

let file_content = fs::read_to_string(&env_file.path).unwrap();
let expected_content = format!("{}\n{}\n", "EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2",);
assert_eq!(expected_content, file_content);
}

#[test]
fn clear_env_file_multiple_new_lines_after_linkup() {
let content = format!(
"{}\n{}\n{}\n\n{}\n{}\n\n\n\n",
"EXISTING_1=VALUE_1",
"EXISTING_2=VALUE_2",
"##### Linkup environment - DO NOT EDIT #####",
"SOURCE_1=VALUE_1",
"##### Linkup environment - DO NOT EDIT #####",
);
let env_file = TestFile::create(&content);

clear_env_file("service_1", &env_file.path).unwrap();

let file_content = fs::read_to_string(&env_file.path).unwrap();
let expected_content = format!("{}\n{}\n", "EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2",);
assert_eq!(expected_content, file_content);
}

#[test]
fn write_and_clear() {
let source = TestFile::create("SOURCE_1=VALUE_1\nSOURCE_2=VALUE_2");
Expand All @@ -240,7 +275,7 @@ mod test {

// Check post clear content
let file_content = fs::read_to_string(&target.path).unwrap();
let expected_content = format!("{}\n{}", "EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2",);
let expected_content = format!("{}\n{}\n", "EXISTING_1=VALUE_1", "EXISTING_2=VALUE_2",);
assert_eq!(file_content, expected_content);
}

Expand Down