Skip to content

Commit 7c8ccb3

Browse files
authored
Merge pull request #2 from Dropelikeit/feature/add-phpflex
Add Docker-based Bats tests for install/uninstall scripts
2 parents 501e2eb + 404093e commit 7c8ccb3

File tree

8 files changed

+236
-0
lines changed

8 files changed

+236
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
COMMIT=test
2+
DOCKER_COMPOSE_TEST=docker compose -f docker-compose.yaml
3+
DOCKER_COMPOSE_PHPFLEX_FUNCTION_TEST=docker compose -f docker-compose.phpflextest.yaml
4+
5+
.PHONY: test
6+
test:
7+
${DOCKER_COMPOSE_TEST} run --rm phpflex bats tests/test_install.bats tests/test_uninstall.bats
8+
${DOCKER_COMPOSE_PHPFLEX_FUNCTION_TEST} run --rm phpflextest bats tests/test_phpflex.bats

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ chmod -x uninstall.sh && \
4949
source ~/<.zshrc | .bashrc>
5050
```
5151

52+
# Tests
53+
54+
---
55+
56+
The scripts `install.sh` and `uninstall.sh` in the dir `src` are tested with [bats](https://bats-core.readthedocs.io/en/stable/).
57+
58+
To ensure that your configuration files (.zshrc or .bashrc) are not changed or even rendered unusable by the tests, a Docker image was added to the project. The tests should only be executed within the Docker container, otherwise the tests could change or even delete their configuration file.
59+
60+
The Docker image is based on an Ubuntu image with Homebrew installed.
61+
62+
If you want to run the tests, you only need to execute the following command:
63+
64+
```bash
65+
make test
66+
```
67+
68+
When the command is executed, a Docker container is built and the tests are executed in it.
69+
5270
# Contributions
5371

5472
---

docker-compose.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
phpflex:
3+
build:
4+
context: .
5+
dockerfile: docker/Dockerfile
6+
volumes:
7+
- ./:/workspace
8+
working_dir: /workspace

docker/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM homebrew/brew
2+
3+
LABEL authors="Marcel Strahl <[email protected]>"
4+
5+
COPY src/install.sh /usr/local/bin/install.sh
6+
COPY src/uninstall.sh /usr/local/bin/uninstall.sh
7+
COPY tests/test_install.bats /usr/local/bin/test_install.bats
8+
COPY tests/test_uninstall.bats /usr/local/bin/test_uninstall.bats
9+
10+
# Update Homebrew and install necessary packages
11+
RUN brew update && \
12+
brew tap shivammathur/php && \
13+
brew install [email protected] && \
14+
brew install [email protected] && \
15+
brew install [email protected] && \
16+
brew install bats-core && \
17+
# Clean up Homebrew cache to reduce image size
18+
brew cleanup
19+
20+
EXPOSE 9000
21+
22+
CMD ["php", "-a"]
File renamed without changes.
File renamed without changes.

tests/test_install.bats

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bats
2+
3+
HOME_DIR=$HOME
4+
ZSHRC="$HOME_DIR/.zshrc"
5+
BASHRC="$HOME_DIR/.bashrc"
6+
7+
GREEN="\033[32m"
8+
RED="\033[31m"
9+
10+
setup() {
11+
# Create temporary config files
12+
touch "$ZSHRC"
13+
touch "$BASHRC"
14+
}
15+
16+
teardown() {
17+
# Remove temporary config files
18+
rm -f "$ZSHRC"
19+
rm -f "$BASHRC"
20+
}
21+
22+
@test "Run install.sh without errors" {
23+
run bash /usr/local/bin/install.sh
24+
[ "$status" -eq 0 ]
25+
}
26+
27+
@test "Check if phpFlex function is added to .zshrc when using zsh" {
28+
SHELL="/bin/zsh"
29+
export SHELL
30+
31+
run bash /usr/local/bin/install.sh
32+
grep -q "phpFlex()" "$ZSHRC"
33+
[ "$status" -eq 0 ]
34+
}
35+
36+
@test "Check if phpFlex function is added to .bashrc when using bash" {
37+
SHELL="/bin/bash"
38+
export SHELL
39+
40+
run bash /usr/local/bin/install.sh
41+
grep -q "phpFlex()" "$BASHRC"
42+
[ "$status" -eq 0 ]
43+
}
44+
45+
@test "Check script behavior for unsupported shell" {
46+
SHELL="/usr/bin/fish"
47+
export SHELL
48+
49+
run bash /usr/local/bin/install.sh
50+
[ "$status" -eq 1 ]
51+
[[ "$output" =~ "Unsupported shell. Only zsh and bash are supported." ]]
52+
}
53+
54+
@test "Ensure phpFlex function is not duplicated in .zshrc" {
55+
SHELL="/bin/zsh"
56+
export SHELL
57+
58+
# Add function manually first
59+
echo "phpFlex()" >> "$ZSHRC"
60+
61+
run bash /usr/local/bin/install.sh
62+
[ "$status" -eq 0 ]
63+
64+
# Ensure there's only one occurrence of the phpFlex function
65+
occurrences=$(grep -c "phpFlex()" "$ZSHRC")
66+
[ "$occurrences" -eq 1 ]
67+
}
68+
69+
@test "Check clickable link is generated for zsh" {
70+
SHELL="/bin/zsh"
71+
export SHELL
72+
73+
run bash /usr/local/bin/install.sh
74+
[[ "$output" =~ "source $ZSHRC" ]]
75+
}

tests/test_uninstall.bats

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bats
2+
3+
HOME_DIR=$HOME
4+
ZSHRC="$HOME_DIR/.zshrc"
5+
BASHRC="$HOME_DIR/.bashrc"
6+
7+
setup() {
8+
# Create temporary config files
9+
touch "$ZSHRC"
10+
touch "$BASHRC"
11+
}
12+
13+
teardown() {
14+
# Remove temporary config files
15+
rm -f "$ZSHRC"*
16+
rm -f "$BASHRC"*
17+
}
18+
19+
@test "Run install.sh without errors" {
20+
run bash /usr/local/bin/install.sh
21+
[ "$status" -eq 0 ]
22+
}
23+
24+
@test "Check if phpFlex function is added to .zshrc when using zsh" {
25+
SHELL="/bin/zsh"
26+
export SHELL
27+
28+
run bash /usr/local/bin/install.sh
29+
grep -q "phpFlex()" "$ZSHRC"
30+
[ "$status" -eq 0 ]
31+
}
32+
33+
@test "Check if phpFlex function is added to .bashrc when using bash" {
34+
SHELL="/bin/bash"
35+
export SHELL
36+
37+
run bash /usr/local/bin/install.sh
38+
grep -q "phpFlex()" "$BASHRC"
39+
[ "$status" -eq 0 ]
40+
}
41+
42+
@test "Check install script behavior for unsupported shell" {
43+
SHELL="/usr/bin/fish"
44+
export SHELL
45+
46+
run bash /usr/local/bin/install.sh
47+
[ "$status" -eq 1 ]
48+
[[ "$output" =~ "Unsupported shell. Only zsh and bash are supported." ]]
49+
}
50+
51+
@test "Run uninstall.sh without errors" {
52+
SHELL="/bin/zsh"
53+
export SHELL
54+
55+
# First, install the function
56+
run bash /usr/local/bin/install.sh
57+
[ "$status" -eq 0 ]
58+
59+
# Then, uninstall it
60+
run bash /usr/local/bin/uninstall.sh
61+
[ "$status" -eq 0 ]
62+
}
63+
64+
@test "Check if phpFlex function is removed from .zshrc when using zsh" {
65+
SHELL="/bin/zsh"
66+
export SHELL
67+
68+
# First, install the function
69+
run bash /usr/local/bin/install.sh
70+
[ "$status" -eq 0 ]
71+
72+
# Then, uninstall it
73+
run bash /usr/local/bin/uninstall.sh
74+
[ "$status" -eq 0 ]
75+
76+
# Ensure the function is removed
77+
! grep -q "phpFlex()" "$ZSHRC"
78+
[ "$status" -eq 0 ]
79+
}
80+
81+
@test "Check if phpFlex function is removed from .bashrc when using bash" {
82+
SHELL="/bin/bash"
83+
export SHELL
84+
85+
# First, install the function
86+
run bash /usr/local/bin/install.sh
87+
[ "$status" -eq 0 ]
88+
89+
# Then, uninstall it
90+
run bash /usr/local/bin/uninstall.sh
91+
[ "$status" -eq 0 ]
92+
93+
# Ensure the function is removed
94+
! grep -q "phpFlex()" "$BASHRC"
95+
[ "$status" -eq 0 ]
96+
}
97+
98+
@test "Check uninstall script behavior for unsupported shell" {
99+
SHELL="/usr/bin/fish"
100+
export SHELL
101+
102+
run bash /usr/local/bin/uninstall.sh
103+
[ "$status" -eq 1 ]
104+
[[ "$output" =~ "Unsupported shell. Only zsh and bash are supported." ]]
105+
}

0 commit comments

Comments
 (0)