def test_fails_when_file_exists(self): path = os.path.join(self.get_temp_dir(), "test.txt") with open(path, "w"): pass with self.assertRaises(exporter_lib.OutputFileExistsError) as cm: exporter_lib._open_excl(path) self.assertEqual(str(cm.exception), path)
def test_propagates_other_errors(self): path = os.path.join(self.get_temp_dir(), "enoent", "test.txt") with self.assertRaises(OSError) as cm: exporter_lib._open_excl(path) self.assertEqual(cm.exception.errno, errno.ENOENT)
def test_success(self): path = os.path.join(self.get_temp_dir(), "test.txt") with exporter_lib._open_excl(path) as outfile: outfile.write("hello\n") with open(path) as infile: self.assertEqual(infile.read(), "hello\n")