summaryrefslogtreecommitdiff
path: root/morphlib/savefile_tests.py
blob: dc11086c0cf6264094a84d5cc1c16b4b70b008a8 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (C) 2012,2015  Codethink Limited
#
# 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; version 2 of the License.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.


import os
import shutil
import tempfile
import unittest

import savefile


class SaveFileTests(unittest.TestCase):

    def cat(self, filename):
        with open(filename) as f:
            return f.read()

    def mkfile(self, filename, contents):
        with open(filename, 'w') as f:
            f.write(contents)

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()
        self.basename = 'filename'
        self.filename = os.path.join(self.tempdir, self.basename)

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

    def test_there_are_no_files_initially(self):
        self.assertEqual(os.listdir(self.tempdir), [])

    def test_sets_real_filename(self):
        f = savefile.SaveFile(self.filename, 'w')
        self.assertEqual(f.real_filename, self.filename)

    def test_sets_name_to_temporary_name(self):
        f = savefile.SaveFile(self.filename, 'w')
        self.assertNotEqual(f.name, self.filename)

    def test_saves_new_file(self):
        f = savefile.SaveFile(self.filename, 'w')
        f.write('foo')
        f.close()
        self.assertEqual(os.listdir(self.tempdir), [self.basename])
        self.assertEqual(self.cat(self.filename), 'foo')

    def test_overwrites_existing_file(self):
        self.mkfile(self.filename, 'yo!')
        f = savefile.SaveFile(self.filename, 'w')
        f.write('foo')
        f.close()
        self.assertEqual(os.listdir(self.tempdir), [self.basename])
        self.assertEqual(self.cat(self.filename), 'foo')

    def test_leaves_no_file_after_aborted_new_file(self):
        f = savefile.SaveFile(self.filename, 'w')
        f.write('foo')
        f.abort()
        self.assertEqual(os.listdir(self.tempdir), [])

    def test_leaves_original_file_after_aborted_overwrite(self):
        self.mkfile(self.filename, 'yo!')
        f = savefile.SaveFile(self.filename, 'w')
        f.write('foo')
        f.abort()
        self.assertEqual(os.listdir(self.tempdir), [self.basename])
        self.assertEqual(self.cat(self.filename), 'yo!')

    def test_saves_normally_with_with(self):
        with savefile.SaveFile(self.filename, 'w') as f:
            f.write('foo')
        self.assertEqual(os.listdir(self.tempdir), [self.basename])
        self.assertEqual(self.cat(self.filename), 'foo')

    def test_saves_normally_with_exception_within_with(self):
        try:
            with savefile.SaveFile(self.filename, 'w') as f:
                f.write('foo')
                raise Exception()
        except Exception:
            pass
        self.assertEqual(os.listdir(self.tempdir), [self.basename])
        self.assertEqual(self.cat(self.filename), 'foo')