File: test_tempdir.py

package info (click to toggle)
duplicity 3.0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,492 kB
  • sloc: python: 25,523; sh: 976; ansic: 392; makefile: 83
file content (90 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download
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
79
80
81
82
83
84
85
86
87
88
89
90
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
#
# Copyright 2002 Ben Escoto
# Copyright 2007 Kenneth Loafman
#
# This file is part of duplicity.
#
# Duplicity is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# Duplicity is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


import os
import tempfile
import unittest

from duplicity import tempdir
from . import UnitTestCase


class TempDirTest(UnitTestCase):
    def test_all(self):
        td = tempdir.default()

        # are generated temp files unique?
        self.assertTrue(td.mktemp() != td.mktemp())

        # create and remove a temp dir
        dir = td.mktemp()  # pylint: disable=redefined-builtin
        os.mkdir(dir)
        os.rmdir(dir)

        # test mkstemp()
        fd, fname = td.mkstemp()
        os.close(fd)
        os.unlink(fname)
        td.forget(fname)

        # test mkstemp_file()
        fo, fname = td.mkstemp_file()
        fo.close()  # don't forget, leave to cleanup()

        # cleanup
        td.cleanup()

    def test_dirname(self):
        """
        test if we generated a dirname
        """
        td = tempdir.default()
        dirname = td.dir()
        self.assertTrue(dirname is not None)

        """
        test if duplicity's temp files are created in our temp dir
        """
        f1d, f1_name = tempdir.default().mkstemp()
        f1_dirname = os.path.dirname(f1_name)

        self.assertTrue(dirname == f1_dirname)

        """
        test if tempfile creates in our temp dir now as well by default
        """
        f2 = tempfile.NamedTemporaryFile()
        f2_dirname = os.path.dirname(f2.name)

        self.assertTrue(dirname == f2_dirname)

        # cleanup
        os.close(f1d)
        os.unlink(f1_name)
        td.forget(f1_name)
        f2.close()

        td.cleanup()


if __name__ == "__main__":
    unittest.main()