-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_encoding.py
115 lines (86 loc) · 3.23 KB
/
test_encoding.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
"""Tests for encodings.py"""
import os
import stat
from flaky import flaky
import pytest
from spyder.utils.encoding import is_text_file, get_coding, write
from spyder.py3compat import to_text_string, PY2
if PY2:
import pathlib2 as pathlib
else:
import pathlib
__location__ = os.path.realpath(os.path.join(os.getcwd(),
os.path.dirname(__file__)))
@pytest.mark.order(1)
@pytest.mark.skipif(os.name == 'nt' and PY2, reason='Fails on Win!')
def test_symlinks(tmpdir):
"""
Check that modifying symlinks files changes source file and keeps symlinks.
"""
base_dir = tmpdir.mkdir("symlinks")
base_file = base_dir.join("symlinks_text.txt")
base_file_path = to_text_string(base_file)
# Write base file
write("Some text for symlink", base_file_path)
# Create symlink
symlink_file = pathlib.Path(base_dir.join(
'link-to-symlinks_text.txt'))
symlink_file.symlink_to(base_file_path)
symlink_file_path = to_text_string(symlink_file)
# Assert the symlink was created
assert os.path.islink(symlink_file_path)
# Write using the symlink
encoding = write("New text for symlink", symlink_file_path)
# Assert symlink is valid and contents of the file
assert os.path.islink(symlink_file_path)
assert base_file.read_text(encoding) == symlink_file.read_text(encoding)
assert symlink_file.read_text(encoding) == 'New text for symlink'
def test_permissions(tmpdir):
"""Check that file permissions are preserved."""
p_file = tmpdir.mkdir("permissions").join("permissions_text.txt")
p_file = to_text_string(p_file)
# Write file and define execution permissions
write("Some text", p_file)
st = os.stat(p_file)
mode = st.st_mode | stat.S_IEXEC
os.chmod(p_file, mode)
old_mode = os.stat(p_file).st_mode
# Write the file and check permissions
write("Some text and more", p_file)
new_mode = os.stat(p_file).st_mode
assert old_mode == new_mode
@flaky(max_runs=10)
def test_timestamp(tmpdir):
"""Check that the modification timestamp is preserved."""
tmp_file = tmpdir.mkdir("timestamp").join('test_file.txt')
tmp_file = to_text_string(tmp_file)
# Write a file
write("Test text", tmp_file)
st = os.stat(tmp_file)
actual_creation_time = st.st_atime
# Write the file and check that creation time is preserved.
write('New text', tmp_file)
creation_time = os.stat(tmp_file).st_atime
assert actual_creation_time == creation_time
def test_is_text_file(tmpdir):
p = tmpdir.mkdir("sub").join("random_text.txt")
p.write("Some random text")
assert is_text_file(str(p)) == True
@pytest.mark.parametrize(
'expected_encoding, text_file',
[('utf-8', 'utf-8.txt'),
('windows-1252', 'windows-1252.txt'),
('ascii', 'ascii.txt'),
('Big5', 'Big5.txt'),
('KOI8-R', 'KOI8-R.txt'),
])
def test_files_encodings(expected_encoding, text_file):
with open(os.path.join(__location__, text_file), 'rb') as f:
text = f.read()
assert get_coding(text).lower() == expected_encoding.lower()
if __name__ == '__main__':
pytest.main()