From 58c27def47d5c35e8fe302d7a200c196a5076407 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Mon, 16 Dec 2024 19:01:14 +0000 Subject: [PATCH] yarn-plugin: refactor retrieval of workspace root portable path for testability Signed-off-by: MT Lewis --- .../src/util/getCurrentBackstageVersion.ts | 10 ++- .../src/util/getWorkspaceRoot.test.ts | 70 +++++++++++++++++++ .../yarn-plugin/src/util/getWorkspaceRoot.ts | 24 +++++++ 3 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 packages/yarn-plugin/src/util/getWorkspaceRoot.test.ts create mode 100644 packages/yarn-plugin/src/util/getWorkspaceRoot.ts diff --git a/packages/yarn-plugin/src/util/getCurrentBackstageVersion.ts b/packages/yarn-plugin/src/util/getCurrentBackstageVersion.ts index 83233747ce..5280854fb3 100644 --- a/packages/yarn-plugin/src/util/getCurrentBackstageVersion.ts +++ b/packages/yarn-plugin/src/util/getCurrentBackstageVersion.ts @@ -16,15 +16,13 @@ import assert from 'assert'; import { valid as semverValid } from 'semver'; -import { npath, ppath, xfs } from '@yarnpkg/fslib'; -import { BACKSTAGE_JSON, findPaths } from '@backstage/cli-common'; +import { ppath, xfs } from '@yarnpkg/fslib'; +import { BACKSTAGE_JSON } from '@backstage/cli-common'; import { memoize } from './memoize'; +import { getWorkspaceRoot } from './getWorkspaceRoot'; export const getCurrentBackstageVersion = memoize(() => { - const workspaceRoot = npath.toPortablePath( - findPaths(npath.fromPortablePath(ppath.cwd())).targetRoot, - ); - const backstageJsonPath = ppath.join(workspaceRoot, BACKSTAGE_JSON); + const backstageJsonPath = ppath.join(getWorkspaceRoot(), BACKSTAGE_JSON); let backstageVersion: string | null = null; try { diff --git a/packages/yarn-plugin/src/util/getWorkspaceRoot.test.ts b/packages/yarn-plugin/src/util/getWorkspaceRoot.test.ts new file mode 100644 index 0000000000..11538b3d50 --- /dev/null +++ b/packages/yarn-plugin/src/util/getWorkspaceRoot.test.ts @@ -0,0 +1,70 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { findPaths, Paths } from '@backstage/cli-common'; + +const setPlatform = (platform: string) => { + Object.defineProperty(process, `platform`, { + configurable: true, + value: platform, + }); +}; + +describe('getWorkspaceRoot', () => { + /** + * Yarn uses a PortablePath type which uses the posix separator + * regardless of platform and prefixes absolute paths with a separator. + * + * https://yarnpkg.com/api/yarnpkg-fslib#type-safe-paths + */ + describe.each` + platform | native | portable + ${'darwin'} | ${'/test/workspace/'} | ${'/test/workspace/'} + ${'win32'} | ${'C:\\test\\workspace\\'} | ${'/C:/test/workspace/'} + `('platform: $platform', ({ platform, native, portable }) => { + let realPlatform: string; + let getWorkspaceRoot: () => string; + let mockFindPaths: jest.MockedFunction; + + beforeEach(() => { + realPlatform = process.platform; + setPlatform(platform); + + jest.resetModules(); + + mockFindPaths = jest.fn(); + + jest.doMock('@backstage/cli-common', () => ({ + ...jest.requireActual('@backstage/cli-common'), + findPaths: mockFindPaths, + })); + + getWorkspaceRoot = require('./getWorkspaceRoot').getWorkspaceRoot; + }); + + afterEach(() => { + setPlatform(realPlatform); + }); + + it('returns an appropriately-formatted workspace root path', () => { + mockFindPaths.mockReturnValue({ + targetRoot: native, + } as Paths); + + expect(getWorkspaceRoot()).toEqual(portable); + }); + }); +}); diff --git a/packages/yarn-plugin/src/util/getWorkspaceRoot.ts b/packages/yarn-plugin/src/util/getWorkspaceRoot.ts new file mode 100644 index 0000000000..d6f3e98e10 --- /dev/null +++ b/packages/yarn-plugin/src/util/getWorkspaceRoot.ts @@ -0,0 +1,24 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { npath, ppath } from '@yarnpkg/fslib'; +import { findPaths } from '@backstage/cli-common'; + +export const getWorkspaceRoot = () => { + return npath.toPortablePath( + findPaths(npath.fromPortablePath(ppath.cwd())).targetRoot, + ); +};