fix: lint-staged test runner to not fail if no tests are collected, update CONTRIBUTING.md instructions for test credentials

pull/5503/head
Jason Stirnaman 2024-06-25 11:37:28 -05:00
parent bb166b0bdb
commit f9bd0891d9
2 changed files with 55 additions and 17 deletions

View File

@ -1,24 +1,48 @@
// Lint-staged configuration. This file must export a lint-staged configuration object.
function lintStagedContent(paths, productPath) {
const name = `staged-${productPath.replace(/\//g, '-')}`;
function testStagedContent(paths, productPath) {
const productName = productPath.replace(/\//g, '-');
const CONTENT = `staged-${productName}`;
const TEST = `pytest-${productName}`;
return [
// Remove any existing test container and volume
`sh -c "docker rm -f ${CONTENT} || true"`,
`sh -c "docker rm -f ${TEST} || true"`,
`docker build . -f Dockerfile.tests -t influxdata-docs/tests:latest`,
// Remove any existing test container.
`docker rm -f ${name} || true`,
`docker run --name ${name} --mount type=volume,target=/app/content --mount type=bind,src=./content,dst=/src/content --mount type=bind,src=./static/downloads,dst=/app/data
// Copy staged content to a volume and run the prepare script
`docker run --name ${CONTENT}
--mount type=volume,source=staged-content,target=/app/content
--mount type=bind,src=./content,dst=/src/content
--mount type=bind,src=./static/downloads,dst=/app/data
influxdata-docs/tests --files "${paths.join(' ')}"`,
`docker build . -f Dockerfile.pytest -t influxdata-docs/pytest:latest`,
`docker build .
-f Dockerfile.pytest
-t influxdata-docs/pytest:latest`,
// Run test runners. If tests fail, the container will be removed,
//but the "test-" container will remain until the next run.
`docker run --env-file ${productPath}/.env.test
--volumes-from ${name} --rm
influxdata-docs/pytest --codeblocks ${productPath}/`
// Run test runners.
// This script first checks if there are any tests to run using `pytest --collect-only`.
// If there are tests, it runs them; otherwise, it exits with a success code.
// Whether tests pass or fail, the container is removed,
// but the CONTENT container will remain until the next run.
`sh -c "docker run --rm --name ${TEST}-collector \
--env-file ${productPath}/.env.test \
--volumes-from ${CONTENT} \
influxdata-docs/pytest --codeblocks --collect-only \
${productPath}/ > /dev/null 2>&1; \
TEST_COLLECT_EXIT_CODE=$?; \
if [ $TEST_COLLECT_EXIT_CODE -eq 5 ]; then \
echo 'No tests to run.'; \
exit 0; \
else \
docker run --rm --name ${TEST} \
--env-file ${productPath}/.env.test \
--volumes-from ${CONTENT} \
influxdata-docs/pytest --codeblocks --exitfirst ${productPath}/;
fi"`
];
}
@ -30,9 +54,11 @@ export default {
// "*.md": paths => `prettier --check ${paths.join(' ')}`,
"content/influxdb/cloud-dedicated/**/*.md":
paths => lintStagedContent(paths, 'content/influxdb/cloud-dedicated'),
paths => [...testStagedContent(paths, 'content/influxdb/cloud-dedicated')],
"content/influxdb/cloud-serverless/**/*.md":
paths => [...testStagedContent(paths, 'content/influxdb/cloud-serverless')],
"content/influxdb/clustered/**/*.md":
paths => lintStagedContent(paths, 'content/influxdb/clustered'),
paths => [...testStagedContent(paths, 'content/influxdb/clustered')],
// "content/influxdb/cloud-serverless/**/*.md": "docker compose run -T lint --config=content/influxdb/cloud-serverless/.vale.ini --minAlertLevel=error",

View File

@ -71,7 +71,7 @@ Make your suggested changes being sure to follow the [style and formatting guide
docs-v2 uses Husky to manage Git hook scripts.
When you try to commit your changes (for example, `git commit`), Git runs
scripts configured in `.husky/pre-commit`, including linting and tests for your staged files.
scripts configured in `.husky/pre-commit`, including linting and tests for your **staged** files.
### Skip pre-commit hooks
@ -85,13 +85,25 @@ git commit -m "<COMMIT_MESSAGE>" --no-verify
For more options, see the [Husky documentation](https://typicode.github.io/husky/how-to.html#skipping-git-hooks).
### Configure test credentials
To configure credentials for tests, set the usual InfluxDB environment variables
for each product inside a `content/influxdb/<PRODUCT_DIRECTORY>/.env.test` file.
The Docker commands in the `.lintstagedrc.mjs` lint-staged configuration load
the `.env.test` as product-specific environment variables.
**Warning**: To prevent accidentally adding credentials to the docs-v2 repo,
Git is configured to ignore `.env*` files. Don't add your `.env.test` files to Git.
Consider backing them up on your local machine in case of accidental deletion.
### Pre-commit linting and testing
When you try to commit your changes using `git commit` or your editor,
the project automatically runs pre-commit checks for spelling, punctuation,
and style on your staged files.
The pre-commit hook calls [`lint-staged`](https://github.com/lint-staged/lint-staged) using the configuration in `package.json`.
The pre-commit hook calls [`lint-staged`](https://github.com/lint-staged/lint-staged) using the configuration in `.lintstagedrc.mjs`.
To run `lint-staged` scripts manually (without committing), enter the following
command in your terminal: