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: added flushFilePath() #4240

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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
31 changes: 22 additions & 9 deletions packages/salesforcedx-utils-vscode/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

export { isNullOrUndefined, extractJsonObject } from './utils';
export {
ensureDirectoryExists,
getTestResultsFolder,
getRelativeProjectPath,
fileExtensionsMatch
} from './paths';

export {
TraceFlags
} from './traceFlags';

export {
TraceFlagsRemover
} from './traceFlagsRemover';

export {
isNullOrUndefined,
extractJsonObject,
flushFilePath,
flushFilePaths
} from './utils';

export {
isAlphaNumString,
isInteger,
isIntegerInRange,
isAlphaNumSpaceString,
isRecordIdFormat
} from './validations';
export {
ensureDirectoryExists,
getTestResultsFolder,
getRelativeProjectPath,
fileExtensionsMatch
} from './paths';
export { TraceFlags } from './traceFlags';
export { TraceFlagsRemover } from './traceFlagsRemover';
21 changes: 20 additions & 1 deletion packages/salesforcedx-utils-vscode/src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import * as fs from 'fs';

export function isNullOrUndefined(object: any): object is null | undefined {
if (object === null || object === undefined) {
return true;
Expand All @@ -14,11 +16,28 @@ export function isNullOrUndefined(object: any): object is null | undefined {
}

export function extractJsonObject(str: string): any {

const jsonString = str.substring(
str.indexOf('{'),
str.lastIndexOf('}') + 1
);

return JSON.parse(jsonString);
}

// There's a bug in VS Code where, after a file has been renamed,
// the URI that VS Code passes to the command is stale and is the
// original URL. See https://github.com/microsoft/vscode/issues/152993.
//
// To get around this, fs.realpathSync.native() is called to get the
// URI with the actual file name.
export function flushFilePath(filePath: string): string {
return fs.realpathSync.native(filePath);
}

export function flushFilePaths(filePaths: string[]): string[] {
for (let i = 0; i < filePaths.length; i++) {
filePaths[i] = flushFilePath(filePaths[i]);
}

return filePaths;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import {
import {
notificationService
} from '@salesforce/salesforcedx-utils-vscode/out/src/commands';
import {
flushFilePath
} from '@salesforce/salesforcedx-utils-vscode/out/src/helpers';
import * as vscode from 'vscode';
import { nls } from '../messages';
import {
Expand Down Expand Up @@ -89,8 +92,10 @@ async function getApexTestClassName(sourceUri: vscode.Uri): Promise<string | und
}

await testOutlineProvider.refresh();
const testClassName = testOutlineProvider.getTestClassName(sourceUri);

let testClassName = testOutlineProvider.getTestClassName(sourceUri);
if (testClassName) {
testClassName = flushFilePath(testClassName);
}
return testClassName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { testSetup } from '@salesforce/core/lib/testSetup';
import { SfdxCommandlet } from '@salesforce/salesforcedx-utils-vscode/out/src';
import { notificationService } from '@salesforce/salesforcedx-utils-vscode/out/src/commands';
import * as helpers from '@salesforce/salesforcedx-utils-vscode/out/src/helpers';
import { expect } from 'chai';
import { createSandbox, SinonSandbox } from 'sinon';
import * as vscode from 'vscode';
Expand Down Expand Up @@ -143,6 +144,9 @@ describe('Force Launch Replay Debugger', () => {
sb.stub(SfdxCommandlet.prototype, 'run')
.returns(undefined);

sb.stub(helpers, 'flushFilePath')
.returns('foo.cls');

const executeCommandSpy = sb.spy(vscode.commands, 'executeCommand');

await forceLaunchApexReplayDebuggerWithCurrentFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ import {
Command,
SfdxCommandBuilder
} from '@salesforce/salesforcedx-utils-vscode/out/src/cli';
import {
flushFilePath
} from '@salesforce/salesforcedx-utils-vscode/out/src/helpers';
import {
CancelResponse,
ContinueResponse,
ParametersGatherer,
PreconditionChecker
} from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import * as vscode from 'vscode';
import { SfdxCommandlet, SfdxCommandletExecutor } from './util/sfdxCommandlet';

import { channelService } from '../channels';
import { nls } from '../messages';
import { notificationService } from '../notifications';
import { telemetryService } from '../telemetry';
import { getRootWorkspacePath, hasRootWorkspace } from '../util';
import { SfdxCommandlet, SfdxCommandletExecutor } from './util/sfdxCommandlet';

export class ForceSourceDeleteExecutor extends SfdxCommandletExecutor<{
filePath: string;
Expand All @@ -43,7 +45,7 @@ export class ManifestChecker implements PreconditionChecker {
private explorerPath: string;

public constructor(uri: vscode.Uri) {
this.explorerPath = uri.fsPath;
this.explorerPath = flushFilePath(uri.fsPath);
}

public check(): boolean {
Expand All @@ -70,7 +72,7 @@ export class ConfirmationAndSourcePathGatherer
private readonly CANCEL = nls.localize('cancel_delete_source_button_text');

public constructor(uri: vscode.Uri) {
this.explorerPath = uri.fsPath;
this.explorerPath = flushFilePath(uri.fsPath);
}

public async gather(): Promise<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class LibraryDeploySourcePathExecutor extends DeployExecutor<
}

export const forceSourceDeploySourcePaths = async (
sourceUri: vscode.Uri | vscode.Uri[] |undefined,
sourceUri: vscode.Uri | vscode.Uri[] | undefined,
uris: vscode.Uri[] | undefined
) => {
if (!sourceUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import {
SfdxCommandBuilder
} from '@salesforce/salesforcedx-utils-vscode/out/src/cli';
import { PostconditionChecker } from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import {
CancelResponse,
ContinueResponse
ContinueResponse,
PostconditionChecker
} from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import { ComponentSet } from '@salesforce/source-deploy-retrieve';
import * as vscode from 'vscode';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {
flushFilePaths
} from '@salesforce/salesforcedx-utils-vscode/out/src/helpers';
import {
ContinueResponse,
ParametersGatherer
Expand All @@ -12,14 +15,18 @@ import * as vscode from 'vscode';

export class LibraryPathsGatherer implements ParametersGatherer<string[]> {
private uris: vscode.Uri[];

public constructor(uris: vscode.Uri[]) {
this.uris = uris;
}

public async gather(): Promise<ContinueResponse<string[]>> {
const sourcePaths = this.uris.map(uri => uri.fsPath);
const flushedSourcePaths = flushFilePaths(sourcePaths);

return {
type: 'CONTINUE',
data: sourcePaths
data: flushedSourcePaths
};
}
}
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-core/src/messages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* decorations, e.g., $(x) uses the https://octicons.github.com/ and should not
* be localized
*
* If ommitted, we will assume _message.
* If omitted, we will assume _message.
*/
export const messages = {
channel_name: 'Salesforce CLI',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as helpers from '@salesforce/salesforcedx-utils-vscode/out/src/helpers';
import { ContinueResponse } from '@salesforce/salesforcedx-utils-vscode/out/src/types';
import { expect } from 'chai';
import * as path from 'path';
Expand Down Expand Up @@ -51,43 +52,64 @@ describe('ManifestChecker', () => {
'package.xml'
);
const manifestUri = { fsPath: manifestFilePath } as vscode.Uri;

const flushFilePathStub = sinon.stub(helpers, 'flushFilePath')
.returns(manifestFilePath);

const checker = new ManifestChecker(manifestUri);
const response = checker.check();
expect(response).to.be.false;

flushFilePathStub.restore();
});

it('passes the check if the selected resource is not in the manifest directory', () => {
const sourcePath = path.join(workspaceFolderPath, 'src', 'exampleFile.js');
const sourceUri = { fsPath: sourcePath } as vscode.Uri;

const flushFilePathStub = sinon.stub(helpers, 'flushFilePath')
.returns(sourcePath);

const checker = new ManifestChecker(sourceUri);
const response = checker.check();
expect(response).to.be.true;

flushFilePathStub.restore();
});
});

describe('ConfirmationAndSourcePathGatherer', () => {
const examplePath = path.join('example', 'path');
const explorerPath = { fsPath: examplePath } as vscode.Uri;
const explorerPathUri = { fsPath: examplePath } as vscode.Uri;

let informationMessageStub: sinon.SinonStub;
let flushFilePathStub: sinon.SinonStub;

beforeEach(() => {
informationMessageStub = sinon.stub(
vscode.window,
'showInformationMessage'
);

flushFilePathStub = sinon.stub(
helpers,
'flushFilePath'
);

flushFilePathStub.returns(examplePath);
});

afterEach(() => {
informationMessageStub.restore();
flushFilePathStub.restore();
});

it('Should return cancel if the user cancels the command', async () => {
informationMessageStub.returns(
nls.localize('cancel_delete_source_button_text')
);

const gatherer = new ConfirmationAndSourcePathGatherer(explorerPath);
const gatherer = new ConfirmationAndSourcePathGatherer(explorerPathUri);
const response = await gatherer.gather();
expect(informationMessageStub.calledOnce).to.be.true;
expect(response.type).to.equal('CANCEL');
Expand All @@ -98,7 +120,7 @@ describe('ConfirmationAndSourcePathGatherer', () => {
nls.localize('confirm_delete_source_button_text')
);

const gatherer = new ConfirmationAndSourcePathGatherer(explorerPath);
const gatherer = new ConfirmationAndSourcePathGatherer(explorerPathUri);
const response = (await gatherer.gather()) as ContinueResponse<{
filePath: string;
}>;
Expand Down
Loading