summaryrefslogtreecommitdiff
path: root/test/test_file_io.py
blob: 376095681978bbe66863dcd2ebc929c05ea77f59 (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
import unittest
import os
import shutil

import makesite
from test import path


class FileIOTest(unittest.TestCase):
    """Tests for file I/O functions."""

    def test_fread(self):
        text = 'foo\nbar\n'
        filepath = path.temppath('foo.txt')
        with open(filepath, 'w') as f:
            f.write(text)
        text_read = makesite.fread(filepath)
        os.remove(filepath)
        self.assertEqual(text_read, text)

    def test_fwrite(self):
        text = 'baz\nqux\n'
        filepath = path.temppath('foo.txt')
        makesite.fwrite(filepath, text)
        with open(filepath) as f:
            text_read = f.read()
        os.remove(filepath)
        self.assertEqual(text_read, text)

    def test_fwrite_makedir(self):
        text = 'baz\nqux\n'
        dirpath = path.temppath('foo', 'bar')
        filepath = os.path.join(dirpath, 'foo.txt')
        makesite.fwrite(filepath, text)
        with open(filepath) as f:
            text_read = f.read()
        self.assertTrue(os.path.isdir(dirpath))
        shutil.rmtree(path.temppath('foo'))
        self.assertEqual(text_read, text)