-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #822 from astronomerritt/print_demo_command
Printing/testing demo command.
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
def get_demo_command(): | ||
""" | ||
Returns the current working version of the Sorcha demo command as a string. | ||
If the Sorcha run command changes, updating this function will ensure | ||
associated unit tests pass. | ||
Parameters | ||
----------- | ||
None. | ||
Returns | ||
----------- | ||
None. | ||
""" | ||
|
||
return "sorcha -c sorcha_config_demo.ini -p sspp_testset_colours.txt -ob sspp_testset_orbits.des -pd baseline_v2.0_1yr.db -o ./ -t testrun_e2e" | ||
|
||
|
||
def print_demo_command(printall=True): | ||
""" | ||
Prints the current working version of the Sorcha demo command to the terminal, with | ||
optional functionality to also tell the user how to copy the demo files. | ||
Parameters | ||
----------- | ||
printall : boolean | ||
When True, prints the demo command plus the instructions for copying the demo files. | ||
When False, prints the demo command only. | ||
Returns | ||
----------- | ||
None. | ||
""" | ||
|
||
current_demo_command = get_demo_command() | ||
|
||
print("\nThe command to run the Sorcha demo in this version of Sorcha is:\n") | ||
|
||
print("\033[1;32;40m" + current_demo_command + "\033[0m\n") | ||
|
||
print("WARNING: This command assumes that the demo files are in your working directory.\n") | ||
|
||
if printall: | ||
print("You can copy the demo files into your working directory by running:\n") | ||
|
||
print("\033[1;32;40msorcha_copy_demo_files\033[0m\n") | ||
|
||
print("Or, to copy them into a directory of your choice, run:\n") | ||
|
||
print("\033[1;32;40msorcha_copy_demo_files -p /path/to/files \033[0m\n") | ||
|
||
print( | ||
"If copying into a directory of your choice, you will need to modify the demo command to path to your files.\n" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
print_demo_command() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This test file checks to see if Sorcha runs correctly and produces output | ||
# using the current demo command. | ||
# It does not check the output for correctness. This is covered by test_demo_end2end.py. | ||
|
||
import os | ||
import pytest | ||
import glob | ||
from pathlib import Path | ||
|
||
|
||
@pytest.fixture | ||
def setup_and_teardown_for_demo_command_line(): | ||
# Record initial working directory | ||
initial_wd = os.getcwd() | ||
|
||
# Find where the demo files are installed on this machine | ||
path_to_file = os.path.abspath(__file__) | ||
path_to_demo = os.path.join(str(Path(path_to_file).parents[2]), "demo") | ||
|
||
# Move to the demo directory | ||
os.chdir(path_to_demo) | ||
|
||
# Yield to pytest to run the test | ||
yield | ||
|
||
# After running the test, delete the created files... | ||
|
||
os.remove("testrun_e2e.csv") | ||
|
||
os.remove(glob.glob("*sorcha.err")[0]) | ||
os.remove(glob.glob("*sorcha.log")[0]) | ||
|
||
# And move back to initial working directory. | ||
os.chdir(initial_wd) | ||
|
||
|
||
def test_demo_command_line(setup_and_teardown_for_demo_command_line): | ||
"""This checks to see if the current demo command-line command for Sorcha | ||
results in a successful run, i.e.: it runs without error and produces output. | ||
It does not check the actual output for correctness. This is done by | ||
test_demo_end2end.py. | ||
""" | ||
|
||
from sorcha.utilities.sorcha_demo_command import get_demo_command | ||
|
||
current_demo_command = get_demo_command() | ||
|
||
# usually the ephemeris files have already been downloaded by the | ||
# ephemeris end-to-end test, but we can't rely on test order for this to | ||
# work! if the files already exist in the default location this will do nothing. | ||
os.system("bootstrap_sorcha_data_files") | ||
|
||
os.system(current_demo_command) | ||
|
||
assert os.path.exists("testrun_e2e.csv") | ||
|
||
# also check to make sure the error log is empty :) | ||
error_log = glob.glob("*sorcha.err")[0] | ||
|
||
assert os.stat(error_log).st_size == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
def test_copy_demo_command(capsys): | ||
from sorcha.utilities.sorcha_demo_command import print_demo_command, get_demo_command | ||
|
||
print_demo_command(printall=True) | ||
current_demo_command = get_demo_command() | ||
|
||
captured = capsys.readouterr() | ||
|
||
expected = ( | ||
"\nThe command to run the Sorcha demo in this version of Sorcha is:\n\n" | ||
+ "\033[1;32;40m" | ||
+ current_demo_command | ||
+ "\033[0m\n\n" | ||
+ "WARNING: This command assumes that the demo files are in your working directory.\n\n" | ||
+ "You can copy the demo files into your working directory by running:\n\n" | ||
+ "\033[1;32;40msorcha_copy_demo_files\033[0m\n\n" | ||
+ "Or, to copy them into a directory of your choice, run:\n\n" | ||
+ "\033[1;32;40msorcha_copy_demo_files -p /path/to/files \033[0m\n\n" | ||
+ "If copying into a directory of your choice, you will need to modify the demo command to path to your files.\n\n" | ||
) | ||
|
||
assert captured.out == expected |