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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import mozunit
from mozpack.copier import FileCopier, FileRegistry
from mozpack.manifests import InstallManifest, UnreadableInstallManifest
from mozpack.test.test_files import TestWithTmpDir
class TestInstallManifest(TestWithTmpDir):
def test_construct(self):
m = InstallManifest()
self.assertEqual(len(m), 0)
def test_malformed(self):
f = self.tmppath("manifest")
open(f, "w").write("junk\n")
with self.assertRaises(UnreadableInstallManifest):
InstallManifest(f)
def test_adds(self):
m = InstallManifest()
m.add_link("s_source", "s_dest")
m.add_copy("c_source", "c_dest")
m.add_required_exists("e_dest")
m.add_optional_exists("o_dest")
m.add_pattern_link("ps_base", "ps/*", "ps_dest")
m.add_pattern_copy("pc_base", "pc/**", "pc_dest")
m.add_preprocess("p_source", "p_dest", "p_source.pp")
m.add_content("content", "content")
self.assertEqual(len(m), 8)
self.assertIn("s_dest", m)
self.assertIn("c_dest", m)
self.assertIn("p_dest", m)
self.assertIn("e_dest", m)
self.assertIn("o_dest", m)
self.assertIn("content", m)
with self.assertRaises(ValueError):
m.add_link("s_other", "s_dest")
with self.assertRaises(ValueError):
m.add_copy("c_other", "c_dest")
with self.assertRaises(ValueError):
m.add_preprocess("p_other", "p_dest", "p_other.pp")
with self.assertRaises(ValueError):
m.add_required_exists("e_dest")
with self.assertRaises(ValueError):
m.add_optional_exists("o_dest")
with self.assertRaises(ValueError):
m.add_pattern_link("ps_base", "ps/*", "ps_dest")
with self.assertRaises(ValueError):
m.add_pattern_copy("pc_base", "pc/**", "pc_dest")
with self.assertRaises(ValueError):
m.add_content("content", "content")
def _get_test_manifest(self):
m = InstallManifest()
m.add_link(self.tmppath("s_source"), "s_dest")
m.add_copy(self.tmppath("c_source"), "c_dest")
m.add_preprocess(
self.tmppath("p_source"),
"p_dest",
self.tmppath("p_source.pp"),
"#",
{"FOO": "BAR", "BAZ": "QUX"},
)
m.add_required_exists("e_dest")
m.add_optional_exists("o_dest")
m.add_pattern_link("ps_base", "*", "ps_dest")
m.add_pattern_copy("pc_base", "**", "pc_dest")
m.add_content("the content\non\nmultiple lines", "content")
return m
def test_serialization(self):
m = self._get_test_manifest()
p = self.tmppath("m")
m.write(path=p)
self.assertTrue(os.path.isfile(p))
with open(p) as fh:
c = fh.read()
self.assertEqual(c.count("\n"), 9)
lines = c.splitlines()
self.assertEqual(len(lines), 9)
self.assertEqual(lines[0], "5")
m2 = InstallManifest(path=p)
self.assertEqual(m, m2)
p2 = self.tmppath("m2")
m2.write(path=p2)
with open(p2) as fh:
c2 = fh.read()
self.assertEqual(c, c2)
def test_populate_registry(self):
m = self._get_test_manifest()
r = FileRegistry()
m.populate_registry(r)
self.assertEqual(len(r), 6)
self.assertEqual(
r.paths(), ["c_dest", "content", "e_dest", "o_dest", "p_dest", "s_dest"]
)
def test_pattern_expansion(self):
source = self.tmppath("source")
os.mkdir(source)
os.mkdir("%s/base" % source)
os.mkdir("%s/base/foo" % source)
with open("%s/base/foo/file1" % source, "a"):
pass
with open("%s/base/foo/file2" % source, "a"):
pass
m = InstallManifest()
m.add_pattern_link("%s/base" % source, "**", "dest")
c = FileCopier()
m.populate_registry(c)
self.assertEqual(c.paths(), ["dest/foo/file1", "dest/foo/file2"])
def test_write_expand_pattern(self):
source = self.tmppath("source")
os.mkdir(source)
os.mkdir("%s/base" % source)
os.mkdir("%s/base/foo" % source)
with open("%s/base/foo/file1" % source, "a"):
pass
with open("%s/base/foo/file2" % source, "a"):
pass
m = InstallManifest()
m.add_pattern_link("%s/base" % source, "**", "dest")
track = self.tmppath("track")
m.write(path=track, expand_pattern=True)
m = InstallManifest(path=track)
self.assertEqual(
sorted(dest for dest in m._dests), ["dest/foo/file1", "dest/foo/file2"]
)
def test_or(self):
m1 = self._get_test_manifest()
orig_length = len(m1)
m2 = InstallManifest()
m2.add_link("s_source2", "s_dest2")
m2.add_copy("c_source2", "c_dest2")
m1 |= m2
self.assertEqual(len(m2), 2)
self.assertEqual(len(m1), orig_length + 2)
self.assertIn("s_dest2", m1)
self.assertIn("c_dest2", m1)
def test_copier_application(self):
dest = self.tmppath("dest")
os.mkdir(dest)
to_delete = self.tmppath("dest/to_delete")
with open(to_delete, "a"):
pass
with open(self.tmppath("s_source"), "w") as fh:
fh.write("symlink!")
with open(self.tmppath("c_source"), "w") as fh:
fh.write("copy!")
with open(self.tmppath("p_source"), "w") as fh:
fh.write("#define FOO 1\npreprocess!")
with open(self.tmppath("dest/e_dest"), "a"):
pass
with open(self.tmppath("dest/o_dest"), "a"):
pass
m = self._get_test_manifest()
c = FileCopier()
m.populate_registry(c)
result = c.copy(dest)
self.assertTrue(os.path.exists(self.tmppath("dest/s_dest")))
self.assertTrue(os.path.exists(self.tmppath("dest/c_dest")))
self.assertTrue(os.path.exists(self.tmppath("dest/p_dest")))
self.assertTrue(os.path.exists(self.tmppath("dest/e_dest")))
self.assertTrue(os.path.exists(self.tmppath("dest/o_dest")))
self.assertTrue(os.path.exists(self.tmppath("dest/content")))
self.assertFalse(os.path.exists(to_delete))
with open(self.tmppath("dest/s_dest")) as fh:
self.assertEqual(fh.read(), "symlink!")
with open(self.tmppath("dest/c_dest")) as fh:
self.assertEqual(fh.read(), "copy!")
with open(self.tmppath("dest/p_dest")) as fh:
self.assertEqual(fh.read(), "preprocess!")
self.assertEqual(
result.updated_files,
set(
self.tmppath(p)
for p in ("dest/s_dest", "dest/c_dest", "dest/p_dest", "dest/content")
),
)
self.assertEqual(
result.existing_files,
set([self.tmppath("dest/e_dest"), self.tmppath("dest/o_dest")]),
)
self.assertEqual(result.removed_files, {to_delete})
self.assertEqual(result.removed_directories, set())
def test_preprocessor(self):
manifest = self.tmppath("m")
deps = self.tmppath("m.pp")
dest = self.tmppath("dest")
include = self.tmppath("p_incl")
with open(include, "w") as fh:
fh.write("#define INCL\n")
time = os.path.getmtime(include) - 3
os.utime(include, (time, time))
with open(self.tmppath("p_source"), "w") as fh:
fh.write("#ifdef FOO\n#if BAZ == QUX\nPASS1\n#endif\n#endif\n")
fh.write("#ifdef DEPTEST\nPASS2\n#endif\n")
fh.write("#include p_incl\n#ifdef INCLTEST\nPASS3\n#endif\n")
time = os.path.getmtime(self.tmppath("p_source")) - 3
os.utime(self.tmppath("p_source"), (time, time))
# Create and write a manifest with the preprocessed file, then apply it.
# This should write out our preprocessed file.
m = InstallManifest()
m.add_preprocess(
self.tmppath("p_source"), "p_dest", deps, "#", {"FOO": "BAR", "BAZ": "QUX"}
)
m.write(path=manifest)
m = InstallManifest(path=manifest)
c = FileCopier()
m.populate_registry(c)
c.copy(dest)
self.assertTrue(os.path.exists(self.tmppath("dest/p_dest")))
with open(self.tmppath("dest/p_dest")) as fh:
self.assertEqual(fh.read(), "PASS1\n")
# Create a second manifest with the preprocessed file, then apply it.
# Since this manifest does not exist on the disk, there should not be a
# dependency on it, and the preprocessed file should not be modified.
m2 = InstallManifest()
m2.add_preprocess(
self.tmppath("p_source"), "p_dest", deps, "#", {"DEPTEST": True}
)
c = FileCopier()
m2.populate_registry(c)
result = c.copy(dest)
self.assertFalse(self.tmppath("dest/p_dest") in result.updated_files)
self.assertTrue(self.tmppath("dest/p_dest") in result.existing_files)
# Write out the second manifest, then load it back in from the disk.
# This should add the dependency on the manifest file, so our
# preprocessed file should be regenerated with the new defines.
# We also set the mtime on the destination file back, so it will be
# older than the manifest file.
m2.write(path=manifest)
time = os.path.getmtime(manifest) - 1
os.utime(self.tmppath("dest/p_dest"), (time, time))
m2 = InstallManifest(path=manifest)
c = FileCopier()
m2.populate_registry(c)
self.assertTrue(c.copy(dest))
with open(self.tmppath("dest/p_dest")) as fh:
self.assertEqual(fh.read(), "PASS2\n")
# Set the time on the manifest back, so it won't be picked up as
# modified in the next test
time = os.path.getmtime(manifest) - 1
os.utime(manifest, (time, time))
# Update the contents of a file included by the source file. This should
# cause the destination to be regenerated.
with open(include, "w") as fh:
fh.write("#define INCLTEST\n")
time = os.path.getmtime(include) - 1
os.utime(self.tmppath("dest/p_dest"), (time, time))
c = FileCopier()
m2.populate_registry(c)
self.assertTrue(c.copy(dest))
with open(self.tmppath("dest/p_dest")) as fh:
self.assertEqual(fh.read(), "PASS2\nPASS3\n")
def test_preprocessor_dependencies(self):
manifest = self.tmppath("m")
deps = self.tmppath("m.pp")
dest = self.tmppath("dest")
source = self.tmppath("p_source")
destfile = self.tmppath("dest/p_dest")
include = self.tmppath("p_incl")
os.mkdir(dest)
with open(source, "w") as fh:
fh.write("#define SRC\nSOURCE\n")
time = os.path.getmtime(source) - 3
os.utime(source, (time, time))
with open(include, "w") as fh:
fh.write("INCLUDE\n")
time = os.path.getmtime(source) - 3
os.utime(include, (time, time))
# Create and write a manifest with the preprocessed file.
m = InstallManifest()
m.add_preprocess(source, "p_dest", deps, "#", {"FOO": "BAR", "BAZ": "QUX"})
m.write(path=manifest)
time = os.path.getmtime(source) - 5
os.utime(manifest, (time, time))
# Now read the manifest back in, and apply it. This should write out
# our preprocessed file.
m = InstallManifest(path=manifest)
c = FileCopier()
m.populate_registry(c)
self.assertTrue(c.copy(dest))
with open(destfile) as fh:
self.assertEqual(fh.read(), "SOURCE\n")
# Next, modify the source to #INCLUDE another file.
with open(source, "w") as fh:
fh.write("SOURCE\n#include p_incl\n")
time = os.path.getmtime(source) - 1
os.utime(destfile, (time, time))
# Apply the manifest, and confirm that it also reads the newly included
# file.
m = InstallManifest(path=manifest)
c = FileCopier()
m.populate_registry(c)
c.copy(dest)
with open(destfile) as fh:
self.assertEqual(fh.read(), "SOURCE\nINCLUDE\n")
# Set the time on the source file back, so it won't be picked up as
# modified in the next test.
time = os.path.getmtime(source) - 1
os.utime(source, (time, time))
# Now, modify the include file (but not the original source).
with open(include, "w") as fh:
fh.write("INCLUDE MODIFIED\n")
time = os.path.getmtime(include) - 1
os.utime(destfile, (time, time))
# Apply the manifest, and confirm that the change to the include file
# is detected. That should cause the preprocessor to run again.
m = InstallManifest(path=manifest)
c = FileCopier()
m.populate_registry(c)
c.copy(dest)
with open(destfile) as fh:
self.assertEqual(fh.read(), "SOURCE\nINCLUDE MODIFIED\n")
# ORing an InstallManifest should copy file dependencies
m = InstallManifest()
m |= InstallManifest(path=manifest)
c = FileCopier()
m.populate_registry(c)
e = c._files["p_dest"]
self.assertEqual(e.extra_depends, [manifest])
def test_add_entries_from(self):
source = self.tmppath("source")
os.mkdir(source)
os.mkdir("%s/base" % source)
os.mkdir("%s/base/foo" % source)
with open("%s/base/foo/file1" % source, "a"):
pass
with open("%s/base/foo/file2" % source, "a"):
pass
m = InstallManifest()
m.add_pattern_link("%s/base" % source, "**", "dest")
p = InstallManifest()
p.add_entries_from(m)
self.assertEqual(len(p), 1)
c = FileCopier()
p.populate_registry(c)
self.assertEqual(c.paths(), ["dest/foo/file1", "dest/foo/file2"])
q = InstallManifest()
q.add_entries_from(m, base="target")
self.assertEqual(len(q), 1)
d = FileCopier()
q.populate_registry(d)
self.assertEqual(d.paths(), ["target/dest/foo/file1", "target/dest/foo/file2"])
# Some of the values in an InstallManifest include destination
# information that is present in the keys. Verify that we can
# round-trip serialization.
r = InstallManifest()
r.add_entries_from(m)
r.add_entries_from(m, base="target")
self.assertEqual(len(r), 2)
temp_path = self.tmppath("temp_path")
r.write(path=temp_path)
s = InstallManifest(path=temp_path)
e = FileCopier()
s.populate_registry(e)
self.assertEqual(
e.paths(),
[
"dest/foo/file1",
"dest/foo/file2",
"target/dest/foo/file1",
"target/dest/foo/file2",
],
)
if __name__ == "__main__":
mozunit.main()
|