gexp.scm 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-gexp)
  20. #:use-module (guix store)
  21. #:use-module (guix monads)
  22. #:use-module (guix gexp)
  23. #:use-module (guix grafts)
  24. #:use-module (guix derivations)
  25. #:use-module (guix packages)
  26. #:use-module (guix build-system trivial)
  27. #:use-module (guix tests)
  28. #:use-module ((guix build utils) #:select (with-directory-excursion))
  29. #:use-module ((guix utils) #:select (call-with-temporary-directory))
  30. #:use-module (gnu packages)
  31. #:use-module (gnu packages base)
  32. #:use-module (gnu packages bootstrap)
  33. #:use-module ((guix diagnostics) #:select (guix-warning-port))
  34. #:use-module (srfi srfi-1)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-64)
  37. #:use-module (rnrs io ports)
  38. #:use-module (ice-9 match)
  39. #:use-module (ice-9 regex)
  40. #:use-module (ice-9 popen)
  41. #:use-module (ice-9 ftw))
  42. ;; Test the (guix gexp) module.
  43. (define %store
  44. (open-connection-for-tests))
  45. ;; Globally disable grafts because they can trigger early builds.
  46. (%graft? #f)
  47. ;; For white-box testing.
  48. (define (gexp-inputs x)
  49. ((@@ (guix gexp) gexp-inputs) x))
  50. (define (gexp-outputs x)
  51. ((@@ (guix gexp) gexp-outputs) x))
  52. (define (gexp->sexp . x)
  53. (apply (@@ (guix gexp) gexp->sexp) x))
  54. (define* (gexp->sexp* exp #:optional target)
  55. (run-with-store %store (gexp->sexp exp (%current-system) target)
  56. #:guile-for-build (%guile-for-build)))
  57. (define (gexp-input->tuple input)
  58. (list (gexp-input-thing input) (gexp-input-output input)
  59. (gexp-input-native? input)))
  60. (define %extension-package
  61. ;; Example of a package to use when testing 'with-extensions'.
  62. (dummy-package "extension"
  63. (build-system trivial-build-system)
  64. (arguments
  65. `(#:guile ,%bootstrap-guile
  66. #:modules ((guix build utils))
  67. #:builder
  68. (begin
  69. (use-modules (guix build utils))
  70. (let* ((out (string-append (assoc-ref %outputs "out")
  71. "/share/guile/site/"
  72. (effective-version))))
  73. (mkdir-p out)
  74. (call-with-output-file (string-append out "/hg2g.scm")
  75. (lambda (port)
  76. (define defmod 'define-module) ;fool Geiser
  77. (write `(,defmod (hg2g)
  78. #:export (the-answer))
  79. port)
  80. (write '(define the-answer 42) port)))))))))
  81. (test-begin "gexp")
  82. (test-equal "no references"
  83. '(display "hello gexp->approximate-sexp!")
  84. (gexp->approximate-sexp #~(display "hello gexp->approximate-sexp!")))
  85. (test-equal "unquoted gexp"
  86. '(display "hello")
  87. (let ((inside #~"hello"))
  88. (gexp->approximate-sexp #~(display #$inside))))
  89. (test-equal "unquoted gexp (native)"
  90. '(display "hello")
  91. (let ((inside #~"hello"))
  92. (gexp->approximate-sexp #~(display #+inside))))
  93. (test-equal "spliced gexp"
  94. '(display '(fresh vegetables))
  95. (let ((inside #~(fresh vegetables)))
  96. (gexp->approximate-sexp #~(display '(#$@inside)))))
  97. (test-equal "unspliced gexp, approximated"
  98. ;; (*approximate*) is really an implementation detail
  99. '(display '(*approximate*))
  100. (let ((inside (file-append coreutils "/bin/hello")))
  101. (gexp->approximate-sexp #~(display '(#$@inside)))))
  102. (test-equal "unquoted gexp, approximated"
  103. '(display '(*approximate*))
  104. (let ((inside (file-append coreutils "/bin/hello")))
  105. (gexp->approximate-sexp #~(display '#$inside))))
  106. (test-equal "no refs"
  107. '(display "hello!")
  108. (let ((exp (gexp (display "hello!"))))
  109. (and (gexp? exp)
  110. (null? (gexp-inputs exp))
  111. (gexp->sexp* exp))))
  112. (test-equal "sexp->gexp"
  113. '(a b (c d) e)
  114. (let ((exp (sexp->gexp '(a b (c d) e))))
  115. (and (gexp? exp)
  116. (null? (gexp-inputs exp))
  117. (gexp->sexp* exp))))
  118. (test-equal "unquote"
  119. '(display `(foo ,(+ 2 3)))
  120. (let ((exp (gexp (display `(foo ,(+ 2 3))))))
  121. (and (gexp? exp)
  122. (null? (gexp-inputs exp))
  123. (gexp->sexp* exp))))
  124. (test-assert "one input package"
  125. (let ((exp (gexp (display (ungexp coreutils)))))
  126. (and (gexp? exp)
  127. (match (gexp-inputs exp)
  128. ((input)
  129. (eq? (gexp-input-thing input) coreutils)))
  130. (equal? `(display ,(derivation->output-path
  131. (package-derivation %store coreutils)))
  132. (gexp->sexp* exp)))))
  133. (test-assert "one input package, dotted list"
  134. (let ((exp (gexp (coreutils . (ungexp coreutils)))))
  135. (and (gexp? exp)
  136. (match (gexp-inputs exp)
  137. ((input)
  138. (eq? (gexp-input-thing input) coreutils)))
  139. (equal? `(coreutils . ,(derivation->output-path
  140. (package-derivation %store coreutils)))
  141. (gexp->sexp* exp)))))
  142. (test-assert "one input origin"
  143. (let ((exp (gexp (display (ungexp (package-source coreutils))))))
  144. (and (gexp? exp)
  145. (match (gexp-inputs exp)
  146. ((input)
  147. (and (eq? (gexp-input-thing input) (package-source coreutils))
  148. (string=? (gexp-input-output input) "out"))))
  149. (equal? `(display ,(derivation->output-path
  150. (package-source-derivation
  151. %store (package-source coreutils))))
  152. (gexp->sexp* exp)))))
  153. (test-assert "one local file"
  154. (let* ((file (search-path %load-path "guix.scm"))
  155. (local (local-file file))
  156. (exp (gexp (display (ungexp local))))
  157. (intd (add-to-store %store (basename file) #f
  158. "sha256" file)))
  159. (and (gexp? exp)
  160. (match (gexp-inputs exp)
  161. ((input)
  162. (and (eq? (gexp-input-thing input) local)
  163. (string=? (gexp-input-output input) "out"))))
  164. (equal? `(display ,intd) (gexp->sexp* exp)))))
  165. (test-assert "one local file, symlink"
  166. (let ((file (search-path %load-path "guix.scm"))
  167. (link (tmpnam)))
  168. (dynamic-wind
  169. (const #t)
  170. (lambda ()
  171. (symlink (canonicalize-path file) link)
  172. (let* ((local (local-file link "my-file" #:recursive? #f))
  173. (exp (gexp (display (ungexp local))))
  174. (intd (add-to-store %store "my-file" #f
  175. "sha256" file)))
  176. (and (gexp? exp)
  177. (match (gexp-inputs exp)
  178. ((input)
  179. (and (eq? (gexp-input-thing input) local)
  180. (string=? (gexp-input-output input) "out"))))
  181. (equal? `(display ,intd) (gexp->sexp* exp)))))
  182. (lambda ()
  183. (false-if-exception (delete-file link))))))
  184. (test-equal "local-file, relative file name"
  185. (canonicalize-path (search-path %load-path "guix/base32.scm"))
  186. (let ((directory (dirname (search-path %load-path
  187. "guix/build-system/gnu.scm"))))
  188. (with-directory-excursion directory
  189. (let ((file (local-file "../guix/base32.scm")))
  190. (local-file-absolute-file-name file)))))
  191. (test-equal "local-file, non-literal relative file name"
  192. (canonicalize-path (search-path %load-path "guix/base32.scm"))
  193. (let ((directory (dirname (search-path %load-path
  194. "guix/build-system/gnu.scm"))))
  195. (with-directory-excursion directory
  196. (let ((file (local-file (string-copy "../base32.scm"))))
  197. (local-file-absolute-file-name file)))))
  198. (test-assertm "local-file, #:select?"
  199. (mlet* %store-monad ((select? -> (lambda (file stat)
  200. (member (basename file)
  201. '("guix.scm" "tests"
  202. "gexp.scm"))))
  203. (file -> (local-file ".." "directory"
  204. #:recursive? #t
  205. #:select? select?))
  206. (dir (lower-object file)))
  207. (return (and (store-path? dir)
  208. (equal? (scandir dir)
  209. '("." ".." "guix.scm" "tests"))
  210. (equal? (scandir (string-append dir "/tests"))
  211. '("." ".." "gexp.scm"))))))
  212. (test-assert "one plain file"
  213. (let* ((file (plain-file "hi" "Hello, world!"))
  214. (exp (gexp (display (ungexp file))))
  215. (expected (add-text-to-store %store "hi" "Hello, world!")))
  216. (and (gexp? exp)
  217. (match (gexp-inputs exp)
  218. ((input)
  219. (and (eq? (gexp-input-thing input) file)
  220. (string=? (gexp-input-output input) "out"))))
  221. (equal? `(display ,expected) (gexp->sexp* exp)))))
  222. (test-assert "same input twice"
  223. (let ((exp (gexp (begin
  224. (display (ungexp coreutils))
  225. (display (ungexp coreutils))))))
  226. (and (gexp? exp)
  227. (match (gexp-inputs exp)
  228. ((input)
  229. (and (eq? (gexp-input-thing input) coreutils)
  230. (string=? (gexp-input-output input) "out"))))
  231. (let ((e `(display ,(derivation->output-path
  232. (package-derivation %store coreutils)))))
  233. (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
  234. (test-assert "two input packages, one derivation, one file"
  235. (let* ((drv (build-expression->derivation
  236. %store "foo" 'bar
  237. #:guile-for-build (package-derivation %store %bootstrap-guile)))
  238. (txt (add-text-to-store %store "foo" "Hello, world!"))
  239. (exp (gexp (begin
  240. (display (ungexp coreutils))
  241. (display (ungexp %bootstrap-guile))
  242. (display (ungexp drv))
  243. (display (ungexp txt))))))
  244. (define (match-input thing)
  245. (lambda (input)
  246. (eq? (gexp-input-thing input) thing)))
  247. (and (gexp? exp)
  248. (= 4 (length (gexp-inputs exp)))
  249. (every (lambda (input)
  250. (find (match-input input) (gexp-inputs exp)))
  251. (list drv coreutils %bootstrap-guile txt))
  252. (let ((e0 `(display ,(derivation->output-path
  253. (package-derivation %store coreutils))))
  254. (e1 `(display ,(derivation->output-path
  255. (package-derivation %store %bootstrap-guile))))
  256. (e2 `(display ,(derivation->output-path drv)))
  257. (e3 `(display ,txt)))
  258. (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
  259. (test-assert "file-append"
  260. (let* ((drv (package-derivation %store %bootstrap-guile))
  261. (fa (file-append %bootstrap-guile "/bin/guile"))
  262. (exp #~(here we go #$fa)))
  263. (and (match (gexp->sexp* exp)
  264. (('here 'we 'go (? string? result))
  265. (string=? result
  266. (string-append (derivation->output-path drv)
  267. "/bin/guile"))))
  268. (match (gexp-inputs exp)
  269. ((input)
  270. (and (eq? (gexp-input-thing input) fa)
  271. (string=? (gexp-input-output input) "out")))))))
  272. (test-assert "file-append, output"
  273. (let* ((drv (package-derivation %store glibc))
  274. (fa (file-append glibc "/lib" "/debug"))
  275. (exp #~(foo #$fa:debug)))
  276. (and (match (gexp->sexp* exp)
  277. (('foo (? string? result))
  278. (string=? result
  279. (string-append (derivation->output-path drv "debug")
  280. "/lib/debug"))))
  281. (match (gexp-inputs exp)
  282. ((input)
  283. (and (eq? (gexp-input-thing input) fa)
  284. (string=? (gexp-input-output input) "debug")))))))
  285. (test-assert "file-append, nested"
  286. (let* ((drv (package-derivation %store glibc))
  287. (dir (file-append glibc "/bin"))
  288. (slash (file-append dir "/"))
  289. (file (file-append slash "getent"))
  290. (exp #~(foo #$file)))
  291. (and (match (gexp->sexp* exp)
  292. (('foo (? string? result))
  293. (string=? result
  294. (string-append (derivation->output-path drv)
  295. "/bin/getent"))))
  296. (match (gexp-inputs exp)
  297. ((input)
  298. (eq? (gexp-input-thing input) file))))))
  299. (test-assert "file-append, raw store item"
  300. (let* ((obj (plain-file "example.txt" "Hello!"))
  301. (a (file-append obj "/a"))
  302. (b (file-append a "/b"))
  303. (c (file-append b "/c"))
  304. (exp #~(list #$c))
  305. (item (run-with-store %store (lower-object obj)))
  306. (lexp (run-with-store %store (lower-gexp exp))))
  307. (and (equal? (lowered-gexp-sexp lexp)
  308. `(list ,(string-append item "/a/b/c")))
  309. (equal? (lowered-gexp-sources lexp)
  310. (list item))
  311. (null? (lowered-gexp-inputs lexp)))))
  312. (test-assertm "with-parameters for %current-system"
  313. (mlet* %store-monad ((system -> (match (%current-system)
  314. ("aarch64-linux" "x86_64-linux")
  315. (_ "aarch64-linux")))
  316. (drv (package->derivation coreutils system))
  317. (obj -> (with-parameters ((%current-system system))
  318. coreutils))
  319. (result (lower-object obj)))
  320. (return (string=? (derivation-file-name drv)
  321. (derivation-file-name result)))))
  322. (test-assertm "with-parameters for %current-target-system"
  323. (mlet* %store-monad ((target -> "riscv64-linux-gnu")
  324. (drv (package->cross-derivation coreutils target))
  325. (obj -> (with-parameters
  326. ((%current-target-system target))
  327. coreutils))
  328. (result (lower-object obj)))
  329. (return (string=? (derivation-file-name drv)
  330. (derivation-file-name result)))))
  331. (test-assert "with-parameters + file-append"
  332. (let* ((system (match (%current-system)
  333. ("aarch64-linux" "x86_64-linux")
  334. (_ "aarch64-linux")))
  335. (drv (package-derivation %store coreutils system))
  336. (param (make-parameter 7))
  337. (exp #~(here we go #$(with-parameters ((%current-system system)
  338. (param 42))
  339. (if (= (param) 42)
  340. (file-append coreutils "/bin/touch")
  341. %bootstrap-guile)))))
  342. (match (gexp->sexp* exp)
  343. (('here 'we 'go (? string? result))
  344. (string=? result
  345. (string-append (derivation->output-path drv)
  346. "/bin/touch"))))))
  347. (test-equal "let-system"
  348. (list `(begin ,(%current-system) #t) '(system-binding)
  349. 'low '() '())
  350. (let* ((exp #~(begin
  351. #$(let-system system system)
  352. #t))
  353. (low (run-with-store %store (lower-gexp exp))))
  354. (list (lowered-gexp-sexp low)
  355. (match (gexp-inputs exp)
  356. ((input)
  357. (and (eq? (struct-vtable (gexp-input-thing input))
  358. (@@ (guix gexp) <system-binding>))
  359. (string=? (gexp-input-output input) "out")
  360. '(system-binding)))
  361. (x x))
  362. 'low
  363. (lowered-gexp-inputs low)
  364. (lowered-gexp-sources low))))
  365. (test-equal "let-system, target"
  366. (list `(list ,(%current-system) #f)
  367. `(list ,(%current-system) "aarch64-linux-gnu"))
  368. (let ((exp #~(list #$@(let-system (system target)
  369. (list system target)))))
  370. (list (gexp->sexp* exp)
  371. (gexp->sexp* exp "aarch64-linux-gnu"))))
  372. (test-equal "let-system, ungexp-native, target"
  373. `(here it is: ,(%current-system) #f)
  374. (let ((exp #~(here it is: #+@(let-system (system target)
  375. (list system target)))))
  376. (gexp->sexp* exp "aarch64-linux-gnu")))
  377. (test-equal "let-system, nested"
  378. (list `(system* ,(string-append "qemu-system-" (%current-system))
  379. "-m" "256")
  380. '(system-binding))
  381. (let ((exp #~(system*
  382. #+(let-system (system target)
  383. (file-append (@@ (gnu packages virtualization)
  384. qemu)
  385. "/bin/qemu-system-"
  386. system))
  387. "-m" "256")))
  388. (list (match (gexp->sexp* exp)
  389. (('system* command rest ...)
  390. `(system* ,(and (string-prefix? (%store-prefix) command)
  391. (basename command))
  392. ,@rest))
  393. (x x))
  394. (match (gexp-inputs exp)
  395. ((input)
  396. (and (eq? (struct-vtable (gexp-input-thing input))
  397. (@@ (guix gexp) <system-binding>))
  398. (string=? (gexp-input-output input) "out")
  399. (gexp-input-native? input)
  400. '(system-binding)))
  401. (x x)))))
  402. (test-assert "ungexp + ungexp-native"
  403. (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
  404. (ungexp coreutils)
  405. (ungexp-native glibc)
  406. (ungexp binutils))))
  407. (target "mips64el-linux")
  408. (guile (derivation->output-path
  409. (package-derivation %store %bootstrap-guile)))
  410. (cu (derivation->output-path
  411. (package-cross-derivation %store coreutils target)))
  412. (libc (derivation->output-path
  413. (package-derivation %store glibc)))
  414. (bu (derivation->output-path
  415. (package-cross-derivation %store binutils target))))
  416. (and (lset= equal?
  417. `((,%bootstrap-guile "out" #t)
  418. (,coreutils "out" #f)
  419. (,glibc "out" #t)
  420. (,binutils "out" #f))
  421. (map gexp-input->tuple (gexp-inputs exp)))
  422. (equal? `(list ,guile ,cu ,libc ,bu)
  423. (gexp->sexp* exp target)))))
  424. (test-equal "ungexp + ungexp-native, nested"
  425. `((,%bootstrap-guile "out" #f) (,coreutils "out" #t))
  426. (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
  427. (ungexp %bootstrap-guile)))))
  428. (map gexp-input->tuple (gexp-inputs exp))))
  429. (test-equal "ungexp + ungexp-native, nested, special mixture"
  430. `((,coreutils "out" #t))
  431. (let* ((foo (gexp (foo (ungexp-native coreutils))))
  432. (exp (gexp (bar (ungexp foo)))))
  433. (map gexp-input->tuple (gexp-inputs exp))))
  434. (test-assert "input list"
  435. (let ((exp (gexp (display
  436. '(ungexp (list %bootstrap-guile coreutils)))))
  437. (guile (derivation->output-path
  438. (package-derivation %store %bootstrap-guile)))
  439. (cu (derivation->output-path
  440. (package-derivation %store coreutils))))
  441. (and (lset= equal?
  442. `((,%bootstrap-guile "out" #f) (,coreutils "out" #f))
  443. (map gexp-input->tuple (gexp-inputs exp)))
  444. (equal? `(display '(,guile ,cu))
  445. (gexp->sexp* exp)))))
  446. (test-assert "input list + ungexp-native"
  447. (let* ((target "mips64el-linux")
  448. (exp (gexp (display
  449. (cons '(ungexp-native (list %bootstrap-guile coreutils))
  450. '(ungexp (list glibc binutils))))))
  451. (guile (derivation->output-path
  452. (package-derivation %store %bootstrap-guile)))
  453. (cu (derivation->output-path
  454. (package-derivation %store coreutils)))
  455. (xlibc (derivation->output-path
  456. (package-cross-derivation %store glibc target)))
  457. (xbu (derivation->output-path
  458. (package-cross-derivation %store binutils target))))
  459. (and (lset= equal?
  460. `((,%bootstrap-guile "out" #t) (,coreutils "out" #t)
  461. (,glibc "out" #f) (,binutils "out" #f))
  462. (map gexp-input->tuple (gexp-inputs exp)))
  463. (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
  464. (gexp->sexp* exp target)))))
  465. (test-assert "input list splicing"
  466. (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
  467. (outputs (list (derivation->output-path
  468. (package-derivation %store glibc)
  469. "debug")
  470. (derivation->output-path
  471. (package-derivation %store %bootstrap-guile))))
  472. (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
  473. (and (lset= equal?
  474. `((,glibc "debug" #f) (,%bootstrap-guile "out" #f))
  475. (map gexp-input->tuple (gexp-inputs exp)))
  476. (equal? (gexp->sexp* exp)
  477. `(list ,@(cons 5 outputs))))))
  478. (test-assert "input list splicing + ungexp-native-splicing"
  479. (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
  480. %bootstrap-guile))
  481. (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
  482. (and (lset= equal?
  483. `((,glibc "debug" #t) (,%bootstrap-guile "out" #t))
  484. (map gexp-input->tuple (gexp-inputs exp)))
  485. (equal? (gexp->sexp* exp) ;native
  486. (gexp->sexp* exp "mips64el-linux")))))
  487. (test-assert "gexp list splicing + ungexp-splicing"
  488. (let* ((inner (gexp (ungexp-native glibc)))
  489. (exp (gexp (list (ungexp-splicing (list inner))))))
  490. (and (equal? `((,glibc "out" #t))
  491. (map gexp-input->tuple (gexp-inputs exp)))
  492. (equal? (gexp->sexp* exp) ;native
  493. (gexp->sexp* exp "mips64el-linux")))))
  494. (test-equal "output list"
  495. 2
  496. (let ((exp (gexp (begin (mkdir (ungexp output))
  497. (mkdir (ungexp output "bar"))))))
  498. (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
  499. (test-assert "output list, combined gexps"
  500. (let* ((exp0 (gexp (mkdir (ungexp output))))
  501. (exp1 (gexp (mkdir (ungexp output "foo"))))
  502. (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
  503. (and (lset= equal?
  504. (append (gexp-outputs exp0) (gexp-outputs exp1))
  505. (gexp-outputs exp2))
  506. (= 2 (length (gexp-outputs exp2))))))
  507. (test-equal "output list, combined gexps, duplicate output"
  508. 1
  509. (let* ((exp0 (gexp (mkdir (ungexp output))))
  510. (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
  511. (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
  512. (length (gexp-outputs exp2))))
  513. (test-assert "output list + ungexp-splicing list, combined gexps"
  514. (let* ((exp0 (gexp (mkdir (ungexp output))))
  515. (exp1 (gexp (mkdir (ungexp output "foo"))))
  516. (exp2 (gexp (begin (display "hi!")
  517. (ungexp-splicing (list exp0 exp1))))))
  518. (and (lset= equal?
  519. (append (gexp-outputs exp0) (gexp-outputs exp1))
  520. (gexp-outputs exp2))
  521. (= 2 (length (gexp-outputs exp2))))))
  522. (test-assertm "gexp->file"
  523. (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
  524. (guile (package-file %bootstrap-guile))
  525. (sexp (gexp->sexp exp (%current-system) #f))
  526. (drv (gexp->file "foo" exp))
  527. (out -> (derivation->output-path drv))
  528. (done (built-derivations (list drv)))
  529. (refs (references* out)))
  530. (return (and (equal? sexp (call-with-input-file out read))
  531. (equal? (list guile) refs)))))
  532. (test-assertm "gexp->file + file-append"
  533. (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
  534. "/bin/guile"))
  535. (guile (package-file %bootstrap-guile))
  536. (drv (gexp->file "foo" exp))
  537. (out -> (derivation->output-path drv))
  538. (done (built-derivations (list drv)))
  539. (refs (references* out)))
  540. (return (and (equal? (string-append guile "/bin/guile")
  541. (call-with-input-file out read))
  542. (equal? (list guile) refs)))))
  543. (test-assertm "gexp->file + #:splice?"
  544. (mlet* %store-monad ((exp -> (list
  545. #~(define foo 'bar)
  546. #~(define guile #$%bootstrap-guile)))
  547. (guile (package-file %bootstrap-guile))
  548. (drv (gexp->file "splice" exp #:splice? #t))
  549. (out -> (derivation->output-path drv))
  550. (done (built-derivations (list drv)))
  551. (refs (references* out)))
  552. (pk 'splice out)
  553. (return (and (equal? `((define foo 'bar)
  554. (define guile ,guile)
  555. ,(call-with-input-string "" read))
  556. (call-with-input-file out
  557. (lambda (port)
  558. (list (read port) (read port) (read port)))))
  559. (equal? (list guile) refs)))))
  560. (test-assertm "gexp->derivation"
  561. (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
  562. (exp -> (gexp
  563. (begin
  564. (mkdir (ungexp output))
  565. (chdir (ungexp output))
  566. (symlink
  567. (string-append (ungexp %bootstrap-guile)
  568. "/bin/guile")
  569. "foo")
  570. (symlink (ungexp file)
  571. (ungexp output "2nd")))))
  572. (drv (gexp->derivation "foo" exp))
  573. (out -> (derivation->output-path drv))
  574. (out2 -> (derivation->output-path drv "2nd"))
  575. (done (built-derivations (list drv)))
  576. (refs (references* out))
  577. (refs2 (references* out2))
  578. (guile (package-file %bootstrap-guile "bin/guile")))
  579. (return (and (string=? (readlink (string-append out "/foo")) guile)
  580. (string=? (readlink out2) file)
  581. (equal? refs (list (dirname (dirname guile))))
  582. (equal? refs2 (list file))
  583. (null? (derivation-properties drv))))))
  584. (test-assertm "gexp->derivation properties"
  585. (mlet %store-monad ((drv (gexp->derivation "foo"
  586. #~(mkdir #$output)
  587. #:properties '((type . test)))))
  588. (return (equal? '((type . test))
  589. (derivation-properties drv)))))
  590. (test-assertm "gexp->derivation vs. grafts"
  591. (mlet* %store-monad ((graft? (set-grafting #f))
  592. (p0 -> (dummy-package "dummy"
  593. (arguments
  594. '(#:implicit-inputs? #f))))
  595. (r -> (package (inherit p0) (name "DuMMY")))
  596. (p1 -> (package (inherit p0) (replacement r)))
  597. (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
  598. (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
  599. (void (set-guile-for-build %bootstrap-guile))
  600. (drv0 (gexp->derivation "t" exp0 #:graft? #t))
  601. (drv1 (gexp->derivation "t" exp1 #:graft? #t))
  602. (drv1* (gexp->derivation "t" exp1 #:graft? #f))
  603. (_ (set-grafting graft?)))
  604. (return (and (not (string=? (derivation->output-path drv0)
  605. (derivation->output-path drv1)))
  606. (string=? (derivation->output-path drv0)
  607. (derivation->output-path drv1*))))))
  608. (test-assertm "gexp->derivation, composed gexps"
  609. (mlet* %store-monad ((exp0 -> (gexp (begin
  610. (mkdir (ungexp output))
  611. (chdir (ungexp output)))))
  612. (exp1 -> (gexp (symlink
  613. (string-append (ungexp %bootstrap-guile)
  614. "/bin/guile")
  615. "foo")))
  616. (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
  617. (drv (gexp->derivation "foo" exp))
  618. (out -> (derivation->output-path drv))
  619. (done (built-derivations (list drv)))
  620. (guile (package-file %bootstrap-guile "bin/guile")))
  621. (return (string=? (readlink (string-append out "/foo"))
  622. guile))))
  623. (test-assertm "gexp->derivation, default system"
  624. ;; The default system should be the one at '>>=' time, not the one at
  625. ;; invocation time. See <http://bugs.gnu.org/18002>.
  626. (let ((system (%current-system))
  627. (mdrv (parameterize ((%current-system "foobar64-linux"))
  628. (gexp->derivation "foo"
  629. (gexp
  630. (mkdir (ungexp output)))))))
  631. (mlet %store-monad ((drv mdrv))
  632. (return (string=? system (derivation-system drv))))))
  633. (test-assertm "gexp->derivation, local-file"
  634. (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
  635. (intd (interned-file file #:recursive? #f))
  636. (local -> (local-file file))
  637. (exp -> (gexp (begin
  638. (stat (ungexp local))
  639. (symlink (ungexp local)
  640. (ungexp output)))))
  641. (drv (gexp->derivation "local-file" exp)))
  642. (mbegin %store-monad
  643. (built-derivations (list drv))
  644. (return (string=? (readlink (derivation->output-path drv))
  645. intd)))))
  646. (test-assertm "gexp->derivation, cross-compilation"
  647. (mlet* %store-monad ((target -> "mips64el-linux")
  648. (exp -> (gexp (list (ungexp coreutils)
  649. (ungexp output))))
  650. (xdrv (gexp->derivation "foo" exp
  651. #:target target))
  652. (refs (references*
  653. (derivation-file-name xdrv)))
  654. (xcu (package->cross-derivation coreutils
  655. target))
  656. (cu (package->derivation coreutils)))
  657. (return (and (member (derivation-file-name xcu) refs)
  658. (not (member (derivation-file-name cu) refs))))))
  659. (test-assertm "gexp->derivation, ungexp-native"
  660. (mlet* %store-monad ((target -> "mips64el-linux")
  661. (exp -> (gexp (list (ungexp-native coreutils)
  662. (ungexp output))))
  663. (xdrv (gexp->derivation "foo" exp
  664. #:target target))
  665. (drv (gexp->derivation "foo" exp)))
  666. (return (string=? (derivation-file-name drv)
  667. (derivation-file-name xdrv)))))
  668. (test-assertm "gexp->derivation, ungexp + ungexp-native"
  669. (mlet* %store-monad ((target -> "mips64el-linux")
  670. (exp -> (gexp (list (ungexp-native coreutils)
  671. (ungexp glibc)
  672. (ungexp output))))
  673. (xdrv (gexp->derivation "foo" exp
  674. #:target target))
  675. (refs (references*
  676. (derivation-file-name xdrv)))
  677. (xglibc (package->cross-derivation glibc target))
  678. (cu (package->derivation coreutils)))
  679. (return (and (member (derivation-file-name cu) refs)
  680. (member (derivation-file-name xglibc) refs)))))
  681. (test-assertm "gexp->derivation, ungexp-native + composed gexps"
  682. (mlet* %store-monad ((target -> "mips64el-linux")
  683. (exp0 -> (gexp (list 1 2
  684. (ungexp coreutils))))
  685. (exp -> (gexp (list 0 (ungexp-native exp0))))
  686. (xdrv (gexp->derivation "foo" exp
  687. #:target target))
  688. (drv (gexp->derivation "foo" exp)))
  689. (return (string=? (derivation-file-name drv)
  690. (derivation-file-name xdrv)))))
  691. (test-assertm "gexp->derivation, store copy"
  692. (let ((build-one #~(call-with-output-file #$output
  693. (lambda (port)
  694. (display "This is the one." port))))
  695. (build-two (lambda (one)
  696. #~(begin
  697. (mkdir #$output)
  698. (symlink #$one (string-append #$output "/one"))
  699. (call-with-output-file (string-append #$output "/two")
  700. (lambda (port)
  701. (display "This is the second one." port))))))
  702. (build-drv #~(begin
  703. (use-modules (guix build store-copy)
  704. (guix build utils)
  705. (srfi srfi-1))
  706. (define (canonical-file? file)
  707. ;; Copied from (guix tests).
  708. (let ((st (lstat file)))
  709. (or (not (string-prefix? (%store-directory) file))
  710. (eq? 'symlink (stat:type st))
  711. (and (= 1 (stat:mtime st))
  712. (zero? (logand #o222 (stat:mode st)))))))
  713. (mkdir #$output)
  714. (populate-store '("graph") #$output
  715. #:deduplicate? #f)
  716. ;; Check whether 'populate-store' canonicalizes
  717. ;; permissions and timestamps.
  718. (unless (every canonical-file? (find-files #$output))
  719. (error "not canonical!" #$output)))))
  720. (mlet* %store-monad ((one (gexp->derivation "one" build-one))
  721. (two (gexp->derivation "two" (build-two one)))
  722. (drv (gexp->derivation "store-copy" build-drv
  723. #:references-graphs
  724. `(("graph" ,two))
  725. #:modules
  726. '((guix build store-copy)
  727. (guix progress)
  728. (guix records)
  729. (guix sets)
  730. (guix build utils))))
  731. (ok? (built-derivations (list drv)))
  732. (out -> (derivation->output-path drv)))
  733. (let ((one (derivation->output-path one))
  734. (two (derivation->output-path two)))
  735. (return (and ok?
  736. (file-exists? (string-append out "/" one))
  737. (file-exists? (string-append out "/" two))
  738. (file-exists? (string-append out "/" two "/two"))
  739. (string=? (readlink (string-append out "/" two "/one"))
  740. one)))))))
  741. (test-assertm "imported-files"
  742. (mlet* %store-monad
  743. ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
  744. ("a/b/c" . ,(search-path %load-path
  745. "guix/derivations.scm"))
  746. ("p/q" . ,(search-path %load-path "guix.scm"))
  747. ("p/z" . ,(search-path %load-path "guix/store.scm"))))
  748. (dir (imported-files files)))
  749. (mbegin %store-monad
  750. (return
  751. (every (match-lambda
  752. ((path . source)
  753. (equal? (call-with-input-file (string-append dir "/" path)
  754. get-bytevector-all)
  755. (call-with-input-file source
  756. get-bytevector-all))))
  757. files)))))
  758. (test-assertm "imported-files with file-like objects"
  759. (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
  760. (q-scm -> (search-path %load-path "ice-9/q.scm"))
  761. (files -> `(("a/b/c" . ,q-scm)
  762. ("p/q" . ,plain)))
  763. (drv (imported-files files)))
  764. (define (file=? file1 file2)
  765. ;; Assume deduplication is in place.
  766. (= (stat:ino (stat file1))
  767. (stat:ino (stat file2))))
  768. (mbegin %store-monad
  769. (built-derivations (list (pk 'drv drv)))
  770. (mlet %store-monad ((dir -> (derivation->output-path drv))
  771. (plain* (text-file "foo" "bar!"))
  772. (q-scm* (interned-file q-scm "c")))
  773. (return
  774. (and (file=? (string-append dir "/a/b/c") q-scm*)
  775. (file=? (string-append dir "/p/q") plain*)))))))
  776. (test-equal "gexp-modules & ungexp"
  777. '((bar) (foo))
  778. ((@@ (guix gexp) gexp-modules)
  779. #~(foo #$(with-imported-modules '((foo)) #~+)
  780. #+(with-imported-modules '((bar)) #~-))))
  781. (test-equal "gexp-modules & ungexp-splicing"
  782. '((foo) (bar))
  783. ((@@ (guix gexp) gexp-modules)
  784. #~(foo #$@(list (with-imported-modules '((foo)) #~+)
  785. (with-imported-modules '((bar)) #~-)))))
  786. (test-assert "gexp-modules deletes duplicates" ;<https://bugs.gnu.org/32966>
  787. (let ((make-file (lambda ()
  788. ;; Use 'eval' to make sure we get an object that's not
  789. ;; 'eq?' nor 'equal?' due to the closures it embeds.
  790. (eval '(scheme-file "bar.scm" #~(define-module (bar)))
  791. (current-module)))))
  792. (define result
  793. ((@@ (guix gexp) gexp-modules)
  794. (with-imported-modules `(((bar) => ,(make-file))
  795. ((bar) => ,(make-file))
  796. (foo) (foo))
  797. #~+)))
  798. (match result
  799. (((('bar) '=> (? scheme-file?)) ('foo)) #t))))
  800. (test-equal "gexp-modules and literal Scheme object"
  801. '()
  802. (gexp-modules #t))
  803. (test-assert "gexp-modules, warning"
  804. (string-match "tests/gexp.scm:[0-9]+:[0-9]+: warning: \
  805. importing.* \\(guix config\\) from the host"
  806. (call-with-output-string
  807. (lambda (port)
  808. (parameterize ((guix-warning-port port))
  809. (let* ((x (with-imported-modules '((guix config))
  810. #~(+ 1 2 3)))
  811. (y #~(+ 39 #$x)))
  812. (gexp-modules y)))))))
  813. (test-assertm "gexp->derivation #:modules"
  814. (mlet* %store-monad
  815. ((build -> #~(begin
  816. (use-modules (guix build utils))
  817. (mkdir-p (string-append #$output "/guile/guix/nix"))
  818. #t))
  819. (drv (gexp->derivation "test-with-modules" build
  820. #:modules '((guix build utils)))))
  821. (mbegin %store-monad
  822. (built-derivations (list drv))
  823. (let* ((p (derivation->output-path drv))
  824. (s (stat (string-append p "/guile/guix/nix"))))
  825. (return (eq? (stat:type s) 'directory))))))
  826. (test-assertm "gexp->derivation & with-imported-modules"
  827. ;; Same test as above, but using 'with-imported-modules'.
  828. (mlet* %store-monad
  829. ((build -> (with-imported-modules '((guix build utils))
  830. #~(begin
  831. (use-modules (guix build utils))
  832. (mkdir-p (string-append #$output "/guile/guix/nix"))
  833. #t)))
  834. (drv (gexp->derivation "test-with-modules" build)))
  835. (mbegin %store-monad
  836. (built-derivations (list drv))
  837. (let* ((p (derivation->output-path drv))
  838. (s (stat (string-append p "/guile/guix/nix"))))
  839. (return (eq? (stat:type s) 'directory))))))
  840. (test-assertm "gexp->derivation & nested with-imported-modules"
  841. (mlet* %store-monad
  842. ((build1 -> (with-imported-modules '((guix build utils))
  843. #~(begin
  844. (use-modules (guix build utils))
  845. (mkdir-p (string-append #$output "/guile/guix/nix"))
  846. #t)))
  847. (build2 -> (with-imported-modules '((guix build bournish))
  848. #~(begin
  849. (use-modules (guix build bournish)
  850. (system base compile))
  851. #+build1
  852. (call-with-output-file (string-append #$output "/b")
  853. (lambda (port)
  854. (write
  855. (read-and-compile (open-input-string "cd /foo")
  856. #:from %bournish-language
  857. #:to 'scheme)
  858. port))))))
  859. (drv (gexp->derivation "test-with-modules" build2)))
  860. (mbegin %store-monad
  861. (built-derivations (list drv))
  862. (let* ((p (derivation->output-path drv))
  863. (s (stat (string-append p "/guile/guix/nix")))
  864. (b (string-append p "/b")))
  865. (return (and (eq? (stat:type s) 'directory)
  866. (equal? '(chdir "/foo")
  867. (call-with-input-file b read))))))))
  868. (test-assertm "gexp->derivation & with-imported-module & computed module"
  869. (mlet* %store-monad
  870. ((module -> (scheme-file "x" #~(;; splice!
  871. (define-module (foo bar)
  872. #:export (the-answer))
  873. (define the-answer 42))
  874. #:splice? #t))
  875. (build -> (with-imported-modules `(((foo bar) => ,module)
  876. (guix build utils))
  877. #~(begin
  878. (use-modules (guix build utils)
  879. (foo bar))
  880. mkdir-p
  881. (call-with-output-file #$output
  882. (lambda (port)
  883. (write the-answer port))))))
  884. (drv (gexp->derivation "thing" build))
  885. (out -> (derivation->output-path drv)))
  886. (mbegin %store-monad
  887. (built-derivations (list drv))
  888. (return (= 42 (call-with-input-file out read))))))
  889. (test-equal "gexp-extensions & ungexp"
  890. (list sed grep)
  891. ((@@ (guix gexp) gexp-extensions)
  892. #~(foo #$(with-extensions (list grep) #~+)
  893. #+(with-extensions (list sed) #~-))))
  894. (test-equal "gexp-extensions & ungexp-splicing"
  895. (list grep sed)
  896. ((@@ (guix gexp) gexp-extensions)
  897. #~(foo #$@(list (with-extensions (list grep) #~+)
  898. (with-imported-modules '((foo))
  899. (with-extensions (list sed) #~-))))))
  900. (test-equal "gexp-extensions and literal Scheme object"
  901. '()
  902. ((@@ (guix gexp) gexp-extensions) #t))
  903. (test-assertm "gexp->derivation & with-extensions"
  904. ;; Create a fake Guile extension and make sure it is accessible both to the
  905. ;; imported modules and to the derivation build script.
  906. (mlet* %store-monad
  907. ((extension -> %extension-package)
  908. (module -> (scheme-file "x" #~( ;; splice!
  909. (define-module (foo)
  910. #:use-module (hg2g)
  911. #:export (multiply))
  912. (define (multiply x)
  913. (* the-answer x)))
  914. #:splice? #t))
  915. (build -> (with-extensions (list extension)
  916. (with-imported-modules `((guix build utils)
  917. ((foo) => ,module))
  918. #~(begin
  919. (use-modules (guix build utils)
  920. (hg2g) (foo))
  921. (call-with-output-file #$output
  922. (lambda (port)
  923. (write (list the-answer (multiply 2))
  924. port)))))))
  925. (drv (gexp->derivation "thingie" build
  926. ;; %BOOTSTRAP-GUILE is 2.0.
  927. #:effective-version "2.0"))
  928. (out -> (derivation->output-path drv)))
  929. (mbegin %store-monad
  930. (built-derivations (list drv))
  931. (return (equal? '(42 84) (call-with-input-file out read))))))
  932. (test-assertm "lower-gexp"
  933. (mlet* %store-monad
  934. ((extension -> %extension-package)
  935. (extension-drv (package->derivation %extension-package))
  936. (coreutils-drv (package->derivation coreutils))
  937. (exp -> (with-extensions (list extension)
  938. (with-imported-modules `((guix build utils))
  939. #~(begin
  940. (use-modules (guix build utils)
  941. (hg2g))
  942. #$coreutils:debug
  943. mkdir-p
  944. the-answer))))
  945. (lexp (lower-gexp exp
  946. #:effective-version "2.0")))
  947. (define (matching-input drv output)
  948. (lambda (input)
  949. (and (eq? (derivation-input-derivation input) drv)
  950. (equal? (derivation-input-sub-derivations input)
  951. (list output)))))
  952. (mbegin %store-monad
  953. (return (and (find (matching-input extension-drv "out")
  954. (lowered-gexp-inputs (pk 'lexp lexp)))
  955. (find (matching-input coreutils-drv "debug")
  956. (lowered-gexp-inputs lexp))
  957. (member (string-append
  958. (derivation->output-path extension-drv)
  959. "/share/guile/site/2.0")
  960. (lowered-gexp-load-path lexp))
  961. (= 2 (length (lowered-gexp-load-path lexp)))
  962. (member (string-append
  963. (derivation->output-path extension-drv)
  964. "/lib/guile/2.0/site-ccache")
  965. (lowered-gexp-load-compiled-path lexp))
  966. (= 2 (length (lowered-gexp-load-compiled-path lexp)))
  967. (eq? (derivation-input-derivation (lowered-gexp-guile lexp))
  968. (%guile-for-build)))))))
  969. (test-assertm "lower-gexp, raw-derivation-file"
  970. (mlet* %store-monad ((thing -> (program-file "prog" #~(display "hi!")))
  971. (exp -> #~(list #$(raw-derivation-file thing)))
  972. (drv (lower-object thing))
  973. (lexp (lower-gexp exp #:effective-version "2.0")))
  974. (return (and (equal? `(list ,(derivation-file-name drv))
  975. (lowered-gexp-sexp lexp))
  976. (equal? (list (derivation-file-name drv))
  977. (lowered-gexp-sources lexp))
  978. (null? (lowered-gexp-inputs lexp))))))
  979. (test-eq "lower-gexp, non-self-quoting input"
  980. +
  981. (guard (c ((gexp-input-error? c)
  982. (gexp-error-invalid-input c)))
  983. (run-with-store %store
  984. (lower-gexp #~(foo #$+)))))
  985. (test-equal "lower-gexp, character literal"
  986. '(#\+)
  987. (lowered-gexp-sexp
  988. (run-with-store %store
  989. (lower-gexp #~(#\+)))))
  990. (test-assertm "gexp->derivation #:references-graphs"
  991. (mlet* %store-monad
  992. ((one (text-file "one" (random-text)))
  993. (two (gexp->derivation "two"
  994. #~(symlink #$one #$output:chbouib)))
  995. (build -> (with-imported-modules '((guix build store-copy)
  996. (guix progress)
  997. (guix records)
  998. (guix sets)
  999. (guix build utils))
  1000. #~(begin
  1001. (use-modules (guix build store-copy))
  1002. (with-output-to-file #$output
  1003. (lambda ()
  1004. (write (map store-info-item
  1005. (call-with-input-file "guile"
  1006. read-reference-graph)))))
  1007. (with-output-to-file #$output:one
  1008. (lambda ()
  1009. (write (map store-info-item
  1010. (call-with-input-file "one"
  1011. read-reference-graph)))))
  1012. (with-output-to-file #$output:two
  1013. (lambda ()
  1014. (write (map store-info-item
  1015. (call-with-input-file "two"
  1016. read-reference-graph))))))))
  1017. (drv (gexp->derivation "ref-graphs" build
  1018. #:references-graphs `(("one" ,one)
  1019. ("two" ,two "chbouib")
  1020. ("guile" ,%bootstrap-guile))))
  1021. (ok? (built-derivations (list drv)))
  1022. (guile-drv (package->derivation %bootstrap-guile))
  1023. (bash (interned-file (search-bootstrap-binary "bash"
  1024. (%current-system))
  1025. "bash" #:recursive? #t))
  1026. (g-one -> (derivation->output-path drv "one"))
  1027. (g-two -> (derivation->output-path drv "two"))
  1028. (g-guile -> (derivation->output-path drv)))
  1029. (return (and ok?
  1030. (equal? (call-with-input-file g-one read) (list one))
  1031. (lset= string=?
  1032. (call-with-input-file g-two read)
  1033. (list one (derivation->output-path two "chbouib")))
  1034. ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
  1035. (lset= string=?
  1036. (call-with-input-file g-guile read)
  1037. (list (derivation->output-path guile-drv) bash))))))
  1038. (test-assertm "gexp->derivation #:references-graphs cross-compilation"
  1039. ;; The objects passed in #:references-graphs implicitly refer to
  1040. ;; cross-compiled derivations. Make sure this is the case.
  1041. (mlet* %store-monad ((drv1 (lower-object coreutils (%current-system)
  1042. #:target "i586-pc-gnu"))
  1043. (drv2 (lower-object coreutils (%current-system)
  1044. #:target #f))
  1045. (drv3 (gexp->derivation "three"
  1046. #~(symlink #$coreutils #$output)
  1047. #:target "i586-pc-gnu"
  1048. #:references-graphs
  1049. `(("coreutils" ,coreutils))))
  1050. (refs (references* (derivation-file-name drv3))))
  1051. (return (and (member (derivation-file-name drv1) refs)
  1052. (not (member (derivation-file-name drv2) refs))))))
  1053. (test-assertm "gexp->derivation #:allowed-references"
  1054. (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
  1055. #~(begin
  1056. (mkdir #$output)
  1057. (chdir #$output)
  1058. (symlink #$output "self")
  1059. (symlink #$%bootstrap-guile
  1060. "guile"))
  1061. #:allowed-references
  1062. (list "out" %bootstrap-guile))))
  1063. (built-derivations (list drv))))
  1064. (test-assertm "gexp->derivation #:allowed-references, specific output"
  1065. (mlet* %store-monad ((in (gexp->derivation "thing"
  1066. #~(begin
  1067. (mkdir #$output:ok)
  1068. (mkdir #$output:not-ok))))
  1069. (drv (gexp->derivation "allowed-refs"
  1070. #~(begin
  1071. (pk #$in:not-ok)
  1072. (mkdir #$output)
  1073. (chdir #$output)
  1074. (symlink #$output "self")
  1075. (symlink #$in:ok "ok"))
  1076. #:allowed-references
  1077. (list "out"
  1078. (gexp-input in "ok")))))
  1079. (built-derivations (list drv))))
  1080. (test-assert "gexp->derivation #:allowed-references, disallowed"
  1081. (let ((drv (run-with-store %store
  1082. (gexp->derivation "allowed-refs"
  1083. #~(begin
  1084. (mkdir #$output)
  1085. (chdir #$output)
  1086. (symlink #$%bootstrap-guile "guile"))
  1087. #:allowed-references '()))))
  1088. (guard (c ((store-protocol-error? c) #t))
  1089. (build-derivations %store (list drv))
  1090. #f)))
  1091. (test-assertm "gexp->derivation #:disallowed-references, allowed"
  1092. (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
  1093. #~(begin
  1094. (mkdir #$output)
  1095. (chdir #$output)
  1096. (symlink #$output "self")
  1097. (symlink #$%bootstrap-guile
  1098. "guile"))
  1099. #:disallowed-references '())))
  1100. (built-derivations (list drv))))
  1101. (test-assert "gexp->derivation #:disallowed-references"
  1102. (let ((drv (run-with-store %store
  1103. (gexp->derivation "disallowed-refs"
  1104. #~(begin
  1105. (mkdir #$output)
  1106. (chdir #$output)
  1107. (symlink #$%bootstrap-guile "guile"))
  1108. #:disallowed-references (list %bootstrap-guile)))))
  1109. (guard (c ((store-protocol-error? c) #t))
  1110. (build-derivations %store (list drv))
  1111. #f)))
  1112. (define shebang
  1113. (string-append "#!" (derivation->output-path (%guile-for-build))
  1114. "/bin/guile --no-auto-compile"))
  1115. ;; If we're going to hit the silly shebang limit (128 chars on Linux-based
  1116. ;; systems), then skip the following test.
  1117. (test-skip (if (> (string-length shebang) 127) 2 0))
  1118. (test-assertm "gexp->script"
  1119. (mlet* %store-monad ((n -> (random (expt 2 50)))
  1120. (exp -> (gexp
  1121. (system*
  1122. (string-append (ungexp %bootstrap-guile)
  1123. "/bin/guile")
  1124. "-c" (object->string
  1125. '(display (expt (ungexp n) 2))))))
  1126. (drv (gexp->script "guile-thing" exp
  1127. #:guile %bootstrap-guile))
  1128. (out -> (derivation->output-path drv))
  1129. (done (built-derivations (list drv))))
  1130. (let* ((pipe (open-input-pipe out))
  1131. (str (get-string-all pipe)))
  1132. (return (and (zero? (close-pipe pipe))
  1133. (= (expt n 2) (string->number str)))))))
  1134. (test-assert "gexp->script #:module-path"
  1135. (call-with-temporary-directory
  1136. (lambda (directory)
  1137. (define str
  1138. "Fake (guix base32) module!")
  1139. (mkdir (string-append directory "/guix"))
  1140. (call-with-output-file (string-append directory "/guix/base32.scm")
  1141. (lambda (port)
  1142. (write `(begin (define-module (guix base32))
  1143. (define-public %fake! ,str))
  1144. port)))
  1145. (run-with-store %store
  1146. (mlet* %store-monad ((exp -> (with-imported-modules '((guix base32))
  1147. (gexp (begin
  1148. (use-modules (guix base32))
  1149. (write (list %load-path
  1150. %fake!))))))
  1151. (drv (gexp->script "guile-thing" exp
  1152. #:guile %bootstrap-guile
  1153. #:module-path (list directory)))
  1154. (out -> (derivation->output-path drv))
  1155. (done (built-derivations (list drv))))
  1156. (let* ((pipe (open-input-pipe out))
  1157. (data (read pipe)))
  1158. (return (and (zero? (close-pipe pipe))
  1159. (match data
  1160. ((load-path str*)
  1161. (and (string=? str* str)
  1162. (not (member directory load-path)))))))))))))
  1163. (test-assertm "program-file"
  1164. (let* ((n (random (expt 2 50)))
  1165. (exp (with-imported-modules '((guix build utils))
  1166. (gexp (begin
  1167. (use-modules (guix build utils))
  1168. (display (ungexp n))))))
  1169. (file (program-file "program" exp
  1170. #:guile %bootstrap-guile)))
  1171. (mlet* %store-monad ((drv (lower-object file))
  1172. (out -> (derivation->output-path drv)))
  1173. (mbegin %store-monad
  1174. (built-derivations (list drv))
  1175. (let* ((pipe (open-input-pipe out))
  1176. (str (get-string-all pipe)))
  1177. (return (and (zero? (close-pipe pipe))
  1178. (= n (string->number str)))))))))
  1179. (test-assert "program-file #:module-path"
  1180. (call-with-temporary-directory
  1181. (lambda (directory)
  1182. (define text (random-text))
  1183. (call-with-output-file (string-append directory "/stupid-module.scm")
  1184. (lambda (port)
  1185. (write `(begin (define-module (stupid-module))
  1186. (define-public %stupid-thing ,text))
  1187. port)))
  1188. (let* ((exp (with-imported-modules '((stupid-module))
  1189. (gexp (begin
  1190. (use-modules (stupid-module))
  1191. (display %stupid-thing)))))
  1192. (file (program-file "program" exp
  1193. #:guile %bootstrap-guile
  1194. #:module-path (list directory))))
  1195. (run-with-store %store
  1196. (mlet* %store-monad ((drv (lower-object file))
  1197. (out -> (derivation->output-path drv)))
  1198. (mbegin %store-monad
  1199. (built-derivations (list drv))
  1200. (let* ((pipe (open-input-pipe out))
  1201. (str (get-string-all pipe)))
  1202. (return (and (zero? (close-pipe pipe))
  1203. (string=? text str)))))))))))
  1204. (test-assertm "program-file & with-extensions"
  1205. (let* ((exp (with-extensions (list %extension-package)
  1206. (gexp (begin
  1207. (use-modules (hg2g))
  1208. (display the-answer)))))
  1209. (file (program-file "program" exp
  1210. #:guile %bootstrap-guile)))
  1211. (mlet* %store-monad ((drv (lower-object file))
  1212. (out -> (derivation->output-path drv)))
  1213. (mbegin %store-monad
  1214. (built-derivations (list drv))
  1215. (let* ((pipe (open-input-pipe out))
  1216. (str (get-string-all pipe)))
  1217. (return (and (zero? (close-pipe pipe))
  1218. (= 42 (string->number str)))))))))
  1219. (test-assertm "program-file #:system"
  1220. (let* ((exp (with-imported-modules '((guix build utils))
  1221. (gexp (begin
  1222. (use-modules (guix build utils))
  1223. (display "hi!")))))
  1224. (system (if (string=? (%current-system) "x86_64-linux")
  1225. "armhf-linux"
  1226. "x86_64-linux"))
  1227. (file (program-file "program" exp)))
  1228. (mlet %store-monad ((drv (lower-object file system)))
  1229. (return (and (string=? (derivation-system drv) system)
  1230. (find (lambda (input)
  1231. (let ((drv (pk (derivation-input-derivation input))))
  1232. (and (string=? (derivation-name drv)
  1233. "module-import-compiled")
  1234. (string=? (derivation-system drv)
  1235. system))))
  1236. (derivation-inputs drv)))))))
  1237. (test-assertm "scheme-file"
  1238. (let* ((text (plain-file "foo" "Hello, world!"))
  1239. (scheme (scheme-file "bar" #~(list "foo" #$text))))
  1240. (mlet* %store-monad ((drv (lower-object scheme))
  1241. (text (lower-object text))
  1242. (out -> (derivation->output-path drv)))
  1243. (mbegin %store-monad
  1244. (built-derivations (list drv))
  1245. (mlet %store-monad ((refs (references* out)))
  1246. (return (and (equal? refs (list text))
  1247. (equal? `(list "foo" ,text)
  1248. (call-with-input-file out read)))))))))
  1249. (test-assertm "raw-derivation-file"
  1250. (let* ((exp #~(let ((drv #$(raw-derivation-file coreutils)))
  1251. (when (file-exists? drv)
  1252. (symlink drv #$output)))))
  1253. (mlet* %store-monad ((dep (lower-object coreutils))
  1254. (drv (gexp->derivation "drv-ref" exp))
  1255. (out -> (derivation->output-path drv)))
  1256. (mbegin %store-monad
  1257. (built-derivations (list drv))
  1258. (mlet %store-monad ((refs (references* out)))
  1259. (return (and (member (derivation-file-name dep)
  1260. (derivation-sources drv))
  1261. (not (member (derivation-file-name dep)
  1262. (map derivation-input-path
  1263. (derivation-inputs drv))))
  1264. (equal? (readlink out) (derivation-file-name dep))
  1265. (equal? refs (list (derivation-file-name dep))))))))))
  1266. (test-assert "text-file*"
  1267. (run-with-store %store
  1268. (mlet* %store-monad
  1269. ((drv (package->derivation %bootstrap-guile))
  1270. (guile -> (derivation->output-path drv))
  1271. (file (text-file "bar" "This is bar."))
  1272. (text (text-file* "foo"
  1273. %bootstrap-guile "/bin/guile "
  1274. (gexp-input %bootstrap-guile "out") "/bin/guile "
  1275. drv "/bin/guile "
  1276. file))
  1277. (done (built-derivations (list text)))
  1278. (out -> (derivation->output-path text))
  1279. (refs (references* out)))
  1280. ;; Make sure we get the right references and the right content.
  1281. (return (and (lset= string=? refs (list guile file))
  1282. (equal? (call-with-input-file out get-string-all)
  1283. (string-append guile "/bin/guile "
  1284. guile "/bin/guile "
  1285. guile "/bin/guile "
  1286. file)))))
  1287. #:guile-for-build (package-derivation %store %bootstrap-guile)))
  1288. (test-assertm "mixed-text-file"
  1289. (mlet* %store-monad ((file -> (mixed-text-file "mixed"
  1290. "export PATH="
  1291. %bootstrap-guile "/bin"))
  1292. (drv (lower-object file))
  1293. (out -> (derivation->output-path drv))
  1294. (guile-drv (package->derivation %bootstrap-guile))
  1295. (guile -> (derivation->output-path guile-drv)))
  1296. (mbegin %store-monad
  1297. (built-derivations (list drv))
  1298. (mlet %store-monad ((refs (references* out)))
  1299. (return (and (string=? (string-append "export PATH=" guile "/bin")
  1300. (call-with-input-file out get-string-all))
  1301. (equal? refs (list guile))))))))
  1302. (test-assertm "file-union"
  1303. (mlet* %store-monad ((union -> (file-union "union"
  1304. `(("a" ,(plain-file "a" "1"))
  1305. ("b/c/d" ,(plain-file "d" "2"))
  1306. ("e" ,(plain-file "e" "3")))))
  1307. (drv (lower-object union))
  1308. (out -> (derivation->output-path drv)))
  1309. (define (contents=? file str)
  1310. (string=? (call-with-input-file (string-append out "/" file)
  1311. get-string-all)
  1312. str))
  1313. (mbegin %store-monad
  1314. (built-derivations (list drv))
  1315. (return (and (contents=? "a" "1")
  1316. (contents=? "b/c/d" "2")
  1317. (contents=? "e" "3"))))))
  1318. (test-assert "gexp->derivation vs. %current-target-system"
  1319. (let ((mval (gexp->derivation "foo"
  1320. #~(begin
  1321. (mkdir #$output)
  1322. (foo #+gnu-make))
  1323. #:target #f)))
  1324. ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
  1325. ;; influence.
  1326. (parameterize ((%current-target-system "fooooo"))
  1327. (derivation? (run-with-store %store mval)))))
  1328. (test-assertm "lower-object"
  1329. (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
  1330. (drv2 (lower-object (package-source coreutils)))
  1331. (item (lower-object (plain-file "foo" "Hello!"))))
  1332. (return (and (derivation? drv1) (derivation? drv2)
  1333. (store-path? item)))))
  1334. (test-assertm "lower-object, computed-file"
  1335. (let* ((text (plain-file "foo" "Hello!"))
  1336. (exp #~(begin
  1337. (mkdir #$output)
  1338. (symlink #$%bootstrap-guile
  1339. (string-append #$output "/guile"))
  1340. (symlink #$text (string-append #$output "/text"))))
  1341. (computed (computed-file "computed" exp)))
  1342. (mlet* %store-monad ((text (lower-object text))
  1343. (guile-drv (lower-object %bootstrap-guile))
  1344. (comp-drv (lower-object computed))
  1345. (comp -> (derivation->output-path comp-drv)))
  1346. (mbegin %store-monad
  1347. (built-derivations (list comp-drv))
  1348. (return (and (string=? (readlink (string-append comp "/guile"))
  1349. (derivation->output-path guile-drv))
  1350. (string=? (readlink (string-append comp "/text"))
  1351. text)))))))
  1352. (test-equal "lower-object, computed-file, #:system"
  1353. '("mips64el-linux")
  1354. (run-with-store %store
  1355. (let* ((exp #~(symlink #$coreutils #$output))
  1356. (computed (computed-file "computed" exp
  1357. #:guile %bootstrap-guile)))
  1358. ;; Make sure that the SYSTEM argument to 'lower-object' is honored.
  1359. (mlet* %store-monad ((drv (lower-object computed "mips64el-linux"))
  1360. (refs (references* (derivation-file-name drv))))
  1361. (return (delete-duplicates
  1362. (filter-map (lambda (file)
  1363. (and (string-suffix? ".drv" file)
  1364. (let ((drv (read-derivation-from-file
  1365. file)))
  1366. (derivation-system drv))))
  1367. (cons (derivation-file-name drv)
  1368. refs))))))))
  1369. (test-assert "lower-object & gexp-input-error?"
  1370. (guard (c ((gexp-input-error? c)
  1371. (gexp-error-invalid-input c)))
  1372. (run-with-store %store
  1373. (lower-object (current-module))
  1374. #:guile-for-build (%guile-for-build))))
  1375. (test-assert "printer"
  1376. (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
  1377. \"/bin/uname\"\\) [[:graph:]]+tests/gexp\\.scm:[0-9]+:[0-9]+ [[:xdigit:]]+>$"
  1378. (with-output-to-string
  1379. (lambda ()
  1380. (write
  1381. (gexp (string-append (ungexp coreutils)
  1382. "/bin/uname")))))))
  1383. (test-assert "printer vs. ungexp-splicing"
  1384. (string-match "^#<gexp .* [[:xdigit:]]+>$"
  1385. (with-output-to-string
  1386. (lambda ()
  1387. ;; #~(begin #$@#~())
  1388. (write
  1389. (gexp (begin (ungexp-splicing (gexp ())))))))))
  1390. (test-equal "sugar"
  1391. '(gexp (foo (ungexp bar) (ungexp baz "out")
  1392. (ungexp (chbouib 42))
  1393. (ungexp-splicing (list x y z))
  1394. (ungexp-native foo) (ungexp-native foo "out")
  1395. (ungexp-native (chbouib 42))
  1396. (ungexp-native-splicing (list x y z))))
  1397. '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
  1398. #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
  1399. (test-assertm "gexp->file, cross-compilation"
  1400. (mlet* %store-monad ((target -> "aarch64-linux-gnu")
  1401. (exp -> (gexp (list (ungexp coreutils))))
  1402. (xdrv (gexp->file "foo" exp #:target target))
  1403. (refs (references*
  1404. (derivation-file-name xdrv)))
  1405. (xcu (package->cross-derivation coreutils
  1406. target))
  1407. (cu (package->derivation coreutils)))
  1408. (return (and (member (derivation-file-name xcu) refs)
  1409. (not (member (derivation-file-name cu) refs))))))
  1410. (test-assertm "gexp->file, cross-compilation with default target"
  1411. (mlet* %store-monad ((target -> "aarch64-linux-gnu")
  1412. (_ (set-current-target target))
  1413. (exp -> (gexp (list (ungexp coreutils))))
  1414. (xdrv (gexp->file "foo" exp))
  1415. (refs (references*
  1416. (derivation-file-name xdrv)))
  1417. (xcu (package->cross-derivation coreutils
  1418. target))
  1419. (cu (package->derivation coreutils)))
  1420. (return (and (member (derivation-file-name xcu) refs)
  1421. (not (member (derivation-file-name cu) refs))))))
  1422. (test-assertm "gexp->script, cross-compilation"
  1423. (mlet* %store-monad ((target -> "aarch64-linux-gnu")
  1424. (exp -> (gexp (list (ungexp coreutils))))
  1425. (xdrv (gexp->script "foo" exp #:target target))
  1426. (refs (references*
  1427. (derivation-file-name xdrv)))
  1428. (xcu (package->cross-derivation coreutils
  1429. target))
  1430. (cu (package->derivation coreutils)))
  1431. (return (and (member (derivation-file-name xcu) refs)
  1432. (not (member (derivation-file-name cu) refs))))))
  1433. (test-assertm "gexp->script, cross-compilation with default target"
  1434. (mlet* %store-monad ((target -> "aarch64-linux-gnu")
  1435. (_ (set-current-target target))
  1436. (exp -> (gexp (list (ungexp coreutils))))
  1437. (xdrv (gexp->script "foo" exp))
  1438. (refs (references*
  1439. (derivation-file-name xdrv)))
  1440. (xcu (package->cross-derivation coreutils
  1441. target))
  1442. (cu (package->derivation coreutils)))
  1443. (return (and (member (derivation-file-name xcu) refs)
  1444. (not (member (derivation-file-name cu) refs))))))
  1445. (test-end "gexp")
  1446. ;; Local Variables:
  1447. ;; eval: (put 'test-assertm 'scheme-indent-function 1)
  1448. ;; End: