Skip to content

Commit

Permalink
Make cwd use pathlib (#3383)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea authored Apr 28, 2023
1 parent d738f83 commit 3480fa9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
5 changes: 3 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import subprocess
import sys
import warnings
from pathlib import Path

import pytest

# Ensure we always run from the root of the repository
if os.getcwd() != os.path.dirname(__file__):
os.chdir(os.path.dirname(__file__))
if Path.cwd() != Path(__file__).parent:
os.chdir(Path(__file__).parent)

# checking if user is running pytest without installing test dependencies:
missing = []
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def _previous_revision() -> Iterator[None]:
stderr=subprocess.DEVNULL,
)
try:
with cwd(worktree_dir):
with cwd(pathlib.Path(worktree_dir)):
subprocess.run(
[*GIT_CMD, "checkout", revision], # noqa: S603
stdout=subprocess.DEVNULL,
Expand Down
4 changes: 2 additions & 2 deletions src/ansiblelint/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def normpath_path(path: str | Path) -> Path:


@contextmanager
def cwd(path: str | Path) -> Iterator[None]:
def cwd(path: Path) -> Iterator[None]:
"""Context manager for temporary changing current working directory."""
old_pwd = os.getcwd()
old_pwd = Path.cwd()
os.chdir(path)
try:
yield
Expand Down
15 changes: 0 additions & 15 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"""PyTest fixtures for testing the project."""
from __future__ import annotations

import os
import shutil
import subprocess
from contextlib import contextmanager
from pathlib import Path
from typing import TYPE_CHECKING

Expand All @@ -15,24 +13,11 @@
from ansiblelint.yaml_utils import FormattedYAML

if TYPE_CHECKING:
from collections.abc import Iterator

from _pytest import nodes
from _pytest.config import Config
from _pytest.config.argparsing import Parser


@contextmanager
def cwd(path: str) -> Iterator[None]:
"""Context manager for chdir."""
old_pwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old_pwd)


def pytest_addoption(parser: Parser) -> None:
"""Add --regenerate-formatting-fixtures option to pytest."""
parser.addoption(
Expand Down
9 changes: 4 additions & 5 deletions test/test_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ansiblelint.__main__ import initialize_logger
from ansiblelint.file_utils import (
Lintable,
cwd,
expand_path_vars,
expand_paths_vars,
find_project_root,
Expand All @@ -21,8 +22,6 @@
)
from ansiblelint.runner import Runner

from .conftest import cwd

if TYPE_CHECKING:
from _pytest.capture import CaptureFixture
from _pytest.logging import LogCaptureFixture
Expand Down Expand Up @@ -310,7 +309,7 @@ def mockreturn(options: Options) -> dict[str, Any]: # noqa: ARG001
def test_find_project_root_1(tmp_path: Path) -> None:
"""Verify find_project_root()."""
# this matches black behavior in absence of any config files or .git/.hg folders.
with cwd(str(tmp_path)):
with cwd(tmp_path):
path, method = find_project_root([])
assert str(path) == "/"
assert method == "file system root"
Expand All @@ -321,7 +320,7 @@ def test_find_project_root_dotconfig() -> None:
# this expects to return examples folder as project root because this
# folder already has an .config/ansible-lint.yml file inside, which should
# be enough.
with cwd("examples"):
with cwd(Path("examples")):
assert os.path.exists(
".config/ansible-lint.yml",
), "Test requires config file inside .config folder."
Expand Down Expand Up @@ -567,7 +566,7 @@ def test_bug_2513(
os.makedirs(os.path.dirname(os.path.expanduser(filename)), exist_ok=True)
lintable = Lintable(filename, content="---\n- hosts: all\n")
lintable.write(force=True)
with cwd(str(tmp_path)):
with cwd(tmp_path):
results = Runner(filename, rules=default_rules_collection).run()
assert len(results) == 1
assert results[0].rule.id == "name"

0 comments on commit 3480fa9

Please sign in to comment.