Skip to content

Commit bcaf84e

Browse files
committed
Add testing using bats
1 parent 1168f11 commit bcaf84e

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# OSX
2+
.DS_Store
3+
4+
# Node
5+
node_modules

Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:alpine
2+
3+
RUN apk add --no-cache bash
4+
5+
RUN mkdir -p /app
6+
WORKDIR /app
7+
8+
COPY . /app
9+
RUN npm install
10+
11+
CMD ./node_modules/.bin/bats wait-for.bats

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ services:
3939
command: sh -c './wait-for db:5432 -- npm start'
4040
depends_on:
4141
- db
42-
```
42+
```
43+
44+
## Testing
45+
46+
Ironically testing is done using [bats](https://github.com/sstephenson/bats), which on the other hand is depending on [bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)).
47+
48+
docker build -t wait-for .
49+
docker run -t wait-for

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"bats": "^0.4.2"
4+
}
5+
}

wait-for

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ USAGE
2222
wait_for() {
2323
command="$*"
2424
for i in `seq $TIMEOUT` ; do
25-
nc -z "$HOST" "$PORT"
25+
nc -z "$HOST" "$PORT" > /dev/null 2>&1
26+
2627
result=$?
2728
if [ $result -eq 0 ] ; then
2829
if [ -n "$command" ] ; then

wait-for.bats

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bats
2+
3+
@test "google should be immediately found" {
4+
run ./wait-for google.com:80 -- echo 'success'
5+
6+
[ "$output" = "success" ]
7+
}
8+
9+
@test "nonexistent server should not start command" {
10+
run ./wait-for -t 1 noserver:9999 -- echo 'success'
11+
12+
[ "$status" -ne 0 ]
13+
[ "$output" != "success" ]
14+
}

0 commit comments

Comments
 (0)