Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(test-utils): decode this CLI's folder from its file URL, not its URI path (#7805)

Aashish Mahatob3c7554ec8fd
diff --git a/packages/cli/test-utils/release-fixtures.mjs b/packages/cli/test-utils/release-fixtures.mjs
index fd39dcad4e..09fabf6d2a 100644
--- a/packages/cli/test-utils/release-fixtures.mjs
+++ b/packages/cli/test-utils/release-fixtures.mjs
@@ -10,6 +10,7 @@ import {createHash} from 'node:crypto';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
import {releaseTree} from '../foundation/release/cache.mjs';
/** @type {string[]} */
@@ -173,9 +174,14 @@ export const CLI_VERSION = JSON.parse(
fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'),
).version;
-/** This CLI package's folder. */
+/**
+ * This CLI package's folder. Derived with `fileURLToPath`, because
+ * `URL.pathname` is a URI path rather than a filesystem one: it keeps the
+ * percent-encoding of a checkout under a space, `%`, `#` or non-ASCII
+ * directory, and puts a leading slash before a Windows drive letter.
+ */
const THIS_CLI = path.resolve(
- path.dirname(new URL(import.meta.url).pathname),
+ path.dirname(fileURLToPath(import.meta.url)),
'..',
);
@@ -187,6 +193,14 @@ const THIS_CLI = path.resolve(
* @returns {string} dir
*/
export function linkThisCliAsProjectCli(dir) {
+ if (!fs.existsSync(THIS_CLI)) {
+ // Fail here rather than leave a dangling link: a project whose `cli` entry
+ // does not resolve answers as the carried Core instead of as its own CLI,
+ // which surfaces as an unrelated assertion in whichever test ran next.
+ throw new Error(
+ `linkThisCliAsProjectCli: this CLI package is not at ${THIS_CLI}`,
+ );
+ }
const pkgFile = path.join(dir, 'package.json');
if (!fs.existsSync(pkgFile)) {
writeJson(pkgFile, {
diff --git a/packages/cli/test-utils/release-fixtures.test.mjs b/packages/cli/test-utils/release-fixtures.test.mjs
new file mode 100644
index 0000000000..e1a6302c63
--- /dev/null
+++ b/packages/cli/test-utils/release-fixtures.test.mjs
@@ -0,0 +1,51 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file release-fixtures.test.mjs
+ * @input The project-CLI fixture builder
+ * @output Proof that a fixture project links a CLI that really resolves
+ * @position Test utility coverage for the shared release fixtures
+ */
+
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {afterEach, describe, expect, it} from 'vitest';
+import {linkThisCliAsProjectCli} from './release-fixtures.mjs';
+
+/** @type {string[]} */
+const made = [];
+
+function tempProject() {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-project-cli-'));
+ made.push(dir);
+ return dir;
+}
+
+afterEach(() => {
+ for (const dir of made.splice(0)) {
+ fs.rmSync(dir, {recursive: true, force: true});
+ }
+});
+
+describe('linkThisCliAsProjectCli', () => {
+ it('links a fixture project to this CLI package on disk', () => {
+ const dir = tempProject();
+ linkThisCliAsProjectCli(dir);
+
+ const link = path.join(dir, 'node_modules', '@astryxdesign', 'cli');
+ // realpathSync throws ENOENT when a link's target is missing, which is
+ // what a percent-encoded (and therefore nonexistent) package folder
+ // produces: the link itself is created whether or not the target exists.
+ const target = fs.realpathSync(link);
+ const manifest = JSON.parse(
+ fs.readFileSync(path.join(target, 'package.json'), 'utf8'),
+ );
+ expect(manifest.name).toBe('@astryxdesign/cli');
+ expect(
+ fs.existsSync(
+ path.join(target, ...manifest.bin.astryx.split('/').filter(Boolean)),
+ ),
+ ).toBe(true);
+ });
+});