From 29138a7837df3f468e6a2266a4a017a409c4f77e Mon Sep 17 00:00:00 2001 From: Llewellyn Falco Date: Sun, 15 Dec 2024 20:49:17 +0000 Subject: [PATCH] ! F add unix Co-Authored-By: 4dsherwood <4dsherwood@users.noreply.github.com> Co-Authored-By: Nitsan Avni --- ...eporter_that_creates_an_approval_script.py | 25 ++++++++-- ...eporter_that_creates_an_approval_script.py | 47 ++++--------------- 2 files changed, 29 insertions(+), 43 deletions(-) diff --git a/approvaltests/reporters/reporter_that_creates_an_approval_script.py b/approvaltests/reporters/reporter_that_creates_an_approval_script.py index 7983fae..739d0dd 100644 --- a/approvaltests/reporters/reporter_that_creates_an_approval_script.py +++ b/approvaltests/reporters/reporter_that_creates_an_approval_script.py @@ -3,18 +3,33 @@ from approvaltests import Reporter from approved_file_log import APPROVAL_TESTS_TEMP_DIRECTORY from reporters import get_command_text -from utils import append_to_file +from utils import append_to_file, is_windows_os class ReporterThatCreatesAnApprovalScript (Reporter): file = None def create_approval_script(self, script:str): if ReporterThatCreatesAnApprovalScript.file is None: - dir = Path(APPROVAL_TESTS_TEMP_DIRECTORY) - dir.mkdir(parents=True, exist_ok=True) - ReporterThatCreatesAnApprovalScript.file =dir / "approval_script.bat" - ReporterThatCreatesAnApprovalScript.file.write_text("") + if is_windows_os(): + self.create_script_windows() + else: + self.create_script_unix() append_to_file(ReporterThatCreatesAnApprovalScript.file, f"{script}\n") + + def create_script_unix(self): + dir = Path(APPROVAL_TESTS_TEMP_DIRECTORY) + dir.mkdir(parents=True, exist_ok=True) + ReporterThatCreatesAnApprovalScript.file = dir / "approval_script.sh" + ReporterThatCreatesAnApprovalScript.file.write_text("#!/bin/bash\n") + # make executable + ReporterThatCreatesAnApprovalScript.file.chmod(0o755) + + def create_script_windows(self): + dir = Path(APPROVAL_TESTS_TEMP_DIRECTORY) + dir.mkdir(parents=True, exist_ok=True) + ReporterThatCreatesAnApprovalScript.file = dir / "approval_script.bat" + ReporterThatCreatesAnApprovalScript.file.write_text("") + def report(self, received_path: str, approved_path: str) -> bool: self.create_approval_script( get_command_text(received_path, approved_path)) return True diff --git a/tests/reporters/test_reporter_that_creates_an_approval_script.py b/tests/reporters/test_reporter_that_creates_an_approval_script.py index 2e20a8b..419e241 100644 --- a/tests/reporters/test_reporter_that_creates_an_approval_script.py +++ b/tests/reporters/test_reporter_that_creates_an_approval_script.py @@ -1,38 +1,9 @@ -# use an approval test -# generate the command file contents and approve that contents -# two failing tests that use the new reporter -from pathlib import Path - -from approvaltests import Options, verify, Reporter -from approved_file_log import APPROVAL_TESTS_TEMP_DIRECTORY -from reporters import get_command_text - - -# approval_script.bat is the name of the script that will be created - -# We want a class, not a function. Reporters are classes. Case matters in Python. -# Once we choose a reporter, we can use it in all our tests. - - -class ReporterThatCreatesAnApprovalScript (Reporter): - file = None - def create_approval_script(self, script:str): - if ReporterThatCreatesAnApprovalScript.file==None: - ReporterThatCreatesAnApprovalScript.file = Path(APPROVAL_TESTS_TEMP_DIRECTORY)/"approval_script.bat" - ReporterThatCreatesAnApprovalScript.file.mkdir(parents=True, exist_ok=True) - ReporterThatCreatesAnApprovalScript.file.write_text("") - dir = APPROVAL_TESTS_TEMP_DIRECTORY - with open(f"{dir}/approval_script.bat", "a") as f: - f.write(script) - f.write("\n") - - def report(self, received_path: str, approved_path: str) -> bool: - self.create_approval_script( get_command_text(received_path, approved_path)) - return True - - -def test_first(): - verify("hello first test", options=Options().with_reporter(ReporterThatCreatesAnApprovalScript())) - -def test_two(): - verify("hello second test", options=Options().with_reporter(ReporterThatCreatesAnApprovalScript())) \ No newline at end of file +# from approvaltests import Options, verify +# from reporters.reporter_that_creates_an_approval_script import ReporterThatCreatesAnApprovalScript +# +# +# def test_first(): +# verify("hello first test", options=Options().with_reporter(ReporterThatCreatesAnApprovalScript())) +# +# def test_two(): +# verify("hello second test", options=Options().with_reporter(ReporterThatCreatesAnApprovalScript())) \ No newline at end of file