class TestParseError(unittest.TestCase): def setUp(self): self.f = NamedTemporaryFile(mode="w", delete=False) def tearDown(self): self.f.close() os.remove(self.f.name) def test_simple(self): text = """\ NName: foo """ error_msg = "Unrecognized keyword: 'NName'" self.assertRaisesRegexp(ParseError, error_msg, lambda : PackageDescription.from_string(text)) def test_simple_filename(self): f = self.f f.write("NName: foo") f.flush() self.assertRaises(ParseError, lambda : PackageDescription.from_file(f.name)) def test_error_string(self): f = self.f f.write("NName: foo") f.flush() try: PackageDescription.from_file(f.name) raise AssertionError("Should raise here !") except ParseError: e = extract_exception() self.assertEqual(str(e), """\ File "%s", line 1 NName: foo ^ Syntax error""" % f.name)
def test_same(self): f1 = NamedTemporaryFile("wt", delete=False) try: f1.write("fofo") f1.close() f2 = NamedTemporaryFile("wt", delete=False) try: f2.write("fofo") f2.close() self.assertTrue(same_content(f1.name, f2.name)) finally: os.remove(f2.name) finally: os.remove(f1.name)
def run_with_tempfile(content, function, mode="w"): """Create a temporary file with the given content, and execute tbe given function after the temporary file has been closed. The file is guaranteed to be deleted whether function succeeds or not """ f = NamedTemporaryFile(mode=mode, delete=False) try: f.write(content) f.close() return function(f.name) finally: f.close() os.remove(f.name)
class TestParseError(unittest.TestCase): def setUp(self): self.f = NamedTemporaryFile(mode="w", delete=False) def tearDown(self): self.f.close() os.remove(self.f.name) def test_simple(self): text = """\ NName: foo """ self.assertRaises(ParseError, lambda : PackageDescription.from_string(text)) try: PackageDescription.from_string(text) raise AssertionError("Should raise here !") except ParseError, e: self.assertEqual(str(e), "yacc: Syntax error at line 1, Token(WORD, 'NName')")
class TestParseError(unittest.TestCase): def setUp(self): self.f = NamedTemporaryFile(mode="w", delete=False) def tearDown(self): self.f.close() os.remove(self.f.name) def test_simple(self): text = """\ NName: foo """ error_msg = """\ yacc: Syntax error at line 1, Token\(WORD, 'NName'\) \t'NName: foo'""" self.assertRaisesRegexp(ParseError, error_msg, lambda: PackageDescription.from_string(text)) def test_simple_filename(self): f = self.f f.write("NName: foo") f.flush() self.assertRaises(ParseError, lambda: PackageDescription.from_file(f.name)) def test_error_string(self): f = self.f f.write("NName: foo") f.flush() try: PackageDescription.from_file(f.name) raise AssertionError("Should raise here !") except ParseError: e = extract_exception() self.assertEqual( str(e), """\ File "%s", line 1 NName: foo ^ Syntax error""" % f.name)