Skip to content

Commit

Permalink
! F add unix
Browse files Browse the repository at this point in the history
Co-Authored-By: 4dsherwood <[email protected]>
Co-Authored-By: Nitsan Avni <[email protected]>
  • Loading branch information
3 people committed Dec 15, 2024
1 parent f5c0e50 commit 29138a7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
47 changes: 9 additions & 38 deletions tests/reporters/test_reporter_that_creates_an_approval_script.py
Original file line number Diff line number Diff line change
@@ -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()))
# 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()))

0 comments on commit 29138a7

Please sign in to comment.