Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFE-4081: Fixed commiting of changes made by set-input #144

Merged
merged 2 commits into from
Nov 22, 2022
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
17 changes: 15 additions & 2 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from cfbs.index import _VERSION_INDEX, Index
from cfbs.git import (
is_git_repo,
git_commit,
git_get_config,
git_set_config,
git_init,
Expand Down Expand Up @@ -1067,9 +1068,21 @@ def _compare_list(a, b):
return 1

path = os.path.join(name, "input.json")
log.debug("Writing json to file '%s'" % path)
write_json(path, data)

log.debug("Comparing with data already in file '%s'" % path)
old_data = read_json(path)
if old_data == data:
log.debug("Input data for '%s' unchanged, nothing to write / commit" % name)
return 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should, ideally, be done before the more expensive checks/comparison above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking it makes sense to always do the expensive checks (validation), even if the (potentially bad) data is already there.


log.debug("Input data for '%s' changed, writing json to file '%s'" % (name, path))
write_json(path, data)
if config.get("git", False):
git_commit(
"Set the input for module '%s'" % name,
edit_commit_msg=False,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No interactivity here and prompting the user if they want to modify the commit message?

Copy link
Member Author

@olehermanse olehermanse Nov 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I am submitting some changes which should explain / clear this up. (In a follow-up).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIP: #145

scope=[path],
)
return 0


Expand Down
20 changes: 20 additions & 0 deletions tests/shell/031_get_set_input_pipe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ rm -rf delete-files

cfbs --non-interactive init
cfbs --non-interactive add delete-files@0.0.1

commit_a=$(git rev-parse HEAD)

echo '[
{
"type": "list",
Expand Down Expand Up @@ -39,4 +42,21 @@ echo '[
}
]' | cfbs set-input delete-files -

commit_b=$(git rev-parse HEAD)

test "x$commit_a" != "x$commit_b"

cfbs get-input delete-files - | cfbs set-input delete-files -

commit_c=$(git rev-parse HEAD)

test "x$commit_b" = "x$commit_c"

# Error if the file has never been added:
git ls-files --error-unmatch delete-files/input.json

# Error if there are staged (added, not yet commited)
git diff --exit-code --staged

# Error if there are uncommited changes (to tracked files):
git diff --exit-code
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ echo '[
"while": "Specify another file you want deleted on your hosts?"
}
]' | cfbs --log=debug set-input delete-files -

# Error if the file has never been added:
git ls-files --error-unmatch delete-files/input.json

# Error if there are staged (added, not yet commited)
git diff --exit-code --staged

# Error if there are uncommited changes (to tracked files):
git diff --exit-code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these checks should be in a nicely-named function in some shared bash source.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, good idea

2 changes: 1 addition & 1 deletion tests/shell/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ bash tests/shell/028_init_masterfiles_version_3.18.2.sh
bash tests/shell/029_init_masterfiles_version_3.18.1-1.sh
bash tests/shell/030_get_set_input.sh
bash tests/shell/031_get_set_input_pipe.sh
bash tests/shell/032_get_set_input_unordered.sh
bash tests/shell/032_set_input_unordered.sh

echo "All cfbs shell tests completed successfully!"