def test_appending(): """appending to an existing logfile should work""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = LOGFILE_NAME LOGGER.input_file("sample.fasta") LOGGER.shutdown() records = Counter() with open(LOGFILE_NAME) as infile: for line in infile: records[line] += 1 vals = set(list(records.values())) assert vals == {1} LOGGER = CachingLogger(create_dir=True) LOGGER.mode = "a" LOGGER.log_file_path = LOGFILE_NAME LOGGER.input_file("sample.fasta") LOGGER.shutdown() records = Counter() with open(LOGFILE_NAME) as infile: for line in infile: records[line] += 1 vals = set(list(records.values())) assert vals == {2} try: os.remove(LOGFILE_NAME) except OSError: pass
def test_mdsum_input(): """md5 sum of input file should be correct""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = LOGFILE_NAME # first file has LF, second has CRLF line endings hex_path = [ ("96eb2c2632bae19eb65ea9224aaafdad", "sample-lf.fasta"), ("e7e219f66be15d8afc7cdb85303305a7", "sample-crlf.fasta"), ] LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") LOGGER.input_file(TEST_ROOTDIR / "sample-crlf.fasta") LOGGER.shutdown() with open(LOGFILE_NAME, "r") as infile: num = 0 for line in infile: for h, p in hex_path: if p in line: assert "input_file" in line line = next(infile) assert h in line num += 1 assert num == len(hex_path) try: shutil.rmtree(DIRNAME) except OSError: pass
def test_set_path_if_exists(): """cannot change an existing logging path""" with TemporaryDirectory(dir=".") as dirname: dirname = Path(dirname) LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = dirname / LOGFILE_NAME LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") with pytest.raises(AttributeError): LOGGER.log_file_path = dirname / "invalid.log" LOGGER.shutdown()
def test_shutdown(): """correctly purges contents""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = LOGFILE_NAME LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") LOGGER.shutdown() try: shutil.rmtree(DIRNAME) except OSError: pass
def test_creates_path(): """creates a log path""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = os.path.join(DIRNAME, LOGFILE_NAME) LOGGER.input_file("sample.fasta") LOGGER.shutdown() assert os.path.exists(DIRNAME) assert os.path.exists(os.path.join(DIRNAME, LOGFILE_NAME)) try: shutil.rmtree(DIRNAME) except OSError: pass
def test_creates_path(): """creates a log path""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = LOGFILE_NAME LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") LOGGER.shutdown() assert DIRNAME.exists() assert LOGFILE_NAME.exists() try: shutil.rmtree(DIRNAME) except OSError: pass
def test_tracks_args(): """details on host, python version should be present in log""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = os.path.join(LOGFILE_NAME) LOGGER.input_file("sample.fasta") LOGGER.shutdown() with open(LOGFILE_NAME, "r") as infile: contents = "".join(infile.readlines()) for label in ["system_details", "python", "user", "command_string"]: assert contents.count(label) == 1, (label, contents.count(label)) try: os.remove(LOGFILE_NAME) except OSError: pass
def test_caching(): """should cache calls prior to logging""" LOGGER = CachingLogger(create_dir=True) LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") assert ("sample-lf.fasta" in LOGGER._messages[-2] and "md5sum" in LOGGER._messages[-1]) LOGGER.log_versions(["numpy"]) assert "numpy==" in LOGGER._messages[-1] LOGGER.log_file_path = LOGFILE_NAME LOGGER.shutdown() try: shutil.rmtree(DIRNAME) except OSError: pass
def test_tracks_args(): """details on host, python version should be present in log""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = LOGFILE_NAME LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") LOGGER.shutdown() contents = LOGFILE_NAME.read_text() for label in ["system_details", "python", "user", "command_string"]: assert contents.count(f"\t{label}") == 1, ( label, contents.count(label), ) try: shutil.rmtree(DIRNAME) except OSError: pass
def test_mdsum_input(): """md5 sum of input file should be correct""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = os.path.join(LOGFILE_NAME) LOGGER.input_file("sample.fasta") LOGGER.shutdown() with open(LOGFILE_NAME, "r") as infile: num = 0 for line in infile: line = line.strip() if "input_file_path md5sum" in line: assert "96eb2c2632bae19eb65ea9224aaafdad" in line num += 1 assert num == 1 try: os.remove(LOGFILE_NAME) except OSError: pass
def test_tracks_versions_empty(): """should track version of scitrack""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = LOGFILE_NAME LOGGER.input_file(TEST_ROOTDIR / "sample-lf.fasta") LOGGER.log_versions() LOGGER.shutdown() contents = LOGFILE_NAME.read_text() for label in ["system_details", "python", "user", "command_string"]: assert contents.count(f"\t{label}") == 1, ( label, contents.count(label), ) for line in contents.splitlines(): if "version :" in line: assert "==%s" % __version__ in line, line try: shutil.rmtree(DIRNAME) except OSError: pass
def test_tracks_versions(): """should track versions""" LOGGER = CachingLogger(create_dir=True) LOGGER.log_file_path = os.path.join(LOGFILE_NAME) LOGGER.input_file("sample.fasta") LOGGER.log_versions(["numpy"]) LOGGER.shutdown() with open(LOGFILE_NAME, "r") as infile: contents = "".join(infile.readlines()) for label in ["system_details", "python", "user", "command_string"]: assert contents.count(label) == 1, (label, contents.count(label)) for line in contents.splitlines(): if "version :" in line: if "numpy" not in line: assert "==%s" % __version__ in line, line else: assert "numpy" in line, line print("\n\n", contents) try: os.remove(LOGFILE_NAME) except OSError: pass