Skip to content

Commit

Permalink
add task_creates_task test, add app-wide list of all tasks created
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 13, 2024
1 parent 34d8a0a commit b2d0006
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"program": "${workspaceFolder}/hancho.py",
"cwd": "${workspaceFolder}/tests",
"args": ["--force", "-j1", "multiple_commands.hancho"],
"args": ["--force", "-j1", "task_creates_task.hancho"],
"console": "integratedTerminal",
"justMyCode": false,
},
Expand Down
19 changes: 14 additions & 5 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ class Encoder(json.JSONEncoder):
"""Types the encoder doesn't understand just get stringified."""

def default(self, o):
if isinstance(o, Path):
return f"Path {o}"
return str(o)

return json.dumps(self, indent=2, cls=Encoder)
Expand Down Expand Up @@ -330,10 +332,17 @@ def __call__(self, files_in, files_out=None, **kwargs):
Path(inspect.stack(context=0)[1].filename).parent, self.root_dir
)
task.work_dir = relpath(Path.cwd(), self.root_dir)
task.load_dir = relpath(Path(app.mod_stack[-1].__file__).parent, self.root_dir)

# A task that's created during task execution instead of module loading will have no mod
# stack entry to pull load_dir from, so it runs from '.' (root_dir) instead.
if app.mod_stack:
task.load_dir = relpath(Path(app.mod_stack[-1].__file__).parent, self.root_dir)
else:
task.load_dir = Path(".")

coroutine = task.run_async()
task.promise = asyncio.create_task(coroutine)
app.all_tasks.append(task)
return task


Expand Down Expand Up @@ -429,9 +438,9 @@ async def task_main(self):

# Check for duplicate task outputs
for file in self.abs_files_out:
if file in app.hancho_outs:
if file in app.all_files_out:
raise NameError(f"Multiple rules build {file}!")
app.hancho_outs.add(file)
app.all_files_out.add(file)

# Check if we need a rebuild
self.reason = self.needs_rerun(self.force)
Expand Down Expand Up @@ -511,7 +520,6 @@ async def run_command(self, command):

# Create the subprocess via asyncio and then await the result.
with Chdir(self.task_dir):
log(f"Running {command}")
proc = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
Expand Down Expand Up @@ -617,7 +625,8 @@ class App:
def __init__(self):
self.hancho_mods = {}
self.mod_stack = []
self.hancho_outs = set()
self.all_tasks = []
self.all_files_out = set()
self.tasks_total = 0
self.tasks_pass = 0
self.tasks_fail = 0
Expand Down
15 changes: 15 additions & 0 deletions tests/task_creates_task.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from hancho import *

rule1 = Rule(
command = "touch {files_out}"
)

def callback(task):
rule1([], "dummy.txt")
return []

rule2 = Rule(
command = callback
)

rule2([], [])
3 changes: 3 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ def test_cancellation(self):
self.assertFalse(Path("build/fail_result.txt").exists())
self.assertFalse(Path("build/should_not_be_created.txt").exists())

def test_task_creates_task(self):
self.assertEqual(0, run_hancho("task_creates_task"))
self.assertTrue(Path("build/dummy.txt").exists())

################################################################################

Expand Down

0 comments on commit b2d0006

Please sign in to comment.