OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 """ |
| 4 Copyright 2014 Google Inc. |
| 5 |
| 6 Use of this source code is governed by a BSD-style license that can be |
| 7 found in the LICENSE file. |
| 8 |
| 9 Test url_or_path.py |
| 10 """ |
| 11 |
| 12 # System-level imports |
| 13 import os |
| 14 import shutil |
| 15 import tempfile |
| 16 import unittest |
| 17 import urllib |
| 18 |
| 19 # Imports from within Skia |
| 20 import url_or_path |
| 21 |
| 22 class UrlOrPathTest(unittest.TestCase): |
| 23 |
| 24 def shortDescription(self): |
| 25 """Tell unittest framework to not print docstrings for test cases.""" |
| 26 return None |
| 27 |
| 28 def test_is_url(self): |
| 29 """Tests is_url(). """ |
| 30 self.assertTrue(url_or_path.is_url('http://1.2.3.4/dir/file')) |
| 31 self.assertTrue(url_or_path.is_url('https://1.2.3.4:8000/dir/file')) |
| 32 self.assertTrue(url_or_path.is_url('file://localhost/dir/file')) |
| 33 self.assertTrue(url_or_path.is_url('file:///dir/file')) |
| 34 self.assertFalse(url_or_path.is_url('file')) |
| 35 self.assertFalse(url_or_path.is_url(os.path.join('dir', 'file'))) |
| 36 self.assertFalse(url_or_path.is_url(os.path.abspath(os.path.join( |
| 37 'dir', 'file')))) |
| 38 |
| 39 def test_join(self): |
| 40 """Tests join(). """ |
| 41 base_url = 'http://1.2.3.4/root' |
| 42 base_filepath = os.path.abspath('root') |
| 43 self.assertEquals(url_or_path.join(base_url, 'dir', 'file'), |
| 44 base_url + '/dir/file') |
| 45 self.assertEquals(url_or_path.join(base_filepath, 'dir', 'file'), |
| 46 os.path.join(base_filepath, 'dir', 'file')) |
| 47 |
| 48 def test_create_filepath_url(self): |
| 49 """Tests create_filepath_url(). """ |
| 50 with self.assertRaises(Exception): |
| 51 url_or_path.create_filepath_url('http://1.2.3.4/path') |
| 52 # Pass absolute filepath. |
| 53 self.assertEquals( |
| 54 url_or_path.create_filepath_url( |
| 55 '%sdir%sfile' % (os.path.sep, os.path.sep)), |
| 56 'file:///dir/file') |
| 57 # Pass relative filepath. |
| 58 self.assertEquals( |
| 59 url_or_path.create_filepath_url(os.path.join('dir', 'file')), |
| 60 'file://%s/dir/file' % urllib.pathname2url(os.getcwd())) |
| 61 |
| 62 def test_read_as_string(self): |
| 63 """Tests read_as_string(). |
| 64 |
| 65 This depends on create_filepath_url() working, |
| 66 but we test that separately above.""" |
| 67 contents = 'these are the contents' |
| 68 _, tempfile_path = tempfile.mkstemp() |
| 69 try: |
| 70 with open(tempfile_path, 'w') as tempfile_handle: |
| 71 tempfile_handle.write(contents) |
| 72 tempfile_url = url_or_path.create_filepath_url(tempfile_path) |
| 73 self.assertEquals(url_or_path.read_as_string(tempfile_path), contents) |
| 74 self.assertEquals(url_or_path.read_as_string(tempfile_url), contents) |
| 75 finally: |
| 76 os.remove(tempfile_path) |
| 77 # After the file was deleted, read_as_string() should raise an exception. |
| 78 with self.assertRaises(Exception): |
| 79 url_or_path.read_as_string(tempfile_path) |
| 80 with self.assertRaises(Exception): |
| 81 url_or_path.read_as_string(tempfile_url) |
| 82 |
| 83 def test_copy_contents(self): |
| 84 """Tests copy_contents(). |
| 85 |
| 86 This depends on read_as_string() and create_filepath_url() working, |
| 87 but we test those separately above.""" |
| 88 contents = 'more contents' |
| 89 tempdir_path = tempfile.mkdtemp() |
| 90 try: |
| 91 source_path = os.path.join(tempdir_path, 'source') |
| 92 source_url = url_or_path.create_filepath_url(source_path) |
| 93 with open(source_path, 'w') as source_handle: |
| 94 source_handle.write(contents) |
| 95 dest_path_1 = os.path.join(tempdir_path, 'new_subdir', 'dest1') |
| 96 dest_path_2 = os.path.join(tempdir_path, 'new_subdir', 'dest2') |
| 97 # Destination subdir does not exist, so copy_contents() should fail |
| 98 # if create_subdirs_if_needed is False. |
| 99 with self.assertRaises(Exception): |
| 100 url_or_path.copy_contents(source_path=source_path, |
| 101 dest_path=dest_path_1, |
| 102 create_subdirs_if_needed=False) |
| 103 with self.assertRaises(Exception): |
| 104 url_or_path.copy_contents(source_path=source_url, |
| 105 dest_path=dest_path_2, |
| 106 create_subdirs_if_needed=False) |
| 107 # If create_subdirs_if_needed is True, it should work. |
| 108 url_or_path.copy_contents(source_path=source_path, |
| 109 dest_path=dest_path_1, |
| 110 create_subdirs_if_needed=True) |
| 111 self.assertEquals(url_or_path.read_as_string(dest_path_1), contents) |
| 112 url_or_path.copy_contents(source_path=source_url, |
| 113 dest_path=dest_path_2, |
| 114 create_subdirs_if_needed=True) |
| 115 self.assertEquals(url_or_path.read_as_string(dest_path_2), contents) |
| 116 finally: |
| 117 shutil.rmtree(tempdir_path) |
| 118 |
| 119 |
| 120 def main(): |
| 121 suite = unittest.TestLoader().loadTestsFromTestCase(UrlOrPathTest) |
| 122 unittest.TextTestRunner(verbosity=2).run(suite) |
| 123 |
| 124 |
| 125 if __name__ == '__main__': |
| 126 main() |
OLD | NEW |