Make the basic tests work

This commit is contained in:
Konstantin Nazarov 2021-05-18 21:37:20 +00:00
parent 117d0afc29
commit aafdca4dee
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 77 additions and 22 deletions

View file

@ -197,6 +197,7 @@ pack_mime() {
} }
new_entry() { new_entry() {
INP="$1"
DIR=$(mktemp -d) DIR=$(mktemp -d)
ENTRY_FILE="$DIR/note.md" ENTRY_FILE="$DIR/note.md"
ENTRY_FILE_START="$(mktemp)" ENTRY_FILE_START="$(mktemp)"
@ -210,25 +211,38 @@ new_entry() {
cp "$ENTRY_FILE" "$ENTRY_FILE_START" cp "$ENTRY_FILE" "$ENTRY_FILE_START"
if [ -t 0 ]; then if [ -n "$INP" ] && [ ! -f "$INP" ] && [ ! -d "$INP" ]; then
die "File or directory doesn't exist: $INP"
fi
if [ -f "$INP" ]; then
cp "$INP" "$DIR/note.md"
elif [ -d "$INP" ]; then
if [ ! -f "$INP/note.md" ]; then
die "File doesn't exist: $INP/note.md"
fi
cp -n "$INP"/* "$DIR/" || true
cat "$INP/note.md" >> "$DIR/note.md"
elif [ -t 0 ]; then
"$EDITOR" "$ENTRY_FILE" "$EDITOR" "$ENTRY_FILE"
else else
ENTRY_FILE_STDIN="$(mktemp)"
while read -r line ; do while read -r line ; do
echo "$line" >> "$ENTRY_FILE_STDIN" echo "$line" >> "$ENTRY_FILE"
done done
HEADERS=$( echo "$(
get_headers "$ENTRY_FILE_STDIN"
get_headers "$ENTRY_FILE"
)" | sort -u -k1,1)
{
echo "$HEADERS"
echo ""
get_body "$ENTRY_FILE_STDIN"
} > "$ENTRY_FILE"
fi fi
MERGED_ENTRY_FILE="$(mktemp)"
HEADERS=$( echo "$(
get_headers "$ENTRY_FILE"
)" | tac | sort -u -k1,1)
{
echo "$HEADERS"
echo ""
get_body "$ENTRY_FILE"
} > "$MERGED_ENTRY_FILE"
mv "$MERGED_ENTRY_FILE" "$ENTRY_FILE"
if ! cmp -s "$ENTRY_FILE" "$ENTRY_FILE_START" ; then if ! cmp -s "$ENTRY_FILE" "$ENTRY_FILE_START" ; then
UNIX_TIMESTAMP=$(date "+%s") UNIX_TIMESTAMP=$(date "+%s")
@ -313,7 +327,7 @@ usage() {
while (( "$#" )); do while (( "$#" )); do
case "$1" in case "$1" in
-n|--new) -n|--new)
new_entry new_entry "$2"
exit 0 exit 0
;; ;;
-l|--list) -l|--list)

View file

@ -26,11 +26,19 @@ testcase() {
} }
assert() { assert() {
if "$@"; then # assert <command> <expected stdout> [stdin]
return 0
else command="$1"
echo "Assert: expected="$(echo -ne "${2:-}")"
fi result="$(eval 2>/dev/null $1 <<< ${3:-})" || true
if [[ "$result" == "$expected" ]]; then
return
fi
result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")"
echo "Expected '$command' == '$expected'. Got: '$result'"
exit 1
} }
new_note_from_stdin() { new_note_from_stdin() {
@ -42,11 +50,44 @@ new_note_from_stdin() {
OUTPUT="$(cat "$(pwd)/notes/cur"/*)" OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
test 1 = 2 assert 'echo "$OUTPUT" | grep Content-Type:' "Content-Type: text/plain; charset=utf-8"
assert 'echo "$OUTPUT" | grep MIME-Version:' "MIME-Version: 1.0"
assert 'echo "$OUTPUT" | grep Content-Disposition:' "Content-Disposition: inline"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
}
new_note_from_file() {
cat > "$TMP/input.md" <<- EOF
Subject: This is a header
# This is a body
EOF
"$BASE_DIR/notes.sh" -n "$TMP/input.md"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
}
new_note_from_dir() {
mkdir "$TMP/inpdir"
cat > "$TMP/inpdir/note.md" <<- EOF
Subject: This is a header
# This is a body
EOF
"$BASE_DIR/notes.sh" -n "$TMP/inpdir"
OUTPUT="$(cat "$(pwd)/notes/cur"/*)"
assert 'echo "$OUTPUT" | grep Subject' "Subject: This is a header"
} }
testcase new_note_from_stdin testcase new_note_from_stdin
testcase new_note_from_file
testcase new_note_from_dir
if [[ "$RESULT" == "0" ]]; then if [[ "$RESULT" == "0" ]]; then
echo "All tests passed." echo "All tests passed."