Skip to content

Commit

Permalink
Merge pull request #2 from nationalarchives/feature/docker-test
Browse files Browse the repository at this point in the history
Make a Docker image for tests
  • Loading branch information
ahosgood authored Aug 20, 2024
2 parents 1233014 + 396a940 commit 7bb72e6
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 72 deletions.
14 changes: 6 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install Python requirements
run: pip install -r test/requirements.txt
- name: Run server
run: flask --app test run --debug --port 5000 &
- name: Install Node dependencies
run: npm install
- name: Start test server
run: docker compose up -d
- name: Wait for response
uses: nev7n/wait_for_response@v1
with:
url: http://localhost:5001/ready/
- name: Run the tests
run: node test-fixtures.mjs || exit 1
17 changes: 9 additions & 8 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ on:
release:
types: [published]

concurrency:
group: deploy-${{ github.ref }}

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install Python requirements
run: pip install -r test/requirements.txt
- name: Run server
run: flask --app test run --debug --port 5000 &
- name: Install Node dependencies
run: npm install
- name: Start test server
run: docker compose up -d
- name: Wait for response
uses: nev7n/wait_for_response@v1
with:
url: http://localhost:5001/ready/
- name: Run the tests
run: node test-fixtures.mjs || exit 1

Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ We test each component against its published [component fixtures](https://github
## Test the templates

```sh
python -m venv venv
python install -r test/requirements.txt
flask --app test run --debug --port 5000
npm install
docker compose up -d
node test-fixtures.mjs
```
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
services:
app:
build:
# context: test
dockerfile: test/Dockerfile
environment:
- ENVIRONMENT=develop
- CONFIG=config.Develop
- SECRET_KEY=abc123
ports:
- 5001:8080
volumes:
- ./test:/app
- ./tna_frontend_jinja:/home/app/tna_frontend_jinja
17 changes: 1 addition & 16 deletions package-lock.json

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

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"scripts": {
"prettier": "prettier --write '*.{mjs,json}'",
"run": "flask --app test run --debug --port 5000",
"prettier": "npx --yes prettier --write '*.{mjs,json}'",
"test": "node test-fixtures.mjs"
},
"dependencies": {
"@nationalarchives/frontend": "^0.2.8",
"diff": "^5.1.0",
"glob": "^10.2.7",
"js-beautify": "^1.14.8",
"node-fetch": "^3.3.1",
"prettier": "^3.2.5"
"node-fetch": "^3.3.1"
}
}
21 changes: 11 additions & 10 deletions test-fixtures.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const fail = (message) => {
};

console.log("Running tests...");
const testEndpoint = "http://127.0.0.1:5000/";
const testEndpoint = "http://127.0.0.1:5001/";
const standardiseHtml = (html) =>
html_beautify(
html
Expand Down Expand Up @@ -93,7 +93,8 @@ for (let i = 0; i < components.length; i++) {
const diff = diffChars(bodyPretty, fixturePretty)
.map(
(part) =>
`${part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
`${
part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
}${part.value === " " ? "█" : part.value}`,
)
.join("");
Expand Down Expand Up @@ -171,7 +172,8 @@ for (let i = 0; i < utilities.length; i++) {
const diff = diffChars(bodyPretty, fixturePretty)
.map(
(part) =>
`${part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
`${
part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
}${part.value === " " ? "█" : part.value}`,
)
.join("");
Expand All @@ -189,12 +191,10 @@ for (let i = 0; i < utilities.length; i++) {

const templatesDirectory = `${tnaFrontendDirectory}/nationalarchives/templates/`;
const { fixtures } = JSON.parse(
fs.readFileSync(`${templatesDirectory}fixtures.json`,
"utf8",
),
fs.readFileSync(`${templatesDirectory}fixtures.json`, "utf8"),
);
const genericFixture = fixtures.find(fixture => fixture.name === "generic")
const testUrl = `${testEndpoint}templates/base`
const genericFixture = fixtures.find((fixture) => fixture.name === "generic");
const testUrl = `${testEndpoint}templates/base`;
console.log("\nTemplates");
const response = await fetch(testUrl)
.then((response) => {
Expand All @@ -218,7 +218,8 @@ if (mismatch) {
const diff = diffChars(bodyPretty, fixturePretty)
.map(
(part) =>
`${part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
`${
part.added ? "\x1b[32m" : part.removed ? "\x1b[31m" : "\x1b[0m"
}${part.value === " " ? "█" : part.value}`,
)
.join("");
Expand All @@ -230,4 +231,4 @@ if (mismatch) {
throw new Error("Fixtures tests failed");
} else {
pass(genericFixture.name);
}
}
8 changes: 8 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM ghcr.io/nationalarchives/tna-python:latest

COPY --chown=app test .
COPY --chown=app ../tna_frontend_jinja /home/app/tna_frontend_jinja

RUN tna-build

CMD ["tna-run", "app:app"]
3 changes: 0 additions & 3 deletions test/__init__.py

This file was deleted.

19 changes: 10 additions & 9 deletions test/app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from flask import Flask
from .components import bp as components_bp
from .utilities import bp as utilities_bp
from .templates import bp as templates_bp

app = Flask(__name__, template_folder="/home/app/tna_frontend_jinja/templates")

def create_app():
app = Flask(__name__, template_folder="../../tna_frontend_jinja/templates")

from .components import bp as components_bp
from .utilities import bp as utilities_bp
from .templates import bp as templates_bp
@app.route("/ready/")
def ready():
return "ok"

app.register_blueprint(components_bp, url_prefix="/components")
app.register_blueprint(utilities_bp, url_prefix="/utilities")
app.register_blueprint(templates_bp, url_prefix="/templates")

return app
app.register_blueprint(components_bp, url_prefix="/components")
app.register_blueprint(utilities_bp, url_prefix="/utilities")
app.register_blueprint(templates_bp, url_prefix="/templates")
2 changes: 1 addition & 1 deletion test/app/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

bp = Blueprint("components", __name__, template_folder="test-templates")

from test.app.components import routes # noqa: E402,F401
from app.components import routes # noqa: E402,F401
2 changes: 1 addition & 1 deletion test/app/components/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from test.app.components import bp
from app.components import bp

from flask import render_template, request

Expand Down
2 changes: 1 addition & 1 deletion test/app/templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

bp = Blueprint("templates", __name__, template_folder="../templates")

from test.app.templates import routes # noqa: E402,F401
from app.templates import routes # noqa: E402,F401
2 changes: 1 addition & 1 deletion test/app/templates/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from test.app.templates import bp
from app.templates import bp

from flask import render_template, request

Expand Down
2 changes: 1 addition & 1 deletion test/app/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

bp = Blueprint("utilities", __name__, template_folder="test-templates")

from test.app.utilities import routes # noqa: E402,F401
from app.utilities import routes # noqa: E402,F401
2 changes: 1 addition & 1 deletion test/app/utilities/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from test.app.utilities import bp
from app.utilities import bp

from flask import render_template, request

Expand Down
Loading

0 comments on commit 7bb72e6

Please sign in to comment.