Skip to content

Commit c18c6ea

Browse files
committed
feat: Add version related functions
* learn-ocaml.el (learn-ocaml-since-upto, learn-ocaml-compat): Add elisp counterpart of Learnocaml_api.Compat, <ocaml-sf/learn-ocaml#426> (learn-ocaml-client-server-min-version): Add/Use learn-ocaml-client-server-min-version-compat. (learn-ocaml-on-load): Add/Use learn-ocaml-feature-passwd-compat. * 001-common/runtests.el (a14_learn-ocaml-compat): Add tests
1 parent 3d47a97 commit c18c6ea

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

learn-ocaml.el

+52-20
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ to the boolean contained in the json returned by the client"
202202
(setq learn-ocaml-use-passwd t)
203203
(setq learn-ocaml-use-passwd nil)))
204204

205+
(defun learn-ocaml-since-upto (since upto)
206+
"Return (SINCE . UPTO) with args possibly nil, denoting (SINCE <= v < UPTO).
207+
Both SINCE and UPTO should be valid version strings."
208+
(unless (or (stringp since) (null since))
209+
(error "SINCE must be a string"))
210+
(unless (or (stringp upto) (null upto))
211+
(error "UPTO must be a string"))
212+
(cons since upto))
213+
214+
(defun learn-ocaml-compat (pred-pair version)
215+
"If PRED-PAIR is (since . upto), return (since <= VERSION < upto).
216+
VERSION should be a list-based version, use `version-to-list' if need be.
217+
See also `learn-ocaml-since-upto'."
218+
(let ((since (car pred-pair)) (upto (cdr pred-pair)))
219+
(and (if since (version-list-<= (version-to-list since) version) t)
220+
(if upto (version-list-< version (version-to-list upto)) t))))
221+
205222
;;
206223
;; package.el shortcut
207224
;;
@@ -360,15 +377,27 @@ Raise (error \"learn-ocaml-await-for...\") if `learn-ocaml-timeout' exceeded."
360377
(string-trim
361378
(cdr (learn-ocaml-command-to-string-await-cmd (list "--version")))))
362379

363-
(defun learn-ocaml-client-server-version ()
364-
"Run \"learn-ocaml-client server-version\"."
365-
;; TODO/FIXME: Update when possible
366-
(learn-ocaml-client-version))
380+
(defconst learn-ocaml-client-server-min-version-compat
381+
(learn-ocaml-since-upto "0.13.0" nil))
367382

368-
(defun learn-ocaml-client-server-min-version ()
383+
;; (learn-ocaml-client-server-min-version
384+
;; (learn-ocaml-client-server-min-version "http://localhost:8080")
385+
(defun learn-ocaml-client-server-min-version (&optional server)
369386
"Return the min of the learn-ocaml server and learn-ocaml-client versions."
370-
;; TODO/FIXME: Update when possible, "learn-ocaml-client server-version --min"
371-
(learn-ocaml-client-version))
387+
(let* ((client (learn-ocaml-client-version))
388+
(command (list "server-version" "--min"))
389+
(version-string
390+
(if (learn-ocaml-compat learn-ocaml-client-server-min-version-compat
391+
(version-to-list client))
392+
;; TODO: Take errors into account in a better way,
393+
;; instead of waiting that `version-to-list' fails
394+
(string-trim (cdr (learn-ocaml-command-to-string-await-cmd
395+
(if server (append command
396+
(list "-s" server))
397+
command))))
398+
client))
399+
(_check (version-to-list version-string)))
400+
version-string))
372401

373402
;;
374403
;; Higher-order functions, sentinels of the make-process wrapper
@@ -1130,6 +1159,9 @@ If TOKEN is \"\", interactively ask a token."
11301159
:secret secret
11311160
:callback rich-callback)))))))
11321161

1162+
(defconst learn-ocaml-feature-passwd-compat
1163+
(learn-ocaml-since-upto "0.15.0" nil))
1164+
11331165
(defun learn-ocaml-on-load (callback)
11341166
"Call `learn-ocaml-login-with-token' and CALLBACK when loading mode."
11351167
(learn-ocaml-give-server-cmd
@@ -1141,19 +1173,19 @@ If TOKEN is \"\", interactively ask a token."
11411173
(progn (message-box "No server found. Please enter the server url.")
11421174
(read-string "Enter server URL: " "https://"))
11431175
server)))
1144-
(if (version-list-<
1145-
;; https://github.com/pfitaxel/learn-ocaml.el/pull/26#pullrequestreview-758063532
1146-
(version-to-list (learn-ocaml-client-server-min-version))
1147-
(version-to-list "0.15.0"))
1148-
(learn-ocaml-login-with-token token new-server-value callback)
1149-
(progn (learn-ocaml-init-server-cmd
1150-
:server new-server-value
1151-
:callback
1152-
(lambda(_)
1153-
(learn-ocaml-server-config (learn-ocaml-client-config-cmd))
1154-
(if learn-ocaml-use-passwd
1155-
(learn-ocaml-login-possibly-with-passwd new-server-value callback)
1156-
(learn-ocaml-login-with-token token new-server-value callback))))))))))))
1176+
;; https://github.com/pfitaxel/learn-ocaml.el/pull/26#pullrequestreview-758063532
1177+
(if (learn-ocaml-compat learn-ocaml-feature-passwd-compat
1178+
(version-to-list
1179+
(learn-ocaml-client-server-min-version new-server-value)))
1180+
(progn (learn-ocaml-init-server-cmd
1181+
:server new-server-value
1182+
:callback
1183+
(lambda(_)
1184+
(learn-ocaml-server-config (learn-ocaml-client-config-cmd))
1185+
(if learn-ocaml-use-passwd
1186+
(learn-ocaml-login-possibly-with-passwd new-server-value callback)
1187+
(learn-ocaml-login-with-token token new-server-value callback)))))
1188+
(learn-ocaml-login-with-token token new-server-value callback))))))))
11571189

11581190
(defun learn-ocaml-logout ()
11591191
"Logout the user from the server by removing the token from the file client.json"

tests/001-common/runtests.el

+9
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,13 @@
169169
(should (string-equal (learn-ocaml-file-path dir file) path))
170170
(should (string-equal (learn-ocaml-file-path "/dummy" path) path))))
171171

172+
(ert-deftest a14_learn-ocaml-compat ()
173+
(let ((v (version-to-list "0.13.0")))
174+
(should (learn-ocaml-compat (learn-ocaml-since-upto "0.12" nil) v))
175+
(should (learn-ocaml-compat (learn-ocaml-since-upto "0.12" "0.13.1") v))
176+
(should (learn-ocaml-compat (learn-ocaml-since-upto "0.12" "0.14.0") v))
177+
(should (learn-ocaml-compat (learn-ocaml-since-upto nil "0.13.1") v))
178+
(should (learn-ocaml-compat (learn-ocaml-since-upto nil "0.14.0") v))
179+
(should (not (learn-ocaml-compat (learn-ocaml-since-upto "0.15.0" nil) v)))))
180+
172181
;;; runtests.el ends here

tests/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ To test learn-ocaml.el w.r.t. another version of learn-ocaml-client:
5252
* Check ex == `learn-ocaml-file-path(dir,file)`
5353
* Check ex == `learn-ocaml-file-path("/dummy",file)`
5454

55+
* `a14_learn-ocaml-compat` (`ert-deftest`)__
56+
* Test several predicates using `learn-ocaml-since-upto`
57+
5558
## Integration tests (`ert-deftest-async`)
5659

5760
**BEWARE** all tests do `rm -f ~/.config/learnocaml/client.json`

0 commit comments

Comments
 (0)