Skip to content

Commit 42eb6d7

Browse files
committed
refactor: shared posixify fn for windows ut
1 parent fffaea9 commit 42eb6d7

File tree

8 files changed

+28
-27
lines changed

8 files changed

+28
-27
lines changed

src/client/deployMessages.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { basename, dirname, extname, join, posix, sep } from 'node:path/posix';
99
import { SfError } from '@salesforce/core';
1010
import { ensureArray } from '@salesforce/kit';
11+
import { posixify } from '../utils/path';
1112
import { ComponentLike, SourceComponent } from '../resolve';
1213
import { registry } from '../registry/registry';
1314
import {
@@ -154,8 +155,9 @@ const hasComponentType = (message: DeployMessage): message is DeployMessage & {
154155

155156
export const toKey = (component: ComponentLike): string => {
156157
const type = typeof component.type === 'string' ? component.type : component.type.name;
157-
return `${type}#${shouldConvertPaths ? component.fullName.split(sep).join(posix.sep) : component.fullName}`;
158+
return `${type}#${shouldConvertPaths ? posixify(component.fullName) : component.fullName}`;
158159
};
159160

160161
const isTrue = (value: BooleanString): boolean => value === 'true' || value === true;
162+
161163
export const shouldConvertPaths = sep !== posix.sep;

src/client/metadataApiDeploy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import JSZip from 'jszip';
1111
import fs from 'graceful-fs';
1212
import { Lifecycle, Messages, SfError } from '@salesforce/core';
1313
import { ensureArray } from '@salesforce/kit';
14+
import { posixify } from '../utils/path';
1415
import { RegistryAccess } from '../registry/registryAccess';
1516
import { ReplacementEvent } from '../convert/types';
1617
import { MetadataConverter } from '../convert/metadataConverter';
@@ -311,8 +312,7 @@ export class MetadataApiDeploy extends MetadataTransfer<
311312
// Add relative file paths to a root of "zip" for MDAPI.
312313
const relPath = join('zip', relative(mdapiPath, fullPath));
313314
// Ensure only posix paths are added to zip files
314-
const relPosixPath = relPath.replace(/\\/g, '/');
315-
zip.file(relPosixPath, fs.createReadStream(fullPath));
315+
zip.file(posixify(relPath), fs.createReadStream(fullPath));
316316
}
317317
}
318318
};

src/convert/replacements.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
*/
77
import { readFile } from 'node:fs/promises';
88
import { Transform, Readable } from 'node:stream';
9-
import { sep, posix, join, isAbsolute } from 'node:path';
9+
import { join, isAbsolute } from 'node:path';
1010
import { Lifecycle, Messages, SfError, SfProject } from '@salesforce/core';
1111
import { minimatch } from 'minimatch';
1212
import { Env } from '@salesforce/kit';
1313
import { ensureString, isString } from '@salesforce/ts-types';
14+
import { posixify } from '../utils/path';
1415
import { SourcePath } from '../common/types';
1516
import { SourceComponent } from '../resolve/sourceComponent';
1617
import { MarkedReplacement, ReplacementConfig, ReplacementEvent } from './types';
@@ -199,7 +200,7 @@ export const matchesFile =
199200
(r: ReplacementConfig): boolean =>
200201
// filenames will be absolute. We don't have convenient access to the pkgDirs,
201202
// so we need to be more open than an exact match
202-
(typeof r.filename === 'string' && posixifyPaths(filename).endsWith(r.filename)) ||
203+
(typeof r.filename === 'string' && posixify(filename).endsWith(r.filename)) ||
203204
(typeof r.glob === 'string' && minimatch(filename, `**/${r.glob}`));
204205

205206
/**
@@ -245,8 +246,6 @@ export const stringToRegex = (input: string): RegExp =>
245246
// eslint-disable-next-line no-useless-escape
246247
new RegExp(input.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g');
247248

248-
export const posixifyPaths = (f: string): string => f.split(sep).join(posix.sep);
249-
250249
/** if replaceWithFile is present, resolve it to an absolute path relative to the projectdir */
251250
const makeAbsolute =
252251
(projectDir: string) =>

src/convert/streams.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { createWriteStream, existsSync, promises as fsPromises } from 'graceful-
1313
import { JsonMap } from '@salesforce/ts-types';
1414
import { XMLBuilder } from 'fast-xml-parser';
1515
import { Logger } from '@salesforce/core';
16+
import { posixify } from '../utils/path';
1617
import { SourceComponent } from '../resolve/sourceComponent';
1718
import { SourcePath } from '../common/types';
1819
import { XML_COMMENT_PROP_NAME, XML_DECL } from '../common/constants';
@@ -234,8 +235,7 @@ export class ZipWriter extends ComponentWriter {
234235

235236
public addToZip(contents: string | Readable | Buffer, path: SourcePath): void {
236237
// Ensure only posix paths are added to zip files
237-
const posixPath = path.replace(/\\/g, '/');
238-
this.zip.file(posixPath, contents);
238+
this.zip.file(posixify(path), contents);
239239
}
240240
}
241241

