Skip to content

Commit

Permalink
add some dynamic tests, fix handling of tasks with callbacks that pro…
Browse files Browse the repository at this point in the history
…duce more tasks
  • Loading branch information
aappleby committed Apr 8, 2024
1 parent b6f2398 commit 609ceac
Show file tree
Hide file tree
Showing 27 changed files with 190 additions and 91 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": ["-f", "-v", "-j1", "-d", "tut00.hancho"],
"cwd": "${workspaceFolder}/tests/dynamic_tests/filelist",
"args": ["-f", "-j1", "build.hancho"],
"console": "integratedTerminal",
"justMyCode": false,
//"preLaunchTask": "Wipe build",
Expand Down
177 changes: 94 additions & 83 deletions hancho.py

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions tests/dynamic_tests/filelist/build.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

import random
from glob import glob

build_config.verbose = True

build_config.task(
desc = "Touch 3 random files in src/ - {build_files}",
command = "touch {build_files}",
source_files = [],
build_files = random.sample(glob("src/*"), 3),
build_path = build_config.source_path,
)

def generate_filelist(task):
with open(task.action.build_files[0], "w") as file:
for f in random.sample(glob("src/*"), 3):
file.write(str(f) + "\n")
return task.action.build_files

filelist_txt = build_config.task(
desc = "Write the names of 3 random files in src/ to {build_files}",
command = generate_filelist,
source_files = [],
build_files = "filelist.txt",
)

def generate_result(task):
"""Read build/filelist.txt and create a new task to cat those files together into result.txt"""
from_filelist = [f.strip() for f in open(task.action.source_files[0], "r").readlines()]
return build_config.task(
desc = "Concatenate {rel_source_files} into {rel_build_files}",
command = "cat {rel_source_files} > {rel_build_files}",
source_files = from_filelist,
build_files = "result.txt",
)

result_txt = build_config.task(
desc = "Read {rel_source_files} and use its contents to generate another task",
command = generate_result,
source_files = filelist_txt,
build_files = [],
)

build_config.task(
desc = "Print the contents of {rel_source_files}",
command = "cat {rel_source_files}",
source_files = result_txt,
build_files = []
)
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text0
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text1
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text2
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text3
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text4
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text5
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text6
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text7
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text8
1 change: 1 addition & 0 deletions tests/dynamic_tests/filelist/src/text9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text9
18 changes: 18 additions & 0 deletions tests/dynamic_tests/random_deps/build.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Touches 3 random source files, then cats 3 random source files into build/result.txt

import random

all_deps = [f"src/text{d}.txt" for d in range(0, 9)]

build_config.task(
command = "touch {build_files}",
source_files = [],
build_files = random.sample(all_deps, 3),
build_path = build_config.source_path,
)

build_config.task(
command = "cat {rel_source_files} > {rel_build_files}",
source_files = random.sample(all_deps, 3),
build_files = "result.txt",
)
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text0
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text1
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text2
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text3
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text4
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text5
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text6
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text7
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text8
1 change: 1 addition & 0 deletions tests/dynamic_tests/random_deps/src/text9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text9
2 changes: 1 addition & 1 deletion tutorial/tut03.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def compile_cpp(source, config):
return result

def link_cpp(tasks, binary, config):
objs = [task.task_config.build_files for task in tasks]
objs = [task.config.build_files for task in tasks]
return config.task(
desc = f"Link {' and '.join(objs)} into {config.build_path + binary}",
command = f"g++ {' '.join(objs)} -o {config.build_path + binary}",
Expand Down
2 changes: 1 addition & 1 deletion tutorial/tut04_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def compile_cpp(source, config):
return result

def link_cpp(tasks, binary, config):
objs = [task.task_config.build_files for task in tasks]
objs = [task.config.build_files for task in tasks]
return config.task(
desc = f"Link {' and '.join(objs)} into {config.build_path + binary}",
command = f"g++ {' '.join(objs)} -o {config.build_path + binary}",
Expand Down
8 changes: 4 additions & 4 deletions tutorial/tut50.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ build_config.build_tag = "tut50"

def sync_callback(task):
#print("Sync callback begin")
for abs_file in task.build_files:
for abs_file in task.action.build_files:
#print(f"Writing {abs_file}")
with open(abs_file, 'w', encoding="utf-8") as file:
file.write("hello world")
#print("Sync callback end")
return task.build_files
return task.action.build_files

fast_task = build_config.rule(command = sync_callback)
fast_task(["src/main.cpp"], ["fast1.txt", "fast2.txt", "fast3.txt"])
Expand All @@ -21,13 +21,13 @@ fast_task(["src/main.cpp"], ["fast1.txt", "fast2.txt", "fast3.txt"])

async def async_callback(task):
#print("Async callback begin")
for abs_file in task.build_files:
for abs_file in task.action.build_files:
#print(f"Writing {abs_file}")
with open(abs_file, 'w', encoding="utf-8") as file:
file.write("hello world")
await asyncio.sleep(0.1)
#print("Async callback end")
return task.build_files
return task.action.build_files

slow_task = build_config.rule(command = async_callback)
slow_task(["src/main.cpp"], ["slow1.txt", "slow2.txt", "slow3.txt"])
Expand Down

0 comments on commit 609ceac

Please sign in to comment.