Skip to content

Commit

Permalink
Merge pull request #164 from olehermanse/master
Browse files Browse the repository at this point in the history
ENT-10944: cfbs build/download: Added validation and error messages
  • Loading branch information
olehermanse authored Nov 29, 2023
2 parents 1325a3c + d557830 commit bd14f47
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
79 changes: 79 additions & 0 deletions cfbs/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,85 @@
from cfbs.pretty import pretty, pretty_file


def validate_config_for_build_field(config):
"""Validate that neccessary fields are in the config for the build/download commands to work"""
if not "build" in config:
user_error(
'A "build" field is missing in ./cfbs.json'
+ " - The 'cfbs build' command loops through all modules in this list to find build steps to perform"
)
if type(config["build"]) is not list:
user_error(
'The "build" field in ./cfbs.json must be a list (of modules involved in the build)'
)
if config["build"] == []:
user_error(
"The \"build\" field in ./cfbs.json is empty - add modules with 'cfbs add'"
)
for index, module in enumerate(config["build"]):
if not "name" in module:
user_error(
"The module at index "
+ str(index)
+ ' of "build" in ./cfbs.json is missing a "name"'
)
name = module["name"]
if type(name) is not str:
user_error(
"The module at index "
+ str(index)
+ ' of "build" in ./cfbs.json has a name which is not a string'
)
if not name:
user_error(
"The module at index "
+ str(index)
+ ' of "build" in ./cfbs.json has an empty name'
)
if (
not "steps" in module
or type(module["steps"]) is not list
or module["steps"] == []
):
user_error(
'Build steps are missing for the "'
+ name
+ '" module in ./cfbs.json - the "steps" field must have a non-empty list of steps to perform (strings)'
)

steps = module["steps"]
not_strings = len([step for step in steps if type(step) is not str])
if not_strings == 1:
user_error(
"The module '"
+ name
+ '\' in "build" in ./cfbs.json has 1 step which is not a string'
)
if not_strings > 1:
user_error(
"The module '"
+ name
+ '\' in "build" in ./cfbs.json has '
+ str(not_strings)
+ " steps which are not strings"
)
empty_strings = len([step for step in steps if step == ""])
if empty_strings == 1:
user_error(
"The module '"
+ name
+ '\' in "build" in ./cfbs.json has 1 step which is empty'
)
if empty_strings > 1:
user_error(
"The module '"
+ name
+ '\' in "build" in ./cfbs.json has '
+ str(empty_strings)
+ " steps which are empty"
)


def init_out_folder():
rm("out", missing_ok=True)
mkdir("out")
Expand Down
8 changes: 7 additions & 1 deletion cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

from cfbs.args import get_args
from cfbs.pretty import pretty, pretty_check_file, pretty_file
from cfbs.build import init_out_folder, perform_build_steps
from cfbs.build import (
init_out_folder,
perform_build_steps,
validate_config_for_build_field,
)
from cfbs.cfbs_config import CFBSConfig, CFBSReturnWithoutCommit
from cfbs.validate import CFBSIndexException, validate_index
from cfbs.internal_file_management import (
Expand Down Expand Up @@ -904,12 +908,14 @@ def _download_dependencies(
@cfbs_command("download")
def download_command(force, ignore_versions=False):
config = CFBSConfig.get_instance()
validate_config_for_build_field(config)
_download_dependencies(config, redownload=force, ignore_versions=ignore_versions)


@cfbs_command("build")
def build_command(ignore_versions=False) -> int:
config = CFBSConfig.get_instance()
validate_config_for_build_field(config)
init_out_folder()
_download_dependencies(config, prefer_offline=True, ignore_versions=ignore_versions)
perform_build_steps(config)
Expand Down
3 changes: 2 additions & 1 deletion tests/shell/026_init_no_masterfiles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ rm -rf .git

cfbs --non-interactive init --masterfiles=no
!( grep '"name": "masterfiles"' cfbs.json )
cfbs build
cfbs status
!( cfbs build )

0 comments on commit bd14f47

Please sign in to comment.