aboutsummaryrefslogtreecommitdiffstats
path: root/test/automated/python-tests.el
diff options
context:
space:
mode:
Diffstat (limited to 'test/automated/python-tests.el')
-rw-r--r--test/automated/python-tests.el269
1 files changed, 269 insertions, 0 deletions
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index b38fbdd122f..ab8eb4816d3 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -34,6 +34,21 @@ always located at the beginning of buffer."
34 (goto-char (point-min)) 34 (goto-char (point-min))
35 ,@body)) 35 ,@body))
36 36
37(defmacro python-tests-with-temp-file (contents &rest body)
38 "Create a `python-mode' enabled file with CONTENTS.
39BODY is code to be executed within the temp buffer. Point is
40always located at the beginning of buffer."
41 (declare (indent 1) (debug t))
42 `(let* ((temp-file (concat (make-temp-file "python-tests") ".py"))
43 (buffer (find-file-noselect temp-file)))
44 (unwind-protect
45 (with-current-buffer buffer
46 (python-mode)
47 (insert ,contents)
48 (goto-char (point-min))
49 ,@body)
50 (and buffer (kill-buffer buffer)))))
51
37(defun python-tests-look-at (string &optional num restore-point) 52(defun python-tests-look-at (string &optional num restore-point)
38 "Move point at beginning of STRING in the current buffer. 53 "Move point at beginning of STRING in the current buffer.
39Optional argument NUM defaults to 1 and is an integer indicating 54Optional argument NUM defaults to 1 and is an integer indicating
@@ -1161,6 +1176,260 @@ def f():
1161 1176
1162;;; Shell integration 1177;;; Shell integration
1163 1178
1179(defvar python-tests-shell-interpreter "python")
1180
1181(ert-deftest python-shell-get-process-name-1 ()
1182 "Check process name calculation on different scenarios."
1183 (python-tests-with-temp-buffer
1184 ""
1185 (should (string= (python-shell-get-process-name nil)
1186 python-shell-buffer-name))
1187 ;; When the `current-buffer' doesn't have `buffer-file-name', even
1188 ;; if dedicated flag is non-nil should not include its name.
1189 (should (string= (python-shell-get-process-name t)
1190 python-shell-buffer-name)))
1191 (python-tests-with-temp-file
1192 ""
1193 ;; `buffer-file-name' is non-nil but the dedicated flag is nil and
1194 ;; should be respected.
1195 (should (string= (python-shell-get-process-name nil)
1196 python-shell-buffer-name))
1197 (should (string=
1198 (python-shell-get-process-name t)
1199 (format "%s[%s]" python-shell-buffer-name buffer-file-name)))))
1200
1201(ert-deftest python-shell-internal-get-process-name-1 ()
1202 "Check the internal process name is config-unique."
1203 (let* ((python-shell-interpreter python-tests-shell-interpreter)
1204 (python-shell-interpreter-args "")
1205 (python-shell-prompt-regexp ">>> ")
1206 (python-shell-prompt-block-regexp "[.][.][.] ")
1207 (python-shell-setup-codes "")
1208 (python-shell-process-environment "")
1209 (python-shell-extra-pythonpaths "")
1210 (python-shell-exec-path "")
1211 (python-shell-virtualenv-path "")
1212 (expected (python-tests-with-temp-buffer
1213 "" (python-shell-internal-get-process-name))))
1214 ;; Same configurations should match.
1215 (should
1216 (string= expected
1217 (python-tests-with-temp-buffer
1218 "" (python-shell-internal-get-process-name))))
1219 (let ((python-shell-interpreter-args "-B"))
1220 ;; A minimal change should generate different names.
1221 (should
1222 (not (string=
1223 expected
1224 (python-tests-with-temp-buffer
1225 "" (python-shell-internal-get-process-name))))))))
1226
1227(ert-deftest python-shell-parse-command-1 ()
1228 "Check the command to execute is calculated correctly.
1229Using `python-shell-interpreter' and
1230`python-shell-interpreter-args'."
1231 :expected-result (if (executable-find python-tests-shell-interpreter)
1232 :passed
1233 :failed)
1234 (let ((python-shell-interpreter (executable-find
1235 python-tests-shell-interpreter))
1236 (python-shell-interpreter-args "-B"))
1237 (should (string=
1238 (format "%s %s"
1239 python-shell-interpreter
1240 python-shell-interpreter-args)
1241 (python-shell-parse-command)))))
1242
1243(ert-deftest python-shell-calculate-process-environment-1 ()
1244 "Test `python-shell-process-environment' modification."
1245 (let* ((original-process-environment process-environment)
1246 (python-shell-process-environment
1247 '("TESTVAR1=value1" "TESTVAR2=value2"))
1248 (process-environment
1249 (python-shell-calculate-process-environment)))
1250 (should (equal (getenv "TESTVAR1") "value1"))
1251 (should (equal (getenv "TESTVAR2") "value2"))))
1252
1253(ert-deftest python-shell-calculate-process-environment-2 ()
1254 "Test `python-shell-extra-pythonpaths' modification."
1255 (let* ((original-process-environment process-environment)
1256 (original-pythonpath (getenv "PYTHONPATH"))
1257 (paths '("path1" "path2"))
1258 (python-shell-extra-pythonpaths paths)
1259 (process-environment
1260 (python-shell-calculate-process-environment)))
1261 (should (equal (getenv "PYTHONPATH")
1262 (concat
1263 (mapconcat 'identity paths path-separator)
1264 path-separator original-pythonpath)))))
1265
1266(ert-deftest python-shell-calculate-process-environment-3 ()
1267 "Test `python-shell-virtualenv-path' modification."
1268 (let* ((original-process-environment process-environment)
1269 (original-path (or (getenv "PATH") ""))
1270 (python-shell-virtualenv-path
1271 (directory-file-name user-emacs-directory))
1272 (process-environment
1273 (python-shell-calculate-process-environment)))
1274 (should (not (getenv "PYTHONHOME")))
1275 (should (string= (getenv "VIRTUAL_ENV") python-shell-virtualenv-path))
1276 (should (equal (getenv "PATH")
1277 (format "%s/bin%s%s"
1278 python-shell-virtualenv-path
1279 path-separator original-path)))))
1280
1281(ert-deftest python-shell-calculate-exec-path-1 ()
1282 "Test `python-shell-exec-path' modification."
1283 (let* ((original-exec-path exec-path)
1284 (python-shell-exec-path '("path1" "path2"))
1285 (exec-path (python-shell-calculate-exec-path)))
1286 (should (equal
1287 exec-path
1288 (append python-shell-exec-path
1289 original-exec-path)))))
1290
1291(ert-deftest python-shell-calculate-exec-path-2 ()
1292 "Test `python-shell-exec-path' modification."
1293 (let* ((original-exec-path exec-path)
1294 (python-shell-virtualenv-path
1295 (directory-file-name user-emacs-directory))
1296 (exec-path (python-shell-calculate-exec-path)))
1297 (should (equal
1298 exec-path
1299 (append (cons
1300 (format "%s/bin" python-shell-virtualenv-path)
1301 original-exec-path))))))
1302
1303(ert-deftest python-shell-make-comint-1 ()
1304 "Check comint creation for global shell buffer."
1305 :expected-result (if (executable-find python-tests-shell-interpreter)
1306 :passed
1307 :failed)
1308 (let* ((python-shell-interpreter
1309 (executable-find python-tests-shell-interpreter))
1310 (proc-name (python-shell-get-process-name nil))
1311 (shell-buffer
1312 (python-tests-with-temp-buffer
1313 "" (python-shell-make-comint
1314 (python-shell-parse-command) proc-name)))
1315 (process (get-buffer-process shell-buffer)))
1316 (unwind-protect
1317 (progn
1318 (set-process-query-on-exit-flag process nil)
1319 (should (process-live-p process))
1320 (with-current-buffer shell-buffer
1321 (should (eq major-mode 'inferior-python-mode))
1322 (should (string= (buffer-name) (format "*%s*" proc-name)))))
1323 (kill-buffer shell-buffer))))
1324
1325(ert-deftest python-shell-make-comint-2 ()
1326 "Check comint creation for internal shell buffer."
1327 :expected-result (if (executable-find python-tests-shell-interpreter)
1328 :passed
1329 :failed)
1330 (let* ((python-shell-interpreter
1331 (executable-find python-tests-shell-interpreter))
1332 (proc-name (python-shell-internal-get-process-name))
1333 (shell-buffer
1334 (python-tests-with-temp-buffer
1335 "" (python-shell-make-comint
1336 (python-shell-parse-command) proc-name nil t)))
1337 (process (get-buffer-process shell-buffer)))
1338 (unwind-protect
1339 (progn
1340 (set-process-query-on-exit-flag process nil)
1341 (should (process-live-p process))
1342 (with-current-buffer shell-buffer
1343 (should (eq major-mode 'inferior-python-mode))
1344 (should (string= (buffer-name) (format " *%s*" proc-name)))))
1345 (kill-buffer shell-buffer))))
1346
1347(ert-deftest python-shell-get-process-1 ()
1348 "Check dedicated shell process preference over global."
1349 :expected-result (if (executable-find python-tests-shell-interpreter)
1350 :passed
1351 :failed)
1352 (python-tests-with-temp-file
1353 ""
1354 (let* ((python-shell-interpreter
1355 (executable-find python-tests-shell-interpreter))
1356 (global-proc-name (python-shell-get-process-name nil))
1357 (dedicated-proc-name (python-shell-get-process-name t))
1358 (global-shell-buffer
1359 (python-shell-make-comint
1360 (python-shell-parse-command) global-proc-name))
1361 (dedicated-shell-buffer
1362 (python-shell-make-comint
1363 (python-shell-parse-command) dedicated-proc-name))
1364 (global-process (get-buffer-process global-shell-buffer))
1365 (dedicated-process (get-buffer-process dedicated-shell-buffer)))
1366 (unwind-protect
1367 (progn
1368 (set-process-query-on-exit-flag global-process nil)
1369 (set-process-query-on-exit-flag dedicated-process nil)
1370 ;; Prefer dedicated if global also exists.
1371 (should (equal (python-shell-get-process) dedicated-process))
1372 (kill-buffer dedicated-shell-buffer)
1373 ;; If there's only global, use it.
1374 (should (equal (python-shell-get-process) global-process))
1375 (kill-buffer global-shell-buffer)
1376 ;; No buffer available.
1377 (should (not (python-shell-get-process))))
1378 (ignore-errors (kill-buffer global-shell-buffer))
1379 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1380
1381(ert-deftest python-shell-get-or-create-process-1 ()
1382 "Check shell process creation fallback."
1383 :expected-result :failed
1384 (python-tests-with-temp-file
1385 ""
1386 ;; XXX: Break early until we can skip stuff. We need to mimic
1387 ;; user interaction because `python-shell-get-or-create-process'
1388 ;; asks for all arguments interactively when a shell process
1389 ;; doesn't exist.
1390 (should nil)
1391 (let* ((python-shell-interpreter
1392 (executable-find python-tests-shell-interpreter))
1393 (use-dialog-box)
1394 (dedicated-process-name (python-shell-get-process-name t))
1395 (dedicated-process (python-shell-get-or-create-process))
1396 (dedicated-shell-buffer (process-buffer dedicated-process)))
1397 (unwind-protect
1398 (progn
1399 (set-process-query-on-exit-flag dedicated-process nil)
1400 ;; Prefer dedicated if not buffer exist.
1401 (should (equal (process-name dedicated-process)
1402 dedicated-process-name))
1403 (kill-buffer dedicated-shell-buffer)
1404 ;; No buffer available.
1405 (should (not (python-shell-get-process))))
1406 (ignore-errors (kill-buffer dedicated-shell-buffer))))))
1407
1408(ert-deftest python-shell-internal-get-or-create-process-1 ()
1409 "Check internal shell process creation fallback."
1410 :expected-result (if (executable-find python-tests-shell-interpreter)
1411 :passed
1412 :failed)
1413 (python-tests-with-temp-file
1414 ""
1415 (should (not (process-live-p (python-shell-internal-get-process-name))))
1416 (let* ((python-shell-interpreter
1417 (executable-find python-tests-shell-interpreter))
1418 (internal-process-name (python-shell-internal-get-process-name))
1419 (internal-process (python-shell-internal-get-or-create-process))
1420 (internal-shell-buffer (process-buffer internal-process)))
1421 (unwind-protect
1422 (progn
1423 (set-process-query-on-exit-flag internal-process nil)
1424 (should (equal (process-name internal-process)
1425 internal-process-name))
1426 (should (equal internal-process
1427 (python-shell-internal-get-or-create-process)))
1428 ;; No user buffer available.
1429 (should (not (python-shell-get-process)))
1430 (kill-buffer internal-shell-buffer))
1431 (ignore-errors (kill-buffer internal-shell-buffer))))))
1432
1164 1433
1165;;; Shell completion 1434;;; Shell completion
1166 1435