Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: windows path replacements #958

Merged
merged 5 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"node": ">=14.0.0"
},
"dependencies": {
"@salesforce/core": "^3.34.8",
"@salesforce/core": "^3.36.0",
"@salesforce/kit": "^1.9.2",
"@salesforce/ts-types": "^1.7.2",
"archiver": "^5.3.1",
Expand All @@ -47,6 +47,7 @@
"@salesforce/ts-sinon": "^1.4.6",
"@types/archiver": "^5.3.1",
"@types/deep-equal-in-any-order": "^1.0.1",
"@types/graceful-fs": "^4.1.6",
"@types/mime": "2.0.3",
"@types/minimatch": "^5.1.2",
"@types/proxy-from-env": "^1.0.1",
Expand Down Expand Up @@ -187,4 +188,4 @@
"output": []
}
}
}
}
5 changes: 4 additions & 1 deletion src/convert/replacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { readFile } from 'fs/promises';
import { Transform, Readable } from 'stream';
import { sep, posix } from 'path';
import { Lifecycle, Messages, SfProject } from '@salesforce/core';
import * as minimatch from 'minimatch';
import { Env } from '@salesforce/kit';
Expand Down Expand Up @@ -194,7 +195,7 @@ export const getReplacements = async (
export const matchesFile = (f: string, r: ReplacementConfig): boolean =>
// filenames will be absolute. We don't have convenient access to the pkgDirs,
// so we need to be more open than an exact match
Boolean((r.filename && f.endsWith(r.filename)) || (r.glob && minimatch(f, `**/${r.glob}`)));
Boolean((r.filename && posixifyPaths(f).endsWith(r.filename)) || (r.glob && minimatch(f, `**/${r.glob}`)));

/**
* Regardless of any components, return the ReplacementConfig that are valid with the current env.
Expand Down Expand Up @@ -228,3 +229,5 @@ export const stringToRegex = (input: string): RegExp =>
// being overly conservative
// eslint-disable-next-line no-useless-escape
new RegExp(input.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g');

export const posixifyPaths = (f: string): string => f.split(sep).join(posix.sep);
29 changes: 21 additions & 8 deletions test/convert/replacements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import * as path from 'path';
import { assert, expect } from 'chai';
import Sinon = require('sinon');
import { Lifecycle } from '@salesforce/core';
import { getReplacements, matchesFile, replacementIterations, stringToRegex } from '../../src/convert/replacements';
import {
getReplacements,
matchesFile,
replacementIterations,
stringToRegex,
posixifyPaths,
} from '../../src/convert/replacements';
import { matchingContentFile } from '../mock';
import * as replacementsForMock from '../../src/convert/replacements';

Expand All @@ -18,6 +24,10 @@ describe('file matching', () => {
expect(matchesFile('foo', { filename: 'foo', ...base })).to.be.true;
expect(matchesFile('bar', { filename: 'foo', ...base })).to.not.be.true;
});
it('paths with separators to cover possibility of windows paths', () => {
expect(matchesFile(path.join('foo', 'bar'), { filename: 'foo/bar', ...base })).to.be.true;
expect(matchesFile(path.join('foo', 'bar'), { filename: 'foo/baz', ...base })).to.not.be.true;
});
it('file matches glob (posix paths)', () => {
expect(matchesFile('foo/bar', { glob: 'foo/**', ...base })).to.be.true;
expect(matchesFile('foo/bar', { glob: 'foo/*', ...base })).to.be.true;
Expand Down Expand Up @@ -59,7 +69,8 @@ describe('marking replacements on a component', () => {
it('marks a string replacement from env', async () => {
assert(cmp.xml);
const result = await getReplacements(cmp, [
{ filename: cmp.xml, stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
// spec says filename path should be posix. The mocks are using join, so on windows they are wrong
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
]);
expect(result).to.deep.equal({
[cmp.xml]: [
Expand All @@ -74,7 +85,9 @@ describe('marking replacements on a component', () => {
});
it('marks string replacements from file', async () => {
assert(cmp.xml);
const result = await getReplacements(cmp, [{ filename: cmp.xml, stringToReplace: 'foo', replaceWithFile: 'bar' }]);
const result = await getReplacements(cmp, [
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithFile: 'bar' },
]);
expect(result).to.deep.equal({
[cmp.xml]: [
{
Expand All @@ -90,7 +103,7 @@ describe('marking replacements on a component', () => {
it('marks regex replacements on a matching file', async () => {
assert(cmp.xml);
const result = await getReplacements(cmp, [
{ filename: cmp.xml, regexToReplace: '.*foo.*', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: posixifyPaths(cmp.xml), regexToReplace: '.*foo.*', replaceWithEnv: 'FOO_REPLACEMENT' },
]);
expect(result).to.deep.equal({
[cmp.xml]: [
Expand All @@ -106,8 +119,8 @@ describe('marking replacements on a component', () => {
it('marks 2 replacements on one file', async () => {
assert(cmp.xml);
const result = await getReplacements(cmp, [
{ filename: cmp.xml, stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: cmp.xml, stringToReplace: 'baz', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'baz', replaceWithEnv: 'FOO_REPLACEMENT' },
]);
expect(result).to.deep.equal({
[cmp.xml]: [
Expand Down Expand Up @@ -155,8 +168,8 @@ describe('marking replacements on a component', () => {
assert(cmp.content);
assert(cmp.xml);
const result = await getReplacements(cmp, [
{ filename: cmp.xml, stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: cmp.content, stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: posixifyPaths(cmp.xml), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
{ filename: posixifyPaths(cmp.content), stringToReplace: 'foo', replaceWithEnv: 'FOO_REPLACEMENT' },
]);
expect(result).to.deep.equal({
[cmp.xml]: [
Expand Down
10 changes: 4 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -890,24 +890,22 @@
strip-ansi "6.0.1"
ts-retry-promise "^0.7.0"

"@salesforce/core@^3.34.6", "@salesforce/core@^3.34.8":
version "3.34.8"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.34.8.tgz#01d74a6548ca395502ad61fe6e6e0ddc83f2ad01"
integrity sha512-2BpnyHMzN7NOg/nXEQNxpditMfQ9oKD30wC1IpTbSeS5JxZYYCAdDZkL0CfbtUceJwj/E3/x6oldiW2zQugrVg==
"@salesforce/core@^3.34.6", "@salesforce/core@^3.36.0":
version "3.36.0"
resolved "https://registry.yarnpkg.com/@salesforce/core/-/core-3.36.0.tgz#cbff147d888eee0b921e368c1fdc1a1a3c2eacab"
integrity sha512-LOeSJgozf5B1S8C98/K3afewRsathfEm+HrXxaFXJjITFfIhxqcIDB5xC1cw0VikfRUlq7dQocmVsvezcK0Ghw==
dependencies:
"@salesforce/bunyan" "^2.0.0"
"@salesforce/kit" "^1.9.2"
"@salesforce/schemas" "^1.5.1"
"@salesforce/ts-types" "^1.7.2"
"@types/graceful-fs" "^4.1.6"
"@types/semver" "^7.3.13"
ajv "^8.12.0"
archiver "^5.3.0"
change-case "^4.1.2"
debug "^3.2.7"
faye "^1.4.0"
form-data "^4.0.0"
graceful-fs "^4.2.11"
js2xmlparser "^4.0.1"
jsforce "^2.0.0-beta.21"
jsonwebtoken "9.0.0"
Expand Down