Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Jul 11, 2022
1 parent a76f230 commit 7505051
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/client/application/diagnostics/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export abstract class BaseDiagnostic implements IDiagnostic {
public readonly severity: DiagnosticSeverity,
public readonly scope: DiagnosticScope,
public readonly resource: Resource,
public readonly invokeHandler: 'always' | 'default' = 'default',
public readonly shouldShowPrompt = true,
public readonly invokeHandler: 'always' | 'default' = 'default',
) {}
}

Expand All @@ -50,12 +50,12 @@ export abstract class BaseDiagnosticsService implements IDiagnosticsService, IDi
return;
}
const diagnosticsToHandle = await asyncFilter(diagnostics, async (item) => {
if (item.invokeHandler && item.invokeHandler === 'always') {
return true;
}
if (!(await this.canHandle(item))) {
return false;
}
if (item.invokeHandler && item.invokeHandler === 'always') {
return true;
}
const key = this.getDiagnosticsKey(item);
if (BaseDiagnosticsService.handledDiagnosticCodeKeys.indexOf(key) !== -1) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export class InvalidLaunchJsonDebuggerDiagnostic extends BaseDiagnostic {
DiagnosticSeverity.Error,
DiagnosticScope.WorkspaceFolder,
resource,
'always',
shouldShowPrompt,
);
}
Expand Down Expand Up @@ -131,9 +130,9 @@ export class InvalidLaunchJsonDebuggerService extends BaseDiagnosticsService {
}

private async handleDiagnostic(diagnostic: IDiagnostic): Promise<void> {
if (!this.canHandle(diagnostic)) {
return;
}
// if (!this.canHandle(diagnostic)) {
// return;
// }
if (!diagnostic.shouldShowPrompt) {
await this.fixLaunchJson(diagnostic.code);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ class InvalidPythonPathInDebuggerDiagnostic extends BaseDiagnostic {
| DiagnosticCodes.InvalidPythonPathInDebuggerSettingsDiagnostic,
resource: Resource,
) {
super(code, messages[code], DiagnosticSeverity.Error, DiagnosticScope.WorkspaceFolder, resource, 'always');
super(
code,
messages[code],
DiagnosticSeverity.Error,
DiagnosticScope.WorkspaceFolder,
resource,
undefined,
'always',
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class PowershellActivationNotAvailableDiagnostic extends BaseDiagnostic {
DiagnosticSeverity.Warning,
DiagnosticScope.Global,
resource,
undefined,
'always',
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class InvalidPythonInterpreterDiagnostic extends BaseDiagnostic {
DiagnosticSeverity.Error,
DiagnosticScope.WorkspaceFolder,
resource,
undefined,
'always',
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import {
IDiagnosticsService,
} from '../../../../client/application/diagnostics/types';
import { CommandsWithoutArgs } from '../../../../client/common/application/commands';
import { IWorkspaceService } from '../../../../client/common/application/types';
import { Commands } from '../../../../client/common/constants';
import { IPlatformService } from '../../../../client/common/platform/types';
import { IConfigurationService, IDisposableRegistry, IPythonSettings } from '../../../../client/common/types';
import { Common } from '../../../../client/common/utils/localize';
import { noop } from '../../../../client/common/utils/misc';
import { IInterpreterHelper, IInterpreterService } from '../../../../client/interpreter/contracts';
import { IServiceContainer } from '../../../../client/ioc/types';
Expand All @@ -38,11 +41,17 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
let settings: typemoq.IMock<IPythonSettings>;
let interpreterService: typemoq.IMock<IInterpreterService>;
let platformService: typemoq.IMock<IPlatformService>;
let workspaceService: typemoq.IMock<IWorkspaceService>;
let helper: typemoq.IMock<IInterpreterHelper>;
const pythonPath = 'My Python Path in Settings';
let serviceContainer: typemoq.IMock<IServiceContainer>;
function createContainer() {
serviceContainer = typemoq.Mock.ofType<IServiceContainer>();
workspaceService = typemoq.Mock.ofType<IWorkspaceService>();
workspaceService.setup((w) => w.workspaceFile).returns(() => undefined);
serviceContainer
.setup((s) => s.get(typemoq.It.isValue(IWorkspaceService)))
.returns(() => workspaceService.object);
messageHandler = typemoq.Mock.ofType<IDiagnosticHandlerService<MessageCommandPrompt>>();
serviceContainer
.setup((s) =>
Expand Down Expand Up @@ -107,17 +116,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
diagnostic.verifyAll();
}
});
test('Can not handle non-InvalidPythonPathInterpreter diagnostics', async () => {
const diagnostic = typemoq.Mock.ofType<IDiagnostic>();
diagnostic
.setup((d) => d.code)
.returns(() => 'Something Else' as any)
.verifiable(typemoq.Times.atLeastOnce());

const canHandle = await diagnosticService.canHandle(diagnostic.object);
expect(canHandle).to.be.equal(false, 'Invalid value');
diagnostic.verifyAll();
});
test('Should return diagnostics if there are no interpreters after double-checking', async () => {
interpreterService
.setup((i) => i.hasInterpreters())
Expand All @@ -130,7 +129,13 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {

const diagnostics = await diagnosticService.diagnose(undefined);
expect(diagnostics).to.be.deep.equal(
[new InvalidPythonInterpreterDiagnostic(DiagnosticCodes.NoPythonInterpretersDiagnostic, undefined)],
[
new InvalidPythonInterpreterDiagnostic(
DiagnosticCodes.NoPythonInterpretersDiagnostic,
undefined,
workspaceService.object,
),
],
'not the same',
);
});
Expand All @@ -148,7 +153,13 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {

const diagnostics = await diagnosticService.diagnose(undefined);
expect(diagnostics).to.be.deep.equal(
[new InvalidPythonInterpreterDiagnostic(DiagnosticCodes.InvalidPythonInterpreterDiagnostic, undefined)],
[
new InvalidPythonInterpreterDiagnostic(
DiagnosticCodes.InvalidPythonInterpreterDiagnostic,
undefined,
workspaceService.object,
),
],
'not the same',
);
settings.verifyAll();
Expand All @@ -175,6 +186,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
const diagnostic = new InvalidPythonInterpreterDiagnostic(
DiagnosticCodes.NoPythonInterpretersDiagnostic,
undefined,
workspaceService.object,
);
const cmd = ({} as any) as IDiagnosticCommand;
let messagePrompt: MessageCommandPrompt | undefined;
Expand All @@ -187,7 +199,10 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
.setup((f) =>
f.createCommand(
typemoq.It.isAny(),
typemoq.It.isObjectWith<CommandOption<'launch', string>>({ type: 'launch' }),
typemoq.It.isObjectWith<CommandOption<'executeVSCCommand', CommandsWithoutArgs>>({
type: 'executeVSCCommand',
options: Commands.Set_Interpreter,
}),
),
)
.returns(() => cmd)
Expand All @@ -198,13 +213,19 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
messageHandler.verifyAll();
commandFactory.verifyAll();
expect(messagePrompt).not.be.equal(undefined, 'Message prompt not set');
expect(messagePrompt!.commandPrompts).to.be.deep.equal([{ prompt: 'Download', command: cmd }]);
expect(messagePrompt!.onClose).to.not.be.equal(undefined, 'onClose handler should be set.');
expect(messagePrompt!.commandPrompts).to.be.deep.equal([
{
prompt: Common.selectPythonInterpreter,
command: cmd,
},
]);
});

test('Handling no currently selected interpreter diagnostic should show select interpreter message', async () => {
const diagnostic = new InvalidPythonInterpreterDiagnostic(
DiagnosticCodes.InvalidPythonInterpreterDiagnostic,
undefined,
workspaceService.object,
);
const cmd = ({} as any) as IDiagnosticCommand;
let messagePrompt: MessageCommandPrompt | undefined;
Expand All @@ -230,15 +251,15 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
messageHandler.verifyAll();
commandFactory.verifyAll();
expect(messagePrompt).not.be.equal(undefined, 'Message prompt not set');
expect(messagePrompt!.onClose).be.equal(undefined, 'onClose handler should not be set.');
expect(messagePrompt!.commandPrompts).to.be.deep.equal([
{ prompt: 'Select Python Interpreter', command: cmd },
{ prompt: Common.selectPythonInterpreter, command: cmd },
]);
});
test('Handling no interpreters diagnostic should return select interpreter cmd', async () => {
const diagnostic = new InvalidPythonInterpreterDiagnostic(
DiagnosticCodes.InvalidPythonInterpreterDiagnostic,
undefined,
workspaceService.object,
);
const cmd = ({} as any) as IDiagnosticCommand;
let messagePrompt: MessageCommandPrompt | undefined;
Expand Down Expand Up @@ -299,6 +320,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => {
const diagnostic = new InvalidPythonInterpreterDiagnostic(
DiagnosticCodes.InvalidPythonInterpreterDiagnostic,
undefined,
workspaceService.object,
);
const cmd = ({} as any) as IDiagnosticCommand;
const diagnosticServiceMock = (typemoq.Mock.ofInstance(diagnosticService) as any) as typemoq.IMock<
Expand Down

0 comments on commit 7505051

Please sign in to comment.