From 8e61be9d4a18c21568b2fb9121352cde3115e710 Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Fri, 22 Mar 2024 04:16:17 +0900 Subject: [PATCH] fix: Remove trim --- src/app.rs | 10 ++---- tests/test_update.py | 76 +++++++++++++++++++++++--------------------- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/src/app.rs b/src/app.rs index af8551e..8276ffd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -23,14 +23,8 @@ pub fn update(base_config: &Config, new_version: &Version) -> Result<()> { replace: "current_version = \"{{new_version}}\"".to_string(), }); for f in files { - let search_text = Tera::one_off(&f.search, &ctx, true) - .unwrap() - .trim_start() - .to_string(); - let replace_text = Tera::one_off(&f.replace, &ctx, true) - .unwrap() - .trim_start() - .to_string(); + let search_text = Tera::one_off(&f.search, &ctx, true).unwrap().to_string(); + let replace_text = Tera::one_off(&f.replace, &ctx, true).unwrap().to_string(); let new_text = build_updates(read_to_string(&f.path).unwrap(), search_text, replace_text); let mut out = File::create(&f.path)?; let _ = out.write(new_text.as_bytes()); diff --git a/tests/test_update.py b/tests/test_update.py index 542c52d..70be3a7 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -3,6 +3,8 @@ import textwrap from pathlib import Path +import pytest + def test_not_configured_env(cmd): """'update' command requires configuration file. @@ -72,51 +74,53 @@ def test_valid_conf__single_line(cmd, tmp_path: Path): # noqa: D103 assert 'current_version = "0.2.0"' in (tmp_path / ".age.toml").read_text() -def test_valid_conf__multi_line(cmd, tmp_path: Path): # noqa: D103 +@pytest.mark.parametrize( + "fileconf,before,after", + [ + ( + textwrap.dedent('''\ + [[files]] + path = "data.txt" + search = """ + Target + ====== + """ + replace = """ + Target + ====== + + v{{new_version}} + ------ + """ + '''), + textwrap.dedent("""\ + Target + ====== + """), + textwrap.dedent("""\ + Target + ====== + + v0.2.0 + ------ + """), + ) + ], +) +def test_valid_conf__multi_line(cmd, tmp_path: Path, fileconf, before, after): # noqa: D103 conf_text = textwrap.dedent( - '''\ + f"""\ current_version = "0.0.0" - [[files]] - path = "data.txt" - search = """ - Target - ====== - """ - replace = """ - Target - ====== - - v{{new_version}} - ------ + {fileconf} """ - ''' ) (tmp_path / ".age.toml").write_text(conf_text) data_txt = tmp_path / "data.txt" - 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()) + # print(data_txt.read_text()) + print(proc.stdout) assert proc.returncode == 0 - assert len(data_txt.read_text().split("\n")) == 10 assert data_txt.read_text() == after