Skip to content

Commit 81b90b1

Browse files
committed
Reimplement the most important functions.
Using the TestLayout classes.
1 parent 6e2634a commit 81b90b1

File tree

3 files changed

+18
-164
lines changed

3 files changed

+18
-164
lines changed

ftplugin/python_pyunit.vim

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class BaseTestLayout(object):
222222
raise NotImplemented("Implement this method in a subclass.")
223223

224224
def get_source_file(self, test_file):
225-
for candidate in self.get_source_file_candidates(test_file):
225+
for candidate in self.get_source_candidates(test_file):
226226
if os.path.exists(candidate):
227227
return candidate
228228
raise RuntimeError("Source file not found.")
@@ -356,95 +356,22 @@ def find_source_root(path):
356356
return os.path.join(find_project_root(path), source_root)
357357

358358

359-
def get_tests_root(path):
360-
loc = vim.eval("g:PyUnitTestsRoot")
361-
return os.sep.join([find_project_root(path), loc])
362-
363-
364359
def get_test_file_for_source_file(path):
365360
impl = get_implementing_class()()
366361
return impl.get_test_file(path)
367362

368363

369364
def find_source_file_for_test_file(path):
370-
testsroot = get_tests_root(path)
371-
PyUnitTestPrefix = vim.eval("g:PyUnitTestPrefix")
372-
373-
rel_path = _relpath(path, testsroot)
374-
parts = rel_path.split(os.sep)
375-
parts = [strip_prefix(p, PyUnitTestPrefix) for p in parts]
376-
sourcefile = os.sep.join(parts)
377-
378-
src_root = find_source_root(path)
379-
tests_structure = vim.eval("g:PyUnitTestsStructure")
380-
if tests_structure == "flat":
381-
# A flat test structure makes it somewhat ambiguous to deduce the test
382-
# file for the given testfile. For example, a test file called
383-
# test_foo_bar.py could belong to these four files:
384-
#
385-
# - foo/bar.py
386-
# - foo/bar/__init__.py
387-
# - foo_bar.py
388-
# - foo_bar/__init__.py
389-
#
390-
# The solution is to try them all and if we find a match, we use that
391-
# file. In case of multiple matches, we simply use the first.
392-
393-
def slashgenerator(length, select_from, select_last_from):
394-
if length <= 0:
395-
for opt in select_last_from:
396-
yield [opt]
397-
else:
398-
for opt in select_from:
399-
for x in slashgenerator(length - 1, select_from, \
400-
select_last_from):
401-
yield [opt] + x
402-
403-
def interlace(x, y):
404-
max_ = max(len(x), len(y))
405-
for i in xrange(max_):
406-
if i < len(x):
407-
yield x[i]
408-
if i < len(y):
409-
yield y[i]
410-
411-
stripped, extension = os.path.splitext(sourcefile)
412-
sourcefile = None
413-
parts = stripped.split("_")
414-
415-
intermediate_pairs = ['/', '_']
416-
last_pair = [extension, '/__init__' + extension]
417-
for slashes in slashgenerator(len(parts) - 1, intermediate_pairs, \
418-
last_pair):
419-
guess = os.path.join(src_root, "".join(interlace(parts, slashes)))
420-
if os.path.isfile(guess):
421-
sourcefile = guess
422-
break
423-
424-
if not sourcefile:
425-
raise Exception("Source file not found.")
426-
else:
427-
# For non-flat tests structures, the source file can either be the
428-
# source file as is (most likely), or an __init__.py file. Try that
429-
# alternative if the regular sourcefile doesn't exist.
430-
sourcefile = os.path.join(src_root, sourcefile)
431-
if not os.path.isfile(sourcefile):
432-
filepath, extension = os.path.splitext(sourcefile)
433-
sourcefile = "".join([filepath, os.sep, "__init__", extension])
434-
if not os.path.isfile(sourcefile):
435-
raise Exception("Source file not found.")
436-
437-
return sourcefile
365+
impl = get_implementing_class()()
366+
for f in impl.get_source_candidates(path):
367+
if os.path.exists(f):
368+
return f
369+
raise Exception("Source file not found.")
438370

439371

440372
def is_test_file(path):
441-
#impl = get_implementing_class()()
442-
#return impl.is_test_file(path)
443-
444-
# Being in the test root means you're a test file
445-
testroot = os.path.abspath(get_tests_root(path))
446-
path = os.path.abspath(path)
447-
return path.startswith(testroot)
373+
impl = get_implementing_class()()
374+
return impl.is_test_file(path)
448375

449376

450377
def _vim_split_cmd(inverted=False):

src/python_unittests.py

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def get_source_candidates(self, test_file):
112112
raise NotImplemented("Implement this method in a subclass.")
113113

