Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1b51ed

Browse files
committed
♻️ Introduce unit-testing
1 parent 424294a commit c1b51ed

7 files changed

Lines changed: 103 additions & 2 deletions

File tree

.github/dependabot.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ updates:
44
directory: "/"
55
schedule:
66
interval: "daily"
7+
- package-ecosystem: "docker"
8+
directory: "/"
9+
schedule:
10+
interval: "daily"

.github/workflows/release.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ on:
77
- '*'
88

99
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
steps:
15+
- uses: actions/checkout@v4
16+
- run: make test
1017
integration-test:
1118
runs-on: ubuntu-latest
1219
permissions:
@@ -27,7 +34,6 @@ jobs:
2734
with:
2835
title: ${{ github.sha }}
2936
tag: ${{ github.run_id }}
30-
3137
release:
3238
runs-on: ubuntu-latest
3339
needs:

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM ubuntu:24.04@sha256:99c35190e22d294cdace2783ac55effc69d32896daaa265f0bbedbcde4fbe3e5 as testEnv
2+
RUN apt-get update && apt-get install -y coreutils bats
3+
4+
ADD mock.sh /usr/local/mock/gh
5+
ADD mock.sh /usr/local/mock/cd
6+
7+
ADD entrypoint.sh /entrypoint.sh
8+
ADD test.bats /test.bats
9+
10+
RUN /test.bats

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.EXPORT_ALL_VARIABLES:
2+
DOCKER_BUILDKIT=0# to prevent caching of test results
3+
4+
test:
5+
podman build .

entrypoint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22

33
if [ ! -z "${INPUT_WORKDIR}" ]; then
44
cd "${INPUT_WORKDIR}"

mock.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
binary="$0"
3+
parameters="$@"
4+
5+
echo "${binary} ${parameters}" >> mockArgs
6+
stdin=$(cat -)
7+
echo "${binary} ${stdin}" >> mockStdin
8+
9+
function mockShouldFail() {
10+
[ "${MOCK_RETURNS[${binary}]}" = "_${parameters}" ]
11+
}
12+
13+
source mockReturns
14+
15+
if [ ! -z "${MOCK_RETURNS[${binary}]}" ] || [ ! -z "${MOCK_RETURNS[${binary} $1]}" ]; then
16+
if mockShouldFail ; then
17+
exit 1
18+
fi
19+
if [ ! -z "${MOCK_RETURNS[${binary} $1]}" ]; then
20+
echo ${MOCK_RETURNS[${binary} $1]}
21+
exit 0
22+
fi
23+
echo ${MOCK_RETURNS[${binary}]}
24+
fi
25+
26+
exit 0

test.bats

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env bats
2+
3+
setup(){
4+
export PATH="/usr/local/mock:/usr/bin"
5+
cat /dev/null >| mockArgs
6+
cat /dev/null >| mockStdin
7+
8+
declare -A -p MOCK_RETURNS=(
9+
['/usr/local/mock/gh']=""
10+
['/usr/local/mock/cd']=""
11+
) > mockReturns
12+
13+
export INPUT_TITLE='TITLE'
14+
}
15+
16+
teardown() {
17+
unset INPUT_WORKDIR
18+
unset INPUT_TAG
19+
}
20+
21+
@test "it creates a release" {
22+
run /entrypoint.sh
23+
24+
expectMockCalledIs "/usr/local/mock/gh release create release-$(date +%Y%m%d%H%M%S) -t TITLE --generate-notes"
25+
}
26+
27+
@test "it creates a release with a predefined tag" {
28+
export INPUT_TAG="TAG"
29+
30+
run /entrypoint.sh
31+
32+
expectMockCalledIs "/usr/local/mock/gh release create ${INPUT_TAG} -t TITLE --generate-notes"
33+
}
34+
35+
@test "it creates a release in a working directory" {
36+
export INPUT_WORKDIR="WORKDIR"
37+
export INPUT_TAG="TAG"
38+
39+
run /entrypoint.sh
40+
41+
expectMockCalledIs "/usr/local/mock/gh release create ${INPUT_TAG} -t TITLE --generate-notes"
42+
}
43+
44+
expectMockCalledIs() {
45+
local expected=$(echo "${1}" | tr -d '\n')
46+
local got=$(cat mockArgs | tr -d '\n')
47+
echo "Expected: |${expected}|
48+
Got: |${got}|"
49+
[ "${got}" == "${expected}" ]
50+
}

0 commit comments

Comments
 (0)