File size: 8,735 Bytes
859a779 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Michael Vogt <[email protected]>
# Copyright (C) 2012 Canonical Ltd.
# Author: Colin Watson <[email protected]>
#
# 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
libdir = get_library_dir()
if libdir:
sys.path.insert(0, libdir)
import apt_pkg
import testcommon
class TestOpenMaybeClearSigned(testcommon.TestCase):
def test_open_trivial(self):
basepath = os.path.dirname(__file__)
fd = apt_pkg.open_maybe_clear_signed_file(
os.path.join(basepath, "./data/test_debs/hello_2.5-1.dsc"))
with os.fdopen(fd) as f:
data = f.read()
self.assertTrue(data.startswith("Format: 1.0\n"))
def test_open_normal(self):
basepath = os.path.dirname(__file__)
fd = apt_pkg.open_maybe_clear_signed_file(
os.path.join(basepath, "./data/misc/foo_Release"))
with os.fdopen(fd) as f:
data = f.read()
self.assertTrue(data.startswith("Origin: Ubuntu\n"))
def xtest_open_does_not_exit(self):
with self.assertRaises(SystemError):
apt_pkg.open_maybe_clear_signed_file("does-not-exists")
class TestTagFile(testcommon.TestCase):
""" test the apt_pkg.TagFile """
def setUp(self):
testcommon.TestCase.setUp(self)
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/*")
# test once for compressed and uncompressed
for testfile in glob.glob(tagfilepath):
# test once using the open() method and once using the path
tagfile = apt_pkg.TagFile(testfile)
for i, stanza in enumerate(tagfile):
pass
self.assertEqual(i, 2)
with open(testfile) as f:
tagfile = apt_pkg.TagFile(f)
for i, stanza in enumerate(tagfile):
pass
self.assertEqual(i, 2)
def test_errors(self):
# Raises SystemError via lbiapt
self.assertRaises(SystemError, apt_pkg.TagFile, "not-there-no-no")
# Raises Type error
self.assertRaises(TypeError, apt_pkg.TagFile, object())
def test_utf8(self):
value = "Tést Persön <[email protected]>"
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 <[email protected]>"
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 <[email protected]>"
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"])
class TestTagSection(testcommon.TestCase):
""" test the apt_pkg.TagFile """
def setUp(self):
testcommon.TestCase.setUp(self)
self.temp_dir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.temp_dir)
def test_write(self):
ts = apt_pkg.TagSection("a: 1\nb: 2\nc: 3\n")
outpath = os.path.join(self.temp_dir, "test")
with io.open(outpath, "w") as outfile:
ts.write(outfile, [], [])
with io.open(outpath) as outfile:
self.assertEqual(outfile.read(), "a: 1\nb: 2\nc: 3\n")
def test_write_order(self):
ts = apt_pkg.TagSection("a: 1\nb: 2\nc: 3\n")
outpath = os.path.join(self.temp_dir, "test")
with io.open(outpath, "w") as outfile:
ts.write(outfile, ["a", "c", "b"], [])
with io.open(outpath) as outfile:
self.assertEqual(outfile.read(), "a: 1\nc: 3\nb: 2\n")
def test_write_invalid_order(self):
ts = apt_pkg.TagSection("a: 1\nb: 2\nc: 3\n")
outpath = os.path.join(self.temp_dir, "test")
with io.open(outpath, "w") as outfile:
self.assertRaises(TypeError, ts.write, outfile, ["a", 1, "b"], [])
def test_write_remove(self):
ts = apt_pkg.TagSection("a: 1\nb: 2\nc: 3\n")
outpath = os.path.join(self.temp_dir, "test")
with io.open(outpath, "w") as outfile:
ts.write(outfile, ["a", "c", "b"], [apt_pkg.TagRemove("a")])
with io.open(outpath) as outfile:
self.assertEqual(outfile.read(), "c: 3\nb: 2\n")
def test_write_rewrite(self):
ts = apt_pkg.TagSection("a: 1\nb: 2\nc: 3\n")
outpath = os.path.join(self.temp_dir, "test")
with io.open(outpath, "w") as outfile:
ts.write(outfile, ["a", "c", "b"], [apt_pkg.TagRewrite("a", "AA")])
with io.open(outpath) as outfile:
self.assertEqual(outfile.read(), u"a: AA\nc: 3\nb: 2\n")
def test_write_rename(self):
ts = apt_pkg.TagSection("a: 1\nb: 2\nc: 3\n")
outpath = os.path.join(self.temp_dir, "test")
with io.open(outpath, "w") as outfile:
ts.write(outfile, ["a", "z", "b"], [apt_pkg.TagRename("c", "z")])
with io.open(outpath) as outfile:
self.assertEqual(outfile.read(), "a: 1\nz: 3\nb: 2\n")
if __name__ == "__main__":
unittest.main()
|