Upload folder using huggingface_hub
Browse files- 6.4.0/tootils/package.json +3 -2
- 6.4.0/tootils/src/app-launcher.ts +174 -0
- 6.4.0/tootils/src/index.ts +109 -13
6.4.0/tootils/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"name": "@self/tootils",
|
| 3 |
-
"version": "0.
|
| 4 |
"description": "Internal test utilities",
|
| 5 |
"type": "module",
|
| 6 |
"main": "src/index.ts",
|
|
@@ -16,7 +16,8 @@
|
|
| 16 |
},
|
| 17 |
"exports": {
|
| 18 |
".": "./src/index.ts",
|
| 19 |
-
"./render": "./src/render.ts"
|
|
|
|
| 20 |
},
|
| 21 |
"scripts": {
|
| 22 |
"package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
|
|
|
|
| 1 |
{
|
| 2 |
"name": "@self/tootils",
|
| 3 |
+
"version": "0.9.0",
|
| 4 |
"description": "Internal test utilities",
|
| 5 |
"type": "module",
|
| 6 |
"main": "src/index.ts",
|
|
|
|
| 16 |
},
|
| 17 |
"exports": {
|
| 18 |
".": "./src/index.ts",
|
| 19 |
+
"./render": "./src/render.ts",
|
| 20 |
+
"./app-launcher": "./src/app-launcher.ts"
|
| 21 |
},
|
| 22 |
"scripts": {
|
| 23 |
"package": "svelte-package --input=. --tsconfig=../../tsconfig.json"
|
6.4.0/tootils/src/app-launcher.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { spawn, type ChildProcess } from "node:child_process";
|
| 2 |
+
import net from "net";
|
| 3 |
+
import path from "path";
|
| 4 |
+
import fs from "fs";
|
| 5 |
+
import os from "os";
|
| 6 |
+
import url from "url";
|
| 7 |
+
|
| 8 |
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
| 9 |
+
const ROOT_DIR = path.resolve(__dirname, "../../..");
|
| 10 |
+
|
| 11 |
+
export interface GradioApp {
|
| 12 |
+
port: number;
|
| 13 |
+
process: ChildProcess;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
export function killGradioApp(process: ChildProcess): void {
|
| 17 |
+
try {
|
| 18 |
+
process.kill("SIGTERM");
|
| 19 |
+
} catch {
|
| 20 |
+
// Process may already be dead
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
export async function findFreePort(
|
| 25 |
+
startPort: number,
|
| 26 |
+
endPort: number
|
| 27 |
+
): Promise<number> {
|
| 28 |
+
for (let port = startPort; port < endPort; port++) {
|
| 29 |
+
if (await isPortFree(port)) {
|
| 30 |
+
return port;
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
throw new Error(`Could not find free port in range ${startPort}-${endPort}`);
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
function isPortFree(port: number): Promise<boolean> {
|
| 37 |
+
return new Promise((resolve, reject) => {
|
| 38 |
+
const sock = net.createConnection(port, "127.0.0.1");
|
| 39 |
+
sock.once("connect", () => {
|
| 40 |
+
sock.end();
|
| 41 |
+
resolve(false);
|
| 42 |
+
});
|
| 43 |
+
sock.once("error", (e: NodeJS.ErrnoException) => {
|
| 44 |
+
sock.destroy();
|
| 45 |
+
if (e.code === "ECONNREFUSED") {
|
| 46 |
+
resolve(true);
|
| 47 |
+
} else {
|
| 48 |
+
reject(e);
|
| 49 |
+
}
|
| 50 |
+
});
|
| 51 |
+
});
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
export function getTestcases(demoName: string): string[] {
|
| 55 |
+
const demoDir = path.join(ROOT_DIR, "demo", demoName);
|
| 56 |
+
if (!fs.existsSync(demoDir)) {
|
| 57 |
+
return [];
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
return fs
|
| 61 |
+
.readdirSync(demoDir)
|
| 62 |
+
.filter((f) => f.endsWith("_testcase.py"))
|
| 63 |
+
.map((f) => path.basename(f, ".py"));
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// Check if a testcase file exists for this demo
|
| 67 |
+
export function hasTestcase(demoName: string, testcaseName: string): boolean {
|
| 68 |
+
const testcaseFile = path.join(
|
| 69 |
+
ROOT_DIR,
|
| 70 |
+
"demo",
|
| 71 |
+
demoName,
|
| 72 |
+
`${testcaseName}_testcase.py`
|
| 73 |
+
);
|
| 74 |
+
return fs.existsSync(testcaseFile);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Get the path to a demo's Python file
|
| 78 |
+
function getDemoFilePath(demoName: string, testcaseName?: string): string {
|
| 79 |
+
if (testcaseName) {
|
| 80 |
+
return path.join(ROOT_DIR, "demo", demoName, `${testcaseName}_testcase.py`);
|
| 81 |
+
}
|
| 82 |
+
return path.join(ROOT_DIR, "demo", demoName, "run.py");
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
export async function launchGradioApp(
|
| 86 |
+
demoName: string,
|
| 87 |
+
workerIndex: number = 0,
|
| 88 |
+
timeout: number = 60000,
|
| 89 |
+
testcaseName?: string
|
| 90 |
+
): Promise<GradioApp> {
|
| 91 |
+
// Partition ports by worker index to avoid collisions
|
| 92 |
+
const basePort = 7860 + workerIndex * 100;
|
| 93 |
+
const port = await findFreePort(basePort, basePort + 99);
|
| 94 |
+
|
| 95 |
+
// Get the path to the demo file
|
| 96 |
+
const demoFilePath = getDemoFilePath(demoName, testcaseName);
|
| 97 |
+
|
| 98 |
+
// Create unique directories for this instance to avoid cache conflicts
|
| 99 |
+
const instanceId = testcaseName
|
| 100 |
+
? `${demoName}_${testcaseName}_${port}`
|
| 101 |
+
: `${demoName}_${port}`;
|
| 102 |
+
const instanceDir = path.join(os.tmpdir(), `gradio_test_${instanceId}`);
|
| 103 |
+
const cacheDir = path.join(instanceDir, "cached_examples");
|
| 104 |
+
const tempDir = path.join(instanceDir, "temp");
|
| 105 |
+
|
| 106 |
+
if (!fs.existsSync(instanceDir)) {
|
| 107 |
+
fs.mkdirSync(instanceDir, { recursive: true });
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Run the demo file directly - the demo's own if __name__ == "__main__" block
|
| 111 |
+
// has the correct launch parameters (show_error, etc.)
|
| 112 |
+
const childProcess = spawn("python", [demoFilePath], {
|
| 113 |
+
stdio: "pipe",
|
| 114 |
+
cwd: ROOT_DIR,
|
| 115 |
+
env: {
|
| 116 |
+
...process.env,
|
| 117 |
+
PYTHONUNBUFFERED: "true",
|
| 118 |
+
GRADIO_ANALYTICS_ENABLED: "False",
|
| 119 |
+
GRADIO_IS_E2E_TEST: "1",
|
| 120 |
+
GRADIO_RESET_EXAMPLES_CACHE: "True",
|
| 121 |
+
// Control the port via environment variable
|
| 122 |
+
GRADIO_SERVER_PORT: port.toString(),
|
| 123 |
+
// Use unique directories per instance to avoid conflicts
|
| 124 |
+
GRADIO_EXAMPLES_CACHE: cacheDir,
|
| 125 |
+
GRADIO_TEMP_DIR: tempDir
|
| 126 |
+
}
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
childProcess.stdout?.setEncoding("utf8");
|
| 130 |
+
childProcess.stderr?.setEncoding("utf8");
|
| 131 |
+
|
| 132 |
+
// Wait for app to be ready
|
| 133 |
+
return new Promise<GradioApp>((resolve, reject) => {
|
| 134 |
+
const timeoutId = setTimeout(() => {
|
| 135 |
+
killGradioApp(childProcess);
|
| 136 |
+
reject(
|
| 137 |
+
new Error(`Gradio app ${demoName} failed to start within ${timeout}ms`)
|
| 138 |
+
);
|
| 139 |
+
}, timeout);
|
| 140 |
+
|
| 141 |
+
let output = "";
|
| 142 |
+
|
| 143 |
+
function handleOutput(data: string): void {
|
| 144 |
+
output += data;
|
| 145 |
+
// Check for Gradio's startup message
|
| 146 |
+
if (
|
| 147 |
+
data.includes("Running on local URL:") ||
|
| 148 |
+
data.includes(`Uvicorn running on`)
|
| 149 |
+
) {
|
| 150 |
+
clearTimeout(timeoutId);
|
| 151 |
+
resolve({ port, process: childProcess });
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
childProcess.stdout?.on("data", handleOutput);
|
| 156 |
+
childProcess.stderr?.on("data", handleOutput);
|
| 157 |
+
|
| 158 |
+
childProcess.on("error", (err) => {
|
| 159 |
+
clearTimeout(timeoutId);
|
| 160 |
+
reject(err);
|
| 161 |
+
});
|
| 162 |
+
|
| 163 |
+
childProcess.on("exit", (code) => {
|
| 164 |
+
if (code !== 0 && code !== null) {
|
| 165 |
+
clearTimeout(timeoutId);
|
| 166 |
+
reject(
|
| 167 |
+
new Error(
|
| 168 |
+
`Gradio app ${demoName} exited with code ${code}.\nOutput: ${output}`
|
| 169 |
+
)
|
| 170 |
+
);
|
| 171 |
+
}
|
| 172 |
+
});
|
| 173 |
+
});
|
| 174 |
+
}
|
6.4.0/tootils/src/index.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
import { test as base, type Locator, type Page } from "@playwright/test";
|
| 2 |
import { spy } from "tinyspy";
|
| 3 |
-
import { performance } from "node:perf_hooks";
|
| 4 |
import url from "url";
|
| 5 |
import path from "path";
|
| 6 |
import fsPromises from "fs/promises";
|
|
|
|
| 7 |
|
| 8 |
import type { SvelteComponent } from "svelte";
|
| 9 |
import type { SpyFn } from "tinyspy";
|
| 10 |
|
|
|
|
|
|
|
| 11 |
export function get_text<T extends HTMLElement>(el: T): string {
|
| 12 |
return el.innerText.trim();
|
| 13 |
}
|
|
@@ -21,30 +23,125 @@ const ROOT_DIR = path.resolve(
|
|
| 21 |
"../../../.."
|
| 22 |
);
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
const test_normal = base.extend<{ setup: void }>({
|
| 25 |
setup: [
|
| 26 |
async ({ page }, use, testInfo): Promise<void> => {
|
| 27 |
-
const
|
| 28 |
-
const
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
await page.goto(`localhost:${port}/${test_name}`);
|
| 32 |
if (
|
| 33 |
process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true" &&
|
| 34 |
!(
|
| 35 |
-
|
| 36 |
-
|
| 37 |
)
|
| 38 |
) {
|
| 39 |
await page.waitForSelector("#svelte-announcer");
|
| 40 |
}
|
| 41 |
await page.waitForLoadState("load");
|
|
|
|
| 42 |
await use();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
},
|
| 44 |
{ auto: true }
|
| 45 |
]
|
| 46 |
});
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
export const test = test_normal;
|
| 49 |
|
| 50 |
export async function wait_for_event(
|
|
@@ -119,11 +216,10 @@ export const drag_and_drop_file = async (
|
|
| 119 |
|
| 120 |
export async function go_to_testcase(
|
| 121 |
page: Page,
|
| 122 |
-
|
| 123 |
): Promise<void> {
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
}
|
| 129 |
}
|
|
|
|
| 1 |
import { test as base, type Locator, type Page } from "@playwright/test";
|
| 2 |
import { spy } from "tinyspy";
|
|
|
|
| 3 |
import url from "url";
|
| 4 |
import path from "path";
|
| 5 |
import fsPromises from "fs/promises";
|
| 6 |
+
import type { ChildProcess } from "node:child_process";
|
| 7 |
|
| 8 |
import type { SvelteComponent } from "svelte";
|
| 9 |
import type { SpyFn } from "tinyspy";
|
| 10 |
|
| 11 |
+
import { launchGradioApp, killGradioApp, hasTestcase } from "./app-launcher";
|
| 12 |
+
|
| 13 |
export function get_text<T extends HTMLElement>(el: T): string {
|
| 14 |
return el.innerText.trim();
|
| 15 |
}
|
|
|
|
| 23 |
"../../../.."
|
| 24 |
);
|
| 25 |
|
| 26 |
+
// Extract testcase name from test title if present
|
| 27 |
+
// Test titles can be:
|
| 28 |
+
// - "case eager_caching_examples: ..."
|
| 29 |
+
// - "test case multimodal_messages chatinterface works..."
|
| 30 |
+
function extractTestcaseFromTitle(
|
| 31 |
+
title: string,
|
| 32 |
+
demoName: string
|
| 33 |
+
): string | undefined {
|
| 34 |
+
// Try pattern: "case <name>:" or "test case <name> "
|
| 35 |
+
const patterns = [/^case\s+(\w+):/, /^test case\s+(\w+)\s/];
|
| 36 |
+
|
| 37 |
+
for (const pattern of patterns) {
|
| 38 |
+
const match = title.match(pattern);
|
| 39 |
+
if (match) {
|
| 40 |
+
const caseName = match[1];
|
| 41 |
+
// Check if this is a testcase (not the main demo)
|
| 42 |
+
if (hasTestcase(demoName, caseName)) {
|
| 43 |
+
return caseName;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
return undefined;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Cache for launched apps - key is "demoName" or "demoName_testcaseName"
|
| 51 |
+
const appCache = new Map<
|
| 52 |
+
string,
|
| 53 |
+
{ port: number; process: ChildProcess; refCount: number }
|
| 54 |
+
>();
|
| 55 |
+
|
| 56 |
+
// Test fixture that launches Gradio app per test
|
| 57 |
const test_normal = base.extend<{ setup: void }>({
|
| 58 |
setup: [
|
| 59 |
async ({ page }, use, testInfo): Promise<void> => {
|
| 60 |
+
const { file, title } = testInfo;
|
| 61 |
+
const demoName = path.basename(file, ".spec.ts");
|
| 62 |
+
|
| 63 |
+
// Check if this is a reload test (they manage their own apps)
|
| 64 |
+
if (demoName.endsWith(".reload")) {
|
| 65 |
+
// For reload tests, don't launch an app - they handle it themselves
|
| 66 |
+
await use();
|
| 67 |
+
return;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Check if this test is for a specific testcase
|
| 71 |
+
const testcaseName = extractTestcaseFromTitle(title, demoName);
|
| 72 |
+
|
| 73 |
+
// Cache key includes testcase if present
|
| 74 |
+
const cacheKey = testcaseName ? `${demoName}_${testcaseName}` : demoName;
|
| 75 |
+
|
| 76 |
+
let appInfo = appCache.get(cacheKey);
|
| 77 |
+
|
| 78 |
+
if (!appInfo) {
|
| 79 |
+
// Launch the app for this test
|
| 80 |
+
const workerIndex = testInfo.workerIndex;
|
| 81 |
+
try {
|
| 82 |
+
const { port, process } = await launchGradioApp(
|
| 83 |
+
demoName,
|
| 84 |
+
workerIndex,
|
| 85 |
+
60000,
|
| 86 |
+
testcaseName
|
| 87 |
+
);
|
| 88 |
+
appInfo = { port, process, refCount: 0 };
|
| 89 |
+
appCache.set(cacheKey, appInfo);
|
| 90 |
+
} catch (error) {
|
| 91 |
+
console.error(`Failed to launch app for ${cacheKey}:`, error);
|
| 92 |
+
throw error;
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
appInfo.refCount++;
|
| 97 |
+
|
| 98 |
+
// Navigate to the app
|
| 99 |
+
await page.goto(`http://localhost:${appInfo.port}`);
|
| 100 |
|
|
|
|
| 101 |
if (
|
| 102 |
process.env?.GRADIO_SSR_MODE?.toLowerCase() === "true" &&
|
| 103 |
!(
|
| 104 |
+
demoName.includes("multipage") ||
|
| 105 |
+
demoName.includes("chatinterface_deep_link")
|
| 106 |
)
|
| 107 |
) {
|
| 108 |
await page.waitForSelector("#svelte-announcer");
|
| 109 |
}
|
| 110 |
await page.waitForLoadState("load");
|
| 111 |
+
|
| 112 |
await use();
|
| 113 |
+
|
| 114 |
+
// Decrement ref count
|
| 115 |
+
appInfo.refCount--;
|
| 116 |
+
|
| 117 |
+
// Note: We don't kill the app here because other tests might
|
| 118 |
+
// still need it. The app will be killed when the process exits.
|
| 119 |
},
|
| 120 |
{ auto: true }
|
| 121 |
]
|
| 122 |
});
|
| 123 |
|
| 124 |
+
// Cleanup apps when the process exits
|
| 125 |
+
process.on("exit", () => {
|
| 126 |
+
for (const [, appInfo] of appCache) {
|
| 127 |
+
killGradioApp(appInfo.process);
|
| 128 |
+
}
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
process.on("SIGINT", () => {
|
| 132 |
+
for (const [, appInfo] of appCache) {
|
| 133 |
+
killGradioApp(appInfo.process);
|
| 134 |
+
}
|
| 135 |
+
process.exit(0);
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
process.on("SIGTERM", () => {
|
| 139 |
+
for (const [, appInfo] of appCache) {
|
| 140 |
+
killGradioApp(appInfo.process);
|
| 141 |
+
}
|
| 142 |
+
process.exit(0);
|
| 143 |
+
});
|
| 144 |
+
|
| 145 |
export const test = test_normal;
|
| 146 |
|
| 147 |
export async function wait_for_event(
|
|
|
|
| 216 |
|
| 217 |
export async function go_to_testcase(
|
| 218 |
page: Page,
|
| 219 |
+
_test_case: string
|
| 220 |
): Promise<void> {
|
| 221 |
+
// With the new setup, each testcase launches its own Gradio app.
|
| 222 |
+
// The fixture detects the testcase from the test title and launches
|
| 223 |
+
// the correct app, so this function is now a no-op.
|
| 224 |
+
// The page is already at the correct testcase app.
|
|
|
|
| 225 |
}
|