Home | History | Annotate | Download | only in tests
      1 #!/usr/bin/env python
      2 # Copyright (c) 2013 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 import os
      6 import unittest
      7 import shutil
      8 import sys
      9 import tempfile
     10 
     11 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
     12 PARENT_DIR = os.path.dirname(SCRIPT_DIR)
     13 CHROME_SRC = os.path.dirname(os.path.dirname(os.path.dirname(PARENT_DIR)))
     14 MOCK_DIR = os.path.join(CHROME_SRC, "third_party", "pymock")
     15 
     16 sys.path.append(PARENT_DIR)
     17 sys.path.append(MOCK_DIR)
     18 
     19 import create_html
     20 import mock
     21 
     22 class TestCreateHtml(unittest.TestCase):
     23   def setUp(self):
     24     self.tempdir = None
     25 
     26   def tearDown(self):
     27     if self.tempdir:
     28       shutil.rmtree(self.tempdir)
     29 
     30   def testBadInput(self):
     31     # Non-existant file
     32     self.assertRaises(create_html.Error, create_html.main, ['foo.nexe'])
     33     # Existing file with wrong extension
     34     self.assertRaises(create_html.Error, create_html.main, [__file__])
     35     # Existing directory
     36     self.assertRaises(create_html.Error, create_html.main, [PARENT_DIR])
     37 
     38   def testCreatesOutput(self):
     39     self.tempdir = tempfile.mkdtemp("_sdktest")
     40     expected_html = os.path.join(self.tempdir, 'foo.html')
     41     nmf_file = os.path.join(self.tempdir, 'foo.nmf')
     42     with mock.patch('sys.stdout'):
     43       with mock.patch('os.path.exists'):
     44         with mock.patch('os.path.isfile'):
     45           options = mock.MagicMock(return_value=False)
     46           options.output = None
     47           create_html.CreateHTML([nmf_file], options)
     48     # Assert that the file was created
     49     self.assertTrue(os.path.exists(expected_html))
     50     # Assert that nothing else was created
     51     self.assertEqual(os.listdir(self.tempdir),
     52                      [os.path.basename(expected_html)])
     53 
     54 
     55 if __name__ == '__main__':
     56   unittest.main()
     57