Skip to content

Commit

Permalink
test: add tests for bsb new
Browse files Browse the repository at this point in the history
split copy_configuration_template into two functions.
  • Loading branch information
drodarie committed Dec 28, 2024
1 parent 43991f2 commit 17e27eb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
23 changes: 21 additions & 2 deletions bsb/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ def get_config_path():
return [*itertools.chain((os.getcwd(),), env_paths, *plugin_paths.values())]


def copy_configuration_template(template, output="network_configuration.json", path=None):
def get_configuration_template(template, path=None):
"""
Returns the configuration template files matching the provided name.
:param str template: name of the configuration template
:param list path: list of paths to search for configuration templates
:rtype: List[str]
"""
path = [
*map(
os.path.abspath,
Expand All @@ -90,7 +97,19 @@ def copy_configuration_template(template, output="network_configuration.json", p
raise ConfigTemplateNotFoundError(
"'%template%' not found in config path %path%", template, path
)
copy_file(files[0], output)
return files


def copy_configuration_template(template, output="network_configuration.json", path=None):
"""
Copy the first configuration template file matching the provided name to the provided
output filename.
:param str template: name of the configuration template
:param str output: name of the output file
:param list path: list of paths to search for configuration templates
"""
copy_file(get_configuration_template(template, path)[0], output)


def format_configuration_content(parser_name: str, config: "Configuration", **kwargs):
Expand Down
65 changes: 64 additions & 1 deletion tests/test_projects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,68 @@
import os
import shutil
import subprocess
import unittest

from bsb_test import skip_parallel

from bsb.config import get_configuration_template, parse_configuration_file


class TestProjects(unittest.TestCase):
pass
@skip_parallel
def test_bsb_new(self):
message = subprocess.check_output(
"bsb new test_bsb_new --quickstart --json".split()
)
self.assertEqual(
message.split(b"\n")[-2], b"Created 'test_bsb_new' project structure."
)
for folder in os.listdir():
if folder == "test_bsb_new":
self.assertTrue(os.path.isdir(folder))
files = os.listdir(folder)
self.assertEqual(len(files), 4)
parsed_files = 0
for filename in files:
if filename.endswith(".json"):
self.assertTrue(filename == "network_configuration.json")
cfg_copied = parse_configuration_file(
get_configuration_template("starting_example.json")[0]
)
cfg = parse_configuration_file(os.path.join(folder, filename))
self.assertEqual(cfg_copied.__tree__(), cfg.__tree__())
parsed_files += 1
elif filename.endswith(".py"):
parsed_files += 1
elif filename.endswith(".toml"):
self.assertEqual(filename, "pyproject.toml")
parsed_files += 1

self.assertEqual(parsed_files, 4)
shutil.rmtree(folder)
break
else:
self.fail("The project folder was not created properly")

@skip_parallel
def test_bsb_new_override(self):
folder = os.path.abspath("test_bsb_new")
command = "bsb new test_bsb_new --quickstart --json"
# Create the project folder
message = subprocess.check_output(command.split())
self.assertEqual(
message.split(b"\n")[-2], b"Created 'test_bsb_new' project structure."
)
# Cannot create folder if it exists already
message = subprocess.check_output(command.split())
self.assertEqual(
message.split(b"\n")[-2],
f"Could not create '{folder}', directory exists.".encode(),
)
# Force recreate the project folder
command += " --exists"
message = subprocess.check_output(command.split())
self.assertEqual(
message.split(b"\n")[-2], b"Created 'test_bsb_new' project structure."
)
shutil.rmtree(folder)

0 comments on commit 17e27eb

Please sign in to comment.