gexp.scm 60 KB

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