File: test_util_atomic.py

package info (click to toggle)
quodlibet 4.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,228 kB
  • sloc: python: 89,728; sh: 381; xml: 110; makefile: 91
file content (102 lines) | stat: -rw-r--r-- 3,232 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
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright 2013 Christoph Reiter, Dino Miniutti
#
# This program 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.

import os
import stat
import shutil

from tests import TestCase, mkdtemp

from quodlibet.util.atomic import atomic_save


class Tatomic_save(TestCase):
    def setUp(self):
        self.dir = mkdtemp()

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

    def test_basic(self):
        filename = os.path.join(self.dir, "foo.txt")

        with open(filename, "wb") as fobj:
            fobj.write(b"nope")

        with atomic_save(filename, "wb") as fobj:
            fobj.write(b"foo")
            temp_name = fobj.name

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"foo")

        assert not os.path.exists(temp_name)
        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])

    def test_non_exist(self):
        filename = os.path.join(self.dir, "foo.txt")

        with atomic_save(filename, "wb") as fobj:
            fobj.write(b"foo")
            temp_name = fobj.name

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"foo")

        assert not os.path.exists(temp_name)
        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])

    def test_readonly(self):
        filename = os.path.join(self.dir, "foo.txt")

        with open(filename, "wb") as fobj:
            fobj.write(b"nope")

        dir_mode = os.stat(self.dir).st_mode
        file_mode = os.stat(filename).st_mode
        # setting directory permissions doesn't work under Windows, so make
        # the file read only, so the rename fails. On the other hand marking
        # the file read only doesn't make rename fail on unix, so make the
        # directory read only as well.
        os.chmod(filename, stat.S_IREAD)
        os.chmod(self.dir, stat.S_IREAD)
        try:
            with self.assertRaises(OSError):
                with atomic_save(filename, "wb") as fobj:
                    fobj.write(b"foo")
        finally:
            # restore permissions
            os.chmod(self.dir, dir_mode)
            os.chmod(filename, file_mode)

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"nope")

        self.assertEqual(os.listdir(self.dir), [os.path.basename(filename)])

    def test_symbolic_link(self):
        filename = os.path.join(self.dir, "foo.txt")
        symlink = os.path.join(self.dir, "foo.link")

        os.symlink(filename, symlink)

        with open(filename, "wb") as fobj:
            fobj.write(b"nope")

        with atomic_save(symlink, "wb") as fobj:
            fobj.write(b"foo")
            temp_name = fobj.name

        with open(filename, "rb") as fobj:
            self.assertEqual(fobj.read(), b"foo")

        assert not os.path.exists(temp_name)
        self.assertEqual(
            sorted(os.listdir(self.dir)),
            sorted([os.path.basename(filename), os.path.basename(symlink)]),
        )
        assert os.path.islink(symlink)