|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import sys |
| 4 | +from shutil import rmtree |
| 5 | +import pytest |
| 6 | +import re |
| 7 | + |
| 8 | + |
| 9 | +def capture(command, instream=None): |
| 10 | + proc = subprocess.Popen( |
| 11 | + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=instream |
| 12 | + ) |
| 13 | + out, err = proc.communicate() |
| 14 | + return out, err, proc.returncode |
| 15 | + |
| 16 | + |
| 17 | +def test_logging_to_file(python_version): |
| 18 | + """Test logging Terminal output to a log file. |
| 19 | + As some data will differ with each log (the timestamps, file paths, line nums etc) |
| 20 | + a regex substitution has been employed to replace the strings that may change with |
| 21 | + whitespace. |
| 22 | + """ |
| 23 | + path_basic_scene = os.path.join("tests", "tests_data", "basic_scenes.py") |
| 24 | + path_output = os.path.join("tests_cache", "media_temp") |
| 25 | + command = [ |
| 26 | + python_version, |
| 27 | + "-m", |
| 28 | + "manim", |
| 29 | + path_basic_scene, |
| 30 | + "SquareToCircle", |
| 31 | + "-l", |
| 32 | + "--log_to_file", |
| 33 | + "--log_dir", |
| 34 | + os.path.join(path_output, "logs"), |
| 35 | + "--media_dir", |
| 36 | + path_output, |
| 37 | + ] |
| 38 | + out, err, exitcode = capture(command) |
| 39 | + log_file_path = os.path.join(path_output, "logs", "SquareToCircle.log") |
| 40 | + assert exitcode == 0, err |
| 41 | + assert os.path.exists(log_file_path), err |
| 42 | + if sys.platform.startswith("win32") or sys.platform.startswith("cygwin"): |
| 43 | + enc = "Windows-1252" |
| 44 | + else: |
| 45 | + enc = "utf-8" |
| 46 | + with open(log_file_path, encoding=enc) as logfile: |
| 47 | + logs = logfile.read() |
| 48 | + # The following regex pattern selects timestamps, file paths and all numbers.. |
| 49 | + pattern = r"(\[?\d+:?]?)|(\['[A-Z]?:?[\/\\].*cfg'])|([A-Z]?:?[\/\\].*mp4)" |
| 50 | + |
| 51 | + logs = re.sub(pattern, lambda m: " " * len((m.group(0))), logs) |
| 52 | + with open( |
| 53 | + os.path.join(os.path.dirname(__file__), "expected.txt"), "r" |
| 54 | + ) as expectedfile: |
| 55 | + expected = re.sub( |
| 56 | + pattern, lambda m: " " * len((m.group(0))), expectedfile.read() |
| 57 | + ) |
| 58 | + assert logs == expected, logs |
0 commit comments