|  | 
|  | 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. | 
|  | 2 | +// Licensed under the MIT License. | 
|  | 3 | + | 
|  | 4 | +'use strict'; | 
|  | 5 | + | 
|  | 6 | +import { expect } from 'chai'; | 
|  | 7 | +import * as fs from 'fs-extra'; | 
|  | 8 | +import * as path from 'path'; | 
|  | 9 | +import * as vscode from 'vscode'; | 
|  | 10 | +import { openFile } from '../../../common'; | 
|  | 11 | +import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants'; | 
|  | 12 | +import { closeActiveWindows, initialize, initializeTest, IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../../../initialize'; | 
|  | 13 | +import { DebuggerFixture } from '../../utils'; | 
|  | 14 | + | 
|  | 15 | +const WS_ROOT = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'test'); | 
|  | 16 | + | 
|  | 17 | +function resolveWSFile(wsRoot: string, ...filePath: string[]): string { | 
|  | 18 | + return path.join(wsRoot, ...filePath); | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +suite('Debugger Integration', () => { | 
|  | 22 | + const file = resolveWSFile(WS_ROOT, 'pythonFiles', 'debugging', 'wait_for_file.py'); | 
|  | 23 | + const doneFile = resolveWSFile(WS_ROOT, 'should-not-exist'); | 
|  | 24 | + const outFile = resolveWSFile(WS_ROOT, 'output.txt'); | 
|  | 25 | + const resource = vscode.Uri.file(file); | 
|  | 26 | + const defaultScriptArgs = [doneFile]; | 
|  | 27 | + let workspaceRoot: vscode.WorkspaceFolder; | 
|  | 28 | + let fix: DebuggerFixture; | 
|  | 29 | + suiteSetup(async function () { | 
|  | 30 | + if (IS_MULTI_ROOT_TEST || !TEST_DEBUGGER) { | 
|  | 31 | + // tslint:disable-next-line:no-invalid-this | 
|  | 32 | + this.skip(); | 
|  | 33 | + } | 
|  | 34 | + await initialize(); | 
|  | 35 | + const ws = vscode.workspace.getWorkspaceFolder(resource); | 
|  | 36 | + workspaceRoot = ws!; | 
|  | 37 | + expect(workspaceRoot).to.not.equal(undefined, 'missing workspace root'); | 
|  | 38 | + }); | 
|  | 39 | + setup(async () => { | 
|  | 40 | + fix = new DebuggerFixture(); | 
|  | 41 | + await initializeTest(); | 
|  | 42 | + await openFile(file); | 
|  | 43 | + }); | 
|  | 44 | + teardown(async () => { | 
|  | 45 | + await fix.cleanUp(); | 
|  | 46 | + fix.addFSCleanup(outFile); | 
|  | 47 | + await closeActiveWindows(); | 
|  | 48 | + }); | 
|  | 49 | + async function setDone() { | 
|  | 50 | + await fs.writeFile(doneFile, ''); | 
|  | 51 | + fix.addFSCleanup(doneFile); | 
|  | 52 | + } | 
|  | 53 | + | 
|  | 54 | + type ConfigName = string; | 
|  | 55 | + type ScriptArgs = string[]; | 
|  | 56 | + const tests: { [key: string]: [ConfigName, ScriptArgs] } = { | 
|  | 57 | + // prettier-ignore | 
|  | 58 | + 'launch': ['launch a file', [...defaultScriptArgs, outFile]], | 
|  | 59 | + // prettier-ignore | 
|  | 60 | + 'attach': ['attach to a local port', defaultScriptArgs], | 
|  | 61 | + 'attach to PID': ['attach to a local PID', defaultScriptArgs] | 
|  | 62 | + // For now we do not worry about "test" debugging. | 
|  | 63 | + }; | 
|  | 64 | + | 
|  | 65 | + suite('run to end', () => { | 
|  | 66 | + for (const kind of Object.keys(tests)) { | 
|  | 67 | + if (kind === 'attach to PID') { | 
|  | 68 | + // Attach-to-pid is still a little finicky | 
|  | 69 | + // so we're skipping it for now. | 
|  | 70 | + continue; | 
|  | 71 | + } | 
|  | 72 | + const [configName, scriptArgs] = tests[kind]; | 
|  | 73 | + test(kind, async () => { | 
|  | 74 | + const session = fix.resolveDebugger(configName, file, scriptArgs, workspaceRoot); | 
|  | 75 | + await session.start(); | 
|  | 76 | + // Any debugger ops would go here. | 
|  | 77 | + await new Promise((r) => setTimeout(r, 300)); // 0.3 seconds | 
|  | 78 | + await setDone(); | 
|  | 79 | + const result = await session.waitUntilDone(); | 
|  | 80 | + | 
|  | 81 | + expect(result.exitCode).to.equal(0, 'bad exit code'); | 
|  | 82 | + const output = result.stdout !== '' ? result.stdout : fs.readFileSync(outFile).toString(); | 
|  | 83 | + expect(output.trim().endsWith('done!')).to.equal(true, `bad output\n${output}`); | 
|  | 84 | + }); | 
|  | 85 | + } | 
|  | 86 | + }); | 
|  | 87 | + | 
|  | 88 | + suite('handles breakpoint', () => { | 
|  | 89 | + for (const kind of ['launch', 'attach']) { | 
|  | 90 | + if (kind === 'attach') { | 
|  | 91 | + // The test isn't working quite right for attach | 
|  | 92 | + // so we skip it for now. | 
|  | 93 | + continue; | 
|  | 94 | + } | 
|  | 95 | + const [configName, scriptArgs] = tests[kind]; | 
|  | 96 | + test(kind, async () => { | 
|  | 97 | + const session = fix.resolveDebugger(configName, file, scriptArgs, workspaceRoot); | 
|  | 98 | + const bp = session.addBreakpoint(file, 21); // line: "time.sleep()" | 
|  | 99 | + await session.start(); | 
|  | 100 | + await session.waitForBreakpoint(bp); | 
|  | 101 | + await setDone(); | 
|  | 102 | + const result = await session.waitUntilDone(); | 
|  | 103 | + | 
|  | 104 | + expect(result.exitCode).to.equal(0, 'bad exit code'); | 
|  | 105 | + const output = result.stdout !== '' ? result.stdout : fs.readFileSync(outFile).toString(); | 
|  | 106 | + expect(output.trim().endsWith('done!')).to.equal(true, `bad output\n${output}`); | 
|  | 107 | + }); | 
|  | 108 | + } | 
|  | 109 | + }); | 
|  | 110 | +}); | 
0 commit comments