Skip to content

Commit 1d75d27

Browse files
committed
Add idris-start-idris2-project which uses idris2 --init to
generate new ipkg file. Why: Idris2 does better job to create the ipkg file than us.
1 parent 52c00f2 commit 1d75d27

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

idris-commands.el

+8-4
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,10 @@ of the term to replace."
12551255
(find-file (car files)))
12561256
(t (find-file (completing-read "Package file: " files nil t))))))
12571257

1258-
(defun idris-start-idris2-project ()
1259-
"Interactively create a new Idris project with ipkg file and source directory."
1258+
(defun idris-start-idris2-project (&optional no-visit)
1259+
"Interactively create a new Idris2 project with ipkg file and source directory.
1260+
1261+
When NO-VISIT is t does not open the generated ipkg file in other window."
12601262
(interactive)
12611263
(cl-flet ((project-name ()
12621264
(let ((project-name (string-trim (read-string "Project name: "))))
@@ -1304,8 +1306,10 @@ of the term to replace."
13041306
(kill-buffer output-buffer)
13051307
(kill-buffer input-buffer)
13061308
(when (file-exists-p ipkg-file)
1307-
(find-file-other-window ipkg-file)
1308-
(insert "-- " project-name "\n")))))
1309+
(with-current-buffer (find-file-noselect ipkg-file)
1310+
(insert "-- " project-name "\n"))
1311+
1312+
(when (null no-visit) (find-file-other-window ipkg-file))))))
13091313

13101314
(defun idris-start-project ()
13111315
"Interactively create a new Idris project, complete with ipkg file."

test/idris-commands-test.el

+53
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,59 @@ closeDistance s1 s2 = closeDistance_rhs s1 s2"
466466
(advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub)
467467
(advice-remove 'idris-eval #'idris-eval-stub))))
468468

469+
(defmacro idris-generate-mock-read (clauses &optional default)
470+
"Generate a function with cond expression from CLAUSES and optional DEFAULT."
471+
`(lambda (prompt &optional &rest _)
472+
(cond
473+
,@(mapcar (lambda (clause)
474+
`((string-match-p ,(if (symbolp (car clause))
475+
;; Convert symbol to string and
476+
;; remove leading colon and
477+
;; replace dashes with a space
478+
(replace-regexp-in-string
479+
"^\\:" ""
480+
(string-replace "-" " " (symbol-name (car clause))))
481+
(car clause))
482+
prompt)
483+
,(cdr clause)))
484+
clauses)
485+
(t (or ,default "")))))
486+
487+
(when (string-match-p "idris2$" idris-interpreter-path)
488+
(ert-deftest idris-start-idris2-project ()
489+
"Test `idris-start-idris2-project' creating a project."
490+
(cl-flet ((read-string-stub (idris-generate-mock-read ((:project-name . "Idris2 Test Project")
491+
(:author . "Joe Doe")
492+
(:source-dir . "mysrc")
493+
(:options . "--some-option"))))
494+
(read-dir-stub
495+
(idris-generate-mock-read
496+
((:create-in . (idris-file-name-concat "test-data" "Idris2TestProject"))))))
497+
(advice-add 'read-string :override #'read-string-stub)
498+
(advice-add 'read-directory-name :override #'read-dir-stub)
499+
(unwind-protect
500+
(progn
501+
(idris-start-idris2-project t)
502+
(let* ((ipkg-file-path (idris-file-name-concat "test-data"
503+
"Idris2TestProject"
504+
"idris2_test_project.ipkg"))
505+
(ipkg-buffer (find-file-noselect ipkg-file-path))
506+
(ipkg-content (with-current-buffer ipkg-buffer
507+
(buffer-substring-no-properties (point-min) (point-max)))))
508+
509+
(should (string-match-p "-- Idris2 Test Project" ipkg-content))
510+
(should (string-match-p "package Idris2TestProject" ipkg-content))
511+
(should (string-match-p "authors = \"Joe Doe\"" ipkg-content))
512+
(should (string-match-p "opts = \"--some-option\"" ipkg-content))
513+
(should (string-match-p "sourcedir = \"mysrc\"" ipkg-content)))
514+
515+
(should (file-exists-p (idris-file-name-concat "test-data"
516+
"Idris2TestProject"
517+
"mysrc"))))
518+
(advice-remove 'read-string #'read-string-stub)
519+
(advice-remove 'read-directory-name #'read-dir-stub)
520+
(delete-directory (idris-file-name-concat "test-data" "Idris2TestProject") t)))))
521+
469522
;; Tests by Yasuhiko Watanabe
470523
;; https://github.com/idris-hackers/idris-mode/pull/537/files
471524
(idris-ert-command-action "test-data/CaseSplit.idr" idris-case-split idris-test-eq-buffer)

0 commit comments

Comments
 (0)