Skip to content

Commit 371fc20

Browse files
committed
move test scenarios to subdirectory and add optional --source_dir
1 parent f2a3222 commit 371fc20

File tree

13 files changed

+49
-14
lines changed

13 files changed

+49
-14
lines changed

resources/scenarios/ln_init.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,6 @@ def funded_lnnodes():
183183

184184
def main():
185185
LNInit().main()
186+
187+
if __name__ == "__main__":
188+
main()

resources/scenarios/miner_std.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ def run_test(self):
7070

7171
def main():
7272
MinerStd().main()
73+
74+
if __name__ == "__main__":
75+
main()

resources/scenarios/reconnaissance.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,6 @@ def run_test(self):
8282

8383
def main():
8484
Reconnaissance().main()
85+
86+
if __name__ == "__main__":
87+
main()

resources/scenarios/signet_miner.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,6 @@ def get_args(parser):
564564

565565
def main():
566566
SignetMinerScenario().main()
567+
568+
if __name__ == "__main__":
569+
main()

resources/scenarios/test_scenarios/__init__.py

Whitespace-only changes.

resources/scenarios/testscenario_buggy_failure.py renamed to resources/scenarios/test_scenarios/buggy_failure.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ def run_test(self):
2222

2323
def main():
2424
Failure().main()
25+
26+
if __name__ == "__main__":
27+
main()

resources/scenarios/testscenario_connect_dag.py renamed to resources/scenarios/test_scenarios/connect_dag.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,6 @@ def assert_connection(self, connector, connectee_index, connection_type: Connect
119119

120120
def main():
121121
ConnectDag().main()
122+
123+
if __name__ == "__main__":
124+
main()

resources/scenarios/testscenario_p2p_interface.py renamed to resources/scenarios/test_scenarios/p2p_interface.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ def run_test(self):
5454

5555
def main():
5656
GetdataTest().main()
57+
58+
if __name__ == "__main__":
59+
main()

resources/scenarios/tx_flood.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ def run_test(self):
6969

7070
def main():
7171
TXFlood().main()
72+
73+
74+
if __name__ == "__main__":
75+
main()

src/warnet/control.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,15 @@ def get_active_network(namespace):
172172
default=False,
173173
help="Stream scenario output and delete container when stopped",
174174
)
175+
@click.option("--source_dir", type=click.Path(exists=True, file_okay=False, dir_okay=True), required=False)
175176
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
176-
def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
177+
def run(scenario_file: str, source_dir, additional_args: tuple[str]):
177178
"""
178179
Run a scenario from a file.
179180
Pass `-- --help` to get individual scenario help
180181
"""
181182
scenario_path = Path(scenario_file).resolve()
182-
scenario_dir = scenario_path.parent
183+
scenario_dir = scenario_path.parent if not source_dir else Path(source_dir).resolve()
183184
scenario_name = scenario_path.stem
184185

185186
if additional_args and ("--help" in additional_args or "-h" in additional_args):
@@ -212,15 +213,24 @@ def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
212213
def filter(path):
213214
if any(needle in str(path) for needle in [".pyc", ".csv", ".DS_Store"]):
214215
return False
215-
return any(
216-
needle in str(path) for needle in ["commander.py", "test_framework", scenario_name]
217-
)
218-
216+
if any(needle in str(path) for needle in ["__init__.py", "commander.py", "test_framework", scenario_path.name]):
217+
print(f"Including: {path}")
218+
return True
219+
return False
220+
221+
# In case the scenario file is not in the root of the archive directory,
222+
# we need to specify its relative path as a submodule
223+
# First get the path of the file relative to the source directory
224+
relative_path = scenario_path.relative_to(scenario_dir)
225+
# Remove the '.py' extension
226+
relative_name = relative_path.with_suffix("")
227+
# Replace path separators with dots and pray the user included __init__.py
228+
module_name = ".".join(relative_name.parts)
219229
# Compile python archive
220230
zipapp.create_archive(
221231
source=scenario_dir,
222232
target=archive_buffer,
223-
main=f"{scenario_name}:main",
233+
main=f"{module_name}:main",
224234
compressed=True,
225235
filter=filter,
226236
)

0 commit comments

Comments
 (0)