|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as path from 'path'; |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import * as fsapi from 'fs-extra'; |
| 7 | +import { EXTENSION_ROOT_DIR } from '../../../../common/constants'; |
| 8 | +import { assert } from 'chai'; |
| 9 | + |
| 10 | +const TEST_PROJECT_DIR = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'ts_tests', 'test_data', 'project'); |
| 11 | +const TIMEOUT = 120000; // 120 seconds |
| 12 | + |
| 13 | +suite('Smoke Tests', function () { |
| 14 | + this.timeout(TIMEOUT); |
| 15 | + |
| 16 | + let disposables: vscode.Disposable[] = []; |
| 17 | + |
| 18 | + setup(async () => { |
| 19 | + disposables = []; |
| 20 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 21 | + }); |
| 22 | + |
| 23 | + teardown(async () => { |
| 24 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 25 | + |
| 26 | + disposables.forEach((d) => d.dispose()); |
| 27 | + disposables = []; |
| 28 | + }); |
| 29 | + |
| 30 | + async function ensurePythonExt(activate?: boolean): Promise<void> { |
| 31 | + const pythonExt = vscode.extensions.getExtension('ms-python.python'); |
| 32 | + assert.ok(pythonExt, 'Python Extension not found'); |
| 33 | + if (activate) { |
| 34 | + await pythonExt?.activate(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + async function ensureBlackExt(activate?: boolean): Promise<void> { |
| 39 | + const extension = vscode.extensions.getExtension('ms-python.black-formatter'); |
| 40 | + assert.ok(extension, 'Black Formatter Extension not found'); |
| 41 | + if (activate) { |
| 42 | + await extension?.activate(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + test('Ensure Black Formatter Extension loads', async () => { |
| 47 | + await vscode.workspace.openTextDocument(path.join(TEST_PROJECT_DIR, 'myscript.py')); |
| 48 | + |
| 49 | + await ensurePythonExt(true); |
| 50 | + await ensureBlackExt(false); |
| 51 | + |
| 52 | + const extension = vscode.extensions.getExtension('ms-python.black-formatter'); |
| 53 | + if (extension) { |
| 54 | + let timeout = TIMEOUT; |
| 55 | + while (!extension.isActive && timeout > 0) { |
| 56 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 57 | + timeout -= 100; |
| 58 | + } |
| 59 | + assert.ok(extension.isActive, `Extension not activated in ${TIMEOUT / 1000} seconds`); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + test('Ensure Black Formatter formats a file on save', async () => { |
| 64 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 65 | + await ensurePythonExt(true); |
| 66 | + const scriptPath = path.join(TEST_PROJECT_DIR, 'myscript.py'); |
| 67 | + |
| 68 | + const unformatted = await fsapi.readFile(path.join(TEST_PROJECT_DIR, 'myscript.unformatted'), { |
| 69 | + encoding: 'utf8', |
| 70 | + }); |
| 71 | + const formatted = await fsapi.readFile(path.join(TEST_PROJECT_DIR, 'myscript.formatted'), { encoding: 'utf8' }); |
| 72 | + await fsapi.writeFile(scriptPath, unformatted, { encoding: 'utf8' }); |
| 73 | + |
| 74 | + await ensureBlackExt(true); |
| 75 | + |
| 76 | + const doc = await vscode.workspace.openTextDocument(scriptPath); |
| 77 | + await vscode.window.showTextDocument(doc); |
| 78 | + |
| 79 | + const editor = vscode.window.activeTextEditor; |
| 80 | + assert.ok(editor, 'No active editor'); |
| 81 | + assert.ok(editor?.document.uri.fsPath.endsWith('myscript.py'), 'Active editor is not myscript.py'); |
| 82 | + |
| 83 | + const formatDone = new Promise<void>((resolve) => { |
| 84 | + const watcher = vscode.workspace.createFileSystemWatcher( |
| 85 | + new vscode.RelativePattern(TEST_PROJECT_DIR, 'myscript.py'), |
| 86 | + true, // We don't need create events |
| 87 | + false, // We need change events |
| 88 | + true, // We don't need delete events |
| 89 | + ); |
| 90 | + disposables.push( |
| 91 | + watcher, |
| 92 | + watcher.onDidChange((e) => { |
| 93 | + const text = fsapi.readFileSync(e.fsPath, { encoding: 'utf8' }); |
| 94 | + if (!text.includes(';')) { |
| 95 | + console.log('Saved with format changes'); |
| 96 | + resolve(); |
| 97 | + } else { |
| 98 | + console.log('Saved without format changes'); |
| 99 | + } |
| 100 | + }), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + const timer = setInterval(() => { |
| 105 | + console.log('Saving file'); |
| 106 | + vscode.commands.executeCommand('workbench.action.files.save'); |
| 107 | + }, 1000); |
| 108 | + disposables.push({ dispose: () => clearInterval(timer) }); |
| 109 | + |
| 110 | + await vscode.commands.executeCommand('workbench.action.files.save'); |
| 111 | + await formatDone; |
| 112 | + const actualText = await fsapi.readFile(scriptPath, { encoding: 'utf8' }); |
| 113 | + assert.equal(actualText, formatted); |
| 114 | + |
| 115 | + //cleanup |
| 116 | + await fsapi.writeFile(scriptPath, '', { encoding: 'utf8' }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments