|
| 1 | +/** |
| 2 | + * Shared test helpers for plugin-metadata tests. |
| 3 | + */ |
| 4 | +import fs from "fs-extra"; |
| 5 | +import path from "path"; |
| 6 | +import os from "os"; |
| 7 | +import yaml from "js-yaml"; |
| 8 | + |
| 9 | +/** Saves and restores process.env around each test. */ |
| 10 | +export function withCleanEnv() { |
| 11 | + let savedEnv: Record<string, string | undefined>; |
| 12 | + return { |
| 13 | + save() { |
| 14 | + savedEnv = { ...process.env }; |
| 15 | + }, |
| 16 | + restore() { |
| 17 | + for (const key of Object.keys(process.env)) { |
| 18 | + if (!(key in savedEnv)) delete process.env[key]; |
| 19 | + } |
| 20 | + Object.assign(process.env, savedEnv); |
| 21 | + }, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +/** Creates a temporary metadata directory with Package CRD YAML files. */ |
| 26 | +export async function createMetadataFixture( |
| 27 | + plugins: Array<{ |
| 28 | + name: string; |
| 29 | + packageName: string; |
| 30 | + dynamicArtifact: string; |
| 31 | + appConfigExamples?: Record<string, unknown>; |
| 32 | + }>, |
| 33 | +): Promise<string> { |
| 34 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "metadata-test-")); |
| 35 | + for (const plugin of plugins) { |
| 36 | + const content: Record<string, unknown> = { |
| 37 | + apiVersion: "extensions.backstage.io/v1alpha1", |
| 38 | + kind: "Package", |
| 39 | + metadata: { name: plugin.name }, |
| 40 | + spec: { |
| 41 | + packageName: plugin.packageName, |
| 42 | + dynamicArtifact: plugin.dynamicArtifact, |
| 43 | + ...(plugin.appConfigExamples |
| 44 | + ? { |
| 45 | + appConfigExamples: [ |
| 46 | + { title: "Default", content: plugin.appConfigExamples }, |
| 47 | + ], |
| 48 | + } |
| 49 | + : {}), |
| 50 | + }, |
| 51 | + }; |
| 52 | + await fs.writeFile( |
| 53 | + path.join(tmpDir, `${plugin.name}.yaml`), |
| 54 | + yaml.dump(content), |
| 55 | + ); |
| 56 | + } |
| 57 | + return tmpDir; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Creates a workspace-like directory structure with metadata, source.json, |
| 62 | + * and plugins-list.yaml. Used for tests that trigger PR OCI URL fetching. |
| 63 | + */ |
| 64 | +export async function createWorkspaceFixture( |
| 65 | + plugins: Array<{ |
| 66 | + name: string; |
| 67 | + packageName: string; |
| 68 | + dynamicArtifact: string; |
| 69 | + appConfigExamples?: Record<string, unknown>; |
| 70 | + }>, |
| 71 | +): Promise<{ wsDir: string; metadataDir: string }> { |
| 72 | + const wsDir = await fs.mkdtemp(path.join(os.tmpdir(), "workspace-test-")); |
| 73 | + const metadataDir = path.join(wsDir, "metadata"); |
| 74 | + await fs.mkdir(metadataDir); |
| 75 | + |
| 76 | + /* eslint-disable @typescript-eslint/naming-convention */ |
| 77 | + await fs.writeFile( |
| 78 | + path.join(wsDir, "source.json"), |
| 79 | + JSON.stringify({ |
| 80 | + repo: "https://github.com/test/repo", |
| 81 | + "repo-ref": "main", |
| 82 | + "repo-flat": false, |
| 83 | + }), |
| 84 | + ); |
| 85 | + /* eslint-enable @typescript-eslint/naming-convention */ |
| 86 | + await fs.writeFile(path.join(wsDir, "plugins-list.yaml"), "{}"); |
| 87 | + |
| 88 | + for (const plugin of plugins) { |
| 89 | + const content: Record<string, unknown> = { |
| 90 | + apiVersion: "extensions.backstage.io/v1alpha1", |
| 91 | + kind: "Package", |
| 92 | + metadata: { name: plugin.name }, |
| 93 | + spec: { |
| 94 | + packageName: plugin.packageName, |
| 95 | + dynamicArtifact: plugin.dynamicArtifact, |
| 96 | + ...(plugin.appConfigExamples |
| 97 | + ? { |
| 98 | + appConfigExamples: [ |
| 99 | + { title: "Default", content: plugin.appConfigExamples }, |
| 100 | + ], |
| 101 | + } |
| 102 | + : {}), |
| 103 | + }, |
| 104 | + }; |
| 105 | + await fs.writeFile( |
| 106 | + path.join(metadataDir, `${plugin.name}.yaml`), |
| 107 | + yaml.dump(content), |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + return { wsDir, metadataDir }; |
| 112 | +} |
0 commit comments