From 11976476805d2b6f84472cb5e1e9dbd6ff1a09dc Mon Sep 17 00:00:00 2001 From: sp1thas Date: Sat, 18 Feb 2023 22:19:12 +0200 Subject: [PATCH] rename pipeline job --- .github/workflows/python-testing.yml | 2 +- .github/workflows/shell-testing.yml | 2 +- Makefile | 10 ++++- README.md | 2 +- docs/installation.md | 2 +- docs/testing.md | 2 +- src/dropboxignore/cli.py | 12 +++++- src/dropboxignore/commands/base.py | 6 +-- src/dropboxignore/commands/delete.py | 4 +- src/dropboxignore/commands/generate.py | 6 +-- src/dropboxignore/enums.py | 4 +- src/dropboxignore/filterer.py | 41 ------------------- .../dropboxignore/filterers}/__init__.py | 0 src/dropboxignore/filterers/base.py | 10 +++++ src/dropboxignore/filterers/dropboxignore.py | 7 ++++ src/dropboxignore/filterers/gitignore.py | 7 ++++ tests/cli/commands/test_delete.py | 6 --- tests/cli/commands/test_generate.py | 6 --- .../{cli/commands => integration}/__init__.py | 0 tests/integration/commands/__init__.py | 0 tests/integration/commands/test_delete.py | 15 +++++++ tests/integration/commands/test_generate.py | 17 ++++++++ .../commands/test_genupi.py | 0 .../commands/test_ignore.py | 0 .../commands/test_list.py | 0 .../commands/test_revert.py | 0 .../commands/test_update.py | 0 tests/unit/commands/test_delete.py | 6 +-- tests/unit/commands/test_generate.py | 14 +++---- 29 files changed, 99 insertions(+), 82 deletions(-) delete mode 100644 src/dropboxignore/filterer.py rename {tests/cli => src/dropboxignore/filterers}/__init__.py (100%) create mode 100644 src/dropboxignore/filterers/base.py create mode 100644 src/dropboxignore/filterers/dropboxignore.py create mode 100644 src/dropboxignore/filterers/gitignore.py delete mode 100644 tests/cli/commands/test_delete.py delete mode 100644 tests/cli/commands/test_generate.py rename tests/{cli/commands => integration}/__init__.py (100%) create mode 100644 tests/integration/commands/__init__.py create mode 100644 tests/integration/commands/test_delete.py create mode 100644 tests/integration/commands/test_generate.py rename tests/{cli => integration}/commands/test_genupi.py (100%) rename tests/{cli => integration}/commands/test_ignore.py (100%) rename tests/{cli => integration}/commands/test_list.py (100%) rename tests/{cli => integration}/commands/test_revert.py (100%) rename tests/{cli => integration}/commands/test_update.py (100%) diff --git a/.github/workflows/python-testing.yml b/.github/workflows/python-testing.yml index 2483bbc..3358ad4 100644 --- a/.github/workflows/python-testing.yml +++ b/.github/workflows/python-testing.yml @@ -23,7 +23,7 @@ env: PYTHON_KEYRING_BACKEND: keyring.backends.null.Keyring jobs: - unit: + testing: runs-on: ${{ matrix.os }} strategy: fail-fast: false diff --git a/.github/workflows/shell-testing.yml b/.github/workflows/shell-testing.yml index 5c3cea5..47ecccb 100644 --- a/.github/workflows/shell-testing.yml +++ b/.github/workflows/shell-testing.yml @@ -49,7 +49,7 @@ jobs: git clone https://github.com/bats-core/bats-support.git libs/bats-support git clone https://github.com/bats-core/bats-file.git libs/bats-file - name: Run bats tests - run: make test + run: make shell-test - name: Coverage report run: | kcov --include-path=src /tmp/coverage bats tests/*.bats diff --git a/Makefile b/Makefile index c655222..93eac7f 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,18 @@ DESTSCRIPTNAME=dropboxignore DESTBINDIR=/usr/local/bin DESTLIBDIR=/usr/local/lib/dropboxignore -# run tests +# run shell tests # bats should be in PATH -test : +shell-tests : git submodule update --init bats tests +# run python tests +python-tests : + poetry run pytest --cov=src tests/ + +test : shell-tests python-tests + # install dropboxignore install : $(info installing dropboxignore in $(DESTDIR)) diff --git a/README.md b/README.md index 43e8b05..e426f1e 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ $ flatpak install me.simakis.dropboxignore ```shell $ git clone https://github.com/sp1thas/dropboxignore.git $ cd dropboxignore -$ make test # optional step, bats should be in your PATH +$ make shell-test # optional step, bats should be in your PATH $ sudo make install ``` diff --git a/docs/installation.md b/docs/installation.md index 2925599..b1b944b 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -39,6 +39,6 @@ Linux systems. For macOS, Homebrew is required. ```bash git clone https://github.com/sp1thas/dropboxignore.git cd dropboxignore - make test # optional step, bats should be in your PATH + make shell-test # optional step, bats should be in your PATH sudo make install ``` diff --git a/docs/testing.md b/docs/testing.md index 120fd99..6160190 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -10,7 +10,7 @@ Tests of dropboxignore are written using [bats](https://github.com/bats-core/bat [source](https://github.com/bats-core/bats-core)). ```shell -$ make test +$ make shell-test ``` If you are planning to implement a new feature, make sure that the necessary test cases have been added. diff --git a/src/dropboxignore/cli.py b/src/dropboxignore/cli.py index 955a72b..1a97771 100644 --- a/src/dropboxignore/cli.py +++ b/src/dropboxignore/cli.py @@ -16,7 +16,11 @@ def ignore(self, path: str = ".", v: int = 1): def generate(self, path: str = ".", v: int = 1): """Generate .dropboxignore files based on existing .gitignore files.""" - pass + from dropboxignore.commands.generate import GenerateCommand + from dropboxignore.filterers.gitignore import GitIgnoreFilterer + + cmd = GenerateCommand(path=path, filterer=GitIgnoreFilterer) + cmd.run() def list(self, path: str = ".", v: int = 1): """List ignored files and folders under the given path.""" @@ -24,7 +28,11 @@ def list(self, path: str = ".", v: int = 1): def delete(self, path: str = ".", v: int = 1): """Delete specific .dropboxignore file or every .dropboxignore file under the given path.""" - pass + from dropboxignore.commands.delete import DeleteCommand + from dropboxignore.filterers.dropboxignore import DropboxIgnoreFilterer + + cmd = DeleteCommand(path=path, filterer=DropboxIgnoreFilterer) + cmd.run() def update(self, path: str = ".", v: int = 1): """Update existing .dropboxignore files if at least one .gitignore file has been changed.""" diff --git a/src/dropboxignore/commands/base.py b/src/dropboxignore/commands/base.py index 8b1c197..8170c4f 100644 --- a/src/dropboxignore/commands/base.py +++ b/src/dropboxignore/commands/base.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import Union, Type -from dropboxignore.filterer import BaseFilterer +from dropboxignore.filterers.base import BaseFilterer @dataclass @@ -26,7 +26,7 @@ def __init__( ) -> None: self.c = Counter() self.path = Path(path) if isinstance(path, str) else path - self.filterer = filterer(base_path=self.path) + self.filterer = filterer(path=self.path) @abstractmethod def run_report(self) -> str: @@ -37,6 +37,6 @@ def run_on_item_path(self, item_path: Path) -> None: pass def run(self): - for item_path in self.filterer.filter(): + for item_path in self.filterer: self.run_on_item_path(item_path) print(self.run_report()) diff --git a/src/dropboxignore/commands/delete.py b/src/dropboxignore/commands/delete.py index 6c632c3..ec49613 100644 --- a/src/dropboxignore/commands/delete.py +++ b/src/dropboxignore/commands/delete.py @@ -1,12 +1,12 @@ from pathlib import Path from dropboxignore.commands.base import BaseCommand -from dropboxignore.enums import IgnoreFiles +from dropboxignore.enums import IgnoreFile class DeleteCommand(BaseCommand): def run_on_item_path(self, item_path: Path) -> None: - if not item_path.name == IgnoreFiles.DROPBOXIGNORE.value: + if not item_path.name == IgnoreFile.DROPBOXIGNORE.value: raise ValueError(f"{item_path} is not a dropboxignore file. {item_path}") try: item_path.unlink(missing_ok=False) diff --git a/src/dropboxignore/commands/generate.py b/src/dropboxignore/commands/generate.py index e28aeb1..0fce2c3 100644 --- a/src/dropboxignore/commands/generate.py +++ b/src/dropboxignore/commands/generate.py @@ -1,7 +1,7 @@ from pathlib import Path from dropboxignore.commands.base import BaseCommand -from dropboxignore.enums import IgnoreFiles +from dropboxignore.enums import IgnoreFile import datetime HEADER = ( @@ -11,14 +11,14 @@ class GenerateCommand(BaseCommand): def run_on_item_path(self, item_path: Path) -> None: - if not item_path.name == IgnoreFiles.GITIGNORE.value: + if not item_path.name == IgnoreFile.GITIGNORE.value: raise ValueError(f"{item_path} is not a gitignore file") if not item_path.exists(): raise ValueError(f"{item_path} does not exists") if not item_path.is_file(): raise ValueError(f"{item_path} is not a file") - di = item_path.parent / IgnoreFiles.DROPBOXIGNORE.value + di = item_path.parent / IgnoreFile.DROPBOXIGNORE.value if di.exists(): raise ValueError(f"{di} already exists") diff --git a/src/dropboxignore/enums.py b/src/dropboxignore/enums.py index c6515f8..9e36bee 100644 --- a/src/dropboxignore/enums.py +++ b/src/dropboxignore/enums.py @@ -1,12 +1,12 @@ from enum import Enum -class OperatingSystems(str, Enum): +class OperatingSystem(str, Enum): LINUX = "Linux" WINDOWS = "Windows" MACOS = "Darwin" -class IgnoreFiles(str, Enum): +class IgnoreFile(str, Enum): GITIGNORE = ".gitignore" DROPBOXIGNORE = ".dropboxignore" diff --git a/src/dropboxignore/filterer.py b/src/dropboxignore/filterer.py deleted file mode 100644 index cd887b6..0000000 --- a/src/dropboxignore/filterer.py +++ /dev/null @@ -1,41 +0,0 @@ -import os -import pathlib -from abc import abstractmethod -from typing import Iterator - -from dropboxignore.enums import IgnoreFiles - - -class BaseFilterer: - def __init__(self, base_path: pathlib.Path): - self.base_path = base_path - - @abstractmethod - def filter(self) -> Iterator[pathlib.Path]: - for _ in []: - yield pathlib.Path("..") - - -class IgnoreFileMatchingFilterer(BaseFilterer): - def __init__(self, base_path: pathlib.Path, ignore_file_name: IgnoreFiles): - super().__init__(base_path=base_path) - self.ignore_file_name = ignore_file_name - - def filter(self) -> Iterator[pathlib.Path]: - for ignore_file in self.base_path.glob(self.ignore_file_name.value): - current_folder = ignore_file.parent - for pattern in self._parse_ignore_file(ignore_file): - for file_path in current_folder.glob(pattern): - yield file_path - - @staticmethod - def _parse_ignore_file(ignore_file: pathlib.Path) -> Iterator[str]: - for l in ignore_file.read_text().split(os.linesep): - line = l.strip() - if not line: - continue - if line.startswith("#"): - continue - if line.startswith("!"): - continue - yield line diff --git a/tests/cli/__init__.py b/src/dropboxignore/filterers/__init__.py similarity index 100% rename from tests/cli/__init__.py rename to src/dropboxignore/filterers/__init__.py diff --git a/src/dropboxignore/filterers/base.py b/src/dropboxignore/filterers/base.py new file mode 100644 index 0000000..1b2c0c8 --- /dev/null +++ b/src/dropboxignore/filterers/base.py @@ -0,0 +1,10 @@ +import pathlib +from typing import Union + + +class BaseFilterer: + def __init__(self, path: Union[str, pathlib.Path]): + self.path = pathlib.Path(path) if isinstance(path, str) else path + + def __iter__(self): + raise NotImplementedError diff --git a/src/dropboxignore/filterers/dropboxignore.py b/src/dropboxignore/filterers/dropboxignore.py new file mode 100644 index 0000000..7a826a3 --- /dev/null +++ b/src/dropboxignore/filterers/dropboxignore.py @@ -0,0 +1,7 @@ +from dropboxignore.filterers.base import BaseFilterer +from dropboxignore.enums import IgnoreFile + + +class DropboxIgnoreFilterer(BaseFilterer): + def __iter__(self): + yield from self.path.glob(f"**/*{IgnoreFile.DROPBOXIGNORE.value}") diff --git a/src/dropboxignore/filterers/gitignore.py b/src/dropboxignore/filterers/gitignore.py new file mode 100644 index 0000000..0c56bab --- /dev/null +++ b/src/dropboxignore/filterers/gitignore.py @@ -0,0 +1,7 @@ +from dropboxignore.filterers.base import BaseFilterer +from dropboxignore.enums import IgnoreFile + + +class GitIgnoreFilterer(BaseFilterer): + def __iter__(self): + yield from self.path.glob(f"**/*{IgnoreFile.GITIGNORE.value}") diff --git a/tests/cli/commands/test_delete.py b/tests/cli/commands/test_delete.py deleted file mode 100644 index 60a3b74..0000000 --- a/tests/cli/commands/test_delete.py +++ /dev/null @@ -1,6 +0,0 @@ -from dropboxignore.cli import _cli_partial as cli -from pathlib import Path - - -def test_delete_success(tmp_path: Path) -> None: - cli(command=f"delete {tmp_path.absolute()}") diff --git a/tests/cli/commands/test_generate.py b/tests/cli/commands/test_generate.py deleted file mode 100644 index f399e4a..0000000 --- a/tests/cli/commands/test_generate.py +++ /dev/null @@ -1,6 +0,0 @@ -from dropboxignore.cli import _cli_partial as cli -from pathlib import Path - - -def test_generate_success(tmp_path: Path) -> None: - cli(command=f"generate {tmp_path.absolute()}") diff --git a/tests/cli/commands/__init__.py b/tests/integration/__init__.py similarity index 100% rename from tests/cli/commands/__init__.py rename to tests/integration/__init__.py diff --git a/tests/integration/commands/__init__.py b/tests/integration/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/commands/test_delete.py b/tests/integration/commands/test_delete.py new file mode 100644 index 0000000..1cc2972 --- /dev/null +++ b/tests/integration/commands/test_delete.py @@ -0,0 +1,15 @@ +from dropboxignore.cli import _cli_partial as cli +from pathlib import Path +from dropboxignore.enums import IgnoreFile + + +def test_delete_success(tmp_path: Path) -> None: + di = tmp_path / IgnoreFile.DROPBOXIGNORE.value + di.touch() + + assert di.exists() + + a = cli(command=f"delete {tmp_path.absolute()}") + + print(a) + assert not di.exists() diff --git a/tests/integration/commands/test_generate.py b/tests/integration/commands/test_generate.py new file mode 100644 index 0000000..16a9a02 --- /dev/null +++ b/tests/integration/commands/test_generate.py @@ -0,0 +1,17 @@ +from dropboxignore.cli import _cli_partial as cli +from pathlib import Path +from dropboxignore.enums import IgnoreFile + + +def test_delete_success(tmp_path: Path) -> None: + gi = tmp_path / IgnoreFile.GITIGNORE.value + di = tmp_path / IgnoreFile.DROPBOXIGNORE.value + gi.touch() + gi.write_text("*.txt\n") + + assert gi.exists() + assert not di.exists() + + cli(command=f"generate {tmp_path.absolute()}") + + assert di.exists() diff --git a/tests/cli/commands/test_genupi.py b/tests/integration/commands/test_genupi.py similarity index 100% rename from tests/cli/commands/test_genupi.py rename to tests/integration/commands/test_genupi.py diff --git a/tests/cli/commands/test_ignore.py b/tests/integration/commands/test_ignore.py similarity index 100% rename from tests/cli/commands/test_ignore.py rename to tests/integration/commands/test_ignore.py diff --git a/tests/cli/commands/test_list.py b/tests/integration/commands/test_list.py similarity index 100% rename from tests/cli/commands/test_list.py rename to tests/integration/commands/test_list.py diff --git a/tests/cli/commands/test_revert.py b/tests/integration/commands/test_revert.py similarity index 100% rename from tests/cli/commands/test_revert.py rename to tests/integration/commands/test_revert.py diff --git a/tests/cli/commands/test_update.py b/tests/integration/commands/test_update.py similarity index 100% rename from tests/cli/commands/test_update.py rename to tests/integration/commands/test_update.py diff --git a/tests/unit/commands/test_delete.py b/tests/unit/commands/test_delete.py index 08c34e4..2fa826a 100644 --- a/tests/unit/commands/test_delete.py +++ b/tests/unit/commands/test_delete.py @@ -1,11 +1,11 @@ from pathlib import Path from dropboxignore.commands.delete import DeleteCommand -from dropboxignore.enums import IgnoreFiles +from dropboxignore.enums import IgnoreFile def test_delete_successful(tmp_path: Path): - di = tmp_path / IgnoreFiles.DROPBOXIGNORE.value + di = tmp_path / IgnoreFile.DROPBOXIGNORE.value di.touch() assert di.exists() @@ -18,7 +18,7 @@ def test_delete_successful(tmp_path: Path): def test_delete_not_exists(tmp_path: Path): - di = tmp_path / IgnoreFiles.DROPBOXIGNORE.value + di = tmp_path / IgnoreFile.DROPBOXIGNORE.value assert not di.exists() diff --git a/tests/unit/commands/test_generate.py b/tests/unit/commands/test_generate.py index 21e7b53..9342bc7 100644 --- a/tests/unit/commands/test_generate.py +++ b/tests/unit/commands/test_generate.py @@ -4,15 +4,15 @@ import pytest from dropboxignore.commands.generate import GenerateCommand -from dropboxignore.enums import IgnoreFiles +from dropboxignore.enums import IgnoreFile def test_generate_successful(tmp_path: Path): - gi = tmp_path / IgnoreFiles.GITIGNORE.value + gi = tmp_path / IgnoreFile.GITIGNORE.value gi.write_text("*.txt") - di = tmp_path / IgnoreFiles.DROPBOXIGNORE.value + di = tmp_path / IgnoreFile.DROPBOXIGNORE.value assert not di.exists() @@ -40,7 +40,7 @@ def test_generate_not_gitignore_file_input(tmp_path: Path): def test_generate_gitignore_file_not_exists(tmp_path: Path): - gi = tmp_path / IgnoreFiles.GITIGNORE.value + gi = tmp_path / IgnoreFile.GITIGNORE.value assert not gi.exists() @@ -52,7 +52,7 @@ def test_generate_gitignore_file_not_exists(tmp_path: Path): def test_generate_gitignore_file_not_file(tmp_path: Path): - gi = tmp_path / IgnoreFiles.GITIGNORE.value + gi = tmp_path / IgnoreFile.GITIGNORE.value gi.mkdir() @@ -66,8 +66,8 @@ def test_generate_gitignore_file_not_file(tmp_path: Path): def test_generate_dropboxignore_file_already_exists(tmp_path: Path): - gi = tmp_path / IgnoreFiles.GITIGNORE.value - di = tmp_path / IgnoreFiles.DROPBOXIGNORE.value + gi = tmp_path / IgnoreFile.GITIGNORE.value + di = tmp_path / IgnoreFile.DROPBOXIGNORE.value gi.touch() di.touch()