diff --git a/cfbs/commands.py b/cfbs/commands.py index fd116dd3..1053e2e5 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -45,7 +45,7 @@ perform_build, ) from cfbs.cfbs_config import CFBSConfig, CFBSReturnWithoutCommit -from cfbs.validate import validate_config +from cfbs.validate import validate_config, validate_config_raise_exceptions from cfbs.internal_file_management import ( clone_url_repo, SUPPORTED_URI_SCHEMES, @@ -295,9 +295,7 @@ def init_command(index=None, masterfiles=None, non_interactive=False) -> int: @cfbs_command("status") def status_command() -> int: config = CFBSConfig.get_instance() - if validate_config(config, empty_build_list_ok=True) != 0: - return 1 - config.warn_about_unknown_keys() + validate_config_raise_exceptions(config, empty_build_list_ok=True) print("Name: %s" % config["name"]) print("Description: %s" % config["description"]) print("File: %s" % cfbs_filename()) @@ -392,7 +390,7 @@ def add_command( checksum=None, ) -> Union[Result, int]: config = CFBSConfig.get_instance() - config.warn_about_unknown_keys() + validate_config_raise_exceptions(config, empty_build_list_ok=True) r = config.add_command(to_add, added_by, checksum) config.save() return r @@ -402,7 +400,7 @@ def add_command( @commit_after_command("Removed module%s %s", [PLURAL_S, FIRST_ARG_SLIST]) def remove_command(to_remove: List[str]): config = CFBSConfig.get_instance() - config.warn_about_unknown_keys() + validate_config_raise_exceptions(config, empty_build_list_ok=True) if "build" not in config: raise CFBSExitError( 'Cannot remove any modules because the "build" key is missing from cfbs.json' @@ -570,7 +568,7 @@ def _someone_needs_me(this) -> bool: @commit_after_command("Updated module%s", [PLURAL_S]) def update_command(to_update) -> Result: config = CFBSConfig.get_instance() - config.warn_about_unknown_keys() + validate_config_raise_exceptions(config, empty_build_list_ok=True) build = config["build"] # Update all modules in build if none specified @@ -1046,7 +1044,7 @@ def analyze_command( @commit_after_command("Added input for module%s", [PLURAL_S]) def input_command(args, input_from="cfbs input") -> Result: config = CFBSConfig.get_instance() - config.warn_about_unknown_keys() + validate_config_raise_exceptions(config, empty_build_list_ok=True) do_commit = False files_to_commit = [] for module_name in args: diff --git a/tests/shell/018_update_input_one_variable.sh b/tests/shell/018_update_input_one_variable.sh index 3482143e..d33f49af 100644 --- a/tests/shell/018_update_input_one_variable.sh +++ b/tests/shell/018_update_input_one_variable.sh @@ -8,6 +8,7 @@ rm -rf .git rm -rf example-module cp -r ../shell/018_update_input_one_variable/example-module . cp ../shell/018_update_input_one_variable/example-cfbs.json cfbs.json +cfbs validate cfbs --loglevel=debug --non-interactive update grep '"label": "Filepath"' example-module/input.json diff --git a/tests/shell/018_update_input_one_variable/example-cfbs.json b/tests/shell/018_update_input_one_variable/example-cfbs.json index daa3be62..b7f26f3b 100644 --- a/tests/shell/018_update_input_one_variable/example-cfbs.json +++ b/tests/shell/018_update_input_one_variable/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "by": "https://github.com/example-user", "version": "1.0.1", @@ -15,20 +16,21 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filepath", "question": "Path of file?", "default": "/tmp/test.txt" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", @@ -38,14 +40,14 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/018_update_input_one_variable/example-module/input.json b/tests/shell/018_update_input_one_variable/example-module/input.json index 63033c38..9e21d916 100644 --- a/tests/shell/018_update_input_one_variable/example-module/input.json +++ b/tests/shell/018_update_input_one_variable/example-module/input.json @@ -2,6 +2,8 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "response": "/tmp/create-single-file.txt", diff --git a/tests/shell/019_update_input_two_variables.sh b/tests/shell/019_update_input_two_variables.sh index 050c8651..dd04a11b 100644 --- a/tests/shell/019_update_input_two_variables.sh +++ b/tests/shell/019_update_input_two_variables.sh @@ -8,6 +8,7 @@ rm -rf .git rm -rf example-module cp -r ../shell/019_update_input_two_variables/example-module . cp ../shell/019_update_input_two_variables/example-cfbs.json cfbs.json +cfbs validate cfbs --loglevel=debug --non-interactive update grep '"label": "Path"' example-module/input.json diff --git a/tests/shell/019_update_input_two_variables/example-cfbs.json b/tests/shell/019_update_input_two_variables/example-cfbs.json index 0abda37f..92439468 100644 --- a/tests/shell/019_update_input_two_variables/example-cfbs.json +++ b/tests/shell/019_update_input_two_variables/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/larsewi/cfbs-test-modules.git", "commit": "3827a0da69d73ec2f973660d3c640b33cad58655", "by": "https://github.com/larsewi", @@ -15,6 +16,8 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Path", "question": "Path of file?", "default": "/tmp/test.txt" @@ -22,30 +25,32 @@ { "type": "string", "variable": "content", + "namespace": "example_module", + "bundle": "example_module", "label": "Contents", "question": "File contents?", - "default" : "Hello CFEngine!" + "default": "Hello CFEngine!" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", "version": "1.0.0", "subdirectory": "example-module", - "dependencies": ["autorun"], "input": [ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" @@ -53,14 +58,14 @@ { "type": "string", "variable": "content", + "namespace": "example_module", + "bundle": "example_module", "label": "Content", "question": "What content should this file have?", "default": "Bye CFEngine!" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/019_update_input_two_variables/example-module/input.json b/tests/shell/019_update_input_two_variables/example-module/input.json index 1c709bb1..30df39ee 100644 --- a/tests/shell/019_update_input_two_variables/example-module/input.json +++ b/tests/shell/019_update_input_two_variables/example-module/input.json @@ -2,6 +2,8 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt", @@ -10,6 +12,8 @@ { "type": "string", "variable": "content", + "namespace": "example_module", + "bundle": "example_module", "label": "Content", "question": "What content should this file have?", "default": "Bye CFEngine!", diff --git a/tests/shell/020_update_input_list.sh b/tests/shell/020_update_input_list.sh index ac95dcdb..32b89c79 100644 --- a/tests/shell/020_update_input_list.sh +++ b/tests/shell/020_update_input_list.sh @@ -8,6 +8,7 @@ rm -rf .git rm -rf example-module cp -r ../shell/020_update_input_list/example-module . cp ../shell/020_update_input_list/example-cfbs.json cfbs.json +cfbs validate cfbs --loglevel=debug --non-interactive update grep '"label": "Filepaths"' example-module/input.json diff --git a/tests/shell/020_update_input_list/example-cfbs.json b/tests/shell/020_update_input_list/example-cfbs.json index 02b4655d..39b9db70 100644 --- a/tests/shell/020_update_input_list/example-cfbs.json +++ b/tests/shell/020_update_input_list/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "commit": "3827a0da69d73ec2f973660d3c640b33cad58655", "by": "https://github.com/example-user", @@ -15,6 +16,8 @@ { "type": "list", "variable": "files", + "namespace": "example_module", + "bundle": "example_module", "label": "Filepaths", "subtype": { "type": "string", @@ -25,15 +28,14 @@ "while": "Create another file?" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", "version": "1.0.0", @@ -42,6 +44,8 @@ { "type": "list", "variable": "files", + "namespace": "example_module", + "bundle": "example_module", "label": "Files", "subtype": { "type": "string", @@ -52,9 +56,7 @@ "while": "Do you want to create another file?" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/020_update_input_list/example-module/input.json b/tests/shell/020_update_input_list/example-module/input.json index ad3b2f94..7756fe29 100644 --- a/tests/shell/020_update_input_list/example-module/input.json +++ b/tests/shell/020_update_input_list/example-module/input.json @@ -2,6 +2,8 @@ { "type": "list", "variable": "files", + "namespace": "example_module", + "bundle": "example_module", "label": "Files", "subtype": { "type": "string", diff --git a/tests/shell/021_update_input_list_with_keys.sh b/tests/shell/021_update_input_list_with_keys.sh index 5429b736..24c9c6b4 100644 --- a/tests/shell/021_update_input_list_with_keys.sh +++ b/tests/shell/021_update_input_list_with_keys.sh @@ -8,6 +8,7 @@ rm -rf .git rm -rf example-module cp -r ../shell/021_update_input_list_with_keys/example-module . cp ../shell/021_update_input_list_with_keys/example-cfbs.json cfbs.json +cfbs validate cfbs --loglevel=debug --non-interactive update grep '"while": "Create another file?"' example-module/input.json diff --git a/tests/shell/021_update_input_list_with_keys/example-cfbs.json b/tests/shell/021_update_input_list_with_keys/example-cfbs.json index b5a4d7b6..dd1457f6 100644 --- a/tests/shell/021_update_input_list_with_keys/example-cfbs.json +++ b/tests/shell/021_update_input_list_with_keys/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "by": "https://github.com/example-user", "version": "1.0.1", @@ -15,6 +16,8 @@ { "type": "list", "variable": "files", + "namespace": "example_module", + "bundle": "example_module", "label": "Files", "subtype": [ { @@ -35,15 +38,14 @@ "while": "Create another file?" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", "version": "1.0.0", @@ -52,6 +54,8 @@ { "type": "list", "variable": "files", + "namespace": "example_module", + "bundle": "example_module", "label": "Files", "subtype": [ { @@ -72,9 +76,7 @@ "while": "Do you want to create another file?" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/021_update_input_list_with_keys/example-module/input.json b/tests/shell/021_update_input_list_with_keys/example-module/input.json index 5873d6bf..7a96c11e 100644 --- a/tests/shell/021_update_input_list_with_keys/example-module/input.json +++ b/tests/shell/021_update_input_list_with_keys/example-module/input.json @@ -2,6 +2,8 @@ { "type": "list", "variable": "files", + "namespace": "example_module", + "bundle": "example_module", "label": "Files", "subtype": [ { diff --git a/tests/shell/022_update_input_fail_variable.sh b/tests/shell/022_update_input_fail_variable.sh index 8060a33d..4564d3d0 100644 --- a/tests/shell/022_update_input_fail_variable.sh +++ b/tests/shell/022_update_input_fail_variable.sh @@ -8,6 +8,7 @@ rm -rf .git rm -rf example-module cp -r ../shell/022_update_input_fail_variable/example-module . cp ../shell/022_update_input_fail_variable/example-cfbs.json cfbs.json +cfbs validate cfbs --loglevel=debug --non-interactive update > output.log 2>&1 grep "Failed to update input data for module 'example-module'" ./output.log diff --git a/tests/shell/022_update_input_fail_variable/example-cfbs.json b/tests/shell/022_update_input_fail_variable/example-cfbs.json index 46942a7d..9f2b6051 100644 --- a/tests/shell/022_update_input_fail_variable/example-cfbs.json +++ b/tests/shell/022_update_input_fail_variable/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "by": "https://github.com/example-user", "version": "1.0.1", @@ -15,20 +16,21 @@ { "type": "string", "variable": "filepath", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", @@ -38,14 +40,14 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/022_update_input_fail_variable/example-module/input.json b/tests/shell/022_update_input_fail_variable/example-module/input.json index 99be4ab8..5d049a9c 100644 --- a/tests/shell/022_update_input_fail_variable/example-module/input.json +++ b/tests/shell/022_update_input_fail_variable/example-module/input.json @@ -2,6 +2,8 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt", diff --git a/tests/shell/023_update_input_fail_number.sh b/tests/shell/023_update_input_fail_number.sh index 093f1e05..d0b6b211 100644 --- a/tests/shell/023_update_input_fail_number.sh +++ b/tests/shell/023_update_input_fail_number.sh @@ -8,6 +8,7 @@ rm -rf .git rm -rf example-module cp -r ../shell/023_update_input_fail_number/example-module . cp ../shell/023_update_input_fail_number/example-cfbs.json cfbs.json +cfbs validate cfbs --loglevel=debug --non-interactive update > output.log 2>&1 grep "Failed to update input data for module 'example-module'" ./output.log diff --git a/tests/shell/023_update_input_fail_number/example-cfbs.json b/tests/shell/023_update_input_fail_number/example-cfbs.json index d229f7e9..6c937f0c 100644 --- a/tests/shell/023_update_input_fail_number/example-cfbs.json +++ b/tests/shell/023_update_input_fail_number/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "by": "https://github.com/example-user", "version": "1.0.1", @@ -15,6 +16,8 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" @@ -22,20 +25,21 @@ { "type": "string", "variable": "content", + "namespace": "example_module", + "bundle": "example_module", "label": "Content", "question": "What content should this file have?", "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", @@ -45,14 +49,14 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/023_update_input_fail_number/example-module/input.json b/tests/shell/023_update_input_fail_number/example-module/input.json index 99be4ab8..5d049a9c 100644 --- a/tests/shell/023_update_input_fail_number/example-module/input.json +++ b/tests/shell/023_update_input_fail_number/example-module/input.json @@ -2,6 +2,8 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt", diff --git a/tests/shell/024_update_input_fail_bundle.sh b/tests/shell/024_update_input_fail_bundle.sh index 4938b22e..4a54f6b6 100644 --- a/tests/shell/024_update_input_fail_bundle.sh +++ b/tests/shell/024_update_input_fail_bundle.sh @@ -9,5 +9,11 @@ rm -rf example-module cp -r ../shell/024_update_input_fail_bundle/example-module . cp ../shell/024_update_input_fail_bundle/example-cfbs.json cfbs.json -cfbs --loglevel=debug --non-interactive update > output.log 2>&1 -grep "Failed to update input data for module 'example-module'" ./output.log +# This used to be a test about cfbs update failing due to changing fields +# which cannot be automatically updated. +# We've now added more strict validation to cfbs update, +# so the test now fails "earlier", during validation of the JSON. +# Effectively, it now tests that cfbs update does validation, and fails on +# the missing namespace field. +! cfbs --loglevel=debug --non-interactive update > output.log 2>&1 +grep 'The "namespace" field is required in module input elements' ./output.log diff --git a/tests/shell/024_update_input_fail_bundle/example-cfbs.json b/tests/shell/024_update_input_fail_bundle/example-cfbs.json index 18a7e529..74ea1f50 100644 --- a/tests/shell/024_update_input_fail_bundle/example-cfbs.json +++ b/tests/shell/024_update_input_fail_bundle/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "by": "https://github.com/example-user", "version": "1.0.1", @@ -14,22 +15,22 @@ "input": [ { "type": "string", - "bundle": "my_bundle", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file should this module create?", "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ] + "steps": ["input ./input.json def.json"] } }, "build": [ { "name": "example-module", "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "commit": "0000000000000000000000000000000000000000", "by": "https://github.com/example-user", @@ -44,9 +45,7 @@ "default": "/tmp/example.txt" } ], - "steps": [ - "input ./input.json def.json" - ], + "steps": ["input ./input.json def.json"], "added_by": "cfbs add" } ] diff --git a/tests/shell/025_add_input_remove/example-cfbs.json b/tests/shell/025_add_input_remove/example-cfbs.json index 6e90cfb5..d3d8f1ec 100644 --- a/tests/shell/025_add_input_remove/example-cfbs.json +++ b/tests/shell/025_add_input_remove/example-cfbs.json @@ -6,6 +6,7 @@ "index": { "example-module": { "description": "Example module", + "tags": [], "repo": "https://github.com/example-user/example-modules.git", "by": "https://github.com/example-user", "version": "1.2.3", @@ -15,14 +16,14 @@ { "type": "string", "variable": "filename", + "namespace": "example_module", + "bundle": "example_module", "label": "Filename", "question": "What file do you want to create?", "default": "/tmp/testfile.txt" } ], - "steps": [ - "input input.json def.json" - ] + "steps": ["input input.json def.json"] } }, "build": [] diff --git a/tests/shell/042_update_from_url.sh b/tests/shell/042_update_from_url.sh index e5463029..79982aac 100644 --- a/tests/shell/042_update_from_url.sh +++ b/tests/shell/042_update_from_url.sh @@ -8,6 +8,8 @@ rm -rf .git rm -rf delete-files cp ../shell/042_update_from_url/example-cfbs.json cfbs.json +cfbs validate + cp -r ../shell/042_update_from_url/delete-files . cfbs --loglevel=debug --non-interactive update diff --git a/tests/shell/042_update_from_url/example-cfbs.json b/tests/shell/042_update_from_url/example-cfbs.json index f0fb3650..1ccd18e7 100644 --- a/tests/shell/042_update_from_url/example-cfbs.json +++ b/tests/shell/042_update_from_url/example-cfbs.json @@ -12,8 +12,7 @@ "added_by": "cfbs add", "steps": [ "copy delete-files.cf services/cfbs/modules/delete-files/delete-files.cf", - "input delete-files/input.json def.json", - "something test" + "input delete-files/input.json def.json" ], "input": [ { diff --git a/tests/shell/043_replace_version.sh b/tests/shell/043_replace_version.sh index b5dfe377..f78c87e5 100644 --- a/tests/shell/043_replace_version.sh +++ b/tests/shell/043_replace_version.sh @@ -6,6 +6,8 @@ cd ./tmp/ # Set up the project we will build: cp ../shell/043_replace_version/example-cfbs.json ./cfbs.json +cfbs validate + mkdir -p subdir cp ../shell/043_replace_version/subdir/example.py ./subdir/example.py diff --git a/tests/shell/044_replace.sh b/tests/shell/044_replace.sh index 0bbd18bc..ca4eff18 100644 --- a/tests/shell/044_replace.sh +++ b/tests/shell/044_replace.sh @@ -6,6 +6,8 @@ cd ./tmp/ # Set up the project we will build: cp ../shell/044_replace/example-cfbs.json ./cfbs.json +cfbs validate + mkdir -p subdir cp ../shell/044_replace/subdir/example.py ./subdir/example.py cp ../shell/044_replace/subdir/example.expected.py ./subdir/example.expected.py