summaryrefslogtreecommitdiff
path: root/chromium/tools/resources/svgo_presubmit_test.py
blob: 6484e438d053629120788be38b2fa95162cdd89c (plain)
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
#!/usr/bin/env python
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import svgo_presubmit
import sys
import tempfile
import unittest


_HERE_PATH = os.path.dirname(__file__)
sys.path.append(os.path.join(_HERE_PATH, '..', '..'))

from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile

_OPTIMIZED_SVG = '''
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#757575"><path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"/></svg>
'''.strip()

_UNOPTIMIZED_SVG = '''
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#757575">
  <path d="M10 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z"></path>
</svg>
'''

class SvgPresubmitTest(unittest.TestCase):
  def tearDown(self):
    os.remove(self._tmp_file)

  def check_contents(self, file_contents):
    tmp_args = {'suffix': '.svg', 'dir': _HERE_PATH, 'delete': False}
    with tempfile.NamedTemporaryFile(**tmp_args) as f:
      self._tmp_file = f.name
      f.write(file_contents)

    input_api = MockInputApi()
    input_api.files = [MockFile(os.path.abspath(self._tmp_file), file_contents.splitlines())]
    input_api.presubmit_local_path = _HERE_PATH

    return svgo_presubmit.CheckOptimized(input_api, MockOutputApi())

  def testUnoptimizedSvg(self):
    results = self.check_contents(_UNOPTIMIZED_SVG)
    self.assertEquals(len(results), 1)
    self.assertTrue(results[0].type == 'notify')
    self.assertTrue('svgo' in results[0].message)

  def testOptimizedSvg(self):
    self.assertEquals(len(self.check_contents(_OPTIMIZED_SVG)), 0)


if __name__ == '__main__':
  unittest.main()