|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, injectable, named } from 'inversify'; |
| 7 | +import * as path from 'path'; |
| 8 | +import { DiagnosticSeverity } from 'vscode'; |
| 9 | +import { IApplicationEnvironment, IWorkspaceService } from '../../../common/application/types'; |
| 10 | +import { IFileSystem } from '../../../common/platform/types'; |
| 11 | +import { IDisposableRegistry, IPersistentState, IPersistentStateFactory, Resource } from '../../../common/types'; |
| 12 | +import { swallowExceptions } from '../../../common/utils/decorators'; |
| 13 | +import { Common, Diagnostics } from '../../../common/utils/localize'; |
| 14 | +import { IServiceContainer } from '../../../ioc/types'; |
| 15 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 16 | +import { IDiagnosticsCommandFactory } from '../commands/types'; |
| 17 | +import { DiagnosticCodes } from '../constants'; |
| 18 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 19 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 20 | + |
| 21 | +export class InvalidTestSettingsDiagnostic extends BaseDiagnostic { |
| 22 | + constructor() { |
| 23 | + super( |
| 24 | + DiagnosticCodes.InvalidTestSettingDiagnostic, |
| 25 | + Diagnostics.invalidTestSettings(), |
| 26 | + DiagnosticSeverity.Error, |
| 27 | + DiagnosticScope.WorkspaceFolder, |
| 28 | + undefined, |
| 29 | + 'always' |
| 30 | + ); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export const InvalidTestSettingsDiagnosticscServiceId = 'InvalidTestSettingsDiagnosticscServiceId'; |
| 35 | + |
| 36 | +@injectable() |
| 37 | +export class InvalidTestSettingDiagnosticsService extends BaseDiagnosticsService { |
| 38 | + protected readonly stateStore: IPersistentState<string[]>; |
| 39 | + private readonly handledWorkspaces: Set<string>; |
| 40 | + constructor(@inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 41 | + @inject(IFileSystem) private readonly fs: IFileSystem, |
| 42 | + @inject(IApplicationEnvironment) private readonly application: IApplicationEnvironment, |
| 43 | + @inject(IPersistentStateFactory) stateFactory: IPersistentStateFactory, |
| 44 | + @inject(IDiagnosticHandlerService) @named(DiagnosticCommandPromptHandlerServiceId) private readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>, |
| 45 | + @inject(IDiagnosticsCommandFactory) private readonly commandFactory: IDiagnosticsCommandFactory, |
| 46 | + @inject(IWorkspaceService) private readonly workspace: IWorkspaceService, |
| 47 | + @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry) { |
| 48 | + super([DiagnosticCodes.InvalidEnvironmentPathVariableDiagnostic], serviceContainer, disposableRegistry, true); |
| 49 | + this.stateStore = stateFactory.createGlobalPersistentState<string[]>('python.unitTest.Settings', []); |
| 50 | + this.handledWorkspaces = new Set<string>(); |
| 51 | + } |
| 52 | + public async diagnose(resource: Resource): Promise<IDiagnostic[]> { |
| 53 | + if (!this.shouldHandleResource(resource)) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + const filesToBeFixed = await this.getFilesToBeFixed(); |
| 57 | + if (filesToBeFixed.length === 0) { |
| 58 | + return []; |
| 59 | + } else { |
| 60 | + return [new InvalidTestSettingsDiagnostic()]; |
| 61 | + } |
| 62 | + } |
| 63 | + public async onHandle(diagnostics: IDiagnostic[]): Promise<void> { |
| 64 | + // This class can only handle one type of diagnostic, hence just use first item in list. |
| 65 | + if (diagnostics.length === 0 || !this.canHandle(diagnostics[0]) || |
| 66 | + !(diagnostics[0] instanceof InvalidTestSettingsDiagnostic)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + const diagnostic = diagnostics[0]; |
| 70 | + const options = [ |
| 71 | + { |
| 72 | + prompt: Diagnostics.updateSettings(), |
| 73 | + command: { |
| 74 | + diagnostic, |
| 75 | + invoke: async (): Promise<void> => { |
| 76 | + const filesToBeFixed = await this.getFilesToBeFixed(); |
| 77 | + await Promise.all(filesToBeFixed.map(file => this.fixSettingInFile(file))); |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | + { prompt: Common.noIWillDoItLater() }, |
| 82 | + { |
| 83 | + prompt: Common.doNotShowAgain(), |
| 84 | + command: this.commandFactory.createCommand(diagnostic, { type: 'ignore', options: DiagnosticScope.Global }) |
| 85 | + } |
| 86 | + ]; |
| 87 | + |
| 88 | + await this.messageService.handle(diagnostic, { commandPrompts: options }); |
| 89 | + } |
| 90 | + public getSettingsFiles() { |
| 91 | + if (!this.workspace.hasWorkspaceFolders) { |
| 92 | + return this.application.userSettingsFile ? [this.application.userSettingsFile] : []; |
| 93 | + } |
| 94 | + return this.workspace.workspaceFolders! |
| 95 | + .map(item => path.join(item.uri.fsPath, '.vscode', 'settings.json')) |
| 96 | + .concat(this.application.userSettingsFile ? [this.application.userSettingsFile] : []); |
| 97 | + } |
| 98 | + public async getFilesToBeFixed() { |
| 99 | + const files = this.getSettingsFiles(); |
| 100 | + const result = await Promise.all(files.map(async file => { |
| 101 | + const needsFixing = await this.doesFileNeedToBeFixed(file); |
| 102 | + return { file, needsFixing }; |
| 103 | + })); |
| 104 | + return result.filter(item => item.needsFixing).map(item => item.file); |
| 105 | + } |
| 106 | + @swallowExceptions('Failed to update settings.json') |
| 107 | + public async fixSettingInFile(filePath: string) { |
| 108 | + const fileContents = await this.fs.readFile(filePath); |
| 109 | + const setting = new RegExp('"python.unitTest', 'g'); |
| 110 | + |
| 111 | + await this.fs.writeFile(filePath, fileContents.replace(setting, '"python.testing')); |
| 112 | + |
| 113 | + // Keep track of updated file. |
| 114 | + this.stateStore.value.push(filePath); |
| 115 | + await this.stateStore.updateValue(this.stateStore.value.slice()); |
| 116 | + } |
| 117 | + @swallowExceptions('Failed to check if file needs to be fixed') |
| 118 | + private async doesFileNeedToBeFixed(filePath: string) { |
| 119 | + // If we have fixed the path to this file once before, |
| 120 | + // then no need to check agian. If user adds subsequently, nothing we can do, |
| 121 | + // as user will see warnings in editor about invalid entries. |
| 122 | + // This will speed up loading of extension (reduce unwanted disc IO). |
| 123 | + if (this.stateStore.value.indexOf(filePath) >= 0) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + const contents = await this.fs.readFile(filePath); |
| 127 | + return contents.indexOf('python.unitTest.') > 0; |
| 128 | + } |
| 129 | + /** |
| 130 | + * Checks whether to handle a particular workspace resource. |
| 131 | + * If required, we'll track that resource to ensure we don't handle it again. |
| 132 | + * This is necessary for multi-root workspaces. |
| 133 | + * |
| 134 | + * @param {Resource} resource |
| 135 | + * @returns {boolean} |
| 136 | + * @memberof InvalidTestSettingDiagnosticsService |
| 137 | + */ |
| 138 | + private shouldHandleResource(resource: Resource): boolean { |
| 139 | + const folder = this.workspace.getWorkspaceFolder(resource); |
| 140 | + |
| 141 | + if (!folder || !resource || !this.workspace.hasWorkspaceFolders) { |
| 142 | + if (this.handledWorkspaces.has('')) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + this.handledWorkspaces.add(''); |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + if (this.handledWorkspaces.has(folder.uri.fsPath)) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + this.handledWorkspaces.add(folder.uri.fsPath); |
| 153 | + return true; |
| 154 | + } |
| 155 | +} |
0 commit comments