metrics_xml_format_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. import os
  7. import sys
  8. import unittest
  9. from unittest import mock
  10. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  11. import gclient_paths_test
  12. import metrics_xml_format
  13. norm = lambda path: path.replace('/', os.sep)
  14. join = os.path.join
  15. class TestBase(gclient_paths_test.TestBase):
  16. def setUp(self):
  17. super().setUp()
  18. # os.path.abspath() doesn't seem to use os.path.getcwd() to compute
  19. # the abspath of a given path.
  20. #
  21. # This mock os.path.abspath such that it uses the mocked getcwd().
  22. mock.patch('os.path.abspath', self.abspath).start()
  23. # gclient_paths.GetPrimarysolutionPath() defaults to src.
  24. self.make_file_tree({'.gclient': ''})
  25. self.cwd = join(self.cwd, 'src')
  26. def abspath(self, path):
  27. if os.path.isabs(path):
  28. return path
  29. return join(self.getcwd(), path)
  30. class GetMetricsDirTest(TestBase):
  31. def testWithAbsolutePath(self):
  32. get = lambda path: metrics_xml_format.GetMetricsDir(norm(path))
  33. self.assertTrue(get('/src/tools/metrics/actions/abc.xml'))
  34. self.assertTrue(get('/src/tools/metrics/histograms/abc.xml'))
  35. self.assertTrue(get('/src/tools/metrics/structured/abc.xml'))
  36. self.assertTrue(get('/src/tools/metrics/ukm/abc.xml'))
  37. self.assertFalse(get('/src/tools/metrics/actions/next/abc.xml'))
  38. self.assertFalse(get('/src/tools/metrics/histograms/next/abc.xml'))
  39. self.assertFalse(get('/src/tools/metrics/structured/next/abc.xml'))
  40. self.assertFalse(get('/src/tools/metrics/ukm/next/abc.xml'))
  41. def testWithRelativePaths(self):
  42. get = lambda path: metrics_xml_format.GetMetricsDir(norm(path))
  43. self.cwd = join(self.cwd, 'tools')
  44. self.assertFalse(get('abc.xml'))
  45. self.assertTrue(get('metrics/actions/abc.xml'))
  46. class FindMetricsXMLFormatTool(TestBase):
  47. def testWithMetricsXML(self):
  48. findTool = metrics_xml_format.FindMetricsXMLFormatterTool
  49. self.assertEqual(
  50. findTool(norm('tools/metrics/actions/abc.xml')),
  51. join(self.getcwd(), norm('tools/metrics/actions/pretty_print.py')),
  52. )
  53. # same test, but with an absolute path.
  54. self.assertEqual(
  55. findTool(join(self.getcwd(),
  56. norm('tools/metrics/actions/abc.xml'))),
  57. join(self.getcwd(), norm('tools/metrics/actions/pretty_print.py')),
  58. )
  59. def testWthNonMetricsXML(self):
  60. findTool = metrics_xml_format.FindMetricsXMLFormatterTool
  61. self.assertEqual(findTool('tools/metrics/actions/next/abc.xml'), '')
  62. def testWithNonCheckout(self):
  63. findTool = metrics_xml_format.FindMetricsXMLFormatterTool
  64. self.cwd = self.root
  65. self.assertEqual(findTool('tools/metrics/actions/abc.xml'), '')
  66. def testWithDifferentCheckout(self):
  67. findTool = metrics_xml_format.FindMetricsXMLFormatterTool
  68. checkout2 = join(self.root, '..', self._testMethodName + '2', 'src')
  69. self.assertEqual(
  70. # this is the case the tool was given a file path that is located
  71. # in a different checkout folder.
  72. findTool(join(checkout2, norm('tools/metrics/actions/abc.xml'))),
  73. '',
  74. )
  75. def testSupportedHistogramsXML(self):
  76. findTool = metrics_xml_format.FindMetricsXMLFormatterTool
  77. self.assertEqual(
  78. findTool(norm('tools/metrics/histograms/enums.xml')),
  79. join(self.getcwd(),
  80. norm('tools/metrics/histograms/pretty_print.py')),
  81. )
  82. def testNotSupportedHistogramsXML(self):
  83. findTool = metrics_xml_format.FindMetricsXMLFormatterTool
  84. self.assertEqual(findTool(norm('tools/metrics/histograms/NO.xml')), '')
  85. if __name__ == '__main__':
  86. unittest.main()