diff --git a/slackme/main.test.ts b/slackme/main.test.ts index 53c6b2e..4b1e338 100644 --- a/slackme/main.test.ts +++ b/slackme/main.test.ts @@ -35,7 +35,7 @@ describe("slackme", async () => { expect(exec.exitCode).toBe(0); exec = await execContainer(id, ["sh", "-c", "slackme"]); expect(exec.stdout.trim()).toStartWith( - "slackme — Send a Slack notification when a command finishes", + "slackme — Send a Slack notification when a command finishes" ); }); @@ -56,6 +56,15 @@ describe("slackme", async () => { }); }); + it("exits with command code", async () => { + const { instance, id } = await setupContainer(); + await writeCoder(id, "echo 'some-url' && exit 1"); + let exec = await execContainer(id, ["sh", "-c", instance.script]); + expect(exec.exitCode).toBe(0); + exec = await execContainer(id, ["sh", "-c", "slackme exit 1"]); + expect(exec.exitCode).toBe(1); + }); + it("formats multiline message", async () => { await assertSlackMessage({ command: "echo test", @@ -107,7 +116,7 @@ executed`, const setupContainer = async ( image = "alpine", - vars: Record = {}, + vars: Record = {} ) => { const state = await runTerraformApply(import.meta.dir, { agent_id: "foo", @@ -150,7 +159,7 @@ const assertSlackMessage = async (opts: { "alpine/curl", opts.format && { slack_message: opts.format, - }, + } ); await writeCoder(id, "echo 'token'"); let exec = await execContainer(id, ["sh", "-c", instance.script]); diff --git a/slackme/slackme.sh b/slackme/slackme.sh index 17f74ec..183aede 100644 --- a/slackme/slackme.sh +++ b/slackme/slackme.sh @@ -72,7 +72,8 @@ fi START=$(date +%s%N) # Run all arguments as a command -$@ +"$@" +CODE=$? END=$(date +%s%N) DURATION_MS=$${DURATION_MS:-$(( (END - START) / 1000000 ))} PRETTY_DURATION=$(pretty_duration $DURATION_MS) @@ -85,3 +86,5 @@ SLACK_MESSAGE=$(echo "$SLACK_MESSAGE" | sed "s|\\$DURATION|$PRETTY_DURATION|g") curl --silent -o /dev/null --header "Authorization: Bearer $BOT_TOKEN" \ -G --data-urlencode "text=$${SLACK_MESSAGE}" \ "$SLACK_URL/api/chat.postMessage?channel=$USER_ID&pretty=1" + +exit $CODE