Skip to content

Commit 6b3fe06

Browse files
Merge pull request #36 from s-ccs/unit-tests
Unit tests addition
2 parents 445adbb + c54c807 commit 6b3fe06

File tree

15 files changed

+184
-12
lines changed

15 files changed

+184
-12
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
data/
1+
/data/
22
config/
33
pyxdf
44
pydataverse

lslautobids/config_globals.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import yaml
3+
import sys
34

45
class CLIArgs:
56
_instance = None
@@ -52,13 +53,20 @@ def parse_yaml_file(yaml_file):
5253
return None
5354

5455

55-
config_file = os.path.join(os.path.expanduser("~"),'.config/lslautobids/autobids_config.yaml')
56+
# Determine config path based on context
57+
if "pytest" in sys.modules:
58+
config_file = os.path.join(os.path.expanduser("~"), ".config/lslautobids/test-autobids_config.yaml")
59+
else:
60+
config_file = os.path.join(os.path.expanduser("~"), ".config/lslautobids/autobids_config.yaml")
5661
config = parse_yaml_file(config_file)
5762

58-
project_root = os.path.join(os.path.expanduser("~"),config['PROJECT_ROOT'])
59-
bids_root = os.path.join(os.path.expanduser("~"),config['BIDS_ROOT'])
60-
project_stim_root = os.path.join(os.path.expanduser("~"),config['PROJECT_STIM_ROOT'])
61-
api_key = config['API_KEY']
62-
dataverse_base_url = config['BASE_URL']
63-
parent_dataverse_name = config['PARENT_DATAVERSE_NAME']
63+
if config:
64+
project_root = os.path.join(os.path.expanduser("~"), config["PROJECT_ROOT"])
65+
bids_root = os.path.join(os.path.expanduser("~"), config["BIDS_ROOT"])
66+
project_stim_root = os.path.join(os.path.expanduser("~"), config["PROJECT_STIM_ROOT"])
67+
api_key = config.get("API_KEY", "")
68+
dataverse_base_url = config.get("BASE_URL", "")
69+
parent_dataverse_name = config.get("PARENT_DATAVERSE_NAME", "")
70+
else:
71+
project_root = bids_root = project_stim_root = api_key = dataverse_base_url = parent_dataverse_name = None
6472

lslautobids/gen_project_config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ def main():
7272
argparser = argparse.ArgumentParser(description='Get the project name')
7373
argparser.add_argument('-p','--project_name', type=str, help='Enter the project name')
7474
argparser.add_argument('-s','--standalone_toml', type=str, default='no', help= 'To enable the standalone toml file creation')
75+
argparser.add_argument('-c','--config_file', type=str, default=None, help='Optional path to a custom YAML config file')
7576
args = argparser.parse_args()
7677

78+
print(f"ARGS: {args}")
79+
80+
# Replace hardcoded path
81+
config_file = args.config_file or os.path.join(os.path.expanduser("~"),'.config/lslautobids/autobids_config.yaml')
82+
7783
# get the project name and check if the project exists
7884
project_name = args.project_name
7985
standalone_toml = args.standalone_toml
@@ -86,9 +92,6 @@ def main():
8692
print('Creating standalone TOML file')
8793
file_path = file_name
8894
else:
89-
90-
# get the config file and parse it
91-
config_file = os.path.join(os.path.expanduser("~"),'.config/lslautobids/autobids_config.yaml')
9295
config = parse_yaml_file(config_file)
9396
project_root = config['PROJECT_ROOT']
9497
# Define the path to the folder where you want to save the TOML file

lslautobids/processing_new_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def process_new_files(file_status: List[str],logger) -> None:
2727
file_name_no_ext, ext = os.path.splitext(file_name)
2828
if re.search(r'_old\d*$', file_name_no_ext):
2929
logger.error(f"File '{file_name}' appears to be a duplicate. It ends with an '_old' suffix. Please manually check the file.")
30-
raise RuntimeError("Duplicate file detected. Please check the file manually.")
30+
raise SystemExit("Duplicate file detected. Please check the file manually.")
3131

3232
processed_files.append(file_name_no_ext + ext)
3333

tests/__init__.py

Whitespace-only changes.

tests/run_all_tests.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
# --- Setup ---
6+
BASE_DIR = os.path.dirname(__file__) # base tests directory
7+
TESTCASES_DIR = os.path.join(BASE_DIR, "testcases")
8+
TEST_UTILS_DIR = os.path.join(BASE_DIR, "test_utils")
9+
10+
# Make both testcases and test_utils importable
11+
sys.path.insert(0, TESTCASES_DIR)
12+
sys.path.insert(0, TEST_UTILS_DIR)
13+
14+
15+
print("Searching for test directories...\n")
16+
17+
for folder in os.listdir(TESTCASES_DIR):
18+
folder_path = os.path.join(TESTCASES_DIR, folder)
19+
20+
if (
21+
folder.startswith("test_")
22+
and os.path.isdir(folder_path)
23+
and any(f.endswith(".py") for f in os.listdir(folder_path))
24+
and os.path.exists(os.path.join(folder_path, "data"))
25+
):
26+
print(f"Running tests in: {folder} which has folder path {folder_path}")
27+
subprocess.run(["pytest", folder_path])
28+
else:
29+
print(f"Skipping: {folder} (no tests or data)\n")

tests/test_utils/path_config.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
def get_root_paths(test_file: str):
4+
"""
5+
Given a test file (__file__), return relevant test root paths.
6+
"""
7+
# Use the test_file argument, not __file__ from path_config.py
8+
test_folder = os.path.basename(os.path.dirname(test_file))
9+
10+
# Go up to the test folder's path and into its `data/` directory
11+
base_dir = os.path.abspath(os.path.join(os.path.dirname(test_file), "data"))
12+
13+
print(f'The base dir in the get_roots_path function is "{base_dir}"')
14+
return {
15+
"project_root": os.path.join(base_dir, "projects"),
16+
"bids_root": os.path.join(base_dir, "bids"),
17+
"project_stim_root": os.path.join(base_dir, "project_stimulus"),
18+
}
19+
20+

tests/testcases/test_old_suffix/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
time event what
2+
00:00 cap size selection
3+
00:00 camera working y/n
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
id age gender handedness dom_eye no_preex_conditions visual_acuity_test remarks

0 commit comments

Comments
 (0)