diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a0406d6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "shell/test/bats"] + path = shell/test/bats + url = https://github.com/bats-core/bats-core.git +[submodule "shell/test/test_helper/bats-support"] + path = shell/test/test_helper/bats-support + url = https://github.com/bats-core/bats-support.git +[submodule "shell/test/test_helper/bats-assert"] + path = shell/test/test_helper/bats-assert + url = https://github.com/bats-core/bats-assert.git diff --git a/shell/next.sh b/shell/next.sh index 35cd496..c2d0f96 100755 --- a/shell/next.sh +++ b/shell/next.sh @@ -37,6 +37,13 @@ if [ $2 ]; then fi fi -# HACK: emacs won't accept piped input as a file name, so we use --eval to open -# the file via find-file. -emacsclient --eval "(progn (find-file \"$NEXTFILE\") (um-journal-header) $UMTAG (message \"creating $NEXTFILE\"))" +UMELISP="(progn (find-file \"$NEXTFILE\") (um-journal-header) $UMTAG (message \"creating $NEXTFILE\"))" + +# for testing and piping +if [ $UMNEXTECHO = true ]; then + echo $NEXTFILE $UMTAG +else + # HACK: emacs won't accept piped input as a file name, so we use --eval to open + # the file via find-file. + emacsclient --eval "$UMELISP" +fi diff --git a/shell/test/bats b/shell/test/bats new file mode 160000 index 0000000..3d3f63d --- /dev/null +++ b/shell/test/bats @@ -0,0 +1 @@ +Subproject commit 3d3f63d9772b98c88586ec0a66bd358a85d9b4a6 diff --git a/shell/test/001.md b/shell/test/mockjournal/001.md similarity index 100% rename from shell/test/001.md rename to shell/test/mockjournal/001.md diff --git a/shell/test/002.blah.md b/shell/test/mockjournal/002.blah.md similarity index 100% rename from shell/test/002.blah.md rename to shell/test/mockjournal/002.blah.md diff --git a/shell/test/004.foo.md b/shell/test/mockjournal/004.foo.md similarity index 100% rename from shell/test/004.foo.md rename to shell/test/mockjournal/004.foo.md diff --git a/shell/test/005.md b/shell/test/mockjournal/005.md similarity index 100% rename from shell/test/005.md rename to shell/test/mockjournal/005.md diff --git a/shell/test/009.boo.md b/shell/test/mockjournal/009.boo.md similarity index 100% rename from shell/test/009.boo.md rename to shell/test/mockjournal/009.boo.md diff --git a/shell/test/098.baa.md b/shell/test/mockjournal/098.baa.md similarity index 100% rename from shell/test/098.baa.md rename to shell/test/mockjournal/098.baa.md diff --git a/shell/test/099.poo.md b/shell/test/mockjournal/099.poo.md similarity index 100% rename from shell/test/099.poo.md rename to shell/test/mockjournal/099.poo.md diff --git a/shell/test/test_helper/bats-assert b/shell/test/test_helper/bats-assert new file mode 160000 index 0000000..e2d855b --- /dev/null +++ b/shell/test/test_helper/bats-assert @@ -0,0 +1 @@ +Subproject commit e2d855bc78619ee15b0c702b5c30fb074101159f diff --git a/shell/test/test_helper/bats-support b/shell/test/test_helper/bats-support new file mode 160000 index 0000000..9bf10e8 --- /dev/null +++ b/shell/test/test_helper/bats-support @@ -0,0 +1 @@ +Subproject commit 9bf10e876dd6b624fe44423f0b35e064225f7556 diff --git a/shell/test/um.bats b/shell/test/um.bats new file mode 100644 index 0000000..234bbde --- /dev/null +++ b/shell/test/um.bats @@ -0,0 +1,82 @@ +setup() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + + # get the containing directory of this file + # use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0, + # as those will point to the bats executable's location or the preprocessed file respectively + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + # make executables visible to PATH + PATH="$DIR/..:$PATH" + # commands depend on being in the journal/ path. + cd $DIR/mockjournal + # so that we can run next.sh without dealing with emacsclient. + export UMNEXTECHO=true +} + +teardown() { + export UMNEXTECHO=false + cd - +} + +@test "um usage" { + run um + assert_output 'usage: um (next|last|cat|rename|tag)' +} + +@test "next empty" { + run um next + assert_output '100.md' +} + +@test "next descriptor" { + run um next foo + assert_output '100.foo.md' +} + +@test "next descriptor tag" { + run um next foo bar + assert_output --partial '(insert "+ bar\n")' +} + +@test "next descriptor +" { + run um next foo + + assert_output --partial '(insert "+ foo\n")' +} + +@test "um last" { + run um last + assert_output '099.poo.md' +} + +@test "um cat" { + run um cat 001.md 002.blah.md + assert_output '# 001 +: 2024.01.14 ++ foo + +--- + +# 002.blah +: 2024.01.14 ++ foo' +} + +um_rename_awk() { + echo "$1 +$2" | awk -f ../../rename.awk +} + +@test "rename.awk" { + run um_rename_awk 01.md foo + assert_output '01.md 01.foo.md' + + run um_rename_awk 01.foo.md bar + assert_output '01.foo.md 01.bar.md' +} + +@test "um tag" { + run um tag foo + assert_output '001.md +002.blah.md' +}