Skip to content

Commit 39d4708

Browse files
committed
jupyter unittests
1 parent fd3508a commit 39d4708

2 files changed

Lines changed: 143 additions & 25 deletions

File tree

‎pythonFiles/PythonTools/ipythonServer.py‎

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,30 @@
4949
except ImportError:
5050
from queue import Empty # Python 3
5151

52+
DEBUG = os.environ.get('DEBUG_DJAYAMANNE_IPYTHON') is not None
53+
TEST = os.environ.get('TEST_DJAYAMANNE_IPYTHON') is not None
54+
TEST = True
5255
# The great "support IPython 2, 3, 4" strat begins
53-
try:
54-
import jupyter
55-
except ImportError:
56-
jupyter_era = False
57-
else:
58-
jupyter_era = True
59-
60-
if jupyter_era:
61-
# Jupyter / IPython 4.x
62-
from jupyter_client import KernelManager
63-
from jupyter_client.kernelspec import KernelSpecManager
64-
kernelSpecManager = KernelSpecManager()
65-
else:
66-
from IPython.kernel import KernelManager
67-
from IPython.kernel.kernelspec import KernelSpecManager
68-
kernelSpecManager = KernelSpecManager()
56+
if not TEST:
57+
try:
58+
import jupyter
59+
except ImportError:
60+
jupyter_era = False
61+
else:
62+
jupyter_era = True
63+
64+
if jupyter_era:
65+
# Jupyter / IPython 4.x
66+
from jupyter_client import KernelManager
67+
from jupyter_client.kernelspec import KernelSpecManager
68+
kernelSpecManager = KernelSpecManager()
69+
else:
70+
from IPython.kernel import KernelManager
71+
from IPython.kernel.kernelspec import KernelSpecManager
72+
kernelSpecManager = KernelSpecManager()
6973

7074
# End of the great "support IPython 2, 3, 4" strat
7175

72-
73-
DEBUG = os.environ.get('DEBUG_DJAYAMANNE_IPYTHON') is not None
74-
75-
7676
def _debug_write(out):
7777
if DEBUG:
7878
sys.__stdout__.write(out)
@@ -114,6 +114,7 @@ def acquire(self):
114114
def release(self):
115115
self.lock.release()
116116

