Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 26, 2024
1 parent b42e548 commit cb53a26
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/hancho.py",
"cwd": "${workspaceFolder}/tutorial",
"args": ["-j1", "tut2.hancho"],
"cwd": "${workspaceFolder}/tests",
"args": ["missing_input.hancho"],
"console": "integratedTerminal",
"justMyCode": false,
},
Expand Down
22 changes: 14 additions & 8 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ def relpath(path1, path2):


def joinpath(*args):
"""Produces all possible concatenated paths from the given paths (or arrays of paths)."""
"""Returns an array of all possible concatenated paths from the given paths (or arrays of paths)."""
if len(args) > 2:
return joinpath(args[0], joinpath(*args[1:]))
if isinstance(args[0], list):
return [joinpath(a, args[1]) for a in args[0]]
return [path for prefix in args[0] for path in joinpath(prefix, args[1])]
if isinstance(args[1], list):
return [joinpath(args[0], a) for a in args[1]]
return Path(args[0]) / Path(args[1])

return [path for suffix in args[1] for path in joinpath(args[0], suffix)]
return [Path(args[0]) / Path(args[1])]

def color(red=None, green=None, blue=None):
"""Converts RGB color to ANSI format string."""
Expand Down Expand Up @@ -347,11 +346,11 @@ def expand_template(self, template):
macro = template[span.start() : span.end()]
variant = self.eval_macro(macro)
result += " ".join([str(s) for s in self.flatten(variant)])
except BaseException as exc:
except:
log(color(255, 255, 0))
log(f"Expanding template '{old_template}' failed!")
log(color())
raise exc
raise
template = template[span.end() :]
result += template
return result
Expand All @@ -362,7 +361,14 @@ def eval_macro(self, macro):
raise RecursionError(f"Expanding '{template}' failed to terminate")
self.depth += 1
# pylint: disable=eval-used
result = eval(macro[1:-1], {}, self)
try:
result = eval(macro[1:-1], {}, self)
except:
log(color(255, 255, 0))
log(f"Expanding macro '{macro}' failed!")
log(color())
raise

self.depth -= 1
return result

Expand Down
6 changes: 3 additions & 3 deletions tests/missing_input.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ from hancho import *

build_config.defaults(
command_path = Path.cwd(),
source_path = Path.cwd(),
build_path = Path.cwd()
source_path = Path.cwd() / "src",
build_path = Path.cwd() / "build",
)

rules = load("rules.hancho", build_config)
rules.touch_outputs("src/does_not_exist.txt", "build/missing_src.txt")
rules.touch_outputs("does_not_exist.txt", "missing_src.txt")
18 changes: 9 additions & 9 deletions tests/rules.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ from hancho import *
import os

if os.name == 'nt':
config.depformat = "msvc"
compile_command = "cl.exe /c {rel_source_files} /sourceDependencies {rel_build_deps} /Fo:{files_out}"
build_config.depformat = "msvc"
compile_command = "cl.exe /c {rel_source_files} /sourceDependencies {rel_build_deps} /Fo:{rel_build_files}"
elif os.name == 'posix':
config.depformat = "gcc"
compile_command = "gcc -MMD -c {files_in} -o {files_out}"
build_config.depformat = "gcc"
compile_command = "gcc -MMD -c {rel_source_files} -o {rel_build_files}"
else:
compile_command = "<unknown OS>"

compile_cpp = Rule(
compile_cpp = build_config.rule(
command = compile_command,
files_out = "{swap_ext(files_in, '.o')}",
depfile = "{swap_ext(files_out, '.d')}",
build_files = "{swap_ext(rel_source_files, '.o')}",
build_deps = "{swap_ext(rel_source_files, '.d')}",
)

touch_outputs = Rule(
command = "touch {files_out}"
touch_outputs = build_config.rule(
command = "touch {rel_build_files[0]}"
)
30 changes: 16 additions & 14 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,23 @@ def test_config_inheritance(self):

# This should fail because it was expecting inheritance from its parent.
self.assertNotEqual(0, run_hancho("config_child").returncode)
#
# def test_command_missing(self):
# """Rules with missing commands should fail"""
# result = run_hancho("command_missing")
# self.assertTrue("Config key 'command' was never defined" in result.stderr)
#
# def test_missing_field(self):
# """Missing fields should raise an error when expanded"""
# result = run_hancho("missing_field")
# self.assertTrue("NameError: name 'this_field_does_not_exist' is not defined" in result.stderr)

# def test_missing_input(self):
# """We should fail if an input is missing"""
# self.assertNotEqual(0, run_hancho("missing_input"))
#
def test_command_missing(self):
"""Rules with missing commands should fail"""
result = run_hancho("command_missing")
self.assertTrue("Config key 'command' was never defined" in result.stderr)

def test_missing_field(self):
"""Missing fields should raise an error when expanded"""
result = run_hancho("missing_field")
self.assertTrue("NameError: name 'this_field_does_not_exist' is not defined" in result.stderr)

def test_missing_input(self):
"""We should fail if an input is missing"""
result = run_hancho("missing_input")
self.assertTrue("FileNotFoundError" in result.stderr)
self.assertTrue("does_not_exist.txt" in result.stderr)

# def test_missing_named_dep(self):
# """Missing named dep should fail"""
# self.assertNotEqual(0, run_hancho("missing_named_dep"))
Expand Down

0 comments on commit cb53a26

Please sign in to comment.