src/convert/transformers/staticResourceMetadataTransformer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { JsonMap } from '@salesforce/ts-types';
1212
import { createWriteStream } from 'graceful-fs';
1313
import { Logger, Messages, SfError } from '@salesforce/core';
1414
import { isEmpty } from '@salesforce/kit';
15-
import { baseName } from '../../utils/path';
15+
import { baseName, posixify } from '../../utils/path';
1616
import { WriteInfo } from '../types';
1717
import { SourceComponent } from '../../resolve/sourceComponent';
1818
import { SourcePath } from '../../common/types';
@@ -59,8 +59,7 @@ export class StaticResourceMetadataTransformer extends BaseMetadataTransformer {
5959
// have to walk the component content. Replacements only happen if set on the component.
6060
for (const path of component.walkContent()) {
6161
const replacementStream = getReplacementStreamForReadable(component, path);
62-
const relPath = relative(content, path);
63-
const relPosixPath = relPath.replace(/\\/g, '/');
62+
const relPosixPath = posixify(relative(content, path));
6463
zip.file(relPosixPath, replacementStream);
6564
}
6665

src/utils/path.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import { basename, dirname, extname, sep, join } from 'node:path';
9+
import { posix } from 'node:path/posix';
910
import { Optional } from '@salesforce/ts-types';
1011
import { SfdxFileFormat } from '../convert/types';
1112
import { SourcePath } from '../common/types';
@@ -161,3 +162,5 @@ export const fnJoin =
161162
(a: string) =>
162163
(b: string): string =>
163164
join(a, b);
165+
166+
export const posixify = (f: string): string => f.split(sep).join(posix.sep);

test/convert/replacements.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {
1313
matchesFile,
1414
replacementIterations,
1515
stringToRegex,
16-
posixifyPaths,
1716
envFilter,
1817
} from '../../src/convert/replacements';
1918
import { matchingContentFile } from '../mock';
2019
import * as replacementsForMock from '../../src/convert/replacements';
20+
import { posixify } from '../../src/utils/path';
2121

2222
config.truncateThreshold = 0;
2323

@@ -155,7 +155,7 @@ describe('marking replacements on a component', () => {
155155
assert(cmp.xml);
156156
const result = await getReplacements(cmp, [
157157
// spec says filename path should be posix. The mocks are using join, so on windows they are wrong
158-
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
158+
{ filename: posixify(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
159159
]);
160160
expect(result).to.deep.equal({
161161
[cmp.xml]: [
@@ -171,7 +171,7 @@ describe('marking replacements on a component', () => {
171171
it('marks string replacements from file', async () => {
172172
assert(cmp.xml);
173173
const result = await getReplacements(cmp, [
174-
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithFile: 'bar' },
174+
{ filename: posixify(cmp.xml), stringToReplace: 'foo', replaceWithFile: 'bar' },
175175
]);
176176
expect(result).to.deep.equal({
177177
[cmp.xml]: [
@@ -188,7 +188,7 @@ describe('marking replacements on a component', () => {
188188
it('marks regex replacements on a matching file', async () => {
189189
assert(cmp.xml);
190190
const result = await getReplacements(cmp, [
191-
{ filename: posixifyPaths(cmp.xml), regexToReplace: '.*foo.*', replaceWithEnv: 'FOO_REPLACEMENT' },
191+
{ filename: posixify(cmp.xml), regexToReplace: '.*foo.*', replaceWithEnv: 'FOO_REPLACEMENT' },
192192
]);
193193
expect(result).to.deep.equal({
194194
[cmp.xml]: [
@@ -204,8 +204,8 @@ describe('marking replacements on a component', () => {
204204
it('marks 2 replacements on one file', async () => {
205205
assert(cmp.xml);
206206
const result = await getReplacements(cmp, [
207-
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
208-
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'baz', replaceWithEnv: 'FOO_REPLACEMENT' },
207+
{ filename: posixify(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
208+
{ filename: posixify(cmp.xml), stringToReplace: 'baz', replaceWithEnv: 'FOO_REPLACEMENT' },
209209
]);
210210
expect(result).to.deep.equal({
211211
[cmp.xml]: [
@@ -253,8 +253,8 @@ describe('marking replacements on a component', () => {
253253
assert(cmp.content);
254254
assert(cmp.xml);
255255
const result = await getReplacements(cmp, [
256-
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
257-
{ filename: posixifyPaths(cmp.content), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
256+
{ filename: posixify(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
257+
{ filename: posixify(cmp.content), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
258258
]);
259259
expect(result).to.deep.equal({
260260
[cmp.xml]: [
@@ -280,7 +280,7 @@ describe('marking replacements on a component', () => {
280280
assert(cmp.xml);
281281
try {
282282
await getReplacements(cmp, [
283-
{ filename: posixifyPaths(cmp.xml), regexToReplace: '.*foo.*', replaceWithEnv: 'BAD_ENV' },
283+
{ filename: posixify(cmp.xml), regexToReplace: '.*foo.*', replaceWithEnv: 'BAD_ENV' },
284284
]);
285285
assert.fail('should have thrown');
286286
} catch (e) {
@@ -291,7 +291,7 @@ describe('marking replacements on a component', () => {
291291
assert(cmp.xml);
292292
const result = await getReplacements(cmp, [
293293
{
294-
filename: posixifyPaths(cmp.xml),
294+
filename: posixify(cmp.xml),
295295
regexToReplace: '.*foo.*',
296296
replaceWithEnv: 'BAD_ENV',
297297
allowUnsetEnvVariable: true,

test/mock/client/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77
import JSZip from 'jszip';
8+
import { posixify } from '../../../src/utils/path';
89

910
// eslint-disable-next-line @typescript-eslint/require-await
1011
export async function createMockZip(entries: string[]): Promise<Buffer> {
1112
const zip = JSZip();
12-
for (const entry of entries) {
13-
// Ensure only posix paths are added to zip files
14-
const relPosixPath = entry.replace(/\\/g, '/');
15-
zip.file(relPosixPath, '');
16-
}
13+
// Ensure only posix paths are added to zip files
14+
entries.map((entry) => zip.file(posixify(entry), ''));
1715
return zip.generateAsync({
1816
type: 'nodebuffer',
1917
compression: 'DEFLATE',

0 commit comments

Comments
 (0)