diff --git a/package.json b/package.json index 629a570ae..584f27af1 100644 --- a/package.json +++ b/package.json @@ -218,7 +218,7 @@ "pack:prod": "webpack --mode production", "pack:fast": "webpack --mode development --progress", "copyToConsumers": "node copyToConsumers", - "test": "rimraf coverage && jest && node --test utils/cleanupDBs.test.js", + "test": "rimraf coverage && jest && node --test utils/*.test.js", "test:debug": "jest --runInBand", "test:e2e": "jest -c ./jest.config.playwright.js --detectOpenHandles", "test:file": "jest --coverage=false", diff --git a/test/fx.ts b/test/fx.ts index 07decebce..6a824c4dc 100644 --- a/test/fx.ts +++ b/test/fx.ts @@ -1,6 +1,7 @@ import { DefaultAzureCredential } from "@azure/identity"; import { Frame, Locator, Page, expect } from "@playwright/test"; -import crypto, { webcrypto } from "crypto"; +import { webcrypto } from "crypto"; +import { generateUniqueName as generateUniqueResourceName } from "../utils/testResourceName"; import { TestContainerContext } from "./testData"; // The @azure/cosmos client signs requests with globalThis.crypto (Web Crypto API). @@ -19,16 +20,7 @@ export interface TestNameOptions { } export function generateUniqueName(baseName: string, options?: TestNameOptions): string { - const length = options?.length ?? 1; - const timestamp = options?.timestampped === undefined ? true : options.timestampped; - const prefixed = options?.prefixed === undefined ? true : options.prefixed; - - const runId = process.env.GITHUB_RUN_ID; - const runAttempt = process.env.GITHUB_RUN_ATTEMPT ?? "1"; - const runPrefix = runId ? `${runId}_${runAttempt}_` : ""; - const prefix = prefixed ? `t_${runPrefix}` : ""; - const suffix = timestamp ? `_${Date.now()}` : ""; - return `${prefix}${baseName}${crypto.randomBytes(length).toString("hex")}${suffix}`; + return generateUniqueResourceName(baseName, options, process.env); } export function getAzureCLICredentials(): DefaultAzureCredential { diff --git a/utils/testResourceName.js b/utils/testResourceName.js new file mode 100644 index 000000000..ccfa03a84 --- /dev/null +++ b/utils/testResourceName.js @@ -0,0 +1,16 @@ +const crypto = require("crypto"); + +function generateUniqueName(baseName, options, environment = process.env) { + const length = options?.length ?? 1; + const timestamp = options?.timestampped === undefined ? true : options.timestampped; + const prefixed = options?.prefixed === undefined ? true : options.prefixed; + + const runId = environment.GITHUB_RUN_ID; + const runAttempt = environment.GITHUB_RUN_ATTEMPT ?? "1"; + const runPrefix = runId ? `${runId}_${runAttempt}_` : ""; + const prefix = prefixed ? `t_${runPrefix}` : ""; + const suffix = timestamp ? `_${Date.now()}` : ""; + return `${prefix}${baseName}${crypto.randomBytes(length).toString("hex")}${suffix}`; +} + +module.exports = { generateUniqueName }; diff --git a/utils/testResourceName.test.js b/utils/testResourceName.test.js new file mode 100644 index 000000000..f72871617 --- /dev/null +++ b/utils/testResourceName.test.js @@ -0,0 +1,52 @@ +const assert = require("node:assert/strict"); +const test = require("node:test"); +const { generateUniqueName } = require("./testResourceName"); + +const noRandomSuffix = { length: 0 }; + +test("includes the workflow run and attempt in CI resource names", () => { + const name = generateUniqueName("db", noRandomSuffix, { + GITHUB_RUN_ID: "12345", + GITHUB_RUN_ATTEMPT: "2", + }); + + assert.match(name, /^t_12345_2_db_\d+$/); +}); + +test("defaults a missing workflow attempt to one", () => { + const name = generateUniqueName("db", noRandomSuffix, { GITHUB_RUN_ID: "12345" }); + + assert.match(name, /^t_12345_1_db_\d+$/); +}); + +test("preserves the local resource name format outside GitHub Actions", () => { + const name = generateUniqueName("db", noRandomSuffix, {}); + + assert.match(name, /^t_db_\d+$/); +}); + +test("omits the test and workflow prefixes when requested", () => { + const name = generateUniqueName( + "db", + { ...noRandomSuffix, prefixed: false }, + { + GITHUB_RUN_ID: "12345", + GITHUB_RUN_ATTEMPT: "2", + }, + ); + + assert.match(name, /^db_\d+$/); +}); + +test("omits the timestamp when requested", () => { + const name = generateUniqueName( + "db", + { ...noRandomSuffix, timestampped: false }, + { + GITHUB_RUN_ID: "12345", + GITHUB_RUN_ATTEMPT: "2", + }, + ); + + assert.equal(name, "t_12345_2_db"); +});