diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..349bede --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: Python package + +on: [push, pull_request] + +permissions: + contents: read + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + python-version: ["3.9", "3.10", "3.11"] + + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Test with pytest + run: | + pytest tests/ diff --git a/.gitignore b/.gitignore index 5249f05..1d267dc 100644 --- a/.gitignore +++ b/.gitignore @@ -161,5 +161,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ -# demo, test folder -!demo/** \ No newline at end of file +# testdata folder +!tests/testdata/** \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d969f96 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "python.testing.pytestArgs": ["tests"], + "python.testing.unittestEnabled": false, + "python.testing.pytestEnabled": true +} diff --git a/README.md b/README.md index 6b20f11..3f32a89 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,12 @@ pip install -r requirements.txt python -m files_flattener.cli [] ``` +#### Run the tests + +```sh +pytest tests/ +``` + #### Build the package Ensure `wheel` is installed: @@ -108,10 +114,9 @@ Upload the distribution files to PyPI: ## TODO +- [ ] feat: Add support for spcifying the git repository to clone and flatten. - [ ] refactor: Use click package to implement the CLI. -- [ ] Add dry-run mode to preview the output before writing to the file. - -- [x] Use `.ignore` to exclude or include files for flattening. +- [ ] feat: Add dry-run mode to preview the output before writing to the file. ## License diff --git a/demo/target/.ignore b/demo/target/.ignore deleted file mode 100644 index 4045a45..0000000 --- a/demo/target/.ignore +++ /dev/null @@ -1,4 +0,0 @@ -folder1/file2.txt - -# Exclude itself -.ignore \ No newline at end of file diff --git a/files_flattener/core.py b/files_flattener/core.py index ef1dcd3..5f39266 100644 --- a/files_flattener/core.py +++ b/files_flattener/core.py @@ -2,7 +2,7 @@ import pathspec from concurrent.futures import ThreadPoolExecutor -from .directory_handler import ( +from .identifier_handler import ( DirectoryHandlerFactory, LocalDirectoryHandler, RemoteRepositoryHandler, diff --git a/files_flattener/directory_handler.py b/files_flattener/identifier_handler.py similarity index 90% rename from files_flattener/directory_handler.py rename to files_flattener/identifier_handler.py index 94ddf55..e07b359 100644 --- a/files_flattener/directory_handler.py +++ b/files_flattener/identifier_handler.py @@ -3,7 +3,7 @@ from .common import logger -class DirectoryHandler: +class IdentifierHandler: def __init__(self, identifier): self.identifier = identifier @@ -11,12 +11,12 @@ def prepare(self): raise NotImplementedError -class LocalDirectoryHandler(DirectoryHandler): +class LocalDirectoryHandler(IdentifierHandler): def prepare(self): self.local_directory = self.identifier -class RemoteRepositoryHandler(DirectoryHandler): +class RemoteRepositoryHandler(IdentifierHandler): def prepare(self): self.local_directory = tempfile.mkdtemp() logger.warning( diff --git a/requirements.txt b/requirements.txt index f4b36bd..c888c59 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ colorlog GitPython # For development +pytest twine wheel \ No newline at end of file diff --git a/tests/absimport.py b/tests/absimport.py new file mode 100644 index 0000000..5713f39 --- /dev/null +++ b/tests/absimport.py @@ -0,0 +1,4 @@ +import sys +import os + +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 0000000..fdfa8d0 --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,18 @@ +import absimport # FIRST LINE +import pytest +from files_flattener.core import list_files +from files_flattener.identifier_handler import LocalDirectoryHandler + + +@pytest.mark.parametrize( + "identifier, ignore_file, ignored", + [ + ("tests/testdata/target", None, "folder1/file2.txt"), + ("tests/testdata/target", "tests/testdata/.ignore", "folder1/file2.txt"), + ], +) +def test_list_files(identifier, ignore_file, ignored): + files, handler = list_files(identifier, ignore_file) + assert isinstance(files, list) + assert isinstance(handler, LocalDirectoryHandler) + assert ignored not in files if ignore_file else ignored in files diff --git a/tests/testdata/.ignore b/tests/testdata/.ignore new file mode 100644 index 0000000..6179151 --- /dev/null +++ b/tests/testdata/.ignore @@ -0,0 +1 @@ +folder1/file2.txt diff --git a/demo/output.txt b/tests/testdata/output.txt similarity index 100% rename from demo/output.txt rename to tests/testdata/output.txt diff --git a/demo/target/file3.txt b/tests/testdata/target/file3.txt similarity index 100% rename from demo/target/file3.txt rename to tests/testdata/target/file3.txt diff --git a/demo/target/folder1/file1.txt b/tests/testdata/target/folder1/file1.txt similarity index 100% rename from demo/target/folder1/file1.txt rename to tests/testdata/target/folder1/file1.txt diff --git a/demo/target/folder1/file2.txt b/tests/testdata/target/folder1/file2.txt similarity index 100% rename from demo/target/folder1/file2.txt rename to tests/testdata/target/folder1/file2.txt