Skip to content

Commit

Permalink
Specify comma separated extensions (#6)
Browse files Browse the repository at this point in the history
* Added support for comma separated extensions

* CI/CD fixes

- Added tests
- Fixed python version bug

* Create __init__.py

* Update test_main.py

- fixed attempted import beyond top level package

* Fixed testing func for organized files

* Update workflow.yaml

- Publish pypi package on PR or push
  • Loading branch information
judahpaul16 authored Apr 8, 2024
1 parent 77a079c commit 80c1177
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, 3.10, 3.11]
python-version: [3.8, 3.9, '3.10', 3.11, 3.12]

steps:
- uses: actions/checkout@v4
Expand All @@ -41,10 +41,10 @@ jobs:
- name: Run tests
run: |
pip install pytest
pytest
pytest tests/
- name: Bump patch version
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
if: github.ref == 'refs/heads/main'
run: |
pip install bump2version
bump2version patch
Expand All @@ -56,6 +56,7 @@ jobs:

- name: Build package
run: |
pip install setuptools wheel
python setup.py sdist bdist_wheel
- name: Upload build artifacts
Expand All @@ -67,7 +68,7 @@ jobs:
publish-testpypi:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'pull_request')
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
Expand All @@ -88,7 +89,7 @@ jobs:
deploy:
needs: test
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'created'
if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'pull_request')
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
Expand Down
6 changes: 5 additions & 1 deletion dirconfig/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ def organize_files(task):
rules = task['rules']

for file in os.listdir(source_path):
file_extension = os.path.splitext(file)[1]

for rule in rules:
if file.endswith(rule['extension']):
extensions = [ext.strip() for ext in rule['extension'].split(',')]

if file_extension in extensions:
# For destination paths starting with "/", treat them as absolute paths.
# Otherwise, treat as relative to the source directory.
if rule['destination'].startswith("/"):
Expand Down
Empty file added tests/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
import pytest
import tempfile
import shutil
from unittest.mock import patch
from dirconfig.main import load_config, organize_files, start_daemon, stop_daemon, ChangeHandler

TEST_CONFIG = """
tasks:
- type: file-organization
source: "./test_source"
rules:
- extension: .txt
destination: "text_files"
- extension: .jpg, .jpeg
destination: "images"
"""

@pytest.fixture
def setup_test_env():
"""
Sets up a temporary testing environment, including a test config file
and a test source directory with some files.
"""
original_dir = os.getcwd()
test_dir = tempfile.mkdtemp()
os.chdir(test_dir)

# Create test config file
with open('config.yaml', 'w') as f:
f.write(TEST_CONFIG)

# Create source directory and test files
os.mkdir('test_source')
with open('test_source/test.txt', 'w') as f:
f.write("This is a test text file.")
with open('test_source/image.jpg', 'w') as f:
f.write("This is a test image file.")

yield test_dir # Provide the temporary directory to the test

# Cleanup
os.chdir(original_dir)
shutil.rmtree(test_dir)

def test_load_config():
"""
Tests the load_config function to ensure it correctly loads and parses
the configuration from a YAML file.
"""
with tempfile.NamedTemporaryFile('w', delete=False) as tmpfile:
tmpfile.write(TEST_CONFIG)
tmpfile.close() # Close the file to ensure it's written and flushed

config = load_config(tmpfile.name)
assert 'tasks' in config, "The configuration should have a 'tasks' key"
assert len(config['tasks']) > 0, "There should be at least one task in the configuration"

os.unlink(tmpfile.name) # Clean up the temporary file

def test_organize_files(setup_test_env):
"""
Tests the organize_files function to verify it correctly organizes files
according to the specified rules in the configuration.
"""
config_path = os.path.join(setup_test_env, 'config.yaml')
config = load_config(config_path)
for task in config['tasks']:
if task['type'] == 'file-organization':
organize_files(task)

# Use the setup_test_env path to build the correct assertion paths
text_file_path = os.path.join(setup_test_env, 'test_source', 'text_files', 'test.txt')
image_file_path = os.path.join(setup_test_env, 'test_source', 'images', 'image.jpg')

# Check if the files have been moved to the correct destinations
assert os.path.exists(text_file_path), "Text file should be moved to 'text_files'"
assert os.path.exists(image_file_path), "Image file should be moved to 'images'"

0 comments on commit 80c1177

Please sign in to comment.