diff --git a/cfbs/commands.py b/cfbs/commands.py index 0065ad3f..64e044e3 100644 --- a/cfbs/commands.py +++ b/cfbs/commands.py @@ -1667,11 +1667,15 @@ def set_input_command(name, infile): ) return CFBSCommandGitResult(1) + copied_files = _place_file_input(name, data) + path = os.path.join(name, "input.json") log.debug("Comparing with data already in file '%s'" % path) old_data = read_json(path) - changes_made = old_data != data + # A file can be copied in without input.json itself changing, when the user + # gives a file of the same name from somewhere else: + changes_made = old_data != data or bool(copied_files) if changes_made: write_json(path, data) @@ -1681,7 +1685,7 @@ def set_input_command(name, infile): else: log.debug("Input data for '%s' unchanged, nothing to write / commit" % name) - return CFBSCommandGitResult(0, changes_made, None, [path]) + return CFBSCommandGitResult(0, changes_made, None, [path] + copied_files) @cfbs_command("get-input") diff --git a/tests/shell/061_set_input_file.sh b/tests/shell/061_set_input_file.sh new file mode 100644 index 00000000..2e3797c0 --- /dev/null +++ b/tests/shell/061_set_input_file.sh @@ -0,0 +1,49 @@ +set -e +set -x +cd tests/ +mkdir -p ./tmp/ +cd ./tmp/ +touch cfbs.json && rm cfbs.json +rm -rf .git +rm -rf copy-a-file + +echo '{ + "name": "Example", + "type": "policy-set", + "description": "Example description", + "git": false, + "build": [ + { + "name": "copy-a-file", + "description": "Copy a file.", + "steps": ["input ./input.json def.json"], + "input": [ + { + "type": "file", + "variable": "source", + "namespace": "cfbs", + "bundle": "copy_a_file", + "label": "Source file", + "question": "Which file should be copied?" + } + ] + } + ] +}' > cfbs.json + +# A file from outside the project must be copied into the module's directory, +# with the response replaced by the path of that copy: +echo "some content" > /tmp/cfbs-notes.txt +echo '[{"type": "file", "variable": "source", "namespace": "cfbs", "bundle": "copy_a_file", "label": "Source file", "question": "Which file should be copied?", "response": "/tmp/cfbs-notes.txt"}]' | cfbs set-input copy-a-file - +grep '"response": "./copy-a-file/cfbs-notes.txt"' copy-a-file/input.json +test "$(cat copy-a-file/cfbs-notes.txt)" = "some content" +rm -f /tmp/cfbs-notes.txt + +# A file already part of the project must be referred to as it is, without +# copying it into the module's directory: +echo "already here" > existing.txt +echo '[{"type": "file", "variable": "source", "namespace": "cfbs", "bundle": "copy_a_file", "label": "Source file", "question": "Which file should be copied?", "response": "./existing.txt"}]' | cfbs set-input copy-a-file - +grep '"response": "./existing.txt"' copy-a-file/input.json +test ! -f copy-a-file/existing.txt + +rm -rf copy-a-file existing.txt diff --git a/tests/shell/all.sh b/tests/shell/all.sh index d095c9cb..b59f7176 100644 --- a/tests/shell/all.sh +++ b/tests/shell/all.sh @@ -104,6 +104,7 @@ run_test tests/shell/057_render_input_no_response.sh run_test tests/shell/058_render_input_fail.sh run_test tests/shell/059_input_string_multiline.sh run_test tests/shell/060_input_file.sh +run_test tests/shell/061_set_input_file.sh # Summary _suite_end=$(date +%s)