test_packaging.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import os
  5. import unittest
  6. from cuddlefish import packaging
  7. from cuddlefish.bunch import Bunch
  8. tests_path = os.path.abspath(os.path.dirname(__file__))
  9. static_files_path = os.path.join(tests_path, 'static-files')
  10. def get_configs(pkg_name, dirname='static-files'):
  11. root_path = os.path.join(tests_path, dirname)
  12. pkg_path = os.path.join(root_path, 'packages', pkg_name)
  13. if not (os.path.exists(pkg_path) and os.path.isdir(pkg_path)):
  14. raise Exception('path does not exist: %s' % pkg_path)
  15. target_cfg = packaging.get_config_in_dir(pkg_path)
  16. pkg_cfg = packaging.build_config(root_path, target_cfg)
  17. deps = packaging.get_deps_for_targets(pkg_cfg, [pkg_name])
  18. build = packaging.generate_build_for_target(
  19. pkg_cfg=pkg_cfg,
  20. target=pkg_name,
  21. deps=deps,
  22. is_running_tests=True,
  23. )
  24. return Bunch(target_cfg=target_cfg, pkg_cfg=pkg_cfg, build=build)
  25. class PackagingTests(unittest.TestCase):
  26. def test_bug_588661(self):
  27. configs = get_configs('foo', 'bug-588661-files')
  28. self.assertEqual(configs.build.loader,
  29. 'foo/lib/foo-loader.js')
  30. def test_bug_614712(self):
  31. configs = get_configs('commonjs-naming', 'bug-614712-files')
  32. packages = configs.pkg_cfg.packages
  33. base = os.path.join(tests_path, 'bug-614712-files', 'packages')
  34. self.assertEqual(packages['original-naming'].tests,
  35. [os.path.join(base, 'original-naming', 'tests')])
  36. self.assertEqual(packages['commonjs-naming'].tests,
  37. [os.path.join(base, 'commonjs-naming', 'test')])
  38. def test_basic(self):
  39. configs = get_configs('aardvark')
  40. packages = configs.pkg_cfg.packages
  41. self.assertTrue('addon-sdk' in packages)
  42. self.assertTrue('aardvark' in packages)
  43. self.assertTrue('addon-sdk' in packages.aardvark.dependencies)
  44. self.assertEqual(packages['addon-sdk'].loader, 'lib/sdk/loader/cuddlefish.js')
  45. self.assertTrue(packages.aardvark.main == 'main')
  46. self.assertTrue(packages.aardvark.version == "1.0")
  47. class PackagePath(unittest.TestCase):
  48. def test_packagepath(self):
  49. root_path = os.path.join(tests_path, 'static-files')
  50. pkg_path = os.path.join(root_path, 'packages', 'minimal')
  51. target_cfg = packaging.get_config_in_dir(pkg_path)
  52. pkg_cfg = packaging.build_config(root_path, target_cfg)
  53. base_packages = set(pkg_cfg.packages.keys())
  54. ppath = [os.path.join(tests_path, 'bug-611495-files')]
  55. pkg_cfg2 = packaging.build_config(root_path, target_cfg, packagepath=ppath)
  56. all_packages = set(pkg_cfg2.packages.keys())
  57. self.assertEqual(sorted(["jspath-one"]),
  58. sorted(all_packages - base_packages))
  59. class Directories(unittest.TestCase):
  60. # for bug 652227
  61. packages_path = os.path.join(tests_path, "bug-652227-files", "packages")
  62. def get_config(self, pkg_name):
  63. pkg_path = os.path.join(tests_path, "bug-652227-files", "packages",
  64. pkg_name)
  65. return packaging.get_config_in_dir(pkg_path)
  66. def test_explicit_lib(self):
  67. # package.json provides .lib
  68. p = self.get_config('explicit-lib')
  69. self.assertEqual(os.path.abspath(p.lib[0]),
  70. os.path.abspath(os.path.join(self.packages_path,
  71. "explicit-lib",
  72. "alt2-lib")))
  73. def test_directories_lib(self):
  74. # package.json provides .directories.lib
  75. p = self.get_config('explicit-dir-lib')
  76. self.assertEqual(os.path.abspath(p.lib[0]),
  77. os.path.abspath(os.path.join(self.packages_path,
  78. "explicit-dir-lib",
  79. "alt-lib")))
  80. def test_lib(self):
  81. # package.json is empty, but lib/ exists
  82. p = self.get_config("default-lib")
  83. self.assertEqual(os.path.abspath(p.lib[0]),
  84. os.path.abspath(os.path.join(self.packages_path,
  85. "default-lib",
  86. "lib")))
  87. def test_root(self):
  88. # package.json is empty, no lib/, so files are in root
  89. p = self.get_config('default-root')
  90. self.assertEqual(os.path.abspath(p.lib[0]),
  91. os.path.abspath(os.path.join(self.packages_path,
  92. "default-root")))
  93. def test_locale(self):
  94. # package.json is empty, but locale/ exists and should be used
  95. p = self.get_config("default-locale")
  96. self.assertEqual(os.path.abspath(p.locale),
  97. os.path.abspath(os.path.join(self.packages_path,
  98. "default-locale",
  99. "locale")))
  100. if __name__ == "__main__":
  101. unittest.main()