diff --git a/tests/cli.bats b/tests/cli.bats index e97b8be..f6ba126 100644 --- a/tests/cli.bats +++ b/tests/cli.bats @@ -77,6 +77,6 @@ teardown_file() { run notesium home echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == "/tmp/notesium-test-corpus" ] + [ "${lines[0]}" == "$(realpath /tmp/notesium-test-corpus)" ] } diff --git a/tests/delete.bats b/tests/delete.bats index d668d1c..8d5e4bf 100644 --- a/tests/delete.bats +++ b/tests/delete.bats @@ -9,7 +9,7 @@ _delete_jq() { curl -qs -X DELETE -d "${2}" ${URL}/${1} | jq -r "${3}" ; } setup_file() { command -v jq >/dev/null command -v curl >/dev/null - [ "$(pidof notesium)" == "" ] + [ "$(pgrep -x notesium)" == "" ] [ -e "/tmp/notesium-test-corpus" ] && exit 1 run mkdir /tmp/notesium-test-corpus run cp $BATS_TEST_DIRNAME/fixtures/*.md /tmp/notesium-test-corpus/ @@ -101,16 +101,16 @@ teardown_file() { @test "delete: stop by sending terminate signal" { # force stop otherwise bats will block until timeout (bats-core/issues/205) - run pidof notesium + run pgrep -x notesium echo "$output" echo "could not get pid" [ $status -eq 0 ] - run kill "$(pidof notesium)" + run kill "$(pgrep -x notesium)" echo "$output" [ $status -eq 0 ] - run pidof notesium + run pgrep -x notesium echo "$output" [ $status -eq 1 ] } diff --git a/tests/new.bats b/tests/new.bats index f45bf8b..f0400ef 100644 --- a/tests/new.bats +++ b/tests/new.bats @@ -16,7 +16,7 @@ teardown_file() { run notesium new echo "$output" [ $status -eq 0 ] - [ "$(dirname $output)" == "/tmp/notesium-test-corpus" ] + [ "$(dirname $output)" == "$(realpath /tmp/notesium-test-corpus)" ] } @test "new: basename is 8 chars plus .md extension" { @@ -48,7 +48,7 @@ teardown_file() { run notesium new --ctime=2023-01-16T05:05:00 --verbose echo "$output" [ "${#lines[@]}" -eq 5 ] - [ "${lines[0]}" == "path:/tmp/notesium-test-corpus/63c4dafc.md" ] + [ "${lines[0]}" == "path:$(realpath /tmp/notesium-test-corpus/63c4dafc.md)" ] [ "${lines[1]}" == "filename:63c4dafc.md" ] [ "${lines[2]}" == "epoch:1673845500" ] [ "${lines[3]}" == "ctime:2023-01-16T05:05:00+00:00" ] diff --git a/tests/raw.bats b/tests/raw.bats index bee110f..9f510cc 100644 --- a/tests/raw.bats +++ b/tests/raw.bats @@ -4,12 +4,17 @@ load helpers.sh _curl() { curl -qs "http://localhost:8881/${1}" ; } +_os_arch() { + uname -sm | tr A-Z a-z | sed 's/ /\//;s/x86_64/amd64/;s/aarch64/arm64/' +} + setup_file() { command -v curl >/dev/null export TZ="UTC" export NOTESIUM_DIR="$BATS_TEST_DIRNAME/fixtures" + export EXPECTED_PLATFORM="$(_os_arch)" export PATH="$(realpath $BATS_TEST_DIRNAME/../):$PATH" - [ "$(pidof notesium)" == "" ] + [ "$(pgrep -x notesium)" == "" ] } @test "api/raw: start with custom port and stop-on-idle" { @@ -121,7 +126,7 @@ setup_file() { [[ "${lines[0]}" =~ "version:" ]] [[ "${lines[1]}" =~ "gitversion:v" ]] [[ "${lines[2]}" =~ "buildtime:" ]] - [[ "${lines[3]}" =~ "platform:linux/amd64" ]] + [[ "${lines[3]}" =~ "platform:$EXPECTED_PLATFORM" ]] } @test "api/raw: no command specified error" { @@ -140,16 +145,16 @@ setup_file() { @test "api/raw: stop by sending terminate signal" { # force stop otherwise bats will block until timeout (bats-core/issues/205) - run pidof notesium + run pgrep -x notesium echo "$output" echo "could not get pid" [ $status -eq 0 ] - run kill "$(pidof notesium)" + run kill "$(pgrep -x notesium)" echo "$output" [ $status -eq 0 ] - run pidof notesium + run pgrep -x notesium echo "$output" [ $status -eq 1 ] } diff --git a/tests/runtime.bats b/tests/runtime.bats index 8c924ba..d73e32a 100644 --- a/tests/runtime.bats +++ b/tests/runtime.bats @@ -4,12 +4,17 @@ load helpers.sh _curl_jq() { curl -qs http://localhost:8881/${1} | jq -r "${2}" ; } +_os_arch() { + uname -sm | tr A-Z a-z | sed 's/ /\//;s/x86_64/amd64/;s/aarch64/arm64/' +} + setup_file() { command -v jq >/dev/null command -v curl >/dev/null export NOTESIUM_DIR="$BATS_TEST_DIRNAME/fixtures" + export EXPECTED_PLATFORM="$(_os_arch)" export PATH="$(realpath $BATS_TEST_DIRNAME/../):$PATH" - [ "$(pidof notesium)" == "" ] + [ "$(pgrep -x notesium)" == "" ] } @test "runtime: start with custom port, stop-on-idle and no-check" { @@ -31,7 +36,7 @@ setup_file() { run _curl_jq 'api/runtime' '.platform' echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == "linux/amd64" ] + [ "${lines[0]}" == "$EXPECTED_PLATFORM" ] } @test "runtime: web" { @@ -71,16 +76,16 @@ setup_file() { @test "runtime: stop by sending terminate signal" { # force stop otherwise bats will block until timeout (bats-core/issues/205) - run pidof notesium + run pgrep -x notesium echo "$output" echo "could not get pid" [ $status -eq 0 ] - run kill "$(pidof notesium)" + run kill "$(pgrep -x notesium)" echo "$output" [ $status -eq 0 ] - run pidof notesium + run pgrep -x notesium echo "$output" [ $status -eq 1 ] } diff --git a/tests/version.bats b/tests/version.bats index fddbc4c..d5f2cbf 100644 --- a/tests/version.bats +++ b/tests/version.bats @@ -8,21 +8,32 @@ _gobuild() { go build -o /tmp/notesium-test-version/$gitversion -ldflags " -X main.gitversion=$gitversion \ -X main.buildtime=2024-01-02T01:02:03Z \ - -X main.latestReleaseUrl=http://127.0.0.1:8882" + -X main.latestReleaseUrl=http://127.0.0.1:8882/latest.json" } _mock_latest_release() { tagname="$1" - response='{"tag_name": "'$tagname'", "html_url": "https://github.com/alonswartz/notesium/releases/tag/'$tagname'", "published_at": "2024-02-02T01:02:03Z"}' - echo -en "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: ${#response}\r\n\r\n${response}" | nc -C -l -s 127.0.0.1 -p 8882 -q 1 -w 5 + jq -n --arg tag "$tagname" '{ + tag_name: $tag, + html_url: "https://github.com/alonswartz/notesium/releases/tag/\($tag)", + published_at: "2024-02-02T01:02:03Z" + }' > /tmp/notesium-test-version/latest.json +} + +_os_arch() { + uname -sm | tr A-Z a-z | sed 's/ /\//;s/x86_64/amd64/;s/aarch64/arm64/' } setup_file() { export TZ="UTC" + export EXPECTED_PLATFORM="$(_os_arch)" command -v go >/dev/null - command -v nc >/dev/null + command -v jq >/dev/null + [ "$(pgrep -x notesium)" == "" ] [ -e "/tmp/notesium-test-version" ] && exit 1 run mkdir /tmp/notesium-test-version + export NOTESIUM_DIR="/tmp/notesium-test-version" + export PATH="$(realpath $BATS_TEST_DIRNAME/../):$PATH" } teardown_file() { @@ -31,6 +42,7 @@ teardown_file() { run rm /tmp/notesium-test-version/v0.1.2-2-g1234567-dirty run rm /tmp/notesium-test-version/v0.2.0-beta-0-g1234567 run rm /tmp/notesium-test-version/v0.2.1-beta-0-g1234567 + run rm /tmp/notesium-test-version/latest.json run rmdir /tmp/notesium-test-version } @@ -74,20 +86,21 @@ teardown_file() { [ $status -eq 0 ] [ "${lines[0]}" == '0.1.2' ] } + @test "version: default - patched" { run /tmp/notesium-test-version/v0.1.2-2-g1234567 version echo "$output" [ $status -eq 0 ] [ "${lines[0]}" == '0.1.2+2' ] - } + @test "version: default - patched and dirty" { run /tmp/notesium-test-version/v0.1.2-2-g1234567-dirty version echo "$output" [ $status -eq 0 ] [ "${lines[0]}" == '0.1.2+2-dirty' ] - } + @test "version: default - beta" { run /tmp/notesium-test-version/v0.2.0-beta-0-g1234567 version echo "$output" @@ -102,68 +115,69 @@ teardown_file() { [ "${lines[0]}" == 'version:0.1.2' ] [ "${lines[1]}" == 'gitversion:v0.1.2-0-g1234567' ] [ "${lines[2]}" == 'buildtime:2024-01-02T01:02:03Z' ] - [ "${lines[3]}" == 'platform:linux/amd64' ] + [ "${lines[3]}" == "platform:$EXPECTED_PLATFORM" ] +} + +@test "version: check - start mock release server" { + # mis-using notesium to act as a mock release server + run notesium web --port=8882 --stop-on-idle --webroot=$NOTESIUM_DIR & + echo "$output" } @test "version: check - older" { - run _mock_latest_release "v0.1.3" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.3" run /tmp/notesium-test-version/v0.1.2-0-g1234567 version --check echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2 (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2 ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'A new release is available: 0.1.3 (2024-02-02 01:02)' ] [ "${lines[2]}" == 'https://github.com/alonswartz/notesium/releases' ] } @test "version: check verbose - older" { - run _mock_latest_release "v0.1.3" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.3" run /tmp/notesium-test-version/v0.1.2-0-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2 (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2 ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'A new release is available: 0.1.3 (2024-02-02 01:02)' ] [ "${lines[2]}" == 'https://github.com/alonswartz/notesium/releases' ] assert_line 'comparison:-1' assert_line 'version:0.1.2' assert_line 'gitversion:v0.1.2-0-g1234567' assert_line 'buildtime:2024-01-02T01:02:03Z' - assert_line 'platform:linux/amd64' + assert_line "platform:$EXPECTED_PLATFORM" assert_line 'latest.version:0.1.3' assert_line 'latest.published:2024-02-02T01:02:03Z' assert_line 'latest.release:https://github.com/alonswartz/notesium/releases/tag/v0.1.3' } @test "version: check verbose - match" { - run _mock_latest_release "v0.1.2" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.2" run /tmp/notesium-test-version/v0.1.2-0-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2 (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2 ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'You are using the latest version' ] assert_line 'comparison:0' } @test "version: check verbose - newer" { - run _mock_latest_release "v0.1.1" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.1" run /tmp/notesium-test-version/v0.1.2-0-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2 (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2 ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'You are using a newer version than latest: 0.1.1' ] assert_line 'comparison:1' } @test "version: check verbose - older - patched" { - run _mock_latest_release "v0.1.3" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.3" run /tmp/notesium-test-version/v0.1.2-2-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2+2 (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2+2 ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'A new release is available: 0.1.3 (2024-02-02 01:02)' ] assert_line 'comparison:-1' assert_line 'version:0.1.2+2' @@ -171,12 +185,11 @@ teardown_file() { } @test "version: check verbose - match - patched" { - run _mock_latest_release "v0.1.2" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.2" run /tmp/notesium-test-version/v0.1.2-2-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2+2 (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2+2 ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'You are using the latest version' ] assert_line 'comparison:0' assert_line 'version:0.1.2+2' @@ -184,12 +197,11 @@ teardown_file() { } @test "version: check verbose - match - patched and dirty" { - run _mock_latest_release "v0.1.2" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.2" run /tmp/notesium-test-version/v0.1.2-2-g1234567-dirty version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.1.2+2-dirty (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.1.2+2-dirty ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'You are using the latest version' ] assert_line 'comparison:0' assert_line 'version:0.1.2+2-dirty' @@ -197,12 +209,11 @@ teardown_file() { } @test "version: check verbose - older - beta" { - run _mock_latest_release "v0.2.0" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.2.0" run /tmp/notesium-test-version/v0.2.0-beta-0-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.2.0-beta (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.2.0-beta ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'A new release is available: 0.2.0 (2024-02-02 01:02)' ] assert_line 'comparison:-1' assert_line 'version:0.2.0-beta' @@ -210,12 +221,11 @@ teardown_file() { } @test "version: check verbose - newer - beta" { - run _mock_latest_release "v0.1.2" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.1.2" run /tmp/notesium-test-version/v0.2.0-beta-0-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.2.0-beta (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.2.0-beta ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'You are using a newer version than latest: 0.1.2' ] assert_line 'comparison:1' assert_line 'version:0.2.0-beta' @@ -223,15 +233,30 @@ teardown_file() { } @test "version: check verbose - match - beta" { - run _mock_latest_release "v0.2.0" & - [ "$GITHUB_WORKSPACE" ] && run sleep 1 + run _mock_latest_release "v0.2.0" run /tmp/notesium-test-version/v0.2.1-beta-0-g1234567 version --check --verbose echo "$output" [ $status -eq 0 ] - [ "${lines[0]}" == 'Notesium 0.2.1-beta (linux/amd64)' ] + [ "${lines[0]}" == "Notesium 0.2.1-beta ($EXPECTED_PLATFORM)" ] [ "${lines[1]}" == 'You are using the latest version' ] assert_line 'comparison:0' assert_line 'version:0.2.1-beta' assert_line 'gitversion:v0.2.1-beta-0-g1234567' } +@test "version: check - stop mock release server by sending terminate signal" { + # force stop otherwise bats will block until timeout (bats-core/issues/205) + run pgrep -x notesium + echo "$output" + echo "could not get pid" + [ $status -eq 0 ] + + run kill "$(pgrep -x notesium)" + echo "$output" + [ $status -eq 0 ] + + run pgrep -x notesium + echo "$output" + [ $status -eq 1 ] +} + diff --git a/tests/web.bats b/tests/web.bats index 8f4de5c..a3f2ee8 100644 --- a/tests/web.bats +++ b/tests/web.bats @@ -10,7 +10,7 @@ setup_file() { command -v curl >/dev/null export NOTESIUM_DIR="$BATS_TEST_DIRNAME/fixtures" export PATH="$(realpath $BATS_TEST_DIRNAME/../):$PATH" - [ "$(pidof notesium)" == "" ] + [ "$(pgrep -x notesium)" == "" ] } @test "web: start with custom port and stop-on-idle" { @@ -86,16 +86,16 @@ setup_file() { @test "web: stop by sending terminate signal" { # force stop otherwise bats will block until timeout (bats-core/issues/205) - run pidof notesium + run pgrep -x notesium echo "$output" echo "could not get pid" [ $status -eq 0 ] - run kill "$(pidof notesium)" + run kill "$(pgrep -x notesium)" echo "$output" [ $status -eq 0 ] - run pidof notesium + run pgrep -x notesium echo "$output" [ $status -eq 1 ] } diff --git a/tests/write.bats b/tests/write.bats index 11abcfd..ad4930c 100644 --- a/tests/write.bats +++ b/tests/write.bats @@ -24,7 +24,7 @@ _set_deterministic_mtimes() { setup_file() { command -v jq >/dev/null command -v curl >/dev/null - [ "$(pidof notesium)" == "" ] + [ "$(pgrep -x notesium)" == "" ] [ -e "/tmp/notesium-test-corpus" ] && exit 1 run mkdir /tmp/notesium-test-corpus run cp $BATS_TEST_DIRNAME/fixtures/*.md /tmp/notesium-test-corpus/ @@ -56,16 +56,16 @@ teardown_file() { @test "write: stop NOT writable by sending terminate signal" { # force stop otherwise bats will block until timeout (bats-core/issues/205) - run pidof notesium + run pgrep -x notesium echo "$output" echo "could not get pid" [ $status -eq 0 ] - run kill "$(pidof notesium)" + run kill "$(pgrep -x notesium)" echo "$output" [ $status -eq 0 ] - run pidof notesium + run pgrep -x notesium echo "$output" [ $status -eq 1 ] } @@ -166,16 +166,16 @@ teardown_file() { @test "write: stop by sending terminate signal" { # force stop otherwise bats will block until timeout (bats-core/issues/205) - run pidof notesium + run pgrep -x notesium echo "$output" echo "could not get pid" [ $status -eq 0 ] - run kill "$(pidof notesium)" + run kill "$(pgrep -x notesium)" echo "$output" [ $status -eq 0 ] - run pidof notesium + run pgrep -x notesium echo "$output" [ $status -eq 1 ] }