|
| 1 | +import { ok, strictEqual } from "assert"; |
| 2 | +import { execFileSync } from "child_process"; |
| 3 | +import fs from "fs"; |
| 4 | +import os from "os"; |
| 5 | +import path from "path"; |
| 6 | +import { afterEach, beforeEach, describe, it } from "vitest"; |
| 7 | + |
| 8 | +describe("export_apiview_markdown.py", () => { |
| 9 | + const root = path.resolve(import.meta.dirname, "../.."); |
| 10 | + const scriptPath = path.join(root, "eng/scripts/setup/export_apiview_markdown.py"); |
| 11 | + let tmpDir: string; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "apimd-test-")); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 19 | + }); |
| 20 | + |
| 21 | + function writeTokenJson(data: object): string { |
| 22 | + const tokenPath = path.join(tmpDir, "token.json"); |
| 23 | + fs.writeFileSync(tokenPath, JSON.stringify(data)); |
| 24 | + return tokenPath; |
| 25 | + } |
| 26 | + |
| 27 | + it("generates api.md from a simple token file", () => { |
| 28 | + const tokenPath = writeTokenJson({ |
| 29 | + Language: "Python", |
| 30 | + ReviewLines: [ |
| 31 | + { |
| 32 | + Tokens: [ |
| 33 | + { Value: "class", HasSuffixSpace: true }, |
| 34 | + { Value: "MyClient", HasPrefixSpace: false }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + { |
| 38 | + Tokens: [ |
| 39 | + { Value: "def", HasSuffixSpace: true }, |
| 40 | + { Value: "send(self)", HasPrefixSpace: false }, |
| 41 | + ], |
| 42 | + Children: [ |
| 43 | + { |
| 44 | + Tokens: [{ Value: "..." }], |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + ], |
| 49 | + }); |
| 50 | + |
| 51 | + const outDir = path.join(tmpDir, "output"); |
| 52 | + fs.mkdirSync(outDir); |
| 53 | + execFileSync("python3", [scriptPath, tokenPath, outDir]); |
| 54 | + |
| 55 | + const apiMd = fs.readFileSync(path.join(outDir, "api.md"), "utf-8"); |
| 56 | + ok(apiMd.startsWith("```py"), "Should start with python code fence"); |
| 57 | + ok(apiMd.includes("class"), "Should contain class token"); |
| 58 | + ok(apiMd.includes("MyClient"), "Should contain MyClient token"); |
| 59 | + ok(apiMd.endsWith("```"), "Should end with code fence"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("writes api.md directly when output path is a .md file", () => { |
| 63 | + const tokenPath = writeTokenJson({ |
| 64 | + Language: "Python", |
| 65 | + ReviewLines: [{ Tokens: [{ Value: "class Foo" }] }], |
| 66 | + }); |
| 67 | + |
| 68 | + const outFile = path.join(tmpDir, "custom.md"); |
| 69 | + execFileSync("python3", [scriptPath, tokenPath, outFile]); |
| 70 | + ok(fs.existsSync(outFile), "Should write to the specified .md file"); |
| 71 | + const content = fs.readFileSync(outFile, "utf-8"); |
| 72 | + ok(content.includes("class Foo")); |
| 73 | + }); |
| 74 | + |
| 75 | + it("exits with error for empty ReviewLines", () => { |
| 76 | + const tokenPath = writeTokenJson({ |
| 77 | + Language: "Python", |
| 78 | + ReviewLines: [], |
| 79 | + }); |
| 80 | + |
| 81 | + const outDir = path.join(tmpDir, "output"); |
| 82 | + fs.mkdirSync(outDir); |
| 83 | + // Empty ReviewLines is treated as missing by the script |
| 84 | + let threw = false; |
| 85 | + try { |
| 86 | + execFileSync("python3", [scriptPath, tokenPath, outDir], { stdio: "pipe" }); |
| 87 | + } catch { |
| 88 | + threw = true; |
| 89 | + } |
| 90 | + ok(threw, "Should exit with error for empty ReviewLines"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("resolves language aliases correctly", () => { |
| 94 | + const tokenPath = writeTokenJson({ |
| 95 | + Language: "JavaScript", |
| 96 | + ReviewLines: [{ Tokens: [{ Value: "function foo() {}" }] }], |
| 97 | + }); |
| 98 | + |
| 99 | + const outDir = path.join(tmpDir, "output"); |
| 100 | + fs.mkdirSync(outDir); |
| 101 | + execFileSync("python3", [scriptPath, tokenPath, outDir]); |
| 102 | + |
| 103 | + const apiMd = fs.readFileSync(path.join(outDir, "api.md"), "utf-8"); |
| 104 | + ok(apiMd.startsWith("```js"), "Should use 'js' alias for JavaScript"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("renders nested children with indentation", () => { |
| 108 | + const tokenPath = writeTokenJson({ |
| 109 | + Language: "Python", |
| 110 | + ReviewLines: [ |
| 111 | + { |
| 112 | + Tokens: [{ Value: "class Foo:" }], |
| 113 | + Children: [ |
| 114 | + { |
| 115 | + Tokens: [{ Value: "def bar(self):" }], |
| 116 | + Children: [{ Tokens: [{ Value: "pass" }] }], |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | + ], |
| 121 | + }); |
| 122 | + |
| 123 | + const outDir = path.join(tmpDir, "output"); |
| 124 | + fs.mkdirSync(outDir); |
| 125 | + execFileSync("python3", [scriptPath, tokenPath, outDir]); |
| 126 | + |
| 127 | + const apiMd = fs.readFileSync(path.join(outDir, "api.md"), "utf-8"); |
| 128 | + const lines = apiMd.split("\n"); |
| 129 | + // Children should be indented |
| 130 | + ok( |
| 131 | + lines.some((l: string) => l.startsWith(" ") && l.includes("def bar")), |
| 132 | + "First-level children should have 4-space indent", |
| 133 | + ); |
| 134 | + ok( |
| 135 | + lines.some((l: string) => l.startsWith(" ") && l.includes("pass")), |
| 136 | + "Second-level children should have 8-space indent", |
| 137 | + ); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +describe("generateApiMd token file lookup", () => { |
| 142 | + it("expected token filename follows {package_name}_python.json pattern", () => { |
| 143 | + // Verify the naming convention used by apistubgen |
| 144 | + const packageName = "azure-ai-inference"; |
| 145 | + const expectedFilename = `${packageName}_python.json`; |
| 146 | + strictEqual(expectedFilename, "azure-ai-inference_python.json"); |
| 147 | + }); |
| 148 | +}); |
0 commit comments