Skip to content

Commit

Permalink
Add basic tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbode committed Dec 24, 2024
1 parent 138ceb3 commit 1c497a3
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 3 deletions.
14 changes: 11 additions & 3 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- run: |
curl mise.run | sh
echo 'export PATH=$HOME/.local/bin:$PATH' >> $GITHUB_ENV
- run: mkdir -p ~/.local/share/mise/plugins
- run: ln -s $PWD ~/.local/share/mise/plugins/poetry
- run: mise i poetry@latest python
Expand All @@ -33,12 +35,18 @@ jobs:
- run: cat poetry-installer-error-*.log && exit 1
if: failure()

- name: Run tests
run: |-
mise config set settings.trusted_config_paths "${PWD}/tests"
mise -C "${PWD}/tests" install
make test
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Homebrew
id: set-up-homebrew
Expand All @@ -55,7 +63,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Homebrew
id: set-up-homebrew
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ format-check:
lint:
shellcheck $(SH_SRCFILES)
.PHONY: lint

test:
tests/run_tests.sh
.PHONY: test
2 changes: 2 additions & 0 deletions tests/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
"aqua:bats-core/bats-core" = "latest"
7 changes: 7 additions & 0 deletions tests/01_simple/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tools]
python = { version = "3.12" }
poetry = { version = "latest", pyproject="{{ config_root }}/pyproject.toml" }

[env]
MISE_POETRY_AUTO_INSTALL = "true"
MISE_POETRY_VENV_AUTO = "false"
17 changes: 17 additions & 0 deletions tests/01_simple/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/01_simple/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[virtualenvs]
in-project = true
prefer-active-python = true
12 changes: 12 additions & 0 deletions tests/01_simple/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "0.0.0"
description = "Test"
authors = [
"Josh Bode <[email protected]>",
]
package-mode = false

[tool.poetry.dependencies]
python = ">=3.10,<3.13"
pbr = "^6.0.0"
18 changes: 18 additions & 0 deletions tests/01_simple/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/env bats

@test "check environment: VIRTUAL_ENV is set" {
[[ ${VIRTUAL_ENV} == "${PWD}/.venv" ]]
}

@test "check environment: POETRY_ACTIVE == 1" {
[[ ${POETRY_ACTIVE} == "1" ]]
}

@test "check environment: PATH contains venv bin" {
[[ ":${PATH}:" == *":${VIRTUAL_ENV}/bin:"* ]]
}

@test "check installed packages" {
result=$(which pbr)
[[ ${result} == "${VIRTUAL_ENV}/bin/pbr" ]]
}
7 changes: 7 additions & 0 deletions tests/02_auto_venv_with_lock/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tools]
python = { version = "3.12" }
poetry = { version = "latest", pyproject="{{ config_root }}/pyproject.toml" }

[env]
MISE_POETRY_AUTO_INSTALL = "true"
MISE_POETRY_VENV_AUTO = "true"
17 changes: 17 additions & 0 deletions tests/02_auto_venv_with_lock/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/02_auto_venv_with_lock/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[virtualenvs]
in-project = true
prefer-active-python = true
12 changes: 12 additions & 0 deletions tests/02_auto_venv_with_lock/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "0.0.0"
description = "Test"
authors = [
"Josh Bode <[email protected]>",
]
package-mode = false

[tool.poetry.dependencies]
python = ">=3.10,<3.13"
pbr = "^6.0.0"
18 changes: 18 additions & 0 deletions tests/02_auto_venv_with_lock/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /usr/bin/env bats

@test "check environment: VIRTUAL_ENV is set" {
[[ ${VIRTUAL_ENV} == "${PWD}/.venv" ]]
}

@test "check environment: POETRY_ACTIVE == 1" {
[[ ${POETRY_ACTIVE} == "1" ]]
}

@test "check environment: PATH contains venv bin" {
[[ ":${PATH}:" == *":${VIRTUAL_ENV}/bin:"* ]]
}

@test "check installed packages" {
result=$(which pbr)
[[ ${result} == "${VIRTUAL_ENV}/bin/pbr" ]]
}
7 changes: 7 additions & 0 deletions tests/02_auto_venv_without_lock/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tools]
python = { version = "3.12" }
poetry = { version = "latest", pyproject="{{ config_root }}/pyproject.toml" }

[env]
MISE_POETRY_AUTO_INSTALL = "true"
MISE_POETRY_VENV_AUTO = "true"
3 changes: 3 additions & 0 deletions tests/02_auto_venv_without_lock/poetry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[virtualenvs]
in-project = true
prefer-active-python = true
12 changes: 12 additions & 0 deletions tests/02_auto_venv_without_lock/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[tool.poetry]
name = "test"
version = "0.0.0"
description = "Test"
authors = [
"Josh Bode <[email protected]>",
]
package-mode = false

[tool.poetry.dependencies]
python = ">=3.10,<3.13"
pbr = "^6.0.0"
9 changes: 9 additions & 0 deletions tests/02_auto_venv_without_lock/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/env bats

@test "check environment: VIRTUAL_ENV is not set" {
[[ ! -v VIRTUAL_ENV ]]
}

@test "check environment: POETRY_ACTIVE is not set" {
[[ ! -v POETRY_ACTIVE ]]
}
48 changes: 48 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#! /usr/bin/env bash

set -u
shopt -s nullglob

BASE_DIR=${0%/*}
if [ "${BASE_DIR}" = "." ]; then
BASE_DIR=${PWD}
elif [ "${BASE_DIR}" = "${BASE_DIR#/}" ]; then
BASE_DIR=${PWD}/${BASE_DIR}
fi

SUCCESS=()
FAILURE=()

printf '██ Running tests\n'

FILES=("${BASE_DIR}"/*)
for DIR in "${FILES[@]}"; do
if [[ ! -d ${DIR} ]]; then
continue
fi
NAME=${DIR#"${BASE_DIR}/"}
printf '█ %s\n\n' "${NAME}"

if mise exec -C "${DIR}" -- bats -r "${DIR}"; then
SUCCESS+=("${NAME}")
else
FAILURE+=("${NAME}")
fi
done

declare -i EXIT=0

if ((${#SUCCESS[@]})); then
printf '██ Passed:\n'
printf ' - %s\n' "${SUCCESS[@]}"
printf '\n'
fi

if ((${#FAILURE[@]})); then
printf '██ Failed:\n'
printf ' - %s\n' "${FAILURE[@]}"
printf '\n'
EXIT=1
fi

exit ${EXIT}

0 comments on commit 1c497a3

Please sign in to comment.