Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sendText to send Python code to Terminal REPL for Python >= 3.13 #24765

Merged
merged 15 commits into from
Feb 4, 2025
11 changes: 10 additions & 1 deletion src/client/common/terminal/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useEnvExtension } from '../../envExt/api.internal';
import { ensureTerminalLegacy } from '../../envExt/api.legacy';
import { sleep } from '../utils/async';
import { isWindows } from '../utils/platform';
import { getPythonMinorVersion } from '../../repl/replUtils';

@injectable()
export class TerminalService implements ITerminalService, Disposable {
Expand Down Expand Up @@ -108,7 +109,15 @@ export class TerminalService implements ITerminalService, Disposable {

const config = getConfiguration('python');
const pythonrcSetting = config.get<boolean>('terminal.shellIntegration.enabled');
if ((isPythonShell && !pythonrcSetting) || (isPythonShell && isWindows())) {

const minorVersion = this.options?.resource
? await getPythonMinorVersion(
this.options.resource,
this.serviceContainer.get<IInterpreterService>(IInterpreterService),
)
: undefined;

if ((isPythonShell && !pythonrcSetting) || (isPythonShell && isWindows()) || (minorVersion ?? 0) >= 13) {
// If user has explicitly disabled SI for Python, use sendText for inside Terminal REPL.
terminal.sendText(commandLine);
return undefined;
Expand Down
14 changes: 14 additions & 0 deletions src/client/repl/replUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,17 @@ export function getTabNameForUri(uri: Uri): string | undefined {

return undefined;
}

/**
* Function that will return the minor version of current active Python interpreter.
*/
export async function getPythonMinorVersion(
uri: Uri | undefined,
interpreterService: IInterpreterService,
): Promise<number | undefined> {
if (uri) {
const pythonVersion = await getActiveInterpreter(uri, interpreterService);
return pythonVersion?.version?.minor;
}
return undefined;
}
53 changes: 51 additions & 2 deletions src/test/common/terminals/service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@ import {
TerminalShellExecution,
TerminalShellExecutionEndEvent,
TerminalShellIntegration,
Uri,
Terminal as VSCodeTerminal,
WorkspaceConfiguration,
} from 'vscode';
import { ITerminalManager, IWorkspaceService } from '../../../client/common/application/types';
import { EXTENSION_ROOT_DIR } from '../../../client/common/constants';
import { IPlatformService } from '../../../client/common/platform/types';
import { TerminalService } from '../../../client/common/terminal/service';
import { ITerminalActivator, ITerminalHelper, TerminalShellType } from '../../../client/common/terminal/types';
import {
ITerminalActivator,
ITerminalHelper,
TerminalCreationOptions,
TerminalShellType,
} from '../../../client/common/terminal/types';
import { IDisposableRegistry } from '../../../client/common/types';
import { IServiceContainer } from '../../../client/ioc/types';
import { ITerminalAutoActivation } from '../../../client/terminals/types';
import { createPythonInterpreter } from '../../utils/interpreters';
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis';
import * as platform from '../../../client/common/utils/platform';
import * as extapi from '../../../client/envExt/api.internal';
import { IInterpreterService } from '../../../client/interpreter/contracts';
import { PythonEnvironment } from '../../../client/pythonEnvironments/info';

suite('Terminal Service', () => {
let service: TerminalService;
Expand All @@ -46,6 +54,8 @@ suite('Terminal Service', () => {
let editorConfig: TypeMoq.IMock<WorkspaceConfiguration>;
let isWindowsStub: sinon.SinonStub;
let useEnvExtensionStub: sinon.SinonStub;
let interpreterService: TypeMoq.IMock<IInterpreterService>;
let options: TypeMoq.IMock<TerminalCreationOptions>;

setup(() => {
useEnvExtensionStub = sinon.stub(extapi, 'useEnvExtension');
Expand Down Expand Up @@ -92,6 +102,13 @@ suite('Terminal Service', () => {
disposables = [];

mockServiceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
interpreterService = TypeMoq.Mock.ofType<IInterpreterService>();
interpreterService
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
.returns(() => Promise.resolve(({ path: 'ps' } as unknown) as PythonEnvironment));

options = TypeMoq.Mock.ofType<TerminalCreationOptions>();
options.setup((o) => o.resource).returns(() => Uri.parse('a'));

mockServiceContainer.setup((c) => c.get(ITerminalManager)).returns(() => terminalManager.object);
mockServiceContainer.setup((c) => c.get(ITerminalHelper)).returns(() => terminalHelper.object);
Expand All @@ -100,6 +117,7 @@ suite('Terminal Service', () => {
mockServiceContainer.setup((c) => c.get(IWorkspaceService)).returns(() => workspaceService.object);
mockServiceContainer.setup((c) => c.get(ITerminalActivator)).returns(() => terminalActivator.object);
mockServiceContainer.setup((c) => c.get(ITerminalAutoActivation)).returns(() => terminalAutoActivator.object);
mockServiceContainer.setup((c) => c.get(IInterpreterService)).returns(() => interpreterService.object);
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
isWindowsStub = sinon.stub(platform, 'isWindows');
pythonConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
Expand All @@ -117,6 +135,7 @@ suite('Terminal Service', () => {
}
disposables.filter((item) => !!item).forEach((item) => item.dispose());
sinon.restore();
interpreterService.reset();
});

test('Ensure terminal is disposed', async () => {
Expand Down Expand Up @@ -239,7 +258,7 @@ suite('Terminal Service', () => {
terminal.verify((t) => t.sendText(TypeMoq.It.isValue(textToSend)), TypeMoq.Times.exactly(1));
});

test('Ensure sendText is NOT called when Python shell integration and terminal shell integration are both enabled - Mac, Linux', async () => {
test('Ensure sendText is NOT called when Python shell integration and terminal shell integration are both enabled - Mac, Linux && Python < 3.13', async () => {
isWindowsStub.returns(false);
pythonConfig
.setup((p) => p.get('terminal.shellIntegration.enabled'))
Expand All @@ -261,6 +280,36 @@ suite('Terminal Service', () => {
terminal.verify((t) => t.sendText(TypeMoq.It.isValue(textToSend)), TypeMoq.Times.never());
});

test('Ensure sendText is called when Python shell integration and terminal shell integration are both enabled - Mac, Linux && Python >= 3.13', async () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note we will start using executeCommand again once we resolve all the Python3.13 related issue and start using Python shell integration again.

interpreterService.reset();

interpreterService
.setup((i) => i.getActiveInterpreter(TypeMoq.It.isAny()))
.returns(() =>
Promise.resolve({ path: 'yo', version: { major: 3, minor: 13, patch: 0 } } as PythonEnvironment),
);

isWindowsStub.returns(false);
pythonConfig
.setup((p) => p.get('terminal.shellIntegration.enabled'))
.returns(() => true)
.verifiable(TypeMoq.Times.once());

terminalHelper
.setup((helper) => helper.getEnvironmentActivationCommands(TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns(() => Promise.resolve(undefined));

service = new TerminalService(mockServiceContainer.object, options.object);
const textToSend = 'Some Text';
terminalHelper.setup((h) => h.identifyTerminalShell(TypeMoq.It.isAny())).returns(() => TerminalShellType.bash);
terminalManager.setup((t) => t.createTerminal(TypeMoq.It.isAny())).returns(() => terminal.object);

await service.ensureTerminal();
await service.executeCommand(textToSend, true);

terminal.verify((t) => t.sendText(TypeMoq.It.isValue(textToSend)), TypeMoq.Times.once());
});

test('Ensure sendText IS called even when Python shell integration and terminal shell integration are both enabled - Window', async () => {
isWindowsStub.returns(true);
pythonConfig
Expand Down
43 changes: 23 additions & 20 deletions src/test/terminals/codeExecution/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ import { IServiceContainer } from '../../../client/ioc/types';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
import { CodeExecutionHelper } from '../../../client/terminals/codeExecution/helper';
import { ICodeExecutionHelper } from '../../../client/terminals/types';
import { PYTHON_PATH } from '../../common';
import { PYTHON_PATH, getPythonSemVer } from '../../common';
import { ReplType } from '../../../client/repl/types';

const TEST_FILES_PATH = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'python_files', 'terminalExec');

suite('Terminal - Code Execution Helper', () => {
suite('Terminal - Code Execution Helper', async () => {
let activeResourceService: TypeMoq.IMock<IActiveResourceService>;
let documentManager: TypeMoq.IMock<IDocumentManager>;
let applicationShell: TypeMoq.IMock<IApplicationShell>;
Expand Down Expand Up @@ -234,25 +234,28 @@ suite('Terminal - Code Execution Helper', () => {
expect(normalizedCode).to.be.equal(normalizedExpected);
}

['', '1', '2', '3', '4', '5', '6', '7', '8'].forEach((fileNameSuffix) => {
test(`Ensure code is normalized (Sample${fileNameSuffix})`, async () => {
configurationService
.setup((c) => c.getSettings(TypeMoq.It.isAny()))
.returns({
REPL: {
EnableREPLSmartSend: false,
REPLSmartSend: false,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
const code = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_raw.py`), 'utf8');
const expectedCode = await fs.readFile(
path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_normalized_selection.py`),
'utf8',
);
await ensureCodeIsNormalized(code, expectedCode);
const pythonTestVersion = await getPythonSemVer();
if (pythonTestVersion && pythonTestVersion.minor < 13) {
['', '1', '2', '3', '4', '5', '6', '7', '8'].forEach((fileNameSuffix) => {
test(`Ensure code is normalized (Sample${fileNameSuffix}) - Python < 3.13`, async () => {
configurationService
.setup((c) => c.getSettings(TypeMoq.It.isAny()))
.returns({
REPL: {
EnableREPLSmartSend: false,
REPLSmartSend: false,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
const code = await fs.readFile(path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_raw.py`), 'utf8');
const expectedCode = await fs.readFile(
path.join(TEST_FILES_PATH, `sample${fileNameSuffix}_normalized_selection.py`),
'utf8',
);
await ensureCodeIsNormalized(code, expectedCode);
});
});
});
}

test("Display message if there's no active file", async () => {
documentManager.setup((doc) => doc.activeTextEditor).returns(() => undefined);
Expand Down
130 changes: 67 additions & 63 deletions src/test/terminals/codeExecution/smartSend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import { IServiceContainer } from '../../../client/ioc/types';
import { ICodeExecutionHelper } from '../../../client/terminals/types';
import { Commands, EXTENSION_ROOT_DIR } from '../../../client/common/constants';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
import { PYTHON_PATH } from '../../common';
import { PYTHON_PATH, getPythonSemVer } from '../../common';
import { Architecture } from '../../../client/common/utils/platform';
import { ProcessService } from '../../../client/common/process/proc';
import { l10n } from '../../mocks/vsc';
import { ReplType } from '../../../client/repl/types';

const TEST_FILES_PATH = path.join(EXTENSION_ROOT_DIR, 'src', 'test', 'python_files', 'terminalExec');

suite('REPL - Smart Send', () => {
suite('REPL - Smart Send', async () => {
let documentManager: TypeMoq.IMock<IDocumentManager>;
let applicationShell: TypeMoq.IMock<IApplicationShell>;

Expand Down Expand Up @@ -168,67 +168,71 @@ suite('REPL - Smart Send', () => {
commandManager.verifyAll();
});

test('Smart send should perform smart selection and move cursor', async () => {
configurationService
.setup((c) => c.getSettings(TypeMoq.It.isAny()))
.returns({
REPL: {
REPLSmartSend: true,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

const activeEditor = TypeMoq.Mock.ofType<TextEditor>();
const firstIndexPosition = new Position(0, 0);
const selection = TypeMoq.Mock.ofType<Selection>();
const wholeFileContent = await fs.readFile(path.join(TEST_FILES_PATH, `sample_smart_selection.py`), 'utf8');

selection.setup((s) => s.anchor).returns(() => firstIndexPosition);
selection.setup((s) => s.active).returns(() => firstIndexPosition);
selection.setup((s) => s.isEmpty).returns(() => true);
activeEditor.setup((e) => e.selection).returns(() => selection.object);

documentManager.setup((d) => d.activeTextEditor).returns(() => activeEditor.object);
document.setup((d) => d.getText(TypeMoq.It.isAny())).returns(() => wholeFileContent);
const actualProcessService = new ProcessService();

const { execObservable } = actualProcessService;

processService
.setup((p) => p.execObservable(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => execObservable.apply(actualProcessService, [file, args, options]));

const actualSmartOutput = await codeExecutionHelper.normalizeLines(
'my_dict = {',
ReplType.terminal,
wholeFileContent,
);

// my_dict = { <----- smart shift+enter here
// "key1": "value1",
// "key2": "value2"
// } <---- cursor should be here afterwards, hence offset 3
commandManager
.setup((c) => c.executeCommand('cursorMove', TypeMoq.It.isAny()))
.callback((_, arg2) => {
assert.deepEqual(arg2, {
to: 'down',
by: 'line',
value: 3,
});
return Promise.resolve();
})
.verifiable(TypeMoq.Times.once());

commandManager
.setup((c) => c.executeCommand('cursorEnd'))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());

const expectedSmartOutput = 'my_dict = {\n "key1": "value1",\n "key2": "value2"\n}\n';
expect(actualSmartOutput).to.be.equal(expectedSmartOutput);
commandManager.verifyAll();
});
const pythonTestVersion = await getPythonSemVer();

if (pythonTestVersion && pythonTestVersion.minor < 13) {
test('Smart send should perform smart selection and move cursor - Python < 3.13', async () => {
configurationService
.setup((c) => c.getSettings(TypeMoq.It.isAny()))
.returns({
REPL: {
REPLSmartSend: true,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

const activeEditor = TypeMoq.Mock.ofType<TextEditor>();
const firstIndexPosition = new Position(0, 0);
const selection = TypeMoq.Mock.ofType<Selection>();
const wholeFileContent = await fs.readFile(path.join(TEST_FILES_PATH, `sample_smart_selection.py`), 'utf8');

selection.setup((s) => s.anchor).returns(() => firstIndexPosition);
selection.setup((s) => s.active).returns(() => firstIndexPosition);
selection.setup((s) => s.isEmpty).returns(() => true);
activeEditor.setup((e) => e.selection).returns(() => selection.object);

documentManager.setup((d) => d.activeTextEditor).returns(() => activeEditor.object);
document.setup((d) => d.getText(TypeMoq.It.isAny())).returns(() => wholeFileContent);
const actualProcessService = new ProcessService();

const { execObservable } = actualProcessService;

processService
.setup((p) => p.execObservable(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
.returns((file, args, options) => execObservable.apply(actualProcessService, [file, args, options]));

const actualSmartOutput = await codeExecutionHelper.normalizeLines(
'my_dict = {',
ReplType.terminal,
wholeFileContent,
);

// my_dict = { <----- smart shift+enter here
// "key1": "value1",
// "key2": "value2"
// } <---- cursor should be here afterwards, hence offset 3
commandManager
.setup((c) => c.executeCommand('cursorMove', TypeMoq.It.isAny()))
.callback((_, arg2) => {
assert.deepEqual(arg2, {
to: 'down',
by: 'line',
value: 3,
});
return Promise.resolve();
})
.verifiable(TypeMoq.Times.once());

commandManager
.setup((c) => c.executeCommand('cursorEnd'))
.returns(() => Promise.resolve())
.verifiable(TypeMoq.Times.once());

const expectedSmartOutput = 'my_dict = {\n "key1": "value1",\n "key2": "value2"\n}\n';
expect(actualSmartOutput).to.be.equal(expectedSmartOutput);
commandManager.verifyAll();
});
}

// Do not perform smart selection when there is explicit selection
test('Smart send should not perform smart selection when there is explicit selection', async () => {
Expand Down