-
Notifications
You must be signed in to change notification settings - Fork 87
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
Add more tests for API generation script #581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 { expect, test } from "@jest/globals"; | ||
|
||
import { getRoot, pathExists } from "./fs"; | ||
|
||
test("pathExists() with getRoot()", async () => { | ||
const readme = await pathExists(`${getRoot()}/README.md`); | ||
expect(readme).toBe(true); | ||
|
||
const dir = await pathExists(`${getRoot()}/docs/`); | ||
expect(dir).toBe(true); | ||
|
||
const fake = await pathExists(`${getRoot()}/fake-file`); | ||
expect(fake).toBe(false); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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 { expect, test } from "@jest/globals"; | ||
|
||
import addFrontMatter from "./addFrontMatter"; | ||
import { Pkg } from "../sharedTypes"; | ||
import { SphinxToMdResult } from "./SphinxToMdResult"; | ||
|
||
test("addFrontMatter()", () => { | ||
const results: SphinxToMdResult[] = [ | ||
{ | ||
markdown: "# Hardcoded!", | ||
meta: { | ||
hardcoded_frontmatter: "title: hardcoded\ndescription: Hello world!", | ||
}, | ||
images: [], | ||
isReleaseNotes: false, | ||
}, | ||
{ | ||
markdown: "# Hardcoded with python_api_name!", | ||
meta: { | ||
hardcoded_frontmatter: | ||
"title: hardcoded with python_api_name\ndescription: Hello world!", | ||
python_api_name: "quantum_software.MyClass", | ||
python_api_type: "class", | ||
}, | ||
images: [], | ||
isReleaseNotes: true, | ||
}, | ||
{ | ||
markdown: "# Python API", | ||
meta: { | ||
python_api_name: "quantum_software.MyClass", | ||
python_api_type: "class", | ||
}, | ||
images: [], | ||
isReleaseNotes: false, | ||
}, | ||
{ | ||
markdown: "# Some release notes!", | ||
meta: {}, | ||
images: [], | ||
isReleaseNotes: true, | ||
}, | ||
]; | ||
const pkg = { | ||
versionWithoutPatch: "0.0", | ||
hasSeparateReleaseNotes: true, | ||
title: "My Quantum Software Project", | ||
} as Pkg; | ||
|
||
addFrontMatter(results, pkg); | ||
expect(results.map((result) => result.markdown)).toEqual([ | ||
`--- | ||
title: hardcoded | ||
description: Hello world! | ||
--- | ||
|
||
# Hardcoded! | ||
`, | ||
`--- | ||
title: hardcoded with python_api_name | ||
description: Hello world! | ||
--- | ||
|
||
# Hardcoded with python_api_name! | ||
`, | ||
`--- | ||
title: MyClass | ||
description: API reference for quantum_software.MyClass | ||
in_page_toc_min_heading_level: 1 | ||
python_api_type: class | ||
python_api_name: quantum_software.MyClass | ||
--- | ||
|
||
# Python API | ||
`, | ||
`--- | ||
title: My Quantum Software Project 0.0 release notes | ||
description: Changes made in My Quantum Software Project 0.0 | ||
in_page_toc_max_heading_level: 2 | ||
--- | ||
|
||
# Some release notes! | ||
`, | ||
]); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import { getLastPartFromFullIdentifier } from "../stringUtils"; | |
import { SphinxToMdResult } from "./SphinxToMdResult"; | ||
import { Pkg } from "../sharedTypes"; | ||
|
||
export function addFrontMatter<T extends SphinxToMdResult>( | ||
function addFrontMatter<T extends SphinxToMdResult>( | ||
results: T[], | ||
pkg: Pkg, | ||
): void { | ||
|
@@ -39,11 +39,15 @@ python_api_name: ${result.meta.python_api_name} | |
${markdown} | ||
`; | ||
} else if (result.isReleaseNotes) { | ||
const versionStr = pkg.hasSeparateReleaseNotes | ||
? ` ${pkg.versionWithoutPatch}` | ||
: ""; | ||
const descriptionSuffix = pkg.hasSeparateReleaseNotes | ||
? `in ${pkg.title}${versionStr}` | ||
: `to ${pkg.title}`; | ||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a behavior change for Qiskit, but same for Runtime and Provider. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The English reads poorly to say "Changes made to Qiskit 0.45" because it sounds like you're only changing Qiskit 0.45, when in reality you are changing Qiskit 0.45+. It's not wrong, but makes less sense. (Prepositions are the worst part of languages for me lol) |
||
result.markdown = `--- | ||
title: ${pkg.title}${ | ||
pkg.hasSeparateReleaseNotes ? " " + pkg.versionWithoutPatch : "" | ||
} release notes | ||
description: Changes made to ${pkg.title} | ||
title: ${pkg.title}${versionStr} release notes | ||
description: Changes made ${descriptionSuffix} | ||
in_page_toc_max_heading_level: 2 | ||
--- | ||
|
||
|
@@ -52,3 +56,5 @@ ${markdown} | |
} | ||
} | ||
} | ||
|
||
export default addFrontMatter; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,23 +10,22 @@ | |
// copyright notice, and modified files need to carry a notice indicating | ||
// that they have been altered from the originals. | ||
|
||
import { describe, expect, test } from "@jest/globals"; | ||
import { dedupeIds } from "./dedupeIds"; | ||
import { expect, test } from "@jest/globals"; | ||
|
||
describe("dedupeIds", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same test, I only removed the unnecessary |
||
test("dedupeIds", async () => { | ||
expect( | ||
await dedupeIds(` | ||
<span id="foo" /> | ||
<span id="bar" /> | ||
# foo | ||
<span id="foo" /> | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"<span id="bar" /> | ||
import { dedupeHtmlIds } from "./dedupeHtmlIds"; | ||
|
||
test("dedupeHtmlIds()", async () => { | ||
expect( | ||
await dedupeHtmlIds(` | ||
<span id="foo" /> | ||
<span id="bar" /> | ||
# foo | ||
<span id="foo" /> | ||
`), | ||
).toMatchInlineSnapshot(` | ||
"<span id="bar" /> | ||
|
||
# foo | ||
" | ||
`); | ||
}); | ||
# foo | ||
" | ||
`); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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 { expect, test } from "@jest/globals"; | ||
|
||
import flattenFolders from "./flattenFolders"; | ||
import { SphinxToMdResultWithUrl } from "./SphinxToMdResult"; | ||
|
||
test("flattenFolders()", () => { | ||
const results = [ | ||
{ url: "/api/my-pkg/apidoc/my_module" }, | ||
{ url: "/api/my-pkg/apidocs/my_module2" }, | ||
{ url: "/api/my-pkg/stubs/my_module.foo.Bar" }, | ||
{ url: "/api/my-pkg/release_notes" }, | ||
] as SphinxToMdResultWithUrl[]; | ||
|
||
flattenFolders(results); | ||
expect(results.map((result) => result.url)).toEqual([ | ||
"/api/my-pkg/my_module", | ||
"/api/my-pkg/my_module2", | ||
"/api/my-pkg/my_module.foo.Bar", | ||
"/api/my-pkg/release_notes", | ||
]); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an important trick to make tests more readable, rather than us having to give dummy values for attributes that aren't relevant. We should audit existing tests to use this when possible.