Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
49 changes: 49 additions & 0 deletions tests/shell/061_set_input_file.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/shell/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading