4 Copyright 2014 Google Inc.
6 Use of this source code is governed by a BSD-style license that can be
7 found in the LICENSE file.
12 # System-level imports
19 # Imports from within Skia
23 class UrlUtilsTest(unittest.TestCase):
25 def test_create_filepath_url(self):
26 """Tests create_filepath_url(). """
27 with self.assertRaises(Exception):
28 url_utils.create_filepath_url('http://1.2.3.4/path')
29 # Pass absolute filepath.
31 url_utils.create_filepath_url(
32 '%sdir%sfile' % (os.path.sep, os.path.sep)),
34 # Pass relative filepath.
36 url_utils.create_filepath_url(os.path.join('dir', 'file')),
37 'file://%s/dir/file' % urllib.pathname2url(os.getcwd()))
39 def test_copy_contents(self):
40 """Tests copy_contents(). """
41 contents = 'these are the contents'
42 tempdir_path = tempfile.mkdtemp()
44 source_path = os.path.join(tempdir_path, 'source')
45 source_url = url_utils.create_filepath_url(source_path)
46 with open(source_path, 'w') as source_handle:
47 source_handle.write(contents)
48 dest_path = os.path.join(tempdir_path, 'new_subdir', 'dest')
49 # Destination subdir does not exist, so copy_contents() should fail
50 # if create_subdirs_if_needed is False.
51 with self.assertRaises(Exception):
52 url_utils.copy_contents(source_url=source_url,
54 create_subdirs_if_needed=False)
55 # If create_subdirs_if_needed is True, it should work.
56 url_utils.copy_contents(source_url=source_url,
58 create_subdirs_if_needed=True)
59 self.assertEquals(open(dest_path).read(), contents)
61 shutil.rmtree(tempdir_path)