#!/usr/bin/env python3 # 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 sys import tempfile import unittest _HERE_PATH = os.path.dirname(__file__) sys.path.append(os.path.join(_HERE_PATH, '..', '..')) sys.path.append(os.path.join(_HERE_PATH, '..')) from resources import svgo_presubmit from PRESUBMIT_test_mocks import MockInputApi, MockOutputApi, MockFile _OPTIMIZED_SVG = ( b'' + b'') _UNOPTIMIZED_SVG = (b''' ''') 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.assertEqual(len(results), 1) self.assertTrue(results[0].type == 'notify') self.assertTrue('svgo' in results[0].message) def testOptimizedSvg(self): self.assertEqual(len(self.check_contents(_OPTIMIZED_SVG)), 0) if __name__ == '__main__': unittest.main()