output_managerless_skia_gold_session_unittest.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env vpython3
  2. # Copyright 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. #pylint: disable=protected-access
  6. import os
  7. import re
  8. import sys
  9. import tempfile
  10. from typing import Any
  11. import unittest
  12. import six
  13. if six.PY2:
  14. import mock
  15. else:
  16. import unittest.mock as mock
  17. from pyfakefs import fake_filesystem_unittest
  18. from skia_gold_common import output_managerless_skia_gold_session as omsgs
  19. from skia_gold_common import skia_gold_properties
  20. from skia_gold_common import unittest_utils
  21. createSkiaGoldArgs = unittest_utils.createSkiaGoldArgs
  22. def assertArgWith(test: unittest.TestCase, arg_list: list, arg: Any,
  23. value: Any) -> None:
  24. i = arg_list.index(arg)
  25. test.assertEqual(arg_list[i + 1], value)
  26. class GpuSkiaGoldSessionDiffTest(fake_filesystem_unittest.TestCase):
  27. def setUp(self) -> None:
  28. self.setUpPyfakefs()
  29. self._working_dir = tempfile.mkdtemp()
  30. self._json_keys = tempfile.NamedTemporaryFile(delete=False).name
  31. @mock.patch.object(omsgs.OutputManagerlessSkiaGoldSession,
  32. '_RunCmdForRcAndOutput')
  33. def test_commandCommonArgs(self, cmd_mock: mock.MagicMock) -> None:
  34. cmd_mock.return_value = (None, None)
  35. args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=False)
  36. sgp = skia_gold_properties.SkiaGoldProperties(args)
  37. session = omsgs.OutputManagerlessSkiaGoldSession(self._working_dir,
  38. sgp,
  39. self._json_keys,
  40. 'corpus',
  41. instance='instance')
  42. session.Diff('name', 'png_file', None)
  43. call_args = cmd_mock.call_args[0][0]
  44. self.assertIn('diff', call_args)
  45. assertArgWith(self, call_args, '--corpus', 'corpus')
  46. # TODO(skbug.com/10610): Remove the -public once we go back to using the
  47. # non-public instance, or add a second test for testing that the correct
  48. # instance is chosen if we decide to support both depending on what the
  49. # user is authenticated for.
  50. assertArgWith(self, call_args, '--instance', 'instance-public')
  51. assertArgWith(self, call_args, '--input', 'png_file')
  52. assertArgWith(self, call_args, '--test', 'name')
  53. # TODO(skbug.com/10611): Re-add this assert and remove the check for the
  54. # absence of the directory once we switch back to using the proper working
  55. # directory.
  56. # assertArgWith(self, call_args, '--work-dir', self._working_dir)
  57. self.assertNotIn(self._working_dir, call_args)
  58. i = call_args.index('--out-dir')
  59. # The output directory should not be a subdirectory of the working
  60. # directory.
  61. self.assertNotIn(self._working_dir, call_args[i + 1])
  62. @mock.patch.object(omsgs.OutputManagerlessSkiaGoldSession, '_StoreDiffLinks')
  63. @mock.patch.object(omsgs.OutputManagerlessSkiaGoldSession,
  64. '_RunCmdForRcAndOutput')
  65. def test_explicitLocalPngDirectory(self, cmd_mock: mock.MagicMock, _) -> None:
  66. cmd_mock.return_value = (0, '')
  67. if sys.platform == 'win32':
  68. local_png_dir = 'c:\\tmp\\foo'
  69. else:
  70. local_png_dir = '/tmp/foo'
  71. args = createSkiaGoldArgs(git_revision='a',
  72. skia_gold_local_png_write_directory=local_png_dir)
  73. sgp = skia_gold_properties.SkiaGoldProperties(args)
  74. session = omsgs.OutputManagerlessSkiaGoldSession(self._working_dir, sgp,
  75. self._json_keys, '', '')
  76. _, _ = session.Diff('name', '', None)
  77. self.assertEqual(cmd_mock.call_count, 1)
  78. if six.PY3:
  79. call_args = cmd_mock.call_args.args[0]
  80. else:
  81. call_args = cmd_mock.call_args[0][0]
  82. self.assertIn('--out-dir', call_args)
  83. output_dir = call_args[call_args.index('--out-dir') + 1]
  84. # Directory should be a subdirectory of the directory we gave and be made
  85. # up of the image name and a timestamp.
  86. parent_dir, sub_dir = output_dir.rsplit(os.sep, 1)
  87. self.assertEqual(parent_dir, local_png_dir)
  88. sub_dir = os.path.normpath(sub_dir)
  89. self.assertIsNotNone(re.match(r'^name_\d+$', sub_dir))
  90. class OutputManagerlessSkiaGoldSessionStoreDiffLinksTest(
  91. fake_filesystem_unittest.TestCase):
  92. def setUp(self) -> None:
  93. self.setUpPyfakefs()
  94. self._working_dir = tempfile.mkdtemp()
  95. self._json_keys = tempfile.NamedTemporaryFile(delete=False).name
  96. def test_outputManagerNotNeeded(self) -> None:
  97. args = createSkiaGoldArgs(git_revision='a', local_pixel_tests=True)
  98. sgp = skia_gold_properties.SkiaGoldProperties(args)
  99. session = omsgs.OutputManagerlessSkiaGoldSession(self._working_dir, sgp,
  100. self._json_keys, '', '')
  101. input_filepath = os.path.join(self._working_dir, 'input-inputhash.png')
  102. with open(input_filepath, 'w') as f:
  103. f.write('')
  104. closest_filepath = os.path.join(self._working_dir,
  105. 'closest-closesthash.png')
  106. with open(closest_filepath, 'w') as f:
  107. f.write('')
  108. diff_filepath = os.path.join(self._working_dir, 'diff.png')
  109. with open(diff_filepath, 'w') as f:
  110. f.write('')
  111. session._StoreDiffLinks('foo', None, self._working_dir)
  112. self.assertEqual(session.GetGivenImageLink('foo'),
  113. 'file://' + input_filepath)
  114. self.assertEqual(session.GetClosestImageLink('foo'),
  115. 'file://' + closest_filepath)
  116. self.assertEqual(session.GetDiffImageLink('foo'), 'file://' + diff_filepath)
  117. if __name__ == '__main__':
  118. unittest.main(verbosity=2)