-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check that all pages render (#572)
Closes #511. Example: ``` $ npm run check-pages-render Checked 10 / 71 pages Checked 20 / 71 pages Checked 30 / 71 pages Checked 40 / 71 pages Checked 50 / 71 pages Checked 60 / 71 pages Checked 70 / 71 pages ✅ All pages render without crashing ``` ## Only checks non-API docs by default This script is quite slow, at least on my M1 because the Docker images is built with x86. So, to avoid CI slowing down too much, we only check non-API pages in PR builds. Our nightly cron job checks everything else. ## Does not auto-start Docker We no longer have the file `webServer.ts` thanks to #578, which was a great improvement. Rather than adding back somewhat complex code for us to auto-start the server—and then to periodically ping if it's ready or time out—we expect the user to start up the server. That's acceptable since usually people will rely on CI to run this check. It's too slow for people to be frequently running locally. --------- Co-authored-by: Frank Harkins <frankharkins@hotmail.co.uk>
- Loading branch information
1 parent
704fe05
commit 8ed37f9
Showing
6 changed files
with
190 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// This code is a Qiskit project. | ||
// | ||
// (C) Copyright IBM 2024. | ||
// | ||
// This code is licensed under the Apache License, Version 2.0. You may | ||
// obtain a copy of this license in the LICENSE file in the root directory | ||
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// Any modifications or derivative works of this code must retain this | ||
// copyright notice, and modified files need to carry a notice indicating | ||
// that they have been altered from the originals. | ||
|
||
import { globby } from "globby"; | ||
import yargs from "yargs/yargs"; | ||
import { hideBin } from "yargs/helpers"; | ||
|
||
import { zxMain } from "../lib/zx"; | ||
|
||
const PORT = 3000; | ||
|
||
interface Arguments { | ||
[x: string]: unknown; | ||
currentApis: boolean; | ||
historicalApis: boolean; | ||
qiskitReleaseNotes: boolean; | ||
translations: boolean; | ||
} | ||
|
||
const readArgs = (): Arguments => { | ||
return yargs(hideBin(process.argv)) | ||
.version(false) | ||
.option("current-apis", { | ||
type: "boolean", | ||
default: false, | ||
description: "Check the pages in the current API docs.", | ||
}) | ||
.option("historical-apis", { | ||
type: "boolean", | ||
default: false, | ||
description: | ||
"Check the pages in the historical API docs, e.g. `api/qiskit/0.44`. " + | ||
"Warning: this is slow.", | ||
}) | ||
.option("qiskit-release-notes", { | ||
type: "boolean", | ||
default: false, | ||
description: "Check the pages in the `api/qiskit/release-notes` folder.", | ||
}) | ||
.option("translations", { | ||
type: "boolean", | ||
default: false, | ||
description: "Check the pages in the `translations/` subfolders.", | ||
}) | ||
.parseSync(); | ||
}; | ||
|
||
zxMain(async () => { | ||
const args = readArgs(); | ||
await validateDockerRunning(); | ||
const files = await determineFilePaths(args); | ||
|
||
let allGood = true; | ||
let numFilesChecked = 1; | ||
for (const fp of files) { | ||
const rendered = await canRender(fp); | ||
if (!rendered) { | ||
console.error(`❌ Failed to render: ${fp}`); | ||
allGood = false; | ||
} | ||
|
||
// This script can be slow, so log progress every 10 files. | ||
if (numFilesChecked % 10 == 0) { | ||
console.log(`Checked ${numFilesChecked} / ${files.length} pages`); | ||
} | ||
numFilesChecked++; | ||
} | ||
|
||
if (allGood) { | ||
console.info("✅ All pages render without crashing"); | ||
} else { | ||
console.error( | ||
"💔 Some pages crash when rendering. This is usually due to invalid syntax, such as forgetting " + | ||
"the closing component tag, like `</Admonition>`. You can sometimes get a helpful error message " + | ||
"by previewing the docs locally or in CI. See the README for instructions.", | ||
); | ||
process.exit(1); | ||
} | ||
}); | ||
|
||
async function canRender(fp: string): Promise<boolean> { | ||
const url = pathToUrl(fp); | ||
try { | ||
const response = await fetch(url); | ||
if (response.status >= 300) { | ||
return false; | ||
} | ||
} catch (error) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function pathToUrl(path: string): string { | ||
const strippedPath = path | ||
.replace("docs/", "") | ||
.replace("translations/", "") | ||
.replace(/\.(?:md|mdx|ipynb)$/g, ""); | ||
return `http://localhost:${PORT}/${strippedPath}`; | ||
} | ||
|
||
async function validateDockerRunning(): Promise<void> { | ||
try { | ||
const response = await fetch(`http://localhost:${PORT}`); | ||
if (response.status !== 404) { | ||
console.error( | ||
"Failed to access http://localhost:3000. Have you started the Docker server with `./start`? " + | ||
"Refer to the README for instructions.", | ||
); | ||
process.exit(1); | ||
} | ||
} catch (error) { | ||
console.error( | ||
"Error when accessing http://localhost:3000. Make sure that you've started the Docker server " + | ||
"with `./start`. Refer to the README for instructions.\n\n" + | ||
`${error}`, | ||
); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
async function determineFilePaths(args: Arguments): Promise<string[]> { | ||
const globs = ["docs/**/*.{ipynb,md,mdx}"]; | ||
if (!args.currentApis) { | ||
globs.push("!docs/api/{qiskit,qiskit-ibm-provider,qiskit-ibm-runtime}/*"); | ||
} | ||
if (!args.historicalApis) { | ||
globs.push( | ||
"!docs/api/{qiskit,qiskit-ibm-provider,qiskit-ibm-runtime}/[0-9]*/*", | ||
); | ||
} | ||
if (!args.qiskitReleaseNotes) { | ||
globs.push("!docs/api/qiskit/release-notes/*"); | ||
} | ||
if (args.translations) { | ||
globs.push("translations/**/*.{ipynb,md,mdx}"); | ||
} | ||
return globby(globs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters