Skip to content

Commit

Permalink
move file or directory
Browse files Browse the repository at this point in the history
action to publish prereleases on test.pypi
  • Loading branch information
d-chris committed Jan 2, 2024
1 parent 6671371 commit d1d81c5
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/testbuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: poetry-testpypi

on:
release:
types: [prereleased]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install --without dev
- name: Build and publish
env:
POETRY_PYPI_TOKEN_TESTPYPI: ${{ secrets.TESTPYPI_TOKEN }}
run: |
poetry version $(git describe --tags)
poetry build
poetry publish --repository testpypi
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- `contextmanager` to change current working directory with `with` statement
- `Path().copy()` copy a file or directory to a new path destination
- `Path().delete()` delete a file or directory-tree
- `Path().move()` move a file or directory to a new path destination

## Installation

Expand Down
15 changes: 15 additions & 0 deletions pathlibutil/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ def delete(self, *, recursive: bool = False, missing_ok: bool = False, **kwargs)
raise e

shutil.rmtree(self, **kwargs)

def move(self, dst: str) -> 'Path':
"""
Moves the file or directory to a destination path.
"""
src = self.resolve(strict=True)
dst = Path(dst).resolve()
dst.mkdir(parents=True, exist_ok=True)

try:
_path = shutil.move(str(src), str(dst))
except shutil.Error as e:
raise OSError(e)

return Path(_path)
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ classifiers = [
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
]
keywords = ["pathlib", "hashlib"]
keywords = ["pathlib", "hashlib", "shutil"]
repository = "https://github.com/d-chris/pathlibutil"
include = ["examples/*.py", "LICENSE"]

[tool.poetry.dependencies]
python = ">=3.8,<3.13"
Expand All @@ -26,6 +27,15 @@ pytest-random-order = "^1.1.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.12.0"

[[tool.poetry.source]]
name = "PyPI"
priority = "primary"

[[tool.poetry.source]]
name = "testpypi"
url = "https://test.pypi.org/legacy/"
priority = "explicit"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand All @@ -47,4 +57,11 @@ commands = pytest --random-order
[tool.pytest.ini_options]
minversion = "6.0"
testpaths = "tests"
addopts = "--random-order --cov=pathlibutil --cov-report=term-missing:skip-covered --color=yes --cov-append"
addopts = [
"--random-order",
"--color=yes",
"--cov=pathlibutil",
"--cov-report=term-missing:skip-covered",
"--cov-append",
# "--cov-report=html",
]
42 changes: 42 additions & 0 deletions tests/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,45 @@ def test_delete_directory(tmp_dirpath: Path):
tmp_dirpath.delete(recursive=True)

assert not tmp_dirpath.exists()


def test_move_dir(file: Path, tmp_path: pathlib.Path):

src = tmp_path.joinpath('src')
src.mkdir()

shutil.copy(file, src)

p = Path(src).move(tmp_path / 'dst')

assert p.is_dir()
assert p.parts[-1] == 'src'
assert p.joinpath(file.name).is_file()


def test_move_file(file: Path, tmp_dirpath: Path):

src = tmp_dirpath / file.name

assert src.is_file(), 'setup failed'

dst = src.move(tmp_dirpath / 'dst')

assert src.is_file() == False
assert dst.is_file() == True

dst.move(tmp_dirpath)

assert src.is_file() == True
assert dst.is_file() == False


def test_move_raises(file: Path, tmp_dirpath: Path):

with pytest.raises(FileNotFoundError):
Path('notexists').move(tmp_dirpath)

src = tmp_dirpath / file.name

with pytest.raises(OSError):
src.move(tmp_dirpath)

0 comments on commit d1d81c5

Please sign in to comment.