From d1b0aa86ef39b0b72f148ef32a464abe7fafb9b4 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Thu, 26 Jan 2023 20:12:35 +0000 Subject: [PATCH] Rewrite test to be dumber --- .../merge_production_dotenvs_in_dotenv.py | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py b/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py index 67574866d..e47705fbc 100644 --- a/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py +++ b/{{cookiecutter.project_slug}}/merge_production_dotenvs_in_dotenv.py @@ -30,30 +30,54 @@ def main(): merge(DOTENV_FILE, PRODUCTION_DOTENV_FILES) -@pytest.mark.parametrize("files_count", range(3)) -@pytest.mark.parametrize("append_linesep", [True, False]) -def test_merge(tmp_path: Path, files_count: int, append_linesep: bool): +@pytest.mark.parametrize( + ("input_contents", "append_linesep", "expected_output"), + [ + # No line separator + ([], False, ""), + ([""], False, ""), + (["\n"], False, "\n"), + (["TEST=1"], False, "TEST=1"), + (["THIS=2", "THAT='example'"], False, "THIS=2THAT='example'"), + (["ONE=1\n", "TWO=2"], False, "ONE=1\nTWO=2"), + (["A=0", "B=1", "C=2"], False, "A=0B=1C=2"), + # With line separator + ( + [], + True, + "", + ), + ( + [""], + True, + "\n", + ), + ( + ["JANE=doe"], + True, + "JANE=doe\n", + ), + (["SEP=true", "AR=ator"], True, "SEP=true\nAR=ator\n"), + (["X=x\n", "Y=y", "Z=z\n"], True, "X=x\n\nY=y\nZ=z\n\n"), + ], +) +def test_merge( + tmp_path: Path, + input_contents: list[str], + append_linesep: bool, + expected_output: str, +): output_file = tmp_path / ".env" - expected_output_file_content = "" files_to_merge = [] - for num in range(1, files_count + 1): - merge_filename = f".service{num}" - merge_file = tmp_path / merge_filename - - merge_file_content = merge_filename * num - merge_file.write_text(merge_file_content) - - expected_output_file_content += merge_file_content - if append_linesep: - expected_output_file_content += os.linesep - + for num, input_content in enumerate(input_contents, start=1): + merge_file = tmp_path / f".service{num}" + merge_file.write_text(input_content) files_to_merge.append(merge_file) merge(output_file, files_to_merge, append_linesep) - actual_output_file_content = output_file.read_text() - assert actual_output_file_content == expected_output_file_content + assert output_file.read_text() == expected_output if __name__ == "__main__":