forked from google/yapf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_resources_test.py
396 lines (314 loc) · 13.3 KB
/
file_resources_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# -*- coding: utf-8 -*-
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for yapf.file_resources."""
import contextlib
import os
import shutil
import tempfile
import unittest
from yapf.yapflib import errors
from yapf.yapflib import file_resources
from yapf.yapflib import py3compat
from yapftests import utils
@contextlib.contextmanager
def _restore_working_dir():
curdir = os.getcwd()
try:
yield
finally:
os.chdir(curdir)
class GetExcludePatternsForDir(unittest.TestCase):
def setUp(self): # pylint: disable=g-missing-super-call
self.test_tmpdir = tempfile.mkdtemp()
def tearDown(self): # pylint: disable=g-missing-super-call
shutil.rmtree(self.test_tmpdir)
def _make_test_dir(self, name):
fullpath = os.path.normpath(os.path.join(self.test_tmpdir, name))
os.makedirs(fullpath)
return fullpath
def test_get_exclude_file_patterns(self):
local_ignore_file = os.path.join(self.test_tmpdir, '.yapfignore')
ignore_patterns = ['temp/**/*.py', 'temp2/*.py']
with open(local_ignore_file, 'w') as f:
f.writelines('\n'.join(ignore_patterns))
self.assertEqual(
sorted(file_resources.GetExcludePatternsForDir(self.test_tmpdir)),
sorted(ignore_patterns))
class GetDefaultStyleForDirTest(unittest.TestCase):
def setUp(self): # pylint: disable=g-missing-super-call
self.test_tmpdir = tempfile.mkdtemp()
def tearDown(self): # pylint: disable=g-missing-super-call
shutil.rmtree(self.test_tmpdir)
def test_no_local_style(self):
test_file = os.path.join(self.test_tmpdir, 'file.py')
style_name = file_resources.GetDefaultStyleForDir(test_file)
self.assertEqual(style_name, 'pep8')
def test_no_local_style_custom_default(self):
test_file = os.path.join(self.test_tmpdir, 'file.py')
style_name = file_resources.GetDefaultStyleForDir(
test_file, default_style='custom-default')
self.assertEqual(style_name, 'custom-default')
def test_with_local_style(self):
# Create an empty .style.yapf file in test_tmpdir
style_file = os.path.join(self.test_tmpdir, '.style.yapf')
open(style_file, 'w').close()
test_filename = os.path.join(self.test_tmpdir, 'file.py')
self.assertEqual(style_file,
file_resources.GetDefaultStyleForDir(test_filename))
test_filename = os.path.join(self.test_tmpdir, 'dir1', 'file.py')
self.assertEqual(style_file,
file_resources.GetDefaultStyleForDir(test_filename))
def _touch_files(filenames):
for name in filenames:
open(name, 'a').close()
class GetCommandLineFilesTest(unittest.TestCase):
def setUp(self): # pylint: disable=g-missing-super-call
self.test_tmpdir = tempfile.mkdtemp()
self.old_dir = os.getcwd()
def tearDown(self): # pylint: disable=g-missing-super-call
shutil.rmtree(self.test_tmpdir)
os.chdir(self.old_dir)
def _make_test_dir(self, name):
fullpath = os.path.normpath(os.path.join(self.test_tmpdir, name))
os.makedirs(fullpath)
return fullpath
def test_find_files_not_dirs(self):
tdir1 = self._make_test_dir('test1')
tdir2 = self._make_test_dir('test2')
file1 = os.path.join(tdir1, 'testfile1.py')
file2 = os.path.join(tdir2, 'testfile2.py')
_touch_files([file1, file2])
self.assertEqual(
file_resources.GetCommandLineFiles([file1, file2],
recursive=False,
exclude=None), [file1, file2])
self.assertEqual(
file_resources.GetCommandLineFiles([file1, file2],
recursive=True,
exclude=None), [file1, file2])
def test_nonrecursive_find_in_dir(self):
tdir1 = self._make_test_dir('test1')
tdir2 = self._make_test_dir('test1/foo')
file1 = os.path.join(tdir1, 'testfile1.py')
file2 = os.path.join(tdir2, 'testfile2.py')
_touch_files([file1, file2])
self.assertRaises(
errors.YapfError,
file_resources.GetCommandLineFiles,
command_line_file_list=[tdir1],
recursive=False,
exclude=None)
def test_recursive_find_in_dir(self):
tdir1 = self._make_test_dir('test1')
tdir2 = self._make_test_dir('test2/testinner/')
tdir3 = self._make_test_dir('test3/foo/bar/bas/xxx')
files = [
os.path.join(tdir1, 'testfile1.py'),
os.path.join(tdir2, 'testfile2.py'),
os.path.join(tdir3, 'testfile3.py'),
]
_touch_files(files)
self.assertEqual(
sorted(
file_resources.GetCommandLineFiles([self.test_tmpdir],
recursive=True,
exclude=None)), sorted(files))
def test_recursive_find_in_dir_with_exclude(self):
tdir1 = self._make_test_dir('test1')
tdir2 = self._make_test_dir('test2/testinner/')
tdir3 = self._make_test_dir('test3/foo/bar/bas/xxx')
files = [
os.path.join(tdir1, 'testfile1.py'),
os.path.join(tdir2, 'testfile2.py'),
os.path.join(tdir3, 'testfile3.py'),
]
_touch_files(files)
self.assertEqual(
sorted(
file_resources.GetCommandLineFiles([self.test_tmpdir],
recursive=True,
exclude=['*test*3.py'])),
sorted([
os.path.join(tdir1, 'testfile1.py'),
os.path.join(tdir2, 'testfile2.py'),
]))
def test_find_with_excluded_hidden_dirs(self):
tdir1 = self._make_test_dir('.test1')
tdir2 = self._make_test_dir('test_2')
tdir3 = self._make_test_dir('test.3')
files = [
os.path.join(tdir1, 'testfile1.py'),
os.path.join(tdir2, 'testfile2.py'),
os.path.join(tdir3, 'testfile3.py'),
]
_touch_files(files)
actual = file_resources.GetCommandLineFiles([self.test_tmpdir],
recursive=True,
exclude=['*.test1*'])
self.assertEqual(
sorted(actual),
sorted([
os.path.join(tdir2, 'testfile2.py'),
os.path.join(tdir3, 'testfile3.py'),
]))
def test_find_with_excluded_hidden_dirs_relative(self):
"""Test find with excluded hidden dirs.
A regression test against a specific case where a hidden directory (one
beginning with a period) is being excluded, but it is also an immediate
child of the current directory which has been specified in a relative
manner.
At its core, the bug has to do with overzelous stripping of "./foo" so that
it removes too much from "./.foo" .
"""
tdir1 = self._make_test_dir('.test1')
tdir2 = self._make_test_dir('test_2')
tdir3 = self._make_test_dir('test.3')
files = [
os.path.join(tdir1, 'testfile1.py'),
os.path.join(tdir2, 'testfile2.py'),
os.path.join(tdir3, 'testfile3.py'),
]
_touch_files(files)
# We must temporarily change the current directory, so that we test against
# patterns like ./.test1/file instead of /tmp/foo/.test1/file
with _restore_working_dir():
os.chdir(self.test_tmpdir)
actual = file_resources.GetCommandLineFiles(
[os.path.relpath(self.test_tmpdir)],
recursive=True,
exclude=['*.test1*'])
self.assertEqual(
sorted(actual),
sorted([
os.path.join(
os.path.relpath(self.test_tmpdir), os.path.basename(tdir2),
'testfile2.py'),
os.path.join(
os.path.relpath(self.test_tmpdir), os.path.basename(tdir3),
'testfile3.py'),
]))
def test_find_with_excluded_dirs(self):
tdir1 = self._make_test_dir('test1')
tdir2 = self._make_test_dir('test2/testinner/')
tdir3 = self._make_test_dir('test3/foo/bar/bas/xxx')
files = [
os.path.join(tdir1, 'testfile1.py'),
os.path.join(tdir2, 'testfile2.py'),
os.path.join(tdir3, 'testfile3.py'),
]
_touch_files(files)
os.chdir(self.test_tmpdir)
found = sorted(
file_resources.GetCommandLineFiles(['test1', 'test2', 'test3'],
recursive=True,
exclude=[
'test1',
'test2/testinner/',
]))
self.assertEqual(found, ['test3/foo/bar/bas/xxx/testfile3.py'])
found = sorted(
file_resources.GetCommandLineFiles(['.'],
recursive=True,
exclude=[
'test1',
'test3',
]))
self.assertEqual(found, ['./test2/testinner/testfile2.py'])
def test_find_with_excluded_current_dir(self):
with self.assertRaises(errors.YapfError):
file_resources.GetCommandLineFiles([], False, exclude=['./z'])
class IsPythonFileTest(unittest.TestCase):
def setUp(self): # pylint: disable=g-missing-super-call
self.test_tmpdir = tempfile.mkdtemp()
def tearDown(self): # pylint: disable=g-missing-super-call
shutil.rmtree(self.test_tmpdir)
def test_with_py_extension(self):
file1 = os.path.join(self.test_tmpdir, 'testfile1.py')
self.assertTrue(file_resources.IsPythonFile(file1))
def test_empty_without_py_extension(self):
file1 = os.path.join(self.test_tmpdir, 'testfile1')
self.assertFalse(file_resources.IsPythonFile(file1))
file2 = os.path.join(self.test_tmpdir, 'testfile1.rb')
self.assertFalse(file_resources.IsPythonFile(file2))
def test_python_shebang(self):
file1 = os.path.join(self.test_tmpdir, 'testfile1')
with open(file1, 'w') as f:
f.write(u'#!/usr/bin/python\n')
self.assertTrue(file_resources.IsPythonFile(file1))
file2 = os.path.join(self.test_tmpdir, 'testfile2.run')
with open(file2, 'w') as f:
f.write(u'#! /bin/python2\n')
self.assertTrue(file_resources.IsPythonFile(file1))
def test_with_latin_encoding(self):
file1 = os.path.join(self.test_tmpdir, 'testfile1')
with py3compat.open_with_encoding(file1, mode='w', encoding='latin-1') as f:
f.write(u'#! /bin/python2\n')
self.assertTrue(file_resources.IsPythonFile(file1))
def test_with_invalid_encoding(self):
file1 = os.path.join(self.test_tmpdir, 'testfile1')
with open(file1, 'w') as f:
f.write(u'#! /bin/python2\n')
f.write(u'# -*- coding: iso-3-14159 -*-\n')
self.assertFalse(file_resources.IsPythonFile(file1))
class IsIgnoredTest(unittest.TestCase):
def test_root_path(self):
self.assertTrue(file_resources.IsIgnored('media', ['media']))
self.assertFalse(file_resources.IsIgnored('media', ['media/*']))
def test_sub_path(self):
self.assertTrue(file_resources.IsIgnored('media/a', ['*/a']))
self.assertTrue(file_resources.IsIgnored('media/b', ['media/*']))
self.assertTrue(file_resources.IsIgnored('media/b/c', ['*/*/c']))
def test_trailing_slash(self):
self.assertTrue(file_resources.IsIgnored('z', ['z']))
self.assertTrue(file_resources.IsIgnored('z', ['z/']))
class BufferedByteStream(object):
def __init__(self):
self.stream = py3compat.BytesIO()
def getvalue(self): # pylint: disable=invalid-name
return self.stream.getvalue().decode('utf-8')
@property
def buffer(self):
return self.stream
class WriteReformattedCodeTest(unittest.TestCase):
@classmethod
def setUpClass(cls): # pylint: disable=g-missing-super-call
cls.test_tmpdir = tempfile.mkdtemp()
@classmethod
def tearDownClass(cls): # pylint: disable=g-missing-super-call
shutil.rmtree(cls.test_tmpdir)
def test_write_to_file(self):
s = u'foobar\n'
with utils.NamedTempFile(dirname=self.test_tmpdir) as (f, fname):
file_resources.WriteReformattedCode(
fname, s, in_place=True, encoding='utf-8')
f.flush()
with open(fname) as f2:
self.assertEqual(f2.read(), s)
def test_write_to_stdout(self):
s = u'foobar'
stream = BufferedByteStream() if py3compat.PY3 else py3compat.StringIO()
with utils.stdout_redirector(stream):
file_resources.WriteReformattedCode(
None, s, in_place=False, encoding='utf-8')
self.assertEqual(stream.getvalue(), s)
def test_write_encoded_to_stdout(self):
s = '\ufeff# -*- coding: utf-8 -*-\nresult = "passed"\n' # pylint: disable=anomalous-unicode-escape-in-string
stream = BufferedByteStream() if py3compat.PY3 else py3compat.StringIO()
with utils.stdout_redirector(stream):
file_resources.WriteReformattedCode(
None, s, in_place=False, encoding='utf-8')
self.assertEqual(stream.getvalue(), s)
if __name__ == '__main__':
unittest.main()