Skip to content

Commit 8bab8d8

Browse files
authored
Quality Assurance Improvements (#3)
Test: * Create test_tree_reader.py * Create test_tree_runner.py Treescriptify: * Update tree_reader.py - debug method generate_from_json * Update tree_runner.py - remove unnecessary spaces between arguments Workflow: * Update ci_run.yml - increase code coverage limit to 95, add windows and macos ci tests, add python 3.11 tests, debug strategy matrix os
1 parent 8b0957b commit 8bab8d8

File tree

5 files changed

+96
-12
lines changed

5 files changed

+96
-12
lines changed

.github/workflows/ci_run.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ on:
1111

1212
jobs:
1313
build:
14-
runs-on: ubuntu-latest
1514
strategy:
15+
max-parallel: 3
1616
matrix:
17-
python-version: [ '3.12' ]
17+
os: [ubuntu-latest, windows-latest, macos-latest]
18+
python-version: [ '3.11', '3.12' ]
19+
runs-on: ${{ matrix.os }}
1820
steps:
1921
- uses: actions/checkout@v4
2022
- name: Set up Python ${{ matrix.python-version }}
@@ -23,13 +25,15 @@ jobs:
2325
python-version: ${{ matrix.python-version }}
2426

2527
- name: Install Dependencies
26-
run: python -m pip install --user pytest pytest-cov
28+
run: |
29+
python -m pip install --upgrade pip
30+
pip install pytest pytest-cov
2731
2832
- name: Run unit tests
29-
run: pytest test/ --cov --cov-report=html --cov-fail-under=75
33+
run: pytest test/ --cov --cov-report=html --cov-fail-under=95
3034

3135
- name: Upload Test Coverage Reports
3236
uses: actions/upload-artifact@v4
3337
with:
34-
name: treescriptify-cov
38+
name: treescriptify-cov-${{ matrix.os }}-${{ matrix.python-version }}
3539
path: htmlcov/
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Testing Module Methods
2+
"""
3+
import pytest
4+
5+
from treescriptify.tree_node_data import TreeNodeData
6+
from treescriptify.tree_reader import generate_from_json
7+
8+
9+
def wrap_root_dir(inner_dirs: str) -> str:
10+
return '{"type":"directory", "name":".", "contents":[' + inner_dirs + ']}'
11+
12+
13+
def get_src_dir() -> str:
14+
return '{"type":"directory", "name":"src", "contents":[]}'
15+
16+
17+
def get_file(name: str) -> str:
18+
return '{"type":"file","name":"' + name + '"}'
19+
20+
21+
def test_generate_from_json_empty_dir_returns_empty_list():
22+
tree_gen = generate_from_json(wrap_root_dir(''))
23+
assert list(tree_gen) == []
24+
25+
26+
def test_generate_from_json_src_dir_returns_tree_node():
27+
tree_gen = generate_from_json(wrap_root_dir(get_src_dir()))
28+
assert list(tree_gen) == [TreeNodeData(0, True, 'src')]
29+
30+
31+
@pytest.mark.parametrize(
32+
'test_input,expected',
33+
[
34+
(wrap_root_dir(get_file('build.file')), [TreeNodeData(0, False, 'build.file')]),
35+
(wrap_root_dir(get_file('build.file') + ',\n\t\t' + get_file('req.txt')), [TreeNodeData(0, False, 'build.file'), TreeNodeData(0, False, 'req.txt')]),
36+
]
37+
)
38+
def test_generate_from_json_(test_input, expected):
39+
tree_gen = generate_from_json(test_input)
40+
assert list(tree_gen) == expected
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Testing Module Methods
2+
"""
3+
import pytest
4+
import subprocess
5+
6+
from input.input_data import InputData
7+
from treescriptify.tree_runner import _check_arguments, get_tree_json
8+
9+
10+
def get_cmd_result(*args, **kwargs):
11+
result = type('MyObject', (object,), {'stdout': '[{"type":"directory","name":".","contents":[{"type":"file","name":"test_script_writer.py"},{"type":"file","name":"test_tree_reader.py"},{"type":"file","name":"test_tree_runner.py"}]}]'})
12+
return result
13+
14+
15+
def test_get_tree_json_():
16+
input_data = InputData()
17+
with pytest.MonkeyPatch().context() as c:
18+
c.setattr(subprocess, 'run', lambda *args, **kwargs: get_cmd_result(*args, **kwargs))
19+
result = get_tree_json(input_data)
20+
expected_files = [
21+
"test_script_writer.py",
22+
"test_tree_reader.py",
23+
"test_tree_runner.py",
24+
]
25+
for filename in expected_files:
26+
assert filename in result
27+
28+
29+
@pytest.mark.parametrize(
30+
'test_input,expected',
31+
[
32+
(InputData(include_hidden=False, git_ignore=False), ''),
33+
(InputData(git_ignore=False), '-a'),
34+
(InputData(), '-a --gitignore'),
35+
(InputData(directories_only=True), '-d -a --gitignore'),
36+
(InputData(prune_dirs=True), '--prune -a --gitignore'),
37+
(InputData(directories_only=True, prune_dirs=True), '-d --prune -a --gitignore'),
38+
]
39+
)
40+
def test_check_arguments(test_input, expected):
41+
assert _check_arguments(test_input) == expected

treescriptify/tree_reader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def generate_from_json(json_string: str) -> Generator[TreeNodeData, None, None]:
1010
"""Read the JSON string and generate TreeNodeData for all elements.
1111
"""
12-
full_json = json.loads(json_string)[0]
12+
full_json = json.loads(json_string)
1313
for i in full_json['contents']:
1414
for node in _process_node(i, 0):
1515
yield node

treescriptify/tree_runner.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,20 @@ def get_tree_json(data: InputData) -> str:
1515
shell=True,
1616
timeout=3
1717
)
18-
output = result.stdout
1918
#error = result.stderr
20-
return output
19+
return result.stdout
2120

2221

2322
def _check_arguments(data: InputData) -> str:
2423
"""Check the Input Data and map flags to tree command.
2524
"""
2625
extras = []
2726
if data.directories_only:
28-
extras.append('-d ')
27+
extras.append('-d')
2928
if data.prune_dirs:
30-
extras += ['--prune ']
29+
extras += ['--prune']
3130
if data.include_hidden:
32-
extras += ['-a ']
31+
extras += ['-a']
3332
if data.git_ignore:
34-
extras.append('--gitignore ')
33+
extras.append('--gitignore')
3534
return ' '.join(extras)

0 commit comments

Comments
 (0)