Skip to content

Commit f5641e0

Browse files
committed
move test scenarios to subdirectory and add optional --source_dir
1 parent 8009e44 commit f5641e0

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
@@ -163,14 +163,15 @@ def get_active_network(namespace):
163163

164164
@click.command(context_settings={"ignore_unknown_options": True})
165165
@click.argument("scenario_file", type=click.Path(exists=True, file_okay=True, dir_okay=False))
166+
@click.option("--source_dir", type=click.Path(exists=True, file_okay=False, dir_okay=True), required=False)
166167
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
167-
def run(scenario_file: str, additional_args: tuple[str]):
168+
def run(scenario_file: str, source_dir, additional_args: tuple[str]):
168169
"""
169170
Run a scenario from a file.
170171
Pass `-- --help` to get individual scenario help
171172
"""
172173
scenario_path = Path(scenario_file).resolve()
173-
scenario_dir = scenario_path.parent
174+
scenario_dir = scenario_path.parent if not source_dir else Path(source_dir).resolve()
174175
scenario_name = scenario_path.stem
175176

176177
if additional_args and ("--help" in additional_args or "-h" in additional_args):
@@ -203,15 +204,24 @@ def run(scenario_file: str, additional_args: tuple[str]):
203204
def filter(path):
204205
if any(needle in str(path) for needle in [".pyc", ".csv", ".DS_Store"]):
205206
return False
206-
return any(
207-
needle in str(path) for needle in ["commander.py", "test_framework", scenario_name]
208-
)
209-
207+
if any(needle in str(path) for needle in ["__init__.py", "commander.py", "test_framework", scenario_path.name]):
208+
print(f"Including: {path}")
209+
return True
210+
return False
211+
212+
# In case the scenario file is not in the root of the archive directory,
213+
# we need to specify its relative path as a submodule
214+
# First get the path of the file relative to the source directory
215+
relative_path = scenario_path.relative_to(scenario_dir)
216+
# Remove the '.py' extension
217+
relative_name = relative_path.with_suffix("")
218+
# Replace path separators with dots and pray the user included __init__.py
219+
module_name = ".".join(relative_name.parts)
210220
# Compile python archive
211221
zipapp.create_archive(
212222
source=scenario_dir,
213223
target=archive_buffer,
214-
main=f"{scenario_name}:main",
224+
main=f"{module_name}:main",
215225
compressed=True,
216226
filter=filter,
217227
)

0 commit comments

Comments
 (0)