114114
def get_source_file(self, test_file):
115-
for candidate in self.get_source_file_candidates(test_file):
115+
for candidate in self.get_source_candidates(test_file):
116116
if os.path.exists(candidate):
117117
return candidate
118118
raise RuntimeError("Source file not found.")
@@ -246,95 +246,22 @@ def find_source_root(path):
246246
return os.path.join(find_project_root(path), source_root)
247247

248248

249-
def get_tests_root(path):
250-
loc = vim.eval("g:PyUnitTestsRoot")
251-
return os.sep.join([find_project_root(path), loc])
252-
253-
254249
def get_test_file_for_source_file(path):
255250
impl = get_implementing_class()()
256251
return impl.get_test_file(path)
257252

258253

259254
def find_source_file_for_test_file(path):
260-
testsroot = get_tests_root(path)
261-
PyUnitTestPrefix = vim.eval("g:PyUnitTestPrefix")
262-
263-
rel_path = _relpath(path, testsroot)
264-
parts = rel_path.split(os.sep)
265-
parts = [strip_prefix(p, PyUnitTestPrefix) for p in parts]
266-
sourcefile = os.sep.join(parts)
267-
268-
src_root = find_source_root(path)
269-
tests_structure = vim.eval("g:PyUnitTestsStructure")
270-
if tests_structure == "flat":
271-
# A flat test structure makes it somewhat ambiguous to deduce the test
272-
# file for the given testfile. For example, a test file called
273-
# test_foo_bar.py could belong to these four files:
274-
#
275-
# - foo/bar.py
276-
# - foo/bar/__init__.py
277-
# - foo_bar.py
278-
# - foo_bar/__init__.py
279-
#
280-
# The solution is to try them all and if we find a match, we use that
281-
# file. In case of multiple matches, we simply use the first.
282-
283-
def slashgenerator(length, select_from, select_last_from):
284-
if length <= 0:
285-
for opt in select_last_from:
286-
yield [opt]
287-
else:
288-
for opt in select_from:
289-
for x in slashgenerator(length - 1, select_from, \
290-
select_last_from):
291-
yield [opt] + x
292-
293-
def interlace(x, y):
294-
max_ = max(len(x), len(y))
295-
for i in xrange(max_):
296-
if i < len(x):
297-
yield x[i]
298-
if i < len(y):
299-
yield y[i]
300-
301-
stripped, extension = os.path.splitext(sourcefile)
302-
sourcefile = None
303-
parts = stripped.split("_")
304-
305-
intermediate_pairs = ['/', '_']
306-
last_pair = [extension, '/__init__' + extension]
307-
for slashes in slashgenerator(len(parts) - 1, intermediate_pairs, \
308-
last_pair):
309-
guess = os.path.join(src_root, "".join(interlace(parts, slashes)))
310-
if os.path.isfile(guess):
311-
sourcefile = guess
312-
break
313-
314-
if not sourcefile:
315-
raise Exception("Source file not found.")
316-
else:
317-
# For non-flat tests structures, the source file can either be the
318-
# source file as is (most likely), or an __init__.py file. Try that
319-
# alternative if the regular sourcefile doesn't exist.
320-
sourcefile = os.path.join(src_root, sourcefile)
321-
if not os.path.isfile(sourcefile):
322-
filepath, extension = os.path.splitext(sourcefile)
323-
sourcefile = "".join([filepath, os.sep, "__init__", extension])
324-
if not os.path.isfile(sourcefile):
325-
raise Exception("Source file not found.")
326-
327-
return sourcefile
255+
impl = get_implementing_class()()
256+
for f in impl.get_source_candidates(path):
257+
if os.path.exists(f):
258+
return f
259+
raise Exception("Source file not found.")
328260

329261

330262
def is_test_file(path):
331-
#impl = get_implementing_class()()
332-
#return impl.is_test_file(path)
333-
334-
# Being in the test root means you're a test file
335-
testroot = os.path.abspath(get_tests_root(path))
336-
path = os.path.abspath(path)
337-
return path.startswith(testroot)
263+
impl = get_implementing_class()()
264+
return impl.is_test_file(path)
338265

339266

340267
def _vim_split_cmd(inverted=False):

tests/test_python_unittests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ def test_relpath(self):
393393

394394

395395
def test_is_test_file(self):
396-
self.assertTrue(mod.is_test_file('tests/foo.py'))
396+
self.assertTrue(mod.is_test_file('tests/test_foo.py'))
397397

398398
vimvar['g:PyUnitTestsRoot'] = 'my/test/dir'
399-
self.assertFalse(mod.is_test_file('tests/foo.py'))
399+
self.assertFalse(mod.is_test_file('tests/test_foo.py'))
400400

401401
def test_source_root_for_non_source_file(self):
402402
self.assertRaises(Exception,

0 commit comments

Comments
 (0)