|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:no-any |
| 7 | + |
| 8 | +import { expect } from 'chai'; |
| 9 | +import { Container } from 'inversify'; |
| 10 | +import * as path from 'path'; |
| 11 | +import * as TypeMoq from 'typemoq'; |
| 12 | +import { CancellationTokenSource, OutputChannel, TextDocument, Uri } from 'vscode'; |
| 13 | +import { IDocumentManager, IWorkspaceService } from '../../client/common/application/types'; |
| 14 | +import '../../client/common/extensions'; |
| 15 | +import { IFileSystem, IPlatformService } from '../../client/common/platform/types'; |
| 16 | +import { IConfigurationService, IInstaller, ILintingSettings, ILogger, IOutputChannel, IPythonSettings } from '../../client/common/types'; |
| 17 | +import { IInterpreterService } from '../../client/interpreter/contracts'; |
| 18 | +import { ServiceContainer } from '../../client/ioc/container'; |
| 19 | +import { ServiceManager } from '../../client/ioc/serviceManager'; |
| 20 | +import { BaseLinter } from '../../client/linters/baseLinter'; |
| 21 | +import { Flake8 } from '../../client/linters/flake8'; |
| 22 | +import { LinterManager } from '../../client/linters/linterManager'; |
| 23 | +import { MyPy } from '../../client/linters/mypy'; |
| 24 | +import { Pep8 } from '../../client/linters/pep8'; |
| 25 | +import { Prospector } from '../../client/linters/prospector'; |
| 26 | +import { PyDocStyle } from '../../client/linters/pydocstyle'; |
| 27 | +import { PyLama } from '../../client/linters/pylama'; |
| 28 | +import { Pylint } from '../../client/linters/pylint'; |
| 29 | +import { ILinterManager, ILintingEngine } from '../../client/linters/types'; |
| 30 | +import { initialize } from '../initialize'; |
| 31 | + |
| 32 | +// tslint:disable-next-line:max-func-body-length |
| 33 | +suite('Linting - Arguments', () => { |
| 34 | + let interpreterService: TypeMoq.IMock<IInterpreterService>; |
| 35 | + let engine: TypeMoq.IMock<ILintingEngine>; |
| 36 | + let configService: TypeMoq.IMock<IConfigurationService>; |
| 37 | + let docManager: TypeMoq.IMock<IDocumentManager>; |
| 38 | + let settings: TypeMoq.IMock<IPythonSettings>; |
| 39 | + let lm: ILinterManager; |
| 40 | + let serviceContainer: ServiceContainer; |
| 41 | + let document: TypeMoq.IMock<TextDocument>; |
| 42 | + let outputChannel: TypeMoq.IMock<OutputChannel>; |
| 43 | + let workspaceService: TypeMoq.IMock<IWorkspaceService>; |
| 44 | + const cancellationToken = new CancellationTokenSource().token; |
| 45 | + |
| 46 | + suiteSetup(initialize); |
| 47 | + setup(async () => { |
| 48 | + const cont = new Container(); |
| 49 | + const serviceManager = new ServiceManager(cont); |
| 50 | + |
| 51 | + serviceContainer = new ServiceContainer(cont); |
| 52 | + outputChannel = TypeMoq.Mock.ofType<OutputChannel>(); |
| 53 | + |
| 54 | + const fs = TypeMoq.Mock.ofType<IFileSystem>(); |
| 55 | + fs.setup(x => x.fileExistsAsync(TypeMoq.It.isAny())).returns(() => new Promise<boolean>((resolve, reject) => resolve(true))); |
| 56 | + fs.setup(x => x.arePathsSame(TypeMoq.It.isAnyString(), TypeMoq.It.isAnyString())).returns(() => true); |
| 57 | + serviceManager.addSingletonInstance<IFileSystem>(IFileSystem, fs.object); |
| 58 | + |
| 59 | + serviceManager.addSingletonInstance(IOutputChannel, outputChannel.object); |
| 60 | + |
| 61 | + interpreterService = TypeMoq.Mock.ofType<IInterpreterService>(); |
| 62 | + serviceManager.addSingletonInstance<IInterpreterService>(IInterpreterService, interpreterService.object); |
| 63 | + |
| 64 | + engine = TypeMoq.Mock.ofType<ILintingEngine>(); |
| 65 | + serviceManager.addSingletonInstance<ILintingEngine>(ILintingEngine, engine.object); |
| 66 | + |
| 67 | + docManager = TypeMoq.Mock.ofType<IDocumentManager>(); |
| 68 | + serviceManager.addSingletonInstance<IDocumentManager>(IDocumentManager, docManager.object); |
| 69 | + |
| 70 | + const lintSettings = TypeMoq.Mock.ofType<ILintingSettings>(); |
| 71 | + lintSettings.setup(x => x.enabled).returns(() => true); |
| 72 | + lintSettings.setup(x => x.lintOnSave).returns(() => true); |
| 73 | + |
| 74 | + settings = TypeMoq.Mock.ofType<IPythonSettings>(); |
| 75 | + settings.setup(x => x.linting).returns(() => lintSettings.object); |
| 76 | + |
| 77 | + configService = TypeMoq.Mock.ofType<IConfigurationService>(); |
| 78 | + configService.setup(x => x.getSettings(TypeMoq.It.isAny())).returns(() => settings.object); |
| 79 | + serviceManager.addSingletonInstance<IConfigurationService>(IConfigurationService, configService.object); |
| 80 | + |
| 81 | + workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>(); |
| 82 | + serviceManager.addSingletonInstance<IWorkspaceService>(IWorkspaceService, workspaceService.object); |
| 83 | + |
| 84 | + const logger = TypeMoq.Mock.ofType<ILogger>(); |
| 85 | + serviceManager.addSingletonInstance<ILogger>(ILogger, logger.object); |
| 86 | + |
| 87 | + const installer = TypeMoq.Mock.ofType<IInstaller>(); |
| 88 | + serviceManager.addSingletonInstance<IInstaller>(IInstaller, installer.object); |
| 89 | + |
| 90 | + const platformService = TypeMoq.Mock.ofType<IPlatformService>(); |
| 91 | + serviceManager.addSingletonInstance<IPlatformService>(IPlatformService, platformService.object); |
| 92 | + |
| 93 | + lm = new LinterManager(serviceContainer); |
| 94 | + serviceManager.addSingletonInstance<ILinterManager>(ILinterManager, lm); |
| 95 | + document = TypeMoq.Mock.ofType<TextDocument>(); |
| 96 | + }); |
| 97 | + |
| 98 | + async function testLinter(linter: BaseLinter, fileUri: Uri, expectedArgs: string[]) { |
| 99 | + document.setup(d => d.uri).returns(() => fileUri); |
| 100 | + |
| 101 | + let invoked = false; |
| 102 | + (linter as any).run = (args, doc, token) => { |
| 103 | + expect(args).to.deep.equal(expectedArgs); |
| 104 | + invoked = true; |
| 105 | + return Promise.resolve([]); |
| 106 | + }; |
| 107 | + await linter.lint(document.object, cancellationToken); |
| 108 | + expect(invoked).to.be.equal(true, 'method not invoked'); |
| 109 | + } |
| 110 | + [Uri.file(path.join('users', 'development path to', 'one.py')), Uri.file(path.join('users', 'development', 'one.py'))].forEach(fileUri => { |
| 111 | + test(`Flake8 (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 112 | + const linter = new Flake8(outputChannel.object, serviceContainer); |
| 113 | + const expectedArgs = ['--format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s', fileUri.fsPath.fileToCommandArgument()]; |
| 114 | + await testLinter(linter, fileUri, expectedArgs); |
| 115 | + }); |
| 116 | + test(`Pep8 (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 117 | + const linter = new Pep8(outputChannel.object, serviceContainer); |
| 118 | + const expectedArgs = ['--format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s', fileUri.fsPath.fileToCommandArgument()]; |
| 119 | + await testLinter(linter, fileUri, expectedArgs); |
| 120 | + }); |
| 121 | + test(`Prospector (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 122 | + const linter = new Prospector(outputChannel.object, serviceContainer); |
| 123 | + const expectedArgs = ['--absolute-paths', '--output-format=json', fileUri.fsPath.fileToCommandArgument()]; |
| 124 | + await testLinter(linter, fileUri, expectedArgs); |
| 125 | + }); |
| 126 | + test(`Pylama (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 127 | + const linter = new PyLama(outputChannel.object, serviceContainer); |
| 128 | + const expectedArgs = ['--format=parsable', fileUri.fsPath.fileToCommandArgument()]; |
| 129 | + await testLinter(linter, fileUri, expectedArgs); |
| 130 | + }); |
| 131 | + test(`MyPy (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 132 | + const linter = new MyPy(outputChannel.object, serviceContainer); |
| 133 | + const expectedArgs = [fileUri.fsPath.fileToCommandArgument()]; |
| 134 | + await testLinter(linter, fileUri, expectedArgs); |
| 135 | + }); |
| 136 | + test(`Pydocstyle (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 137 | + const linter = new PyDocStyle(outputChannel.object, serviceContainer); |
| 138 | + const expectedArgs = [fileUri.fsPath.fileToCommandArgument()]; |
| 139 | + await testLinter(linter, fileUri, expectedArgs); |
| 140 | + }); |
| 141 | + test(`Pylint (${fileUri.fsPath.indexOf(' ') > 0 ? 'with spaces' : 'without spaces'})`, async () => { |
| 142 | + const linter = new Pylint(outputChannel.object, serviceContainer); |
| 143 | + document.setup(d => d.uri).returns(() => fileUri); |
| 144 | + |
| 145 | + let invoked = false; |
| 146 | + (linter as any).run = (args, doc, token) => { |
| 147 | + expect(args[args.length - 1]).to.equal(fileUri.fsPath.fileToCommandArgument()); |
| 148 | + invoked = true; |
| 149 | + return Promise.resolve([]); |
| 150 | + }; |
| 151 | + await linter.lint(document.object, cancellationToken); |
| 152 | + expect(invoked).to.be.equal(true, 'method not invoked'); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments