diff options
Diffstat (limited to 'tests/test_tagfile.py')
| -rw-r--r-- | tests/test_tagfile.py | 93 |
1 files changed, 92 insertions, 1 deletions
diff --git a/tests/test_tagfile.py b/tests/test_tagfile.py index 371cc6ee..33197e6a 100644 --- a/tests/test_tagfile.py +++ b/tests/test_tagfile.py @@ -1,18 +1,26 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright (C) 2010 Michael Vogt <mvo@ubuntu.com> +# Copyright (C) 2012 Canonical Ltd. +# Author: Colin Watson <cjwatson@ubuntu.com> # # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. """Unit tests for verifying the correctness of apt_pkg.TagFile""" +from __future__ import print_function, unicode_literals + +import io import glob import os +import shutil +import sys +import tempfile import unittest from test_all import get_library_dir -import sys sys.path.insert(0, get_library_dir()) import apt_pkg @@ -20,6 +28,13 @@ import apt_pkg class TestTagFile(unittest.TestCase): """ test the apt_pkg.TagFile """ + def setUp(self): + apt_pkg.init() + self.temp_dir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.temp_dir) + def test_tag_file(self): basepath = os.path.dirname(__file__) tagfilepath = os.path.join(basepath, "./data/tagfile/*") @@ -38,5 +53,81 @@ class TestTagFile(unittest.TestCase): # Raises Type error self.assertRaises(TypeError, apt_pkg.TagFile, object()) + def test_utf8(self): + value = "Tést Persön <test@example.org>" + packages = os.path.join(self.temp_dir, "Packages") + with io.open(packages, "w", encoding="UTF-8") as packages_file: + print("Maintainer: %s" % value, file=packages_file) + print("", file=packages_file) + if sys.version < '3': + # In Python 2, test the traditional file interface. + with open(packages) as packages_file: + tagfile = apt_pkg.TagFile(packages_file) + tagfile.step() + self.assertEqual( + value.encode("UTF-8"), tagfile.section["Maintainer"]) + with io.open(packages, encoding="UTF-8") as packages_file: + tagfile = apt_pkg.TagFile(packages_file) + tagfile.step() + if sys.version < '3': + self.assertEqual( + value.encode("UTF-8"), tagfile.section["Maintainer"]) + else: + self.assertEqual(value, tagfile.section["Maintainer"]) + + def test_latin1(self): + value = "Tést Persön <test@example.org>" + packages = os.path.join(self.temp_dir, "Packages") + with io.open(packages, "w", encoding="ISO-8859-1") as packages_file: + print("Maintainer: %s" % value, file=packages_file) + print("", file=packages_file) + if sys.version < '3': + # In Python 2, test the traditional file interface. + with open(packages) as packages_file: + tagfile = apt_pkg.TagFile(packages_file) + tagfile.step() + self.assertEqual( + value.encode("ISO-8859-1"), tagfile.section["Maintainer"]) + with io.open(packages) as packages_file: + tagfile = apt_pkg.TagFile(packages_file, bytes=True) + tagfile.step() + self.assertEqual( + value.encode("ISO-8859-1"), tagfile.section["Maintainer"]) + if sys.version >= '3': + # In Python 3, TagFile can pick up the encoding of the file + # object. + with io.open(packages, encoding="ISO-8859-1") as packages_file: + tagfile = apt_pkg.TagFile(packages_file) + tagfile.step() + self.assertEqual(value, tagfile.section["Maintainer"]) + + def test_mixed(self): + value = "Tést Persön <test@example.org>" + packages = os.path.join(self.temp_dir, "Packages") + with io.open(packages, "w", encoding="UTF-8") as packages_file: + print("Maintainer: %s" % value, file=packages_file) + print("", file=packages_file) + with io.open(packages, "a", encoding="ISO-8859-1") as packages_file: + print("Maintainer: %s" % value, file=packages_file) + print("", file=packages_file) + if sys.version < '3': + # In Python 2, test the traditional file interface. + with open(packages) as packages_file: + tagfile = apt_pkg.TagFile(packages_file) + tagfile.step() + self.assertEqual( + value.encode("UTF-8"), tagfile.section["Maintainer"]) + tagfile.step() + self.assertEqual( + value.encode("ISO-8859-1"), tagfile.section["Maintainer"]) + with io.open(packages) as packages_file: + tagfile = apt_pkg.TagFile(packages_file, bytes=True) + tagfile.step() + self.assertEqual( + value.encode("UTF-8"), tagfile.section["Maintainer"]) + tagfile.step() + self.assertEqual( + value.encode("ISO-8859-1"), tagfile.section["Maintainer"]) + if __name__ == "__main__": unittest.main() |
