minetest.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-minetest)
  19. #:use-module (guix build-system minetest)
  20. #:use-module (guix upstream)
  21. #:use-module (guix memoization)
  22. #:use-module (guix import minetest)
  23. #:use-module (guix import utils)
  24. #:use-module (guix tests)
  25. #:use-module (guix packages)
  26. #:use-module (guix git-download)
  27. #:use-module ((gnu packages minetest)
  28. #:select (minetest minetest-technic))
  29. #:use-module ((gnu packages base)
  30. #:select (hello))
  31. #:use-module (json)
  32. #:use-module (ice-9 match)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-64))
  37. ;; Some procedures for populating a ‘fake’ ContentDB server.
  38. (define* (make-package-sexp #:key
  39. (guix-name "minetest-foo")
  40. ;; This is not a proper version number but
  41. ;; ContentDB often does not include version
  42. ;; numbers.
  43. (version "2021-07-25")
  44. (home-page "https://example.org/foo")
  45. (repo "https://example.org/foo.git")
  46. (synopsis "synopsis")
  47. (guix-description "description")
  48. (guix-license
  49. '(list license:cc-by-sa4.0 license:lgpl3+))
  50. (inputs '())
  51. (upstream-name "Author/foo")
  52. #:allow-other-keys)
  53. `(package
  54. (name ,guix-name)
  55. (version ,version)
  56. (source
  57. (origin
  58. (method git-fetch)
  59. (uri (git-reference
  60. (url ,(and (not (eq? repo 'null)) repo))
  61. (commit #f)))
  62. (sha256
  63. (base32 #f))
  64. (file-name (git-file-name name version))))
  65. (build-system minetest-mod-build-system)
  66. ,@(maybe-propagated-inputs inputs)
  67. (home-page ,home-page)
  68. (synopsis ,synopsis)
  69. (description ,guix-description)
  70. (license ,guix-license)
  71. (properties
  72. ,(list 'quasiquote
  73. `((upstream-name . ,upstream-name))))))
  74. (define* (make-package-json #:key
  75. (author "Author")
  76. (name "foo")
  77. (media-license "CC-BY-SA-4.0")
  78. (license "LGPL-3.0-or-later")
  79. (short-description "synopsis")
  80. (long-description "description")
  81. (repo "https://example.org/foo.git")
  82. (website "https://example.org/foo")
  83. (forums 321)
  84. (score 987.654)
  85. (downloads 123)
  86. (type "mod")
  87. #:allow-other-keys)
  88. `(("author" . ,author)
  89. ("content_warnings" . #())
  90. ("created_at" . "2018-05-23T19:58:07.422108")
  91. ("downloads" . ,downloads)
  92. ("forums" . ,forums)
  93. ("issue_tracker" . "https://example.org/foo/issues")
  94. ("license" . ,license)
  95. ("long_description" . ,long-description)
  96. ("maintainers" . #("maintainer"))
  97. ("media_license" . ,media-license)
  98. ("name" . ,name)
  99. ("provides" . #("stuff"))
  100. ("release" . 456)
  101. ("repo" . ,repo)
  102. ("score" . ,score)
  103. ("screenshots" . #())
  104. ("short_description" . ,short-description)
  105. ("state" . "APPROVED")
  106. ("tags" . #("some" "tags"))
  107. ("thumbnail" . null)
  108. ("title" . "The name")
  109. ("type" . ,type)
  110. ("url" . ,(string-append "https://content.minetest.net/packages/"
  111. author "/" name "/download/"))
  112. ("website" . ,website)))
  113. (define* (make-releases-json #:key (commit #f) (title "2021-07-25") #:allow-other-keys)
  114. `#((("commit" . ,commit)
  115. ("downloads" . 469)
  116. ("id" . 8614)
  117. ("max_minetest_version" . null)
  118. ("min_minetest_version" . null)
  119. ("release_date" . "2021-07-25T01:10:23.207584")
  120. ("title" . ,title))))
  121. (define* (make-dependencies-json #:key (author "Author")
  122. (name "foo")
  123. (requirements '(("default" #f ())))
  124. #:allow-other-keys)
  125. `((,(string-append author "/" name)
  126. . ,(list->vector
  127. (map (match-lambda
  128. ((symbolic-name optional? implementations)
  129. `(("is_optional" . ,optional?)
  130. ("name" . ,symbolic-name)
  131. ("packages" . ,(list->vector implementations)))))
  132. requirements)))
  133. ("something/else" . #())))
  134. (define* (make-packages-keys-json #:key (author "Author")
  135. (name "Name")
  136. (type "mod"))
  137. `(("author" . ,author)
  138. ("name" . ,name)
  139. ("type" . ,type)))
  140. (define (call-with-packages thunk . argument-lists)
  141. ;; Don't reuse results from previous tests.
  142. (invalidate-memoization! contentdb-fetch)
  143. (invalidate-memoization! minetest->guix-package)
  144. (define (scm->json-port scm)
  145. (open-input-string (scm->json-string scm)))
  146. (define (handle-package url requested-author requested-name . rest)
  147. (define relevant-argument-list
  148. (any (lambda (argument-list)
  149. (apply (lambda* (#:key (author "Author") (name "foo")
  150. #:allow-other-keys)
  151. (and (equal? requested-author author)
  152. (equal? requested-name name)
  153. argument-list))
  154. argument-list))
  155. argument-lists))
  156. (when (not relevant-argument-list)
  157. (error "the package ~a/~a should be irrelevant, but ~a is fetched"
  158. requested-author requested-name url))
  159. (scm->json-port
  160. (apply (match rest
  161. (("") make-package-json)
  162. (("dependencies" "") make-dependencies-json)
  163. (("releases" "") make-releases-json)
  164. (_ (error "TODO ~a" rest)))
  165. relevant-argument-list)))
  166. (define (handle-mod-search sort)
  167. ;; Produce search results, sorted by SORT in descending order.
  168. (define arguments->key
  169. (match sort
  170. ("score" (lambda* (#:key (score 987.654) #:allow-other-keys)
  171. score))
  172. ("downloads" (lambda* (#:key (downloads 123) #:allow-other-keys)
  173. downloads))))
  174. (define argument-list->key (cut apply arguments->key <>))
  175. (define (greater x y)
  176. (> (argument-list->key x) (argument-list->key y)))
  177. (define sorted-argument-lists (sort-list argument-lists greater))
  178. (define* (arguments->json #:key (author "Author") (name "Foo") (type "mod")
  179. #:allow-other-keys)
  180. (and (string=? type "mod")
  181. `(("author" . ,author)
  182. ("name" . ,name)
  183. ("type" . ,type))))
  184. (define argument-list->json (cut apply arguments->json <>))
  185. (scm->json-port
  186. (list->vector (filter-map argument-list->json sorted-argument-lists))))
  187. (mock ((guix http-client) http-fetch
  188. (lambda* (url #:key headers timeout)
  189. (unless (string-prefix? "mock://api/packages/" url)
  190. (error "the URL ~a should not be used" url))
  191. (define resource
  192. (substring url (string-length "mock://api/packages/")))
  193. (define components (string-split resource #\/))
  194. (match components
  195. ((author name . rest)
  196. (apply handle-package url author name rest))
  197. (((? (cut string-prefix? "?type=mod&q=" <>) query))
  198. (handle-mod-search
  199. (cond ((string-contains query "sort=score") "score")
  200. ((string-contains query "sort=downloads") "downloads")
  201. (#t (error "search query ~a has unknown sort key"
  202. query)))))
  203. (_
  204. (error "the URL ~a should have an author and name component"
  205. url)))))
  206. (parameterize ((%contentdb-api "mock://api/"))
  207. (thunk))))
  208. (define* (minetest->guix-package* #:key (author "Author") (name "foo")
  209. (sort %default-sort-key)
  210. #:allow-other-keys)
  211. (minetest->guix-package (string-append author "/" name) #:sort sort))
  212. (define (imported-package-sexp* primary-arguments . secondary-arguments)
  213. "Ask the importer to import a package specified by PRIMARY-ARGUMENTS,
  214. during a dynamic where that package and the packages specified by
  215. SECONDARY-ARGUMENTS are available on ContentDB."
  216. (apply call-with-packages
  217. (lambda ()
  218. ;; The memoization cache is reset by call-with-packages
  219. (apply minetest->guix-package* primary-arguments))
  220. primary-arguments
  221. secondary-arguments))
  222. (define (imported-package-sexp . extra-arguments)
  223. "Ask the importer to import a package specified by EXTRA-ARGUMENTS,
  224. during a dynamic extent where that package is available on ContentDB."
  225. (imported-package-sexp* extra-arguments))
  226. (define-syntax-rule (test-package test-case . extra-arguments)
  227. (test-equal test-case
  228. (make-package-sexp . extra-arguments)
  229. (imported-package-sexp . extra-arguments)))
  230. (define-syntax-rule (test-package* test-case primary-arguments extra-arguments
  231. ...)
  232. (test-equal test-case
  233. (apply make-package-sexp primary-arguments)
  234. (imported-package-sexp* primary-arguments extra-arguments ...)))
  235. (test-begin "minetest")
  236. ;; Package names
  237. (test-package "minetest->guix-package")
  238. (test-package "minetest->guix-package, _ → - in package name"
  239. #:name "foo_bar"
  240. #:guix-name "minetest-foo-bar"
  241. #:upstream-name "Author/foo_bar")
  242. (test-equal "elaborate names, unambiguous"
  243. "Jeija/mesecons"
  244. (call-with-packages
  245. (cut elaborate-contentdb-name "mesecons")
  246. '(#:name "mesecons" #:author "Jeija")
  247. '(#:name "something" #:author "else")))
  248. (test-equal "elaborate name, ambiguous (highest score)"
  249. "Jeija/mesecons"
  250. (call-with-packages
  251. ;; #:sort "score" is the default
  252. (cut elaborate-contentdb-name "mesecons")
  253. '(#:name "mesecons" #:author "Jeijc" #:score 777)
  254. '(#:name "mesecons" #:author "Jeijb" #:score 888)
  255. '(#:name "mesecons" #:author "Jeija" #:score 999)))
  256. (test-equal "elaborate name, ambiguous (most downloads)"
  257. "Jeija/mesecons"
  258. (call-with-packages
  259. (cut elaborate-contentdb-name "mesecons" #:sort "downloads")
  260. '(#:name "mesecons" #:author "Jeijc" #:downloads 777)
  261. '(#:name "mesecons" #:author "Jeijb" #:downloads 888)
  262. '(#:name "mesecons" #:author "Jeija" #:downloads 999)))
  263. ;; Determining the home page
  264. (test-package "minetest->guix-package, website is used as home page"
  265. #:home-page "web://site"
  266. #:website "web://site")
  267. (test-package "minetest->guix-package, if absent, the forum is used"
  268. #:home-page '(minetest-topic 628)
  269. #:forums 628
  270. #:website 'null)
  271. (test-package "minetest->guix-package, if absent, the git repo is used"
  272. #:home-page "https://github.com/minetest-mods/mesecons"
  273. #:forums 'null
  274. #:website 'null
  275. #:repo "https://github.com/minetest-mods/mesecons")
  276. (test-package "minetest->guix-package, all home page information absent"
  277. #:home-page #f
  278. #:forums 'null
  279. #:website 'null
  280. #:repo 'null)
  281. ;; Determining the version number
  282. (test-package "conventional version number" #:version "1.2.3" #:title "1.2.3")
  283. ;; See e.g. orwell/basic_trains
  284. (test-package "v-prefixed version number" #:version "1.2.3" #:title "v1.2.3")
  285. ;; Many mods on ContentDB use dates as release titles. In that case, the date
  286. ;; will have to do.
  287. (test-package "dates as version number"
  288. #:version "2021-01-01" #:title "2021-01-01")
  289. ;; Dependencies
  290. (test-package* "minetest->guix-package, unambiguous dependency"
  291. (list #:requirements '(("mesecons" #f
  292. ("Jeija/mesecons"
  293. "some-modpack/containing-mese")))
  294. #:inputs '("minetest-mesecons"))
  295. (list #:author "Jeija" #:name "mesecons")
  296. (list #:author "some-modpack" #:name "containing-mese" #:type "modpack"))
  297. (test-package* "minetest->guix-package, ambiguous dependency (highest score)"
  298. (list #:name "frobnicate"
  299. #:guix-name "minetest-frobnicate"
  300. #:upstream-name "Author/frobnicate"
  301. #:requirements '(("frob" #f
  302. ("Author/foo" "Author/bar")))
  303. ;; #:sort "score" is the default
  304. #:inputs '("minetest-bar"))
  305. (list #:author "Author" #:name "foo" #:score 0)
  306. (list #:author "Author" #:name "bar" #:score 9999))
  307. (test-package* "minetest->guix-package, ambiguous dependency (most downloads)"
  308. (list #:name "frobnicate"
  309. #:guix-name "minetest-frobnicate"
  310. #:upstream-name "Author/frobnicate"
  311. #:requirements '(("frob" #f
  312. ("Author/foo" "Author/bar")))
  313. #:inputs '("minetest-bar")
  314. #:sort "downloads")
  315. (list #:author "Author" #:name "foo" #:downloads 0)
  316. (list #:author "Author" #:name "bar" #:downloads 9999))
  317. (test-package "minetest->guix-package, optional dependency"
  318. #:requirements '(("mesecons" #t
  319. ("Jeija/mesecons"
  320. "some-modpack/containing-mese")))
  321. #:inputs '())
  322. ;; See e.g. 'orwell/basic_trains'
  323. (test-package* "minetest->guix-package, multiple dependencies implemented by one mod"
  324. (list #:name "frobnicate"
  325. #:guix-name "minetest-frobnicate"
  326. #:upstream-name "Author/frobnicate"
  327. #:requirements '(("frob" #f ("Author/frob"))
  328. ("frob_x" #f ("Author/frob")))
  329. #:inputs '("minetest-frob"))
  330. (list #:author "Author" #:name "frob"))
  331. ;; License
  332. (test-package "minetest->guix-package, identical licenses"
  333. #:guix-license 'license:lgpl3+
  334. #:license "LGPL-3.0-or-later"
  335. #:media-license "LGPL-3.0-or-later")
  336. ;; Sorting
  337. (let* ((make-package
  338. (lambda arguments
  339. (json->package (apply make-package-json arguments))))
  340. (x (make-package #:score 0))
  341. (y (make-package #:score 1))
  342. (z (make-package #:score 2)))
  343. (test-equal "sort-packages, already sorted"
  344. (list z y x)
  345. (sort-packages (list z y x)))
  346. (test-equal "sort-packages, reverse"
  347. (list z y x)
  348. (sort-packages (list x y z))))
  349. ;; Update detection
  350. (define (upstream-source->sexp upstream-source)
  351. (define url (upstream-source-urls upstream-source))
  352. (unless (git-reference? url)
  353. (error "a <git-reference> is expected"))
  354. `(,(upstream-source-package upstream-source)
  355. ,(upstream-source-version upstream-source)
  356. ,(git-reference-url url)
  357. ,(git-reference-commit url)))
  358. (define* (expected-sexp #:key
  359. (repo "https://example.org/foo.git")
  360. (guix-name "minetest-foo")
  361. (new-version "0.8")
  362. (commit "44941798d222901b8f381b3210957d880b90a2fc")
  363. #:allow-other-keys)
  364. `(,guix-name ,new-version ,repo ,commit))
  365. (define* (example-package #:key
  366. (source 'auto)
  367. (repo "https://example.org/foo.git")
  368. (old-version "0.8")
  369. (commit "44941798d222901b8f381b3210957d880b90a2fc")
  370. #:allow-other-keys)
  371. (package
  372. (name "minetest-foo")
  373. (version old-version)
  374. (source
  375. (if (eq? source 'auto)
  376. (origin
  377. (method git-fetch)
  378. (uri (git-reference
  379. (url repo)
  380. (commit commit #;"808f9ffbd3106da4c92d2367b118b98196c9e81e")))
  381. (sha256 #f) ; not important for the following tests
  382. (file-name (git-file-name name version)))
  383. source))
  384. (build-system minetest-mod-build-system)
  385. (license #f)
  386. (synopsis #f)
  387. (description #f)
  388. (home-page #f)
  389. (properties '((upstream-name . "Author/foo")))))
  390. (define-syntax-rule (test-release test-case . arguments)
  391. (test-equal test-case
  392. (expected-sexp . arguments)
  393. (and=>
  394. (call-with-packages
  395. (cut latest-minetest-release (example-package . arguments))
  396. (list . arguments))
  397. upstream-source->sexp)))
  398. (define-syntax-rule (test-no-release test-case . arguments)
  399. (test-equal test-case
  400. #f
  401. (call-with-packages
  402. (cut latest-minetest-release (example-package . arguments))
  403. (list . arguments))))
  404. (test-release "same version"
  405. #:old-version "0.8" #:title "0.8" #:new-version "0.8"
  406. #:commit "44941798d222901b8f381b3210957d880b90a2fc")
  407. (test-release "new version (dotted)"
  408. #:old-version "0.8" #:title "0.9.0" #:new-version "0.9.0"
  409. #:commit "c8855b991880897b2658dc90164e29c96e2aeb3a")
  410. (test-release "new version (date)"
  411. #:old-version "2014-11-17" #:title "2015-11-04"
  412. #:new-version "2015-11-04"
  413. #:commit "c8855b991880897b2658dc90164e29c96e2aeb3a")
  414. (test-release "new version (git -> dotted)"
  415. #:old-version
  416. (git-version "0.8" "1" "90422555f114d3af35e7cc4b5b6d59a5c226adc4")
  417. #:title "0.9.0" #:new-version "0.9.0"
  418. #:commit "90422555f114d3af35e7cc4b5b6d59a5c226adc4")
  419. ;; There might actually be a new release, but guix cannot compare dates
  420. ;; with regular version numbers.
  421. (test-no-release "dotted -> date"
  422. #:old-version "0.8" #:title "2015-11-04"
  423. #:commit "c8855b991880897b2658dc90164e29c96e2aeb3a")
  424. (test-no-release "date -> dotted"
  425. #:old-version "2014-11-07" #:title "0.8"
  426. #:commit "c8855b991880897b2658dc90164e29c96e2aeb3a")
  427. ;; Don't let "guix refresh -t minetest" tell there are new versions
  428. ;; if Guix has insufficient information to actually perform the update,
  429. ;; when using --with-latest or "guix refresh -u".
  430. (test-no-release "no commit information, no new release"
  431. #:old-version "0.8" #:title "0.9.0" #:new-version "0.9.0"
  432. #:commit #false)
  433. (test-assert "minetest is not a minetest mod"
  434. (not (minetest-package? minetest)))
  435. (test-assert "GNU hello is not a minetest mod"
  436. (not (minetest-package? hello)))
  437. (test-assert "technic is a minetest mod"
  438. (minetest-package? minetest-technic))
  439. (test-assert "upstream-name is required"
  440. (not (minetest-package?
  441. (package (inherit minetest-technic)
  442. (properties '())))))
  443. (test-end "minetest")
  444. ;;; Local Variables:
  445. ;;; eval: (put 'test-package* 'scheme-indent-function 1)
  446. ;;; eval: (put 'test-release 'scheme-indent-function 1)
  447. ;;; eval: (put 'test-no-release 'scheme-indent-function 1)
  448. ;;; End: