commit 4f3dcb847295ce1ddf689783544767368f70fea7
parent 0e3d904a4951d2b80b3b812273c9f212ce4ceac3
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date: Sat, 2 Nov 2024 11:57:56 +0000
Multi-entry multi-line insert is corrected and tested
Diffstat:
3 files changed, 118 insertions(+), 9 deletions(-)
diff --git a/spec/action_spec.sh b/spec/action_spec.sh
@@ -1311,9 +1311,44 @@ Describe 'Action Functions'
The error should equal "$(result)"
End
- It 'inserts the whole standard input'
+ It 'inserts the standard input until the first blank line'
MULTILINE=yes
OVERWRITE=yes
+ o_result() { %text
+ #|Enter contents of subdir/new and
+ #|press Ctrl+D or enter an empty line when finished:
+ }
+ result() {
+ %text:expand
+ #|$ scm_begin
+ #|$ mkdir -p -- ${PREFIX}/subdir
+ #|$ do_encrypt subdir/new.age
+ #|> line 1
+ #|> line 2
+ #|$ scm_add subdir/new.age
+ #|$ scm_commit Add given password for subdir/new to store.
+ }
+ Data
+ #|line 1
+ #|line 2
+ #|
+ #|line 3
+ #|line 4
+ End
+
+ When call do_insert 'subdir/new'
+ The status should be success
+ The output should equal "$(o_result)"
+ The error should equal "$(result)"
+ End
+
+ It 'inserts the whole standard input without blank line'
+ MULTILINE=yes
+ OVERWRITE=yes
+ o_result() { %text
+ #|Enter contents of subdir/new and
+ #|press Ctrl+D or enter an empty line when finished:
+ }
result() {
%text:expand
#|$ scm_begin
@@ -1333,8 +1368,7 @@ Describe 'Action Functions'
When call do_insert 'subdir/new'
The status should be success
- The output should equal \
- 'Enter contents of subdir/new and press Ctrl+D when finished:'
+ The output should equal "$(o_result)"
The error should equal "$(result)"
End
@@ -1381,6 +1415,10 @@ Describe 'Action Functions'
mocklog yesno "$@"
ANSWER=y
}
+ o_result() { %text
+ #|Enter contents of existing and
+ #|press Ctrl+D or enter an empty line when finished:
+ }
result() {
%text:expand
#|$ yesno An entry already exists for existing. Overwrite it?
@@ -1395,8 +1433,7 @@ Describe 'Action Functions'
When call do_insert 'existing'
The status should be success
- The output should equal \
- 'Enter contents of existing and press Ctrl+D when finished:'
+ The output should equal "$(o_result)"
The error should equal "$(result)"
The variable OVERWRITE should equal once
End
@@ -1427,6 +1464,10 @@ Describe 'Action Functions'
mocklog yesno "$@"
ANSWER=y
}
+ o_result() { %text
+ #|Enter contents of existing and
+ #|press Ctrl+D or enter an empty line when finished:
+ }
result() {
%text:expand
#|$ scm_begin
@@ -1440,8 +1481,7 @@ Describe 'Action Functions'
When call do_insert 'existing'
The status should be success
- The output should equal \
- 'Enter contents of existing and press Ctrl+D when finished:'
+ The output should equal "$(o_result)"
The error should equal "$(result)"
End
End
diff --git a/spec/pashage_extra_spec.sh b/spec/pashage_extra_spec.sh
@@ -610,6 +610,68 @@ Describe 'Integrated Command Functions'
The result of function check_git_log should be successful
End
+ It 'inserts several new multi-line entries'
+ stty() { false; }
+ Data
+ #|password-1
+ #| extra spaced line
+ #|
+ #|y
+ #|password-2
+ #| extra tabbed line
+ #|
+ #|password-3
+ End
+ When call cmd_insert -m newdir/pass-1 subdir/file newdir/pass-2
+ The status should be success
+ The error should be blank
+ expected_out() { %text
+ #|Enter contents of newdir/pass-1 and
+ #|press Ctrl+D or enter an empty line when finished:
+ #|An entry already exists for subdir/file. Overwrite it? [y/n]Enter contents of subdir/file and
+ #|press Ctrl+D or enter an empty line when finished:
+ #|Enter contents of newdir/pass-2 and
+ #|press Ctrl+D or enter an empty line when finished:
+ }
+ The output should equal "$(expected_out)"
+ expected_file_1() { %text
+ #|ageRecipient:myself
+ #|age:password-1
+ #|age: extra spaced line
+ }
+ expected_file_2() { %text
+ #|ageRecipient:myself
+ #|age:password-2
+ #|age: extra tabbed line
+ }
+ expected_file_3() { %text
+ #|ageRecipient:myself
+ #|age:password-3
+ }
+ The contents of file "${PREFIX}/newdir/pass-1.age" \
+ should equal "$(expected_file_1)"
+ The contents of file "${PREFIX}/subdir/file.age" \
+ should equal "$(expected_file_2)"
+ The contents of file "${PREFIX}/newdir/pass-2.age" \
+ should equal "$(expected_file_3)"
+ expected_log() { %text
+ #|Add given password for newdir/pass-2 to store.
+ #|
+ #| newdir/pass-2.age | 2 ++
+ #| 1 file changed, 2 insertions(+)
+ #|Add given password for subdir/file to store.
+ #|
+ #| subdir/file.age | 3 ++-
+ #| 1 file changed, 2 insertions(+), 1 deletion(-)
+ #|Add given password for newdir/pass-1 to store.
+ #|
+ #| newdir/pass-1.age | 3 +++
+ #| 1 file changed, 3 insertions(+)
+ setup_log
+ }
+ The result of function check_git_log should be successful
+ End
+
It 'inserts a new single-line entry on the second try'
stty() { :; }
Data
diff --git a/src/pashage.sh b/src/pashage.sh
@@ -785,8 +785,15 @@ do_insert() {
if [ "${MULTILINE}" = yes ]; then
printf '%s\n' \
- "Enter contents of $1 and press Ctrl+D when finished:"
- do_encrypt "$1.age"
+ "Enter contents of $1 and" \
+ "press Ctrl+D or enter an empty line when finished:"
+ while IFS= read -r LINE; do
+ if [ -n "${LINE}" ]; then
+ printf '%s\n' "${LINE}"
+ else
+ break
+ fi
+ done | do_encrypt "$1.age"
elif [ "${ECHO}" = yes ] \
|| ! type stty >/dev/null 2>&1 \