• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3"""
4Copyright 2014 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9Test url_utils.py
10"""
11
12# System-level imports
13import os
14import shutil
15import tempfile
16import unittest
17import urllib
18
19# Imports from within Skia
20import url_utils
21
22
23class UrlUtilsTest(unittest.TestCase):
24
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.
30    self.assertEquals(
31        url_utils.create_filepath_url(
32            '%sdir%sfile' % (os.path.sep, os.path.sep)),
33        'file:///dir/file')
34    # Pass relative filepath.
35    self.assertEquals(
36        url_utils.create_filepath_url(os.path.join('dir', 'file')),
37        'file://%s/dir/file' % urllib.pathname2url(os.getcwd()))
38
39  def test_copy_contents(self):
40    """Tests copy_contents(). """
41    contents = 'these are the contents'
42    tempdir_path = tempfile.mkdtemp()
43    try:
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,
53                                dest_path=dest_path,
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,
57                              dest_path=dest_path,
58                              create_subdirs_if_needed=True)
59      self.assertEquals(open(dest_path).read(), contents)
60    finally:
61      shutil.rmtree(tempdir_path)
62