/usr/share/pyshared/germinate/tests/test_seeds.py is in python-germinate 2.8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | #! /usr/bin/env python
# -*- coding: UTF-8 -*-
"""Unit tests for germinate.seeds."""
# Copyright (C) 2012 Canonical Ltd.
#
# Germinate is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Germinate is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Germinate; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301, USA.
import io
import os
import textwrap
from germinate.seeds import (
AtomicFile,
Seed,
SeedStructure,
SingleSeedStructure,
)
from germinate.tests.helpers import TestCase
class TestAtomicFile(TestCase):
def test_creates_file(self):
"""AtomicFile creates the named file with the requested contents."""
self.useTempDir()
with AtomicFile("foo") as test:
test.write("string")
with open("foo") as handle:
self.assertEqual("string", handle.read())
def test_removes_dot_new(self):
"""AtomicFile does not leave .new files lying around."""
self.useTempDir()
with AtomicFile("foo"):
pass
self.assertFalse(os.path.exists("foo.new"))
class TestSeed(TestCase):
def setUp(self):
self.addSeed("collection.dist", "test")
self.addSeedPackage("collection.dist", "test", "foo")
self.addSeed("collection.dist", "test2")
self.addSeedPackage("collection.dist", "test2", "foo")
self.addSeed("collection.dist", "test3")
self.addSeedPackage("collection.dist", "test3", "bar")
def test_init_no_bzr(self):
"""__init__ can open a seed from a collection without bzr."""
seed = Seed(
["file://%s" % self.seeds_dir], ["collection.dist"], "test")
self.assertEqual("test", seed.name)
self.assertEqual("file://%s" % self.seeds_dir, seed.base)
self.assertEqual("collection.dist", seed.branch)
self.assertEqual(" * foo\n", seed.text)
def test_behaves_as_file(self):
"""A Seed context can be read from as a file object."""
seed = Seed(
["file://%s" % self.seeds_dir], ["collection.dist"], "test")
with seed as seed_file:
lines = list(seed_file)
self.assertTrue(1, len(lines))
self.assertTrue(" * foo\n", lines[0])
def test_equal_if_same_contents(self):
"""Two Seed objects with the same text contents are equal."""
one = Seed(
["file://%s" % self.seeds_dir], ["collection.dist"], "test")
two = Seed(
["file://%s" % self.seeds_dir], ["collection.dist"], "test2")
self.assertEqual(one, two)
def test_not_equal_if_different_contents(self):
"""Two Seed objects with different text contents are not equal."""
one = Seed(
["file://%s" % self.seeds_dir], ["collection.dist"], "test")
three = Seed(
["file://%s" % self.seeds_dir], ["collection.dist"], "test3")
self.assertNotEqual(one, three)
class TestSingleSeedStructure(TestCase):
def test_basic(self):
"""A SingleSeedStructure object has the correct basic properties."""
branch = "collection.dist"
self.addSeed(branch, "base")
self.addSeed(branch, "desktop", parents=["base"])
seed = Seed(["file://%s" % self.seeds_dir], branch, "STRUCTURE")
with seed as seed_file:
structure = SingleSeedStructure(branch, seed_file)
self.assertEqual(["base", "desktop"], structure.seed_order)
self.assertEqual({"base": [], "desktop": ["base"]}, structure.inherit)
self.assertEqual([branch], structure.branches)
self.assertEqual(["base:", "desktop: base"], structure.lines)
self.assertEqual(set(), structure.features)
def test_include(self):
"""SingleSeedStructure parses the "include" directive correctly."""
branch = "collection.dist"
self.addStructureLine(branch, "include other.dist")
seed = Seed(["file://%s" % self.seeds_dir], branch, "STRUCTURE")
with seed as seed_file:
structure = SingleSeedStructure(branch, seed_file)
self.assertEqual([branch, "other.dist"], structure.branches)
def test_feature(self):
"""SingleSeedStructure parses the "feature" directive correctly."""
branch = "collection.dist"
self.addStructureLine(branch, "feature follow-recommends")
seed = Seed(["file://%s" % self.seeds_dir], branch, "STRUCTURE")
with seed as seed_file:
structure = SingleSeedStructure(branch, seed_file)
self.assertEqual(set(["follow-recommends"]), structure.features)
class TestSeedStructure(TestCase):
def test_basic(self):
"""A SeedStructure object has the correct basic properties."""
branch = "collection.dist"
self.addSeed(branch, "base")
self.addSeedPackage(branch, "base", "base-package")
self.addSeed(branch, "desktop", parents=["base"])
self.addSeedPackage(branch, "desktop", "desktop-package")
structure = self.openSeedStructure(branch)
self.assertEqual(branch, structure.branch)
self.assertEqual(set(), structure.features)
self.assertEqual("desktop", structure.supported)
self.assertEqual(["base", "desktop"], structure.names)
self.assertEqual({"base": [], "desktop": ["base"]}, structure._inherit)
def test_dict(self):
"""A SeedStructure can be treated as a dictionary of seeds."""
branch = "collection.dist"
self.addSeed(branch, "base")
self.addSeedPackage(branch, "base", "base-package")
structure = self.openSeedStructure(branch)
self.assertEqual(1, len(structure))
self.assertEqual(["base"], list(structure))
self.assertEqual("base", structure["base"].name)
self.assertEqual(" * base-package\n", structure["base"].text)
def test_multiple(self):
"""SeedStructure follows "include" links to other seed collections."""
one = "one.dist"
two = "two.dist"
self.addSeed(one, "base")
self.addSeedPackage(one, "base", "base-package")
self.addStructureLine(two, "include one.dist")
self.addSeed(two, "desktop")
self.addSeedPackage(two, "desktop", "desktop-package")
structure = self.openSeedStructure(two)
self.assertEqual(two, structure.branch)
self.assertEqual(one, structure["base"].branch)
self.assertEqual(" * base-package\n", structure["base"].text)
self.assertEqual(two, structure["desktop"].branch)
self.assertEqual(" * desktop-package\n", structure["desktop"].text)
def test_later_branches_override_earlier_branches(self):
"""Seeds from later branches override seeds from earlier branches."""
one = "one.dist"
two = "two.dist"
self.addSeed(one, "base")
self.addSeedPackage(one, "base", "base-package")
self.addSeed(one, "desktop")
self.addSeedPackage(one, "desktop", "desktop-package-one")
self.addStructureLine(two, "include one.dist")
self.addSeed(two, "desktop")
self.addSeedPackage(two, "desktop", "desktop-package-two")
structure = self.openSeedStructure(two)
self.assertEqual(["base", "desktop"], sorted(structure))
self.assertEqual(" * desktop-package-two\n", structure["desktop"].text)
def test_limit(self):
"""SeedStructure.limit restricts the set of seed names."""
branch = "collection.dist"
self.addSeed(branch, "one")
self.addSeedPackage(branch, "one", "one")
self.addSeed(branch, "two", parents=["one"])
self.addSeedPackage(branch, "two", "two")
self.addSeed(branch, "three")
self.addSeedPackage(branch, "three", "three")
self.addSeed(branch, "four")
self.addSeedPackage(branch, "four", "four")
structure = self.openSeedStructure(branch)
self.assertEqual(
sorted(["one", "two", "three", "four"]), sorted(structure.names))
structure.limit(["two", "three"])
self.assertEqual(
sorted(["one", "two", "three"]), sorted(structure.names))
def test_add(self):
"""SeedStructure.add adds a custom seed."""
branch = "collection.dist"
self.addSeed(branch, "base")
self.addSeedPackage(branch, "base", "base")
structure = self.openSeedStructure(branch)
structure.add("custom", [" * custom-one", " * custom-two"], "base")
self.assertIn("custom", structure)
self.assertIn("custom", structure.names)
self.assertIn("custom", structure._inherit)
self.assertEqual(["base"], structure._inherit["custom"])
self.assertEqual("custom", structure["custom"].name)
self.assertIsNone(structure["custom"].base)
self.assertIsNone(structure["custom"].branch)
self.assertEqual(
" * custom-one\n * custom-two\n", structure["custom"].text)
def test_write(self):
"""SeedStructure.write writes the text of STRUCTURE."""
branch = "collection.dist"
self.addSeed(branch, "one")
self.addSeedPackage(branch, "one", "one")
self.addSeed(branch, "two", parents=["one"])
self.addSeedPackage(branch, "two", "two")
structure = self.openSeedStructure(branch)
structure.write("structure")
with open("structure") as structure_file:
self.assertEqual("one:\ntwo: one\n", structure_file.read())
def test_write_dot(self):
"""SeedStructure.write_dot writes an appropriate dot file."""
branch = "collection.dist"
self.addSeed(branch, "one")
self.addSeedPackage(branch, "one", "one")
self.addSeed(branch, "two", parents=["one"])
self.addSeedPackage(branch, "two", "two")
structure = self.openSeedStructure(branch)
structure.write_dot("structure.dot")
with open("structure.dot") as structure_dot_file:
self.assertEqual(textwrap.dedent("""\
digraph structure {
node [color=lightblue2, style=filled];
"one" -> "two";
}
"""), structure_dot_file.read())
def test_write_seed_text(self):
"""SeedStructure.write_seed_text writes the text of a seed."""
branch = "collection.dist"
self.addSeed(branch, "one")
self.addSeedPackage(branch, "one", "one-package")
self.addSeed(branch, "two")
self.addSeedPackage(branch, "two", "two-package")
structure = self.openSeedStructure(branch)
structure.write_seed_text("one.seedtext", "one")
with open("one.seedtext") as seed_file:
self.assertEqual(" * one-package\n", seed_file.read())
def test_write_seed_text_utf8(self):
"""SeedStructure.write_seed_text handles UTF-8 text in seeds."""
branch = "collection.dist"
self.addSeed(branch, "base")
self.addSeedPackage(branch, "base", u"base # äöü")
structure = self.openSeedStructure(branch)
structure.write_seed_text("base.seedtext", "base")
with io.open("base.seedtext", encoding="UTF-8") as seed_file:
self.assertEqual(u" * base # äöü\n", seed_file.read())
|