test_mozbuild_reading.py 4.3 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. from __future__ import unicode_literals
  5. import os
  6. import sys
  7. import unittest
  8. from mozunit import main
  9. from mozbuild.base import MozbuildObject
  10. from mozpack.files import FileFinder
  11. from mozbuild.frontend.context import Files
  12. from mozbuild.frontend.reader import (
  13. BuildReader,
  14. EmptyConfig,
  15. )
  16. class TestMozbuildReading(unittest.TestCase):
  17. # This hack is needed to appease running in automation.
  18. def setUp(self):
  19. self._old_env = dict(os.environ)
  20. os.environ.pop('MOZCONFIG', None)
  21. os.environ.pop('MOZ_OBJDIR', None)
  22. def tearDown(self):
  23. os.environ.clear()
  24. os.environ.update(self._old_env)
  25. def _mozbuilds(self, reader):
  26. if not hasattr(self, '_mozbuild_paths'):
  27. self._mozbuild_paths = set(reader.all_mozbuild_paths())
  28. return self._mozbuild_paths
  29. @unittest.skip('failing in SpiderMonkey builds')
  30. def test_filesystem_traversal_reading(self):
  31. """Reading moz.build according to filesystem traversal works.
  32. We attempt to read every known moz.build file via filesystem traversal.
  33. If this test fails, it means that metadata extraction will fail.
  34. """
  35. mb = MozbuildObject.from_environment(detect_virtualenv_mozinfo=False)
  36. config = mb.config_environment
  37. reader = BuildReader(config)
  38. all_paths = self._mozbuilds(reader)
  39. paths, contexts = reader.read_relevant_mozbuilds(all_paths)
  40. self.assertEqual(set(paths), all_paths)
  41. self.assertGreaterEqual(len(contexts), len(paths))
  42. def test_filesystem_traversal_no_config(self):
  43. """Reading moz.build files via filesystem traversal mode with no build config.
  44. This is similar to the above test except no build config is applied.
  45. This will likely fail in more scenarios than the above test because a
  46. lot of moz.build files assumes certain variables are present.
  47. """
  48. here = os.path.abspath(os.path.dirname(__file__))
  49. root = os.path.normpath(os.path.join(here, '..', '..'))
  50. config = EmptyConfig(root)
  51. reader = BuildReader(config)
  52. all_paths = self._mozbuilds(reader)
  53. paths, contexts = reader.read_relevant_mozbuilds(all_paths)
  54. self.assertEqual(set(paths.keys()), all_paths)
  55. self.assertGreaterEqual(len(contexts), len(paths))
  56. def test_orphan_file_patterns(self):
  57. if sys.platform == 'win32':
  58. raise unittest.SkipTest('failing on windows builds')
  59. mb = MozbuildObject.from_environment(detect_virtualenv_mozinfo=False)
  60. try:
  61. config = mb.config_environment
  62. except Exception as e:
  63. if e.message == 'config.status not available. Run configure.':
  64. raise unittest.SkipTest('failing without config.status')
  65. raise
  66. if config.substs['MOZ_BUILD_APP'] == 'js':
  67. raise unittest.SkipTest('failing in Spidermonkey builds')
  68. reader = BuildReader(config)
  69. all_paths = self._mozbuilds(reader)
  70. _, contexts = reader.read_relevant_mozbuilds(all_paths)
  71. finder = FileFinder(config.topsrcdir, find_executables=False,
  72. ignore=['obj*'])
  73. def pattern_exists(pat):
  74. return [p for p in finder.find(pat)] != []
  75. for ctx in contexts:
  76. if not isinstance(ctx, Files):
  77. continue
  78. relsrcdir = ctx.relsrcdir
  79. if not pattern_exists(os.path.join(relsrcdir, ctx.pattern)):
  80. self.fail("The pattern '%s' in a Files() entry in "
  81. "'%s' corresponds to no files in the tree.\n"
  82. "Please update this entry." %
  83. (ctx.pattern, ctx.main_path))
  84. test_files = ctx['IMPACTED_TESTS'].files
  85. for p in test_files:
  86. if not pattern_exists(os.path.relpath(p.full_path, config.topsrcdir)):
  87. self.fail("The pattern '%s' in a dependent tests entry "
  88. "in '%s' corresponds to no files in the tree.\n"
  89. "Please update this entry." %
  90. (p, ctx.main_path))
  91. if __name__ == '__main__':
  92. main()