def test_create_log(self): """ Ensure create_log function is working properly. * create new log file and log a message, then confirm the file and message exist. """ from garage.logger import create_log from garage.utils import get_file_contents, delete_file self._msg("test", "create_log", first=True) tmpfile_dir = tempfile.gettempdir() logname = "garage-test" filename = "{0}.log".format(logname) path = os.path.join(tmpfile_dir, filename) delete_file(path) self._msg("logfile", path) logger = create_log(logname, logfile=path) msg = "this is a test log message" logger.debug(msg) regexp = re.compile(r"^.*{0} - DEBUG - {1}$".format(logname, msg), re.I) data = get_file_contents(path) for lineno, line in enumerate(data.splitlines()): matched = regexp.match(line) if matched: self._msg("match: {0}".format(lineno), matched.group(0)) self.assertTrue(matched) delete_file(path)
def test_get_file_contents_error(self): """ get_file_contents should return None for a non-existent path. """ self._msg("test", "get_file_contents error", first=True) from garage.utils import get_file_contents module_dir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(module_dir, "non-existent-file") data = get_file_contents(path) self._msg("path", path) self._msg("data", data, linebreak=True) self.assertEqual(data, None) data = get_file_contents(None) self._msg("path", path) self._msg("data", data, linebreak=True) self.assertEqual(data, None)
def test_get_file_contents(self): """ Ensure get_file_contents function is working properly. """ self._msg("test", "get_file_contents", first=True) from garage.utils import get_file_contents module_dir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(module_dir, "testfile.txt") data = get_file_contents(path) self._msg("path", path) self._msg("data", data, linebreak=True) self.assertEqual(data, TestData)
def test_load_yaml(self): """ Ensure load_yaml function is working properly. """ from garage.utils import load_yaml, get_file_contents self._msg("test", "load_yaml", first=True) data = {"a": 1, "b": 2, "c": "three"} module_dir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(module_dir, "example_data.yaml") result = load_yaml(get_file_contents(path)) self.assertEqual(result, data) self._msg("path", path) self._msg("expected", data) self._msg("result", result)
def test_load_yaml_docs(self): """ Ensure load_yaml_docs function is working properly. """ from garage.utils import load_yaml_docs, get_file_contents self._msg("test", "load_yaml_docs", first=True) data = [{"a": 1, "b": 2, "c": "three"}, {"x": 0, "y": 1, "z": 2}] module_dir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(module_dir, "example_yaml_docs.yaml") result = load_yaml_docs(get_file_contents(path)) self._msg("path", path) self._msg("expected", data) self._msg("result", result) for i, y in enumerate(result): self.assertEqual(y, data[i]) self._msg("yaml doc", y)
def test_write_file(self): """ Ensure write_file function is working properly. """ from garage.utils import write_file, get_file_contents self._msg("test", "write_file", first=True) filename = "garage-testfile" tmpfile_dir = tempfile.gettempdir() path = os.path.join(tmpfile_dir, filename) result = write_file(path, TestData) self.assertTrue(result) # chect contents data = get_file_contents(path) self._msg("path", path) self._msg("data", data, linebreak=True) self.assertEqual(data, TestData) os.unlink(path)