Skip to content

Commit

Permalink
Fix more things related to the pathlib port
Browse files Browse the repository at this point in the history
Add more tests to improve coverage
  • Loading branch information
Gagi2k committed Aug 7, 2024
1 parent 800579a commit e318e8e
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 11 deletions.
10 changes: 5 additions & 5 deletions qface/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from qface.utils import load_filters
from qface.shell import sh

here = Path(__file__).dirname()
here = Path(__file__).parent
logging.basicConfig()


Expand All @@ -19,7 +19,7 @@ def run_generator(spec, src, dst, features, force):
project = Path(dst).name
system = FileSystem.parse(src)

extra_filters_path = spec.dirname() / 'filters.py'
extra_filters_path = spec.parent / 'filters.py'
extra_filters = load_filters(extra_filters_path)

ctx = {
Expand All @@ -29,7 +29,7 @@ def run_generator(spec, src, dst, features, force):
}

generator = RuleGenerator(
search_path=spec.dirname() / 'templates',
search_path=spec.parent / 'templates',
destination=dst,
context=ctx,
features=features,
Expand All @@ -40,7 +40,7 @@ def run_generator(spec, src, dst, features, force):


@click.command()
@click.option('--rules', type=click.Path(exists=True, file_okay=True))
@click.option('--rules', type=click.Path(exists=True, file_okay=True), required=True)
@click.option('--target', type=click.Path(exists=False, file_okay=False))
@click.option('--reload/--no-reload', default=False, help="Auto reload script on changes")
@click.option('--scaffold/--no-scaffold', default=False, help="Add extrac scaffolding code")
Expand All @@ -55,7 +55,7 @@ def main(rules, target, reload, source, watch, scaffold, feature, force, run):
argv = sys.argv.copy()
argv.remove('--reload')
watch_list = list(source)
watch_list.append(rules.dirname())
watch_list.append(rules.parent)
if watch:
watch_list.append(watch)
monitor(args=argv, watch=watch_list)
Expand Down
8 changes: 3 additions & 5 deletions qface/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Generator(object):
""" enables strict code generation """

def __init__(self, search_path, context={}, force=False):
if not isinstance(search_path, (list, tuple)):
search_path = [search_path]
loader = ChoiceLoader([
FileSystemLoader(search_path),
PackageLoader('qface')
Expand Down Expand Up @@ -209,7 +211,7 @@ def _has_different_content(self, data, path):
if not path.exists():
return True
dataHash = hashlib.new('md5', data.encode('utf-8')).digest()
pathHash = path.read_hash('md5')
pathHash = hashlib.new('md5', path.read_bytes()).digest()
return dataHash != pathHash

def register_filter(self, name, callback):
Expand Down Expand Up @@ -410,10 +412,6 @@ def load_yaml(document: Path, required=False):
sys.exit(-1)
return {}
try:
# Silence the deprecation warning in newer path.py
# but keep supporting older versions
if not hasattr(Path, 'read_text'):
document.read_text = document.text
return yaml.load(document.read_text(), Loader=Loader)
except yaml.YAMLError as exc:
error = document
Expand Down
Empty file added tests/app_templates/filters.py
Empty file.
4 changes: 4 additions & 0 deletions tests/app_templates/rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
frontend:
module:
documents:
- '{{module.module_name|lower}}module.txt': 'module.txt'
1 change: 1 addition & 0 deletions tests/app_templates/templates/module.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{module}}
4 changes: 4 additions & 0 deletions tests/templates/rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
frontend:
module:
documents:
- '{{module.module_name|lower}}module.txt': 'module.txt'
59 changes: 59 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging
import logging.config
import subprocess
import os
import tempfile
from pathlib import Path


# logging.config.fileConfig('logging.ini')
logging.basicConfig()

log = logging.getLogger(__name__)

here = Path(__file__).parent

inputPath = Path('tests/in')
log.debug('input path folder: {0}'.format(inputPath.absolute()))

env = os.environ.copy()
env['PYTHONPATH'] = str(here.parent)

def test_help():
result = subprocess.run(['python3', './qface/app.py', "--help"], stdout=subprocess.PIPE, env=env)
assert result.returncode == 0
assert """
Usage: app.py [OPTIONS] [SOURCE]...
Options:
--rules PATH [required]
--target DIRECTORY
--reload / --no-reload Auto reload script on changes
--scaffold / --no-scaffold Add extrac scaffolding code
--watch DIRECTORY
--feature TEXT
--run TEXT run script after generation
--force / --no-force forces overwriting of files
--help Show this message and exit.
""", result.stdout

def test_generate():
tmpDir = tempfile.TemporaryDirectory()
result = subprocess.run(['python3', './qface/app.py', "--rules", "tests/app_templates/rules.yaml", "--target", tmpDir.name, "--force", "tests/in/com.pelagicore.ivi.tuner.qface"], stdout=subprocess.PIPE, env=env)
assert result.returncode == 0
assert """
merge: com.pelagicore.ivi.tuner.yaml
process: frontend
""", result.stdout

def test_generate_and_run():
tmpDir = tempfile.TemporaryDirectory()
result = subprocess.run(['python3', './qface/app.py', "--rules", "tests/app_templates/rules.yaml", "--target", tmpDir.name, "--force", "tests/in/com.pelagicore.ivi.tuner.qface", "--run", "echo DONE"], stdout=subprocess.PIPE, env=env)
assert result.returncode == 0
assert """
merge: com.pelagicore.ivi.tuner.yaml
process: frontend
$ echo DONE
DONE
""", result.stdout

37 changes: 36 additions & 1 deletion tests/test_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from qface.generator import FileSystem, Generator
from qface.generator import FileSystem, Generator, RuleGenerator
from qface.utils import load_filters
from unittest.mock import patch
from io import StringIO
Expand Down Expand Up @@ -79,6 +79,30 @@ def test_destination_prefix():
assert Path(path).exists() == True
shutil.rmtree(out, ignore_errors=True)

def test_regeneration():
system = FileSystem.parse(inputPath)
out = Path('tests/out')
shutil.rmtree(out, ignore_errors=True)
out.mkdir(parents=True, exist_ok=True)
generator = Generator(search_path='tests/templates')
for module in system.modules:
dst_template = '{{out}}/{{module|lower}}.txt'
ctx = {'out': out.absolute(), 'module': module}
generator.write(dst_template, 'module.txt', ctx)
generator.write(dst_template, 'module.txt', ctx)
path = generator.apply(dst_template, ctx)
assert Path(path).exists() == True
shutil.rmtree(out, ignore_errors=True)

def test_rulegenerator():
system = FileSystem.parse(inputPath)
out = Path('tests/out')
shutil.rmtree(out, ignore_errors=True)
out.mkdir(parents=True, exist_ok=True)
generator = RuleGenerator(search_path='tests/templates', destination=out)
generator.process_rules('tests/templates/rules.yaml', system)
shutil.rmtree(out, ignore_errors=True)

@patch('sys.stderr', new_callable=StringIO)
def test_error_template_syntax_error(mock_stderr):
tmpDir = tempfile.TemporaryDirectory()
Expand Down Expand Up @@ -117,3 +141,14 @@ def test_error_template_doesnt_exist(mock_stderr):
path = generator.apply(dst_template, ctx)
assert Path(path).exists() == False
assert mock_stderr.getvalue() == "/doesnt_exist.txt: error: Template not found\n"

@patch('sys.stderr', new_callable=StringIO)
def test_error_yaml_doesnt_exist(mock_stderr):
tmpDir = tempfile.TemporaryDirectory()
system = FileSystem.parse(inputPath)
out = Path('tests/out')
shutil.rmtree(out, ignore_errors=True)
out.mkdir(parents=True, exist_ok=True)
generator = RuleGenerator(search_path='tests/templates', destination=out)
generator.process_rules('doesnt_exist.txt', system)
assert mock_stderr.getvalue() == "yaml document does not exists: doesnt_exist.txt\n"

0 comments on commit e318e8e

Please sign in to comment.