Merge pull request #5003 from blueyed/off
Fix off-by-one error with lineno in mark collection error
This commit is contained in:
		
						commit
						c92021fc4f
					
				|  | @ -0,0 +1 @@ | ||||||
|  | Fix line offset with mark collection error (off by one). | ||||||
|  | @ -203,7 +203,9 @@ def compile_(source, filename=None, mode="exec", flags=0, dont_inherit=0): | ||||||
| 
 | 
 | ||||||
| def getfslineno(obj): | def getfslineno(obj): | ||||||
|     """ Return source location (path, lineno) for the given object. |     """ Return source location (path, lineno) for the given object. | ||||||
|     If the source cannot be determined return ("", -1) |     If the source cannot be determined return ("", -1). | ||||||
|  | 
 | ||||||
|  |     The line number is 0-based. | ||||||
|     """ |     """ | ||||||
|     from .code import Code |     from .code import Code | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -44,7 +44,7 @@ def get_empty_parameterset_mark(config, argnames, func): | ||||||
|         f_name = func.__name__ |         f_name = func.__name__ | ||||||
|         _, lineno = getfslineno(func) |         _, lineno = getfslineno(func) | ||||||
|         raise Collector.CollectError( |         raise Collector.CollectError( | ||||||
|             "Empty parameter set in '%s' at line %d" % (f_name, lineno) |             "Empty parameter set in '%s' at line %d" % (f_name, lineno + 1) | ||||||
|         ) |         ) | ||||||
|     else: |     else: | ||||||
|         raise LookupError(requested_mark) |         raise LookupError(requested_mark) | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ import sys | ||||||
| import six | import six | ||||||
| 
 | 
 | ||||||
| import pytest | import pytest | ||||||
|  | from _pytest.main import EXIT_INTERRUPTED | ||||||
| from _pytest.mark import EMPTY_PARAMETERSET_OPTION | from _pytest.mark import EMPTY_PARAMETERSET_OPTION | ||||||
| from _pytest.mark import MarkGenerator as Mark | from _pytest.mark import MarkGenerator as Mark | ||||||
| from _pytest.nodes import Collector | from _pytest.nodes import Collector | ||||||
|  | @ -859,20 +860,34 @@ def test_parameterset_for_fail_at_collect(testdir): | ||||||
| 
 | 
 | ||||||
|     config = testdir.parseconfig() |     config = testdir.parseconfig() | ||||||
|     from _pytest.mark import pytest_configure, get_empty_parameterset_mark |     from _pytest.mark import pytest_configure, get_empty_parameterset_mark | ||||||
|     from _pytest.compat import getfslineno |  | ||||||
| 
 | 
 | ||||||
|     pytest_configure(config) |     pytest_configure(config) | ||||||
| 
 | 
 | ||||||
|     test_func = all |     with pytest.raises( | ||||||
|     func_name = test_func.__name__ |         Collector.CollectError, | ||||||
|     _, func_lineno = getfslineno(test_func) |         match=r"Empty parameter set in 'pytest_configure' at line \d\d+", | ||||||
|     expected_errmsg = r"Empty parameter set in '%s' at line %d" % ( |     ): | ||||||
|         func_name, |         get_empty_parameterset_mark(config, ["a"], pytest_configure) | ||||||
|         func_lineno, |  | ||||||
|     ) |  | ||||||
| 
 | 
 | ||||||
|     with pytest.raises(Collector.CollectError, match=expected_errmsg): |     p1 = testdir.makepyfile( | ||||||
|         get_empty_parameterset_mark(config, ["a"], test_func) |         """ | ||||||
|  |         import pytest | ||||||
|  | 
 | ||||||
|  |         @pytest.mark.parametrize("empty", []) | ||||||
|  |         def test(): | ||||||
|  |             pass | ||||||
|  |         """ | ||||||
|  |     ) | ||||||
|  |     result = testdir.runpytest(str(p1)) | ||||||
|  |     result.stdout.fnmatch_lines( | ||||||
|  |         [ | ||||||
|  |             "collected 0 items / 1 errors", | ||||||
|  |             "* ERROR collecting test_parameterset_for_fail_at_collect.py *", | ||||||
|  |             "Empty parameter set in 'test' at line 3", | ||||||
|  |             "*= 1 error in *", | ||||||
|  |         ] | ||||||
|  |     ) | ||||||
|  |     assert result.ret == EXIT_INTERRUPTED | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def test_parameterset_for_parametrize_bad_markname(testdir): | def test_parameterset_for_parametrize_bad_markname(testdir): | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue