Index: gm/rebaseline_server/url_or_path_test.py |
diff --git a/gm/rebaseline_server/url_or_path_test.py b/gm/rebaseline_server/url_or_path_test.py |
new file mode 100755 |
index 0000000000000000000000000000000000000000..30c43ea4d2ceabcd29e909ad4a991240d1917617 |
--- /dev/null |
+++ b/gm/rebaseline_server/url_or_path_test.py |
@@ -0,0 +1,126 @@ |
+#!/usr/bin/python |
+ |
+""" |
+Copyright 2014 Google Inc. |
+ |
+Use of this source code is governed by a BSD-style license that can be |
+found in the LICENSE file. |
+ |
+Test url_or_path.py |
+""" |
+ |
+# System-level imports |
+import os |
+import shutil |
+import tempfile |
+import unittest |
+import urllib |
+ |
+# Imports from within Skia |
+import url_or_path |
+ |
+class UrlOrPathTest(unittest.TestCase): |
+ |
+ def shortDescription(self): |
+ """Tell unittest framework to not print docstrings for test cases.""" |
+ return None |
+ |
+ def test_is_url(self): |
+ """Tests is_url(). """ |
+ self.assertTrue(url_or_path.is_url('http://1.2.3.4/dir/file')) |
+ self.assertTrue(url_or_path.is_url('https://1.2.3.4:8000/dir/file')) |
+ self.assertTrue(url_or_path.is_url('file://localhost/dir/file')) |
+ self.assertTrue(url_or_path.is_url('file:///dir/file')) |
+ self.assertFalse(url_or_path.is_url('file')) |
+ self.assertFalse(url_or_path.is_url(os.path.join('dir', 'file'))) |
+ self.assertFalse(url_or_path.is_url(os.path.abspath(os.path.join( |
+ 'dir', 'file')))) |
+ |
+ def test_join(self): |
+ """Tests join(). """ |
+ base_url = 'http://1.2.3.4/root' |
+ base_filepath = os.path.abspath('root') |
+ self.assertEquals(url_or_path.join(base_url, 'dir', 'file'), |
+ base_url + '/dir/file') |
+ self.assertEquals(url_or_path.join(base_filepath, 'dir', 'file'), |
+ os.path.join(base_filepath, 'dir', 'file')) |
+ |
+ def test_create_filepath_url(self): |
+ """Tests create_filepath_url(). """ |
+ with self.assertRaises(Exception): |
+ url_or_path.create_filepath_url('http://1.2.3.4/path') |
+ # Pass absolute filepath. |
+ self.assertEquals( |
+ url_or_path.create_filepath_url( |
+ '%sdir%sfile' % (os.path.sep, os.path.sep)), |
+ 'file:///dir/file') |
+ # Pass relative filepath. |
+ self.assertEquals( |
+ url_or_path.create_filepath_url(os.path.join('dir', 'file')), |
+ 'file://%s/dir/file' % urllib.pathname2url(os.getcwd())) |
+ |
+ def test_read_as_string(self): |
+ """Tests read_as_string(). |
+ |
+ This depends on create_filepath_url() working, |
+ but we test that separately above.""" |
+ contents = 'these are the contents' |
+ _, tempfile_path = tempfile.mkstemp() |
+ try: |
+ with open(tempfile_path, 'w') as tempfile_handle: |
+ tempfile_handle.write(contents) |
+ tempfile_url = url_or_path.create_filepath_url(tempfile_path) |
+ self.assertEquals(url_or_path.read_as_string(tempfile_path), contents) |
+ self.assertEquals(url_or_path.read_as_string(tempfile_url), contents) |
+ finally: |
+ os.remove(tempfile_path) |
+ # After the file was deleted, read_as_string() should raise an exception. |
+ with self.assertRaises(Exception): |
+ url_or_path.read_as_string(tempfile_path) |
+ with self.assertRaises(Exception): |
+ url_or_path.read_as_string(tempfile_url) |
+ |
+ def test_copy_contents(self): |
+ """Tests copy_contents(). |
+ |
+ This depends on read_as_string() and create_filepath_url() working, |
+ but we test those separately above.""" |
+ contents = 'more contents' |
+ tempdir_path = tempfile.mkdtemp() |
+ try: |
+ source_path = os.path.join(tempdir_path, 'source') |
+ source_url = url_or_path.create_filepath_url(source_path) |
+ with open(source_path, 'w') as source_handle: |
+ source_handle.write(contents) |
+ dest_path_1 = os.path.join(tempdir_path, 'new_subdir', 'dest1') |
+ dest_path_2 = os.path.join(tempdir_path, 'new_subdir', 'dest2') |
+ # Destination subdir does not exist, so copy_contents() should fail |
+ # if create_subdirs_if_needed is False. |
+ with self.assertRaises(Exception): |
+ url_or_path.copy_contents(source_path=source_path, |
+ dest_path=dest_path_1, |
+ create_subdirs_if_needed=False) |
+ with self.assertRaises(Exception): |
+ url_or_path.copy_contents(source_path=source_url, |
+ dest_path=dest_path_2, |
+ create_subdirs_if_needed=False) |
+ # If create_subdirs_if_needed is True, it should work. |
+ url_or_path.copy_contents(source_path=source_path, |
+ dest_path=dest_path_1, |
+ create_subdirs_if_needed=True) |
+ self.assertEquals(url_or_path.read_as_string(dest_path_1), contents) |
+ url_or_path.copy_contents(source_path=source_url, |
+ dest_path=dest_path_2, |
+ create_subdirs_if_needed=True) |
+ self.assertEquals(url_or_path.read_as_string(dest_path_2), contents) |
+ finally: |
+ shutil.rmtree(tempdir_path) |
+ |
+ |
+def main(): |
+ suite = unittest.TestLoader().loadTestsFromTestCase(UrlOrPathTest) |
+ unittest.TextTestRunner(verbosity=2).run(suite) |
+ |
+ |
+if __name__ == '__main__': |
+ main() |