summaryrefslogtreecommitdiff
path: root/test/test_path.py
blob: 91e76e8ce09ad989d61daf718014718920ea5da5 (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
67
68
69
70
71
72
73
74
75
76
77
78
import unittest
import os
import shutil

from test import path

class PathTest(unittest.TestCase):
    def test_temppath(self):
        self.assertTrue(path.temppath())

    def test_move_existing_file(self):
        src = os.path.join(path.temppath(), 'foo.txt')
        dst = os.path.join(path.temppath(), 'bar.txt')
        with open(src, 'w') as f:
            f.write('foo')

        path.move(src, dst)
        self.assertFalse(os.path.isfile(src))
        self.assertTrue(os.path.isfile(dst))

        with open(dst) as f:
            text = f.read()

        os.remove(dst)

        self.assertEqual(text, 'foo')

    def test_move_missing_file(self):
        src = os.path.join(path.temppath(), 'foo.txt')
        dst = os.path.join(path.temppath(), 'bar.txt')
        path.move(src, dst)
        self.assertFalse(os.path.isfile(src))
        self.assertFalse(os.path.isfile(dst))

    def test_move_file_cleanup(self):
        src = os.path.join(path.temppath(), 'foo.txt')
        dst = os.path.join(path.temppath(), 'bar.txt')
        with open(dst, 'w') as f:
            f.write('foo')
        path.move(src, dst)
        self.assertFalse(os.path.isfile(src))
        self.assertFalse(os.path.isfile(dst))

    def test_move_existing_dir(self):
        src = os.path.join(path.temppath(), 'foo')
        srcf = os.path.join(src, 'foo.txt')
        dst = os.path.join(path.temppath(), 'bar')
        dstf = os.path.join(dst, 'foo.txt')

        os.makedirs(src)
        with open(srcf, 'w') as f:
            f.write('foo')

        path.move(src, dst)
        self.assertFalse(os.path.isdir(src))
        self.assertTrue(os.path.isdir(dst))

        with open(dstf) as f:
            text = f.read()

        shutil.rmtree(dst)

        self.assertEqual(text, 'foo')

    def test_move_missing_dir(self):
        src = os.path.join(path.temppath(), 'foo')
        dst = os.path.join(path.temppath(), 'bar')
        path.move(src, dst)
        self.assertFalse(os.path.isdir(src))
        self.assertFalse(os.path.isdir(dst))

    def test_move_dir_cleanup(self):
        src = os.path.join(path.temppath(), 'foo')
        dst = os.path.join(path.temppath(), 'bar')
        os.makedirs(dst)
        path.move(src, dst)
        self.assertFalse(os.path.isdir(src))
        self.assertFalse(os.path.isdir(dst))