summaryrefslogtreecommitdiff
path: root/systrace/catapult/common/py_utils/py_utils/file_util_unittest.py
blob: 4bb19a14225f24b81bf1436177f0f24becbdb019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import errno
import os
import shutil
import tempfile
import unittest

from py_utils import file_util


class FileUtilTest(unittest.TestCase):

  def setUp(self):
    self._tempdir = tempfile.mkdtemp()

  def tearDown(self):
    shutil.rmtree(self._tempdir)

  def testCopySimple(self):
    source_path = os.path.join(self._tempdir, 'source')
    with open(source_path, 'w') as f:
      f.write('data')

    dest_path = os.path.join(self._tempdir, 'dest')

    self.assertFalse(os.path.exists(dest_path))
    file_util.CopyFileWithIntermediateDirectories(source_path, dest_path)
    self.assertTrue(os.path.exists(dest_path))
    self.assertEqual('data', open(dest_path, 'r').read())

  def testCopyMakeDirectories(self):
    source_path = os.path.join(self._tempdir, 'source')
    with open(source_path, 'w') as f:
      f.write('data')

    dest_path = os.path.join(self._tempdir, 'path', 'to', 'dest')

    self.assertFalse(os.path.exists(dest_path))
    file_util.CopyFileWithIntermediateDirectories(source_path, dest_path)
    self.assertTrue(os.path.exists(dest_path))
    self.assertEqual('data', open(dest_path, 'r').read())

  def testCopyOverwrites(self):
    source_path = os.path.join(self._tempdir, 'source')
    with open(source_path, 'w') as f:
      f.write('source_data')

    dest_path = os.path.join(self._tempdir, 'dest')
    with open(dest_path, 'w') as f:
      f.write('existing_data')

    file_util.CopyFileWithIntermediateDirectories(source_path, dest_path)
    self.assertEqual('source_data', open(dest_path, 'r').read())

  def testRaisesError(self):
    source_path = os.path.join(self._tempdir, 'source')
    with open(source_path, 'w') as f:
      f.write('data')

    dest_path = ""
    with self.assertRaises(OSError) as cm:
      file_util.CopyFileWithIntermediateDirectories(source_path, dest_path)
      self.assertEqual(errno.ENOENT, cm.exception.error_code)