Skip to content

Commit

Permalink
fix: Pop head when running replace with multi-line
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Mar 21, 2024
1 parent 9450b6c commit 86e5d79
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
14 changes: 8 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use chrono::Local;
use semver::Version;
use std::collections::VecDeque;
use std::fs::{read_to_string, File};
use std::io::prelude::*;
use std::path::PathBuf;
Expand Down Expand Up @@ -46,16 +47,17 @@ fn make_context(current_version: &Version, new_version: &Version) -> Context {
ctx
}

fn build_updates(input: String, seach_text: String, replace_text: String) -> String {
let lines = seach_text.split("\n").count();
let mut buf: Vec<String> = Vec::new();
fn build_updates(input: String, search_text: String, replace_text: String) -> String {
let lines = search_text.split("\n").count();
let mut buf: VecDeque<String> = VecDeque::new();
let mut output: Vec<String> = Vec::new();
for line in input.split("\n") {
if buf.len() == lines {
output.push(buf.pop().unwrap());
output.push(buf.pop_front().unwrap());
}
buf.push(line.to_string());
if buf.join("\n") == seach_text {
buf.push_back(line.to_string());
let buf_vec: Vec<String> = buf.clone().into();
if buf_vec.join("\n") == search_text {
output.push(replace_text.to_string());
buf.clear();
}
Expand Down
30 changes: 25 additions & 5 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,36 @@ def test_valid_conf__multi_line(cmd, tmp_path: Path): # noqa: D103
Target
======
v {{new_version}}
-----------------
v{{new_version}}
------
"""
'''
)
(tmp_path / ".age.toml").write_text(conf_text)
data_txt = tmp_path / "data.txt"
data_txt.write_text("Target\n======\n")
before = textwrap.dedent("""\
====
Data
====
Target
======
""")
after = textwrap.dedent("""\
====
Data
====
Target
======
v0.2.0
------
""")
data_txt.write_text(before)

proc = cmd("update", "0.2.0")
print(data_txt.read_text())
assert proc.returncode == 0
assert len(data_txt.read_text().split("\n")) == 6
assert "v 0.2.0" in data_txt.read_text()
assert len(data_txt.read_text().split("\n")) == 10
assert data_txt.read_text() == after

0 comments on commit 86e5d79

Please sign in to comment.