117+
117118
class iPythonSocketServer(object):
118119
"""back end for executing REPL code. This base class handles all of the
119120
communication with the remote process while derived classes implement the
@@ -124,6 +125,7 @@ class iPythonSocketServer(object):
124125
_EXIT = to_bytes('EXIT')
125126
_LSTK = to_bytes('LSTK')
126127
_EROR = to_bytes('EROR')
128+
_TEST = to_bytes('TEST')
127129

128130
def __init__(self):
129131
import threading
@@ -183,9 +185,15 @@ def start_processing(self):
183185
try:
184186
if iPythonSocketServer._COMMANDS_WITH_IDS.get(inp) == True:
185187
id = read_string(self.conn)
186-
cmd(self, id)
188+
if TEST:
189+
self.replyWithError(inp, id)
190+
else:
191+
cmd(self, id)
187192
else:
188-
cmd(self)
193+
if TEST:
194+
self.replyWithError(inp, id)
195+
else:
196+
cmd(self)
189197
except:
190198
self.replyWithError(inp, id)
191199
else:
@@ -216,13 +224,21 @@ def start_processing(self):
216224
def check_for_exit_socket_loop(self):
217225
return self.exit_requested
218226

219-
def replyWithError(self, cmd, id):
227+
def replyForTest(self, commandName, id):
228+
message = read_string(self.conn)
229+
with self.send_lock:
230+
_debug_write('Test response for Command: ' + commandName)
231+
write_bytes(self.conn, iPythonSocketServer._TEST)
232+
write_string(self.conn, commandName)
233+
write_string(self.conn, "" if id is None else id)
234+
write_string(self.conn, message)
235+
236+
def replyWithError(self, commandName, id):
220237
with self.send_lock:
221238
_debug_write('Replying with error')
222239
write_bytes(self.conn, iPythonSocketServer._EROR)
223-
write_string(self.conn, cmd)
240+
write_string(self.conn, commandName)
224241
write_string(self.conn, "" if id is None else id)
225-
write_string(self.conn, str(traceback.format_exc()))
226242

227243
def _cmd_exit(self):
228244
"""exits the interactive process"""
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
// // Note: This example test is leveraging the Mocha test framework.
3+
// // Please refer to their documentation on https://mochajs.org/ for help.
4+
5+
6+
// // The module 'assert' provides assertion methods from node
7+
// import * as assert from 'assert';
8+
9+
// // You can import and use all API from the 'vscode' module
10+
// // as well as import your extension to test it
11+
// import * as vscode from 'vscode';
12+
// import * as path from 'path';
13+
// import * as settings from '../client/common/configSettings';
14+
// import * as fs from 'fs-extra';
15+
// import { initialize } from './initialize';
16+
// import { execPythonFile } from '../client/common/utils';
17+
18+
// import { iPythonAdapter } from '../client/jupyter/comms/ipythonAdapter';
19+
// import { SocketServer } from '../client/common/comms/socketServer';
20+
// import * as child_process from 'child_process';
21+
// import { createDeferred } from '../client/common/helpers';
22+
23+
// let pythonSettings = settings.PythonSettings.getInstance();
24+
// let ch = vscode.window.createOutputChannel('Tests');
25+
// let outputChannel = ch;
26+
// let pythoFilesPath = path.join(__dirname, '..', '..', 'src', 'test', 'pythonFiles', 'formatting');
27+
28+
// let childProc: child_process.ChildProcess;
29+
// let socketServer: SocketServer;
30+
// let ipythonAdapter: iPythonAdapter;
31+
32+
// suiteSetup(done => {
33+
// initialize().then(() => {
34+
// const pyFile = path.join(__dirname, '..', '..', '..', '..', 'pythonFiles', 'PythonTools', 'ipythonServer.py');
35+
// socketServer = new SocketServer();
36+
// ipythonAdapter = new iPythonAdapter(this.socketServer);
37+
// socketServer.Start().then(port => {
38+
// const def = createDeferred<any>();
39+
// // We want to test this
40+
// const newEnv = { "TEST_DJAYAMANNE_IPYTHON": "1" };
41+
// Object.assign(newEnv, process.env);
42+
43+
// // Any version of python will do, even without jupyter or ipython
44+
// childProc = child_process.spawn('python', [pyFile, port.toString()], { env: newEnv });
45+
// childProc.stdout.setEncoding('utf8');
46+
// childProc.stderr.setEncoding('utf8');
47+
48+
// childProc.stdout.on('data', (data: string) => {
49+
// outputChannel.append(data);
50+
// });
51+
// childProc.stderr.on('data', (data: string) => {
52+
// outputChannel.append(data);
53+
// });
54+
// done();
55+
56+
// // setTimeout(() => {
57+
// // //this.socketServer.
58+
// // this.ipythonAdapter.ping();
59+
// // this.ipythonAdapter.listKernels().then(x => {
60+
// // const y = x;
61+
// // }, reason => {
62+
// // const y = reason;
63+
// // })
64+
// // }, 1000);
65+
// });
66+
// }, done);
67+
// });
68+
69+
// suiteTeardown(() => {
70+
// });
71+
72+
// suite('Formatting', () => {
73+
// teardown(() => {
74+
// if (vscode.window.activeTextEditor) {
75+
// return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
76+
// }
77+
// });
78+
// function testFormatting(formatter: AutoPep8Formatter | YapfFormatter, formattedContents: string, fileToFormat: string): PromiseLike<void> {
79+
// let textEditor: vscode.TextEditor;
80+
// let textDocument: vscode.TextDocument;
81+
// return vscode.workspace.openTextDocument(fileToFormat).then(document => {
82+
// textDocument = document;
83+
// return vscode.window.showTextDocument(textDocument);
84+
// }).then(editor => {
85+
// textEditor = editor;
86+
// return formatter.formatDocument(textDocument, null, null);
87+
// }).then(edits => {
88+
// return textEditor.edit(editBuilder => {
89+
// edits.forEach(edit => editBuilder.replace(edit.range, edit.newText));
90+
// });
91+
// }).then(edited => {
92+
// assert.equal(textEditor.document.getText(), formattedContents, 'Formatted text is not the same');
93+
// });
94+
// }
95+
// test('AutoPep8', done => {
96+
// testFormatting(new AutoPep8Formatter(ch, pythonSettings, pythoFilesPath), formattedAutoPep8, autoPep8FileToFormat).then(done, done);
97+
// });
98+
99+
// test('Yapf', done => {
100+
// testFormatting(new YapfFormatter(ch, pythonSettings, pythoFilesPath), formattedYapf, yapfFileToFormat).then(done, done);
101+
// });
102+
// });

0 commit comments

Comments
 (0)