gexp.scm 70 KB

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