File: test_encoding.py

package info (click to toggle)
python-fs 2.4.16-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,944 kB
  • sloc: python: 13,048; makefile: 226; sh: 3
file content (57 lines) | stat: -rw-r--r-- 2,151 bytes parent folder | download | duplicates (2)
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
from __future__ import unicode_literals

import os
import platform
import shutil
import six
import tempfile
import unittest

import fs
from fs.osfs import OSFS

if platform.system() != "Windows":

    @unittest.skipIf(platform.system() == "Darwin", "Bad unicode not possible on OSX")
    class TestEncoding(unittest.TestCase):

        TEST_FILENAME = b"foo\xb1bar"
        # fsdecode throws error on Windows
        TEST_FILENAME_UNICODE = fs.fsdecode(TEST_FILENAME)

        def setUp(self):
            dir_path = self.dir_path = tempfile.mkdtemp()
            if six.PY2:
                with open(os.path.join(dir_path, self.TEST_FILENAME), "wb") as f:
                    f.write(b"baz")
            else:
                with open(
                    os.path.join(dir_path, self.TEST_FILENAME_UNICODE), "wb"
                ) as f:
                    f.write(b"baz")

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

        def test_open(self):
            with OSFS(self.dir_path) as test_fs:
                self.assertTrue(test_fs.exists(self.TEST_FILENAME_UNICODE))
                self.assertTrue(test_fs.isfile(self.TEST_FILENAME_UNICODE))
                self.assertFalse(test_fs.isdir(self.TEST_FILENAME_UNICODE))
                with test_fs.open(self.TEST_FILENAME_UNICODE, "rb") as f:
                    self.assertEqual(f.read(), b"baz")
                self.assertEqual(test_fs.readtext(self.TEST_FILENAME_UNICODE), "baz")
                test_fs.remove(self.TEST_FILENAME_UNICODE)
                self.assertFalse(test_fs.exists(self.TEST_FILENAME_UNICODE))

        def test_listdir(self):
            with OSFS(self.dir_path) as test_fs:
                dirlist = test_fs.listdir("/")
                self.assertEqual(dirlist, [self.TEST_FILENAME_UNICODE])
                self.assertEqual(test_fs.readtext(dirlist[0]), "baz")

        def test_scandir(self):
            with OSFS(self.dir_path) as test_fs:
                for info in test_fs.scandir("/"):
                    self.assertIsInstance(info.name, six.text_type)
                    self.assertEqual(info.name, self.TEST_FILENAME_UNICODE)