Skip to content

Commit 45d6a10

Browse files
committed
relative --log-line PR feedback improvements
1 parent 951a639 commit 45d6a10

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

changelog/7336.breaking.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The pytest ``--log-file`` cli or ``log_file`` ini marker is now relative to the configs ``inifile`` directory, as it was always the intention. It was originally introduced as relative to the current working directory unintentionally.

src/_pytest/config/findpaths.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def determine_setup(
161161
args: List[str],
162162
rootdir_cmd_arg: Optional[str] = None,
163163
config: Optional["Config"] = None,
164-
) -> Tuple[py.path.local, Optional[str], Dict[str, Union[str, List[str]]]]:
164+
) -> Tuple[py.path.local, Optional[py.path.local], Dict[str, Union[str, List[str]]]]:
165165
rootdir = None
166166
dirs = get_dirs_from_args(args)
167167
if inifile:

src/_pytest/logging.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,8 @@ def __init__(self, config: Config) -> None:
529529
log_file = get_option_ini(config, "log_file") or os.devnull
530530

531531
# Keep the log file relative to the inidir file (if it exists) (#7336)
532-
inidir = getattr(getattr(config, "inifile", None), "dirname", None)
533-
if log_file != os.devnull and inidir:
534-
log_file = os.path.join(inidir, log_file)
532+
if log_file != os.devnull and config.inifile is not None:
533+
log_file = os.path.join(config.inifile.dirname, log_file)
535534
self.log_file_handler = _FileHandler(log_file, mode="w", encoding="UTF-8")
536535
log_file_format = get_option_ini(config, "log_file_format", "log_format")
537536
log_file_date_format = get_option_ini(

testing/logging/test_reporting.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,9 +1154,10 @@ def test_bad_log(monkeypatch):
11541154
result.assert_outcomes(passed=1)
11551155

11561156

1157-
def test_log_file_is_in_pytest_ini_rootdir(testdir):
1158-
# as per https://docs.pytest.org/en/5.4.3/reference.html log_file
1159-
# the file should be relative to the pytest inifile
1157+
def test_log_file_is_in_pytest_ini_rootdir(testdir, monkeypatch):
1158+
"""
1159+
#7336|#7350 - log_file should be relative to the config inifile
1160+
"""
11601161
testdir.makefile(
11611162
".ini",
11621163
pytest="""
@@ -1176,14 +1177,17 @@ def test_this():
11761177
"""
11771178
)
11781179
p.move(sub.join(p.basename))
1179-
os.chdir(sub.strpath)
1180+
monkeypatch.chdir(sub.strpath)
11801181
testdir.runpytest()
11811182
testdir.chdir()
11821183
files = set(os.listdir())
11831184
assert {"logfile.txt", "pytest.ini"}.issubset(files)
11841185

11851186

1186-
def test_log_file_can_be_specified_to_child_dir(testdir):
1187+
def test_log_file_can_be_specified_to_child_dir(testdir, monkeypatch):
1188+
"""
1189+
#7336|#7350 - log_file should be relative to the config inifile
1190+
"""
11871191
testdir.makefile(
11881192
".ini",
11891193
pytest="""
@@ -1198,19 +1202,21 @@ def test_log_file_can_be_specified_to_child_dir(testdir):
11981202
p = testdir.makepyfile(
11991203
"""
12001204
def test_this():
1201-
import logging
1202-
logging.getLogger().info("Normal message")
1205+
pass
12031206
"""
12041207
)
12051208
p.move(sub.join(p.basename))
1206-
os.chdir(sub.strpath)
1209+
monkeypatch.chdir(sub.strpath)
12071210
testdir.runpytest()
12081211
files = set(os.listdir())
12091212
assert "logfile.txt" in files
12101213
assert "pytest.ini" not in files
12111214

12121215

1213-
def test_log_file_cli_is_also_relative(testdir):
1216+
def test_log_file_cli_is_also_relative(testdir, monkeypatch):
1217+
"""
1218+
#7336|#7350 - log_file should be relative to the config inifile
1219+
"""
12141220
testdir.makefile(
12151221
".ini",
12161222
pytest="""
@@ -1221,12 +1227,11 @@ def test_log_file_cli_is_also_relative(testdir):
12211227
p = testdir.makepyfile(
12221228
"""
12231229
def test_this():
1224-
import logging
1225-
logging.getLogger().info("Normal message")
1230+
pass
12261231
"""
12271232
)
12281233
p.move(sub.join(p.basename))
1229-
os.chdir(sub.strpath)
1234+
monkeypatch.chdir(sub.strpath)
12301235
testdir.runpytest("--log-file", "sub{}logfile.txt".format(os.sep))
12311236
files = set(os.listdir())
12321237
assert "logfile.txt" in files

0 commit comments

Comments
 (0)