Skip to content

Commit

Permalink
Python implementation (#36)
Browse files Browse the repository at this point in the history
* python impl

* fix

* implement commands

* remove --dev param

* add cli tests

* update ci pipelines

* rename pipeline job

* fix make target
  • Loading branch information
sp1thas authored Feb 18, 2023
1 parent 53c0eba commit 9630f50
Show file tree
Hide file tree
Showing 30 changed files with 101 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ env:
PYTHON_KEYRING_BACKEND: keyring.backends.null.Keyring

jobs:
unit:
testing:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/shell-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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-tests
- name: Coverage report
run: |
kcov --include-path=src /tmp/coverage bats tests/*.bats
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-tests # optional step, bats should be in your PATH
$ sudo make install
```

Expand Down
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-tests # optional step, bats should be in your PATH
sudo make install
```
2 changes: 1 addition & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-tests
```

If you are planning to implement a new feature, make sure that the necessary test cases have been added.
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ markdown_extensions:
- md_in_html
- attr_list
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
emoji_index: '!!python/name:materialx.emoji.twemoji'
emoji_generator: '!!python/name:materialx.emoji.to_svg'

repo_url: https://github.com/sp1thas/dropboxignore/

Expand Down
12 changes: 10 additions & 2 deletions src/dropboxignore/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ 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."""
pass

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."""
Expand Down
6 changes: 3 additions & 3 deletions src/dropboxignore/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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())
4 changes: 2 additions & 2 deletions src/dropboxignore/commands/delete.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/dropboxignore/commands/generate.py
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/dropboxignore/enums.py
Original file line number Diff line number Diff line change
@@ -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"
41 changes: 0 additions & 41 deletions src/dropboxignore/filterer.py

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions src/dropboxignore/filterers/base.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions src/dropboxignore/filterers/dropboxignore.py
Original file line number Diff line number Diff line change
@@ -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}")
7 changes: 7 additions & 0 deletions src/dropboxignore/filterers/gitignore.py
Original file line number Diff line number Diff line change
@@ -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}")
6 changes: 0 additions & 6 deletions tests/cli/commands/test_delete.py

This file was deleted.

6 changes: 0 additions & 6 deletions tests/cli/commands/test_generate.py

This file was deleted.

File renamed without changes.
Empty file.
15 changes: 15 additions & 0 deletions tests/integration/commands/test_delete.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions tests/integration/commands/test_generate.py
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/unit/commands/test_delete.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()

Expand Down
14 changes: 7 additions & 7 deletions tests/unit/commands/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()
Expand Down

0 comments on commit 9630f50

Please sign in to comment.