Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1220] Moved docs tests to .docs. #1228

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .drevops/docs/.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ commands:
cmd: ./.utils/lint.sh

test:
usage: Test
cmd: |
ahoy test-scripts
ahoy test-site

test-scripts:
usage: Test scripts
cmd: ./.utils/test.sh

test-site:
usage: Test site
cmd: docker compose run linkchecker http://mkdocs:8080 "$@"

Expand Down
1 change: 0 additions & 1 deletion .drevops/docs/.utils/.aspell.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Ssl
Suboptimal
TBD
Terminalizer
Twig CS Fixer
UAT
UI
UUID
Expand Down
2 changes: 2 additions & 0 deletions .drevops/docs/.utils/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor
node_modules
!package-lock.json
2 changes: 1 addition & 1 deletion .drevops/docs/.utils/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ build() {
echo "Copy combined dir into the container."
docker compose cp "${combined_repo_dir}/." mkdocs:"/tmp/build"
docker compose exec mkdocs git config --global --add safe.directory "/tmp/build"
# The credentials below are only used to produce some entermediate commits
# The credentials below are only used to produce some intermediate commits
# while building the docs. They are not used to push the code.
docker compose exec mkdocs git config --global user.name "Deployment robot"
docker compose exec mkdocs git config --global user.email "[email protected]"
Expand Down
40 changes: 40 additions & 0 deletions .drevops/docs/.utils/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
##
# Run DrevOps docs tests in CI.
#
# LCOV_EXCL_START

set -eu
[ "${DREVOPS_DEBUG-}" = "1" ] && set -x

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"

SCRIPTS_DIR="${ROOT_DIR}/docs/.utils"

TEST_DIR="${ROOT_DIR}/docs/.utils/tests"

# ------------------------------------------------------------------------------

# Configure git username and email if it is not set.
[ "$(git config --global user.name)" = "" ] && echo "==> Configuring global git user name" && git config --global user.name "Test user"
[ "$(git config --global user.email)" = "" ] && echo "==> Configuring global git user email" && git config --global user.email "[email protected]"

# Create stub of local framework.
docker network create amazeeio-network 2>/dev/null || true

echo "==> Run docs tests."

[ ! -d "${TEST_DIR}/node_modules" ] && echo " > Installing test Node dependencies into ${TEST_DIR}." && npm --prefix="${TEST_DIR}" ci

bats() {
pushd "${ROOT_DIR}" >/dev/null || exit 1
if [ -n "${DREVOPS_DEV_TEST_COVERAGE_DIR:-}" ]; then
mkdir -p "${DREVOPS_DEV_TEST_COVERAGE_DIR}"
kcov --include-pattern=.sh,.bash --bash-parse-files-in-dir="${SCRIPTS_DIR}","${TEST_DIR}" --exclude-pattern=vendor,node_modules "${DREVOPS_DEV_TEST_COVERAGE_DIR}" "${TEST_DIR}/node_modules/.bin/bats" "$@"
else
"${TEST_DIR}/node_modules/.bin/bats" "$@"
fi
popd >/dev/null || exit 1
}

bats "${TEST_DIR}/bats/docs.bats"
163 changes: 163 additions & 0 deletions .drevops/docs/.utils/tests/bats/_helper.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#!/usr/bin/env bash
#
# Helpers related to DrevOps common testing functionality.
#
# In some cases, shell may report platform incorrectly. Run with forced platform:
# DOCKER_DEFAULT_PLATFORM=linux/amd64 bats --tap tests/bats/test.bats
#
# shellcheck disable=SC2155,SC2119,SC2120,SC2044,SC2294
#

################################################################################
# BATS HOOK IMPLEMENTATIONS #
################################################################################

setup() {
# The root directory of the project.
export ROOT_DIR="$(dirname "$(cd "$(dirname "${BATS_TEST_DIRNAME}")/../../.." && pwd)")"

[ ! -d "${ROOT_DIR}/.drevops" ] && echo 'ERROR: The test should be run from the ".drevops" directory.' && exit 1

# Register a path to libraries.
export BATS_LIB_PATH="${BATS_TEST_DIRNAME}/../node_modules"
bats_load_library bats-helpers

##
## Application test directories structure setup.
##

# To run installation tests, several fixture directories are required.
# They are defined and created in setup() test method.
#
# Root build directory where the rest of fixture directories located.
# The "build" in this context is a place to store assets produced by the
# installer script during the test.
export BUILD_DIR="${BUILD_DIR:-"${BATS_TEST_TMPDIR//\/\//\/}/drevops-$(date +%s)"}"

# Directory where the installer script is executed.
# May have existing project files (e.g. from previous installations) or be
# empty (to facilitate brand-new install).
export CURRENT_PROJECT_DIR="${BUILD_DIR}/star_wars"

# Directory where the installer script will be sourcing the instance of DrevOps.
# As a part of test setup, the local copy of DrevOps at the last commit is
# copied to this location. This means that during development of tests local
# changes need to be committed.
export LOCAL_REPO_DIR="${BUILD_DIR}/local_repo"

fixture_prepare_dir "${BUILD_DIR}"
fixture_prepare_dir "${CURRENT_PROJECT_DIR}"
fixture_prepare_dir "${LOCAL_REPO_DIR}"

# Allow to override debug variables from environment when developing tests.
export DREVOPS_DEBUG="${TEST_DREVOPS_DEBUG:-}"

##
## SUT files setup.
##

# Copy DrevOps at the last commit.
fixture_export_codebase "${LOCAL_REPO_DIR}" "${ROOT_DIR}" >/dev/null

# Prepare global git config and ignore files required for testing cleanup scenarios.
prepare_global_gitconfig
prepare_global_gitignore

##
## Setting debug mode.
##

# Print debug if "--verbose-run" is passed or TEST_DREVOPS_DEBUG is set to "1".
if [ "${BATS_VERBOSE_RUN:-}" = "1" ] || [ "${TEST_DREVOPS_DEBUG:-}" = "1" ]; then
echo "Verbose run enabled." >&3
echo "BUILD_DIR: ${BUILD_DIR}" >&3
export RUN_STEPS_DEBUG=1

Check warning on line 74 in .drevops/docs/.utils/tests/bats/_helper.bash

View check run for this annotation

Codecov / codecov/patch

.drevops/docs/.utils/tests/bats/_helper.bash#L72-L74

Added lines #L72 - L74 were not covered by tests
fi

# Change directory to the current project directory for each test. Tests
# requiring to operate outside of CURRENT_PROJECT_DIR (like deployment tests)
# should change directory explicitly within their tests.
pushd "${CURRENT_PROJECT_DIR}" >/dev/null || exit 1
}

teardown() {
restore_global_gitignore
popd >/dev/null || cd "${ROOT_DIR}" || exit 1
}

# Print step.
step() {
# Using prefix different from command prefix in SUT for easy debug.
echo "**> STEP: ${1}" >&3

Check warning on line 91 in .drevops/docs/.utils/tests/bats/_helper.bash

View check run for this annotation

Codecov / codecov/patch

.drevops/docs/.utils/tests/bats/_helper.bash#L91

Added line #L91 was not covered by tests
}

# Print sub-step.
substep() {
echo " > ${1}" >&3
}

git_init() {
local allow_receive_update="${1:-0}"
local dir="${2:-$(pwd)}"

assert_not_git_repo "${dir}"
git --work-tree="${dir}" --git-dir="${dir}/.git" init >/dev/null

if [ "${allow_receive_update:-}" -eq 1 ]; then
git --work-tree="${dir}" --git-dir="${dir}/.git" config receive.denyCurrentBranch updateInstead >/dev/null
fi
}

git_add_all_commit() {
local message="${1}"
local dir="${2:-$(pwd)}"

assert_git_repo "${dir}"

git --work-tree="${dir}" --git-dir="${dir}/.git" add -A
git --work-tree="${dir}" --git-dir="${dir}/.git" commit -m "${message}" >/dev/null
commit=$(git --work-tree="${dir}" --git-dir="${dir}/.git" rev-parse HEAD)
echo "${commit}"
}

prepare_global_gitconfig() {
git config --global init.defaultBranch >/dev/null || git config --global init.defaultBranch "main"
}

prepare_global_gitignore() {
filename="${HOME}/.gitignore"
filename_backup="${filename}".bak

if git config --global --list | grep -q core.excludesfile; then
git config --global core.excludesfile >/tmp/git_config_global_exclude

Check warning on line 132 in .drevops/docs/.utils/tests/bats/_helper.bash

View check run for this annotation

Codecov / codecov/patch

.drevops/docs/.utils/tests/bats/_helper.bash#L132

Added line #L132 was not covered by tests
fi

[ -f "${filename}" ] && cp "${filename}" "${filename_backup}"

cat <<EOT >"${filename}"
##
## Temporary files generated by various OSs and IDEs
##
Thumbs.db
._*
.DS_Store
.idea
.idea/*
*.sublime*
.project
.netbeans
.vscode
.vscode/*
nbproject
nbproject/*
EOT

git config --global core.excludesfile "${filename}"
}

restore_global_gitignore() {
filename=${HOME}/.gitignore
filename_backup="${filename}".bak
[ -f "${filename_backup}" ] && cp "${filename_backup}" "${filename}"
[ -f "/tmp/git_config_global_exclude" ] && git config --global core.excludesfile "$(cat /tmp/git_config_global_exclude)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#
# Test for docs publishing functionality.
#
# shellcheck disable=SC2030,SC2031,SC2129
# shellcheck disable=SC2030,SC2031,SC2129,SC2002

load _helper.bash

export BATS_FIXTURE_EXPORT_CODEBASE_ENABLED=1

@test "Docs release" {
update_local_repo

Expand All @@ -23,7 +25,7 @@ load _helper.bash
# The very first version is set as latest.
assert_version "feature-test-branch-first" 1

substep "Test 2: Followup publish from \"feature-test-branch-second\" branch."
substep 'Test 2: Followup publish from "feature-test-branch-second" branch.'
export DOCS_PUBLISH_SRC_BRANCH="feature/test-branch-second"
run ./.utils/publish.sh
assert_success
Expand All @@ -32,7 +34,7 @@ load _helper.bash
assert_version "feature-test-branch-first" 1
assert_version "feature-test-branch-second"

substep "Test 3: Followup publish from \"1.0.0\" tag."
substep 'Test 3: Followup publish from "1.0.0" tag.'
export DOCS_PUBLISH_SRC_BRANCH="main"
export DOCS_PUBLISH_SRC_TAG="1.0.0"
run ./.utils/publish.sh
Expand All @@ -41,7 +43,7 @@ load _helper.bash
assert_version "feature-test-branch-second"
assert_version "1.0.0" 1

substep "Test 4: Followup publish from \"1.1.0\" tag."
substep 'Test 4: Followup publish from "1.1.0" tag.'
export DOCS_PUBLISH_SRC_BRANCH="main"
export DOCS_PUBLISH_SRC_TAG="1.1.0"
run ./.utils/publish.sh
Expand All @@ -51,7 +53,7 @@ load _helper.bash
assert_version "1.0.0"
assert_version "1.1.0" 1

substep "Test 5: Followup publish from \"main\" branch."
substep 'Test 5: Followup publish from "main" branch.'
export DOCS_PUBLISH_SRC_BRANCH="main"
export DOCS_PUBLISH_CANARY_BRANCH="main"
export DOCS_PUBLISH_SRC_TAG=""
Expand All @@ -75,10 +77,10 @@ assert_version() {
assert_file_exists "${REMOTE_REPO_DIR}/CNAME"
assert_file_exists "${REMOTE_REPO_DIR}/versions.json"

actual_has_version="$(cat "${REMOTE_REPO_DIR}/versions.json" | jq 'any(.[]; .version == "'${expected_version}'")')"
actual_has_version="$(cat "${REMOTE_REPO_DIR}/versions.json" | jq 'any(.[]; .version == "'"${expected_version}"'")')"
assert_equal "true" "${actual_has_version}"

actual_has_alias="$(cat "${REMOTE_REPO_DIR}/versions.json" | jq 'any(.[]; .version == "'${expected_version}'" and any(.aliases[]?; . == "latest"))')"
actual_has_alias="$(cat "${REMOTE_REPO_DIR}/versions.json" | jq 'any(.[]; .version == "'"${expected_version}"'" and any(.aliases[]?; . == "latest"))')"
if [ "${expected_has_alias}" -eq 1 ]; then
assert_equal "true" "${actual_has_alias}"
else
Expand Down
52 changes: 52 additions & 0 deletions .drevops/docs/.utils/tests/package-lock.json

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

9 changes: 9 additions & 0 deletions .drevops/docs/.utils/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "drevops-tests",
"version": "1.0.0",
"description": "Packages used for testing of DrevOps",
"license": "GPL-2.0-or-later",
"devDependencies": {
"bats-helpers": "npm:@drevops/bats-helpers@^1.3.1"
}
}
3 changes: 1 addition & 2 deletions .drevops/tests/lint.spelling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ done < <(
find \
"${ROOT_DIR}"/.circleci \
"${ROOT_DIR}"/.docker \
"${ROOT_DIR}"/docs \
"${ROOT_DIR}"/hooks/library \
"${ROOT_DIR}"/scripts \
"${ROOT_DIR}"/patches \
Expand Down Expand Up @@ -53,7 +52,7 @@ for file in "${targets[@]}"; do
# Remove HTML.
sed -E 's/<([^<]+)>//g' |
# Remove code blocks.
sed -n '/\`\`\`/,/\`\`\`/ !p' |
sed '/^[[:space:]]*```/,/^[[:space:]]*```/d' |
# Remove inline code.
sed -n '/\`/,/\`/ !p' |
# Remove anchors.
Expand Down
1 change: 0 additions & 1 deletion .drevops/tests/test.common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ bats "${TEST_DIR}/bats/install.integrations.bats"
bats "${TEST_DIR}/bats/install.demo.bats"
bats "${TEST_DIR}/bats/reset.bats"
bats "${TEST_DIR}/bats/update-drevops.bats"
bats "${TEST_DIR}/bats/docs.bats"
Loading
Loading