Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Nunamaker committed Jul 27, 2024
1 parent 9f2a268 commit 99889aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Stringify is a developer-friendly tool that uses rsync to gather and stringify c
- File preview options
- Summary view with file tree

## Developers

Create a venv and install dependencies:

```bash
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt -r requirements-dev.txt
```

## Installation

```bash
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
],
},
install_requires=[
# Add any dependencies here
"pyyaml>=6.0.1",
],
extras_require={
'dev': ['pytest>=8.3.2'],
Expand Down
13 changes: 9 additions & 4 deletions stringify/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ def load_presets():
logger.error(f"Error reading {PRESETS_FILE}: {e}")
print(f"Error reading {PRESETS_FILE}. Using empty presets.")
else:
with open(DEFAULT_PRESETS_FILE, 'r') as df:
with open(PRESETS_FILE, 'w') as f:
f.write(df.read())
return yaml.safe_load(df)
try:
with open(DEFAULT_PRESETS_FILE, 'r') as df:
content = df.read()
with open(PRESETS_FILE, 'w') as f:
f.write(content)
return yaml.safe_load(content) or {}
except Exception as e:
logger.error(f"Error reading or writing preset files: {e}")
print(f"Error with preset files. Using empty presets.")
return {}


Expand Down
38 changes: 24 additions & 14 deletions tests/test_stringify.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import json
import subprocess
from unittest.mock import patch, MagicMock, mock_open

import pytest

import yaml
from stringify import utils, cli


Expand All @@ -18,18 +17,18 @@ def temp_config(tmp_path):


def test_load_presets(temp_config):
test_preset = {'test_preset': ['--include=*.py']}
temp_config.write_text(json.dumps(test_preset))
test_preset = {'test_preset': {'args': ['--include=*.py']}}
temp_config.write_text(yaml.dump(test_preset))

presets = utils.load_presets()
assert presets == test_preset


def test_save_presets(temp_config):
test_preset = {'test_preset': ['--include=*.py']}
test_preset = {'test_preset': {'args': ['--include=*.py']}}
utils.save_presets(test_preset)

saved_preset = json.loads(temp_config.read_text())
saved_preset = yaml.safe_load(temp_config.read_text())
assert saved_preset == test_preset


Expand All @@ -39,17 +38,26 @@ def test_check_rsync():
assert utils.check_rsync() == True


def test_load_presets_invalid_json(temp_config):
temp_config.write_text('invalid json')
def test_load_presets_invalid_yaml(temp_config):
temp_config.write_text('invalid: yaml: :]')

presets = utils.load_presets()
assert presets == {}


def test_load_presets_file_not_found(temp_config):
temp_config.unlink()
presets = utils.load_presets()
assert presets == {}
mock_default_content = yaml.dump({'default_preset': {'args': ['--include=*.py']}})

with patch('stringify.utils.PRESETS_FILE', 'nonexistent_file'):
with patch('stringify.utils.DEFAULT_PRESETS_FILE', 'default_presets.yaml'):
with patch('builtins.open', mock_open(read_data=mock_default_content)) as mock_file:
presets = utils.load_presets()

assert presets == {'default_preset': {'args': ['--include=*.py']}}
mock_file.assert_any_call('default_presets.yaml', 'r')
mock_file.assert_any_call('nonexistent_file', 'w')
mock_file().write.assert_called_once_with(mock_default_content)


def test_run_rsync():
Expand Down Expand Up @@ -144,17 +152,19 @@ def mock_isfile(path):
])
def test_copy_to_clipboard(system, command):
test_text = "Test clipboard content"
file_list = ["file1.py"]
num_files = 1
with patch('platform.system', return_value=system):
with patch('subprocess.run') as mock_run:
utils.copy_to_clipboard(test_text, ["file1.py"])
utils.copy_to_clipboard(test_text, file_list, num_files)
mock_run.assert_called_once()
assert mock_run.call_args[0][0][0] == command


def test_main(temp_config):
test_args = ['stringify', '--preset', 'test_preset']
test_preset = {'test_preset': ['--include=*.py']}
temp_config.write_text(json.dumps(test_preset))
test_preset = {'test_preset': {'args': ['--include=*.py']}}
temp_config.write_text(yaml.dump(test_preset))

mock_file_list = ['file1.py', 'file2.py', 'file3.py', 'file4.py', 'file5.py', 'file6.py', 'file7.py']
mock_gathered_code = 'print("Hello")\n' * 26
Expand All @@ -166,4 +176,4 @@ def test_main(temp_config):
with patch('stringify.cli.copy_to_clipboard') as mock_copy:
cli.main()
mock_copy.assert_called_once()
mock_copy.assert_called_with(mock_gathered_code, mock_file_list)
mock_copy.assert_called_with(mock_gathered_code, mock_file_list, 7)

0 comments on commit 99889aa

Please sign in to comment.