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: push-on-save working as expected, diagnostics cleared after successful push #4975

Merged
merged 13 commits into from
Jul 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ import { nls } from '../messages';
import { componentSetUtils } from '../services/sdr/componentSetUtils';
import { DeployQueue, sfdxCoreSettings } from '../settings';
import { SfdxPackageDirectories } from '../sfdxProject';
import { createComponentCount, formatException } from './util';
import {
createComponentCount,
formatException,
SfdxCommandletExecutor
} from './util';

type DeployRetrieveResult = DeployResult | RetrieveResult;
type DeployRetrieveOperation = MetadataApiDeploy | MetadataApiRetrieve;

export abstract class DeployRetrieveExecutor<
T
> extends LibraryCommandletExecutor<T> {
protected static errorCollection = vscode.languages.createDiagnosticCollection(
public static errorCollection = vscode.languages.createDiagnosticCollection(
'deploy-errors'
);
protected cancellable: boolean = true;
Expand Down Expand Up @@ -159,6 +163,7 @@ export abstract class DeployExecutor<T> extends DeployRetrieveExecutor<T> {
);
} else {
DeployRetrieveExecutor.errorCollection.clear();
SfdxCommandletExecutor.errorCollection.clear();
}
}
} finally {
Expand Down Expand Up @@ -277,6 +282,7 @@ export abstract class RetrieveExecutor<T> extends DeployRetrieveExecutor<T> {
): Promise<void> {
if (result) {
DeployRetrieveExecutor.errorCollection.clear();
SfdxCommandletExecutor.errorCollection.clear();
const relativePackageDirs = await SfdxPackageDirectories.getPackageDirectoryPaths();
const output = this.createOutput(result, relativePackageDirs);
channelService.appendLine(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { FORCE_SOURCE_PUSH_LOG_NAME } from '../constants';
import { handleDiagnosticErrors } from '../diagnostics';
import { nls } from '../messages';
import { telemetryService } from '../telemetry';
import { DeployRetrieveExecutor } from './baseDeployRetrieve';
import {
CommandParams,
EmptyParametersGatherer,
Expand All @@ -50,9 +51,6 @@ export const pushCommand: CommandParams = {

export class ForceSourcePushExecutor extends SfdxCommandletExecutor<{}> {
private flag: string | undefined;
public errorCollection = vscode.languages.createDiagnosticCollection(
'push-errors'
);
public constructor(
flag?: string,
public params: CommandParams = pushCommand
Expand Down Expand Up @@ -132,7 +130,8 @@ export class ForceSourcePushExecutor extends SfdxCommandletExecutor<{}> {
const telemetry = new TelemetryBuilder();
let success = false;
try {
this.errorCollection.clear();
SfdxCommandletExecutor.errorCollection.clear();
DeployRetrieveExecutor.errorCollection.clear();
if (stdOut) {
const pushParser = new ForcePushResultParser(stdOut);
const errors = pushParser.getErrors();
Expand All @@ -142,15 +141,16 @@ export class ForceSourcePushExecutor extends SfdxCommandletExecutor<{}> {
errors,
workspacePath,
execFilePathOrPaths,
this.errorCollection
SfdxCommandletExecutor.errorCollection
);
} else {
success = true;
}
this.outputResult(pushParser);
}
} catch (e) {
this.errorCollection.clear();
SfdxCommandletExecutor.errorCollection.clear();
DeployRetrieveExecutor.errorCollection.clear();
if (e.name !== 'PushParserFail') {
e.message =
'Error while creating diagnostics for vscode problem view.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import { CommandletExecutor } from './commandletExecutor';

export abstract class SfdxCommandletExecutor<T>
implements CommandletExecutor<T> {
public static errorCollection = vscode.languages.createDiagnosticCollection(
'push-errors'
);
protected showChannelOutput = true;
protected executionCwd = workspaceUtils.getRootWorkspacePath();
protected onDidFinishExecutionEventEmitter = new vscode.EventEmitter<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@salesforce/source-deploy-retrieve';
import * as path from 'path';
import * as vscode from 'vscode';
import { SfdxCommandletExecutor } from '../commands/util';

const notApplicable = 'N/A';

Expand Down Expand Up @@ -104,6 +105,7 @@ export function handleDeployDiagnostics(
errorCollection: vscode.DiagnosticCollection
): vscode.DiagnosticCollection {
errorCollection.clear();
SfdxCommandletExecutor.errorCollection.clear();

const diagnosticMap: Map<string, vscode.Diagnostic[]> = new Map();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class DeployQueue {
default:
displayError(e.message);
}
} finally {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeployQueue.locked is set to false. What does this mean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this unlocks the queue in order to let the subsequent pushes on save be triggered

this.locked = false;
}
this.deployWaitStart = undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ import {
} from '@salesforce/salesforcedx-utils-vscode';
import { ComponentSet } from '@salesforce/source-deploy-retrieve';
import * as fs from 'fs';
import { DeployExecutor } from '../../../src/commands/baseDeployRetrieve';
import { channelService } from '../../../src/channels';
import {
DeployExecutor,
DeployRetrieveExecutor
} from '../../../src/commands/baseDeployRetrieve';
import { SfdxCommandletExecutor } from '../../../src/commands/util';
import { PersistentStorageService } from '../../../src/conflict';
import { WorkspaceContext } from '../../../src/context/workspaceContext';
import { sfdxCoreSettings } from '../../../src/settings';
import * as diagnostics from '../../../src/diagnostics';
import { DeployQueue, sfdxCoreSettings } from '../../../src/settings';
import { SfdxPackageDirectories } from '../../../src/sfdxProject';

jest.mock('@salesforce/source-deploy-retrieve', () => {
return {
Expand Down Expand Up @@ -74,6 +82,11 @@ describe('Deploy Executor', () => {
return new Promise(resolve => resolve(new ComponentSet()));
}
}
class MockErrorCollection {
public static clear(): void {
jest.fn();
}
}

beforeEach(async () => {
jest.spyOn(process, 'cwd').mockReturnValue(dummyProcessCwd);
Expand All @@ -94,7 +107,8 @@ describe('Deploy Executor', () => {
.mockResolvedValue({ pollStatus: jest.fn() } as any);
getEnableSourceTrackingForDeployAndRetrieveMock = jest.spyOn(
sfdxCoreSettings,
'getEnableSourceTrackingForDeployAndRetrieve');
'getEnableSourceTrackingForDeployAndRetrieve'
);
});

it('should create Source Tracking and call ensureLocalTracking before deploying', async () => {
Expand Down Expand Up @@ -148,4 +162,140 @@ describe('Deploy Executor', () => {
expect(ensureLocalTrackingSpy).not.toHaveBeenCalled();
expect(deploySpy).toHaveBeenCalled();
});

it('unsuccessfulOperationHandler', () => {
// Arrange
const mockDeployResult = {
response: {
status: 'Failed'
}
};
const handleDeployDiagnosticsSpy = jest
.spyOn(diagnostics, 'handleDeployDiagnostics')
.mockImplementation(jest.fn());
DeployRetrieveExecutor.errorCollection = MockErrorCollection as any;
const executor = new TestDeployExecutor(
'testDeploy',
'force_source_deploy_with_sourcepath_beta'
);

// Act
(executor as any).unsuccessfulOperationHandler(
mockDeployResult,
DeployRetrieveExecutor.errorCollection
);

expect(handleDeployDiagnosticsSpy).toHaveBeenCalledWith(
mockDeployResult,
DeployRetrieveExecutor.errorCollection
);
});

describe('postOperation', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate describe block for postOperation! Good Work on keeping the separation of concerns in mind! 🎉

let mockUnlock: any;
let unlockSpy: any;
let setPropertiesForFilesDeployMock: any;
let getInstanceSpy: any;
let getPackageDirectoryPathsSpy: any;
let createOutputSpy: any;
let appendLineSpy: any;
beforeEach(() => {
setPropertiesForFilesDeployMock = jest.fn();
getInstanceSpy = jest
.spyOn(PersistentStorageService, 'getInstance')
.mockReturnValue({
setPropertiesForFilesDeploy: setPropertiesForFilesDeployMock
} as any);
getPackageDirectoryPathsSpy = jest
.spyOn(SfdxPackageDirectories, 'getPackageDirectoryPaths')
.mockResolvedValue('path/to/foo' as any);
createOutputSpy = jest
.spyOn(TestDeployExecutor.prototype as any, 'createOutput')
.mockReturnValue('path/to/foo' as any);
appendLineSpy = jest
.spyOn(channelService, 'appendLine')
.mockImplementation(jest.fn());
mockUnlock = jest.fn();
unlockSpy = jest
.spyOn(DeployQueue, 'get')
.mockReturnValue({ unlock: mockUnlock } as any);
DeployRetrieveExecutor.errorCollection = MockErrorCollection as any;
});

it('should clear errors on success', async () => {
// Arrange
const mockDeployResult = {
response: {
status: 'Succeeded'
}
};
const deployRetrieveExecutorClearSpy = jest.spyOn(
DeployRetrieveExecutor.errorCollection,
'clear'
);
SfdxCommandletExecutor.errorCollection = MockErrorCollection as any;
const sfdxCommandletExecutorClearSpy = jest.spyOn(
SfdxCommandletExecutor.errorCollection,
'clear'
);

const executor = new TestDeployExecutor(
'testDeploy',
'force_source_deploy_with_sourcepath_beta'
);

// Act
await (executor as any).postOperation(mockDeployResult);

// Assert
expect(getInstanceSpy).toHaveBeenCalled();
expect(getPackageDirectoryPathsSpy).toHaveBeenCalled();
expect(createOutputSpy).toHaveBeenCalled();
expect(appendLineSpy).toHaveBeenCalled();
expect(setPropertiesForFilesDeployMock).toHaveBeenCalledWith(
mockDeployResult
);
expect(deployRetrieveExecutorClearSpy).toHaveBeenCalled();
expect(sfdxCommandletExecutorClearSpy).toHaveBeenCalled();
expect(unlockSpy).toHaveBeenCalled();
expect(mockUnlock).toHaveBeenCalled();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mockUnlock is returnedValue of unlockSpy. Do we need this expect statement? Also mockUnlock should be a boolean, right?

});

it('should create diagnostics on failure', async () => {
// Arrange
const mockDeployResult = {
response: {
status: 'Failed'
}
};
const unsuccessfulOperationHandlerSpy = jest
.spyOn(
TestDeployExecutor.prototype as any,
'unsuccessfulOperationHandler'
)
.mockImplementation(jest.fn());
const executor = new TestDeployExecutor(
'testDeploy',
'force_source_deploy_with_sourcepath_beta'
);

// Act
await (executor as any).postOperation(mockDeployResult);

// Asserts
expect(getInstanceSpy).toHaveBeenCalled();
expect(getPackageDirectoryPathsSpy).toHaveBeenCalled();
expect(createOutputSpy).toHaveBeenCalled();
expect(appendLineSpy).toHaveBeenCalled();
expect(setPropertiesForFilesDeployMock).toHaveBeenCalledWith(
mockDeployResult
);
expect(unsuccessfulOperationHandlerSpy).toHaveBeenCalledWith(
mockDeployResult,
DeployRetrieveExecutor.errorCollection
);
expect(unlockSpy).toHaveBeenCalled();
expect(mockUnlock).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@
import { ChannelService } from '@salesforce/salesforcedx-utils-vscode';
import { nls } from '@salesforce/salesforcedx-utils-vscode/src/messages';
import { ForceSourcePushExecutor } from '../../../src/commands';
import { DeployRetrieveExecutor } from '../../../src/commands/baseDeployRetrieve';
import { DeployType } from '../../../src/commands/forceSourcePush';
import { CommandParams } from '../../../src/commands/util';
import {
CommandParams,
SfdxCommandletExecutor
} from '../../../src/commands/util';
import { PersistentStorageService } from '../../../src/conflict';
import { dummyPushResult, dummyStdOut } from './data/testData';

describe('ForceSourcePushExecutor', () => {
describe('exitProcessHandlerPush', () => {
class MockErrorCollection {
public static clear(): void {
jest.fn();
}
}
beforeEach(() => {
jest.spyOn(ChannelService, 'getInstance').mockReturnValue({} as any);
jest.spyOn(nls, 'localize').mockReturnValue('');
Expand All @@ -38,7 +47,16 @@ describe('ForceSourcePushExecutor', () => {
const executor = new ForceSourcePushExecutor(flag, pushCommand);
const updateCacheMock = jest.fn();
const executorAsAny = executor as any;
executorAsAny.errorCollection = { clear: jest.fn() };
SfdxCommandletExecutor.errorCollection = MockErrorCollection as any;
DeployRetrieveExecutor.errorCollection = MockErrorCollection as any;
const deployRetrieveExecutorClearSpy = jest.spyOn(
DeployRetrieveExecutor.errorCollection,
'clear'
);
const sfdxCommandletExecutorClearSpy = jest.spyOn(
SfdxCommandletExecutor.errorCollection,
'clear'
);
executorAsAny.updateCache = updateCacheMock;
executorAsAny.getDeployType = jest.fn().mockReturnValue(DeployType.Push);
executorAsAny.logMetric = jest.fn();
Expand All @@ -56,6 +74,8 @@ describe('ForceSourcePushExecutor', () => {

// Assert
expect(updateCacheMock).toHaveBeenCalled();
expect(sfdxCommandletExecutorClearSpy).toHaveBeenCalled();
expect(deployRetrieveExecutorClearSpy).toHaveBeenCalled();
});
});

Expand Down
Loading