gexp.scm 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017 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 tests)
  26. #:use-module ((guix build utils) #:select (with-directory-excursion))
  27. #:use-module (gnu packages)
  28. #:use-module (gnu packages base)
  29. #:use-module (gnu packages bootstrap)
  30. #:use-module (srfi srfi-1)
  31. #:use-module (srfi srfi-34)
  32. #:use-module (srfi srfi-64)
  33. #:use-module (rnrs io ports)
  34. #:use-module (ice-9 match)
  35. #:use-module (ice-9 regex)
  36. #:use-module (ice-9 popen)
  37. #:use-module (ice-9 ftw))
  38. ;; Test the (guix gexp) module.
  39. (define %store
  40. (open-connection-for-tests))
  41. ;; Globally disable grafts because they can trigger early builds.
  42. (%graft? #f)
  43. ;; For white-box testing.
  44. (define (gexp-inputs x)
  45. ((@@ (guix gexp) gexp-inputs) x))
  46. (define (gexp-native-inputs x)
  47. ((@@ (guix gexp) gexp-native-inputs) x))
  48. (define (gexp-outputs x)
  49. ((@@ (guix gexp) gexp-outputs) x))
  50. (define (gexp->sexp . x)
  51. (apply (@@ (guix gexp) gexp->sexp) x))
  52. (define* (gexp->sexp* exp #:optional target)
  53. (run-with-store %store (gexp->sexp exp
  54. #:target target)
  55. #:guile-for-build (%guile-for-build)))
  56. (define-syntax-rule (test-assertm name exp)
  57. (test-assert name
  58. (run-with-store %store exp
  59. #:guile-for-build (%guile-for-build))))
  60. (test-begin "gexp")
  61. (test-equal "no refs"
  62. '(display "hello!")
  63. (let ((exp (gexp (display "hello!"))))
  64. (and (gexp? exp)
  65. (null? (gexp-inputs exp))
  66. (gexp->sexp* exp))))
  67. (test-equal "unquote"
  68. '(display `(foo ,(+ 2 3)))
  69. (let ((exp (gexp (display `(foo ,(+ 2 3))))))
  70. (and (gexp? exp)
  71. (null? (gexp-inputs exp))
  72. (gexp->sexp* exp))))
  73. (test-assert "one input package"
  74. (let ((exp (gexp (display (ungexp coreutils)))))
  75. (and (gexp? exp)
  76. (match (gexp-inputs exp)
  77. (((p "out"))
  78. (eq? p coreutils)))
  79. (equal? `(display ,(derivation->output-path
  80. (package-derivation %store coreutils)))
  81. (gexp->sexp* exp)))))
  82. (test-assert "one input package, dotted list"
  83. (let ((exp (gexp (coreutils . (ungexp coreutils)))))
  84. (and (gexp? exp)
  85. (match (gexp-inputs exp)
  86. (((p "out"))
  87. (eq? p coreutils)))
  88. (equal? `(coreutils . ,(derivation->output-path
  89. (package-derivation %store coreutils)))
  90. (gexp->sexp* exp)))))
  91. (test-assert "one input origin"
  92. (let ((exp (gexp (display (ungexp (package-source coreutils))))))
  93. (and (gexp? exp)
  94. (match (gexp-inputs exp)
  95. (((o "out"))
  96. (eq? o (package-source coreutils))))
  97. (equal? `(display ,(derivation->output-path
  98. (package-source-derivation
  99. %store (package-source coreutils))))
  100. (gexp->sexp* exp)))))
  101. (test-assert "one local file"
  102. (let* ((file (search-path %load-path "guix.scm"))
  103. (local (local-file file))
  104. (exp (gexp (display (ungexp local))))
  105. (intd (add-to-store %store (basename file) #f
  106. "sha256" file)))
  107. (and (gexp? exp)
  108. (match (gexp-inputs exp)
  109. (((x "out"))
  110. (eq? x local)))
  111. (equal? `(display ,intd) (gexp->sexp* exp)))))
  112. (test-assert "one local file, symlink"
  113. (let ((file (search-path %load-path "guix.scm"))
  114. (link (tmpnam)))
  115. (dynamic-wind
  116. (const #t)
  117. (lambda ()
  118. (symlink (canonicalize-path file) link)
  119. (let* ((local (local-file link "my-file" #:recursive? #f))
  120. (exp (gexp (display (ungexp local))))
  121. (intd (add-to-store %store "my-file" #f
  122. "sha256" file)))
  123. (and (gexp? exp)
  124. (match (gexp-inputs exp)
  125. (((x "out"))
  126. (eq? x local)))
  127. (equal? `(display ,intd) (gexp->sexp* exp)))))
  128. (lambda ()
  129. (false-if-exception (delete-file link))))))
  130. (test-equal "local-file, relative file name"
  131. (canonicalize-path (search-path %load-path "guix/base32.scm"))
  132. (let ((directory (dirname (search-path %load-path
  133. "guix/build-system/gnu.scm"))))
  134. (with-directory-excursion directory
  135. (let ((file (local-file "../guix/base32.scm")))
  136. (local-file-absolute-file-name file)))))
  137. (test-assertm "local-file, #:select?"
  138. (mlet* %store-monad ((select? -> (lambda (file stat)
  139. (member (basename file)
  140. '("guix.scm" "tests"
  141. "gexp.scm"))))
  142. (file -> (local-file ".." "directory"
  143. #:recursive? #t
  144. #:select? select?))
  145. (dir (lower-object file)))
  146. (return (and (store-path? dir)
  147. (equal? (scandir dir)
  148. '("." ".." "guix.scm" "tests"))
  149. (equal? (scandir (string-append dir "/tests"))
  150. '("." ".." "gexp.scm"))))))
  151. (test-assert "one plain file"
  152. (let* ((file (plain-file "hi" "Hello, world!"))
  153. (exp (gexp (display (ungexp file))))
  154. (expected (add-text-to-store %store "hi" "Hello, world!")))
  155. (and (gexp? exp)
  156. (match (gexp-inputs exp)
  157. (((x "out"))
  158. (eq? x file)))
  159. (equal? `(display ,expected) (gexp->sexp* exp)))))
  160. (test-assert "same input twice"
  161. (let ((exp (gexp (begin
  162. (display (ungexp coreutils))
  163. (display (ungexp coreutils))))))
  164. (and (gexp? exp)
  165. (match (gexp-inputs exp)
  166. (((p "out"))
  167. (eq? p coreutils)))
  168. (let ((e `(display ,(derivation->output-path
  169. (package-derivation %store coreutils)))))
  170. (equal? `(begin ,e ,e) (gexp->sexp* exp))))))
  171. (test-assert "two input packages, one derivation, one file"
  172. (let* ((drv (build-expression->derivation
  173. %store "foo" 'bar
  174. #:guile-for-build (package-derivation %store %bootstrap-guile)))
  175. (txt (add-text-to-store %store "foo" "Hello, world!"))
  176. (exp (gexp (begin
  177. (display (ungexp coreutils))
  178. (display (ungexp %bootstrap-guile))
  179. (display (ungexp drv))
  180. (display (ungexp txt))))))
  181. (define (match-input thing)
  182. (match-lambda
  183. ((drv-or-pkg _ ...)
  184. (eq? thing drv-or-pkg))))
  185. (and (gexp? exp)
  186. (= 4 (length (gexp-inputs exp)))
  187. (every (lambda (input)
  188. (find (match-input input) (gexp-inputs exp)))
  189. (list drv coreutils %bootstrap-guile txt))
  190. (let ((e0 `(display ,(derivation->output-path
  191. (package-derivation %store coreutils))))
  192. (e1 `(display ,(derivation->output-path
  193. (package-derivation %store %bootstrap-guile))))
  194. (e2 `(display ,(derivation->output-path drv)))
  195. (e3 `(display ,txt)))
  196. (equal? `(begin ,e0 ,e1 ,e2 ,e3) (gexp->sexp* exp))))))
  197. (test-assert "file-append"
  198. (let* ((drv (package-derivation %store %bootstrap-guile))
  199. (fa (file-append %bootstrap-guile "/bin/guile"))
  200. (exp #~(here we go #$fa)))
  201. (and (match (gexp->sexp* exp)
  202. (('here 'we 'go (? string? result))
  203. (string=? result
  204. (string-append (derivation->output-path drv)
  205. "/bin/guile"))))
  206. (match (gexp-inputs exp)
  207. (((thing "out"))
  208. (eq? thing fa))))))
  209. (test-assert "file-append, output"
  210. (let* ((drv (package-derivation %store glibc))
  211. (fa (file-append glibc "/lib" "/debug"))
  212. (exp #~(foo #$fa:debug)))
  213. (and (match (gexp->sexp* exp)
  214. (('foo (? string? result))
  215. (string=? result
  216. (string-append (derivation->output-path drv "debug")
  217. "/lib/debug"))))
  218. (match (gexp-inputs exp)
  219. (((thing "debug"))
  220. (eq? thing fa))))))
  221. (test-assert "file-append, nested"
  222. (let* ((drv (package-derivation %store glibc))
  223. (dir (file-append glibc "/bin"))
  224. (slash (file-append dir "/"))
  225. (file (file-append slash "getent"))
  226. (exp #~(foo #$file)))
  227. (and (match (gexp->sexp* exp)
  228. (('foo (? string? result))
  229. (string=? result
  230. (string-append (derivation->output-path drv)
  231. "/bin/getent"))))
  232. (match (gexp-inputs exp)
  233. (((thing "out"))
  234. (eq? thing file))))))
  235. (test-assert "ungexp + ungexp-native"
  236. (let* ((exp (gexp (list (ungexp-native %bootstrap-guile)
  237. (ungexp coreutils)
  238. (ungexp-native glibc)
  239. (ungexp binutils))))
  240. (target "mips64el-linux")
  241. (guile (derivation->output-path
  242. (package-derivation %store %bootstrap-guile)))
  243. (cu (derivation->output-path
  244. (package-cross-derivation %store coreutils target)))
  245. (libc (derivation->output-path
  246. (package-derivation %store glibc)))
  247. (bu (derivation->output-path
  248. (package-cross-derivation %store binutils target))))
  249. (and (lset= equal?
  250. `((,%bootstrap-guile "out") (,glibc "out"))
  251. (gexp-native-inputs exp))
  252. (lset= equal?
  253. `((,coreutils "out") (,binutils "out"))
  254. (gexp-inputs exp))
  255. (equal? `(list ,guile ,cu ,libc ,bu)
  256. (gexp->sexp* exp target)))))
  257. (test-equal "ungexp + ungexp-native, nested"
  258. (list `((,%bootstrap-guile "out")) '<> `((,coreutils "out")))
  259. (let* ((exp (gexp (list (ungexp-native (gexp (ungexp coreutils)))
  260. (ungexp %bootstrap-guile)))))
  261. (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
  262. (test-equal "ungexp + ungexp-native, nested, special mixture"
  263. `(() <> ((,coreutils "out")))
  264. ;; (gexp-native-inputs exp) used to return '(), wrongfully.
  265. (let* ((foo (gexp (foo (ungexp-native coreutils))))
  266. (exp (gexp (bar (ungexp foo)))))
  267. (list (gexp-inputs exp) '<> (gexp-native-inputs exp))))
  268. (test-assert "input list"
  269. (let ((exp (gexp (display
  270. '(ungexp (list %bootstrap-guile coreutils)))))
  271. (guile (derivation->output-path
  272. (package-derivation %store %bootstrap-guile)))
  273. (cu (derivation->output-path
  274. (package-derivation %store coreutils))))
  275. (and (lset= equal?
  276. `((,%bootstrap-guile "out") (,coreutils "out"))
  277. (gexp-inputs exp))
  278. (equal? `(display '(,guile ,cu))
  279. (gexp->sexp* exp)))))
  280. (test-assert "input list + ungexp-native"
  281. (let* ((target "mips64el-linux")
  282. (exp (gexp (display
  283. (cons '(ungexp-native (list %bootstrap-guile coreutils))
  284. '(ungexp (list glibc binutils))))))
  285. (guile (derivation->output-path
  286. (package-derivation %store %bootstrap-guile)))
  287. (cu (derivation->output-path
  288. (package-derivation %store coreutils)))
  289. (xlibc (derivation->output-path
  290. (package-cross-derivation %store glibc target)))
  291. (xbu (derivation->output-path
  292. (package-cross-derivation %store binutils target))))
  293. (and (lset= equal?
  294. `((,%bootstrap-guile "out") (,coreutils "out"))
  295. (gexp-native-inputs exp))
  296. (lset= equal?
  297. `((,glibc "out") (,binutils "out"))
  298. (gexp-inputs exp))
  299. (equal? `(display (cons '(,guile ,cu) '(,xlibc ,xbu)))
  300. (gexp->sexp* exp target)))))
  301. (test-assert "input list splicing"
  302. (let* ((inputs (list (gexp-input glibc "debug") %bootstrap-guile))
  303. (outputs (list (derivation->output-path
  304. (package-derivation %store glibc)
  305. "debug")
  306. (derivation->output-path
  307. (package-derivation %store %bootstrap-guile))))
  308. (exp (gexp (list (ungexp-splicing (cons (+ 2 3) inputs))))))
  309. (and (lset= equal?
  310. `((,glibc "debug") (,%bootstrap-guile "out"))
  311. (gexp-inputs exp))
  312. (equal? (gexp->sexp* exp)
  313. `(list ,@(cons 5 outputs))))))
  314. (test-assert "input list splicing + ungexp-native-splicing"
  315. (let* ((inputs (list (gexp-input glibc "debug" #:native? #t)
  316. %bootstrap-guile))
  317. (exp (gexp (list (ungexp-native-splicing (cons (+ 2 3) inputs))))))
  318. (and (lset= equal?
  319. `((,glibc "debug") (,%bootstrap-guile "out"))
  320. (gexp-native-inputs exp))
  321. (null? (gexp-inputs exp))
  322. (equal? (gexp->sexp* exp) ;native
  323. (gexp->sexp* exp "mips64el-linux")))))
  324. (test-assert "gexp list splicing + ungexp-splicing"
  325. (let* ((inner (gexp (ungexp-native glibc)))
  326. (exp (gexp (list (ungexp-splicing (list inner))))))
  327. (and (equal? `((,glibc "out")) (gexp-native-inputs exp))
  328. (null? (gexp-inputs exp))
  329. (equal? (gexp->sexp* exp) ;native
  330. (gexp->sexp* exp "mips64el-linux")))))
  331. (test-equal "output list"
  332. 2
  333. (let ((exp (gexp (begin (mkdir (ungexp output))
  334. (mkdir (ungexp output "bar"))))))
  335. (length (gexp-outputs exp)))) ;XXX: <output-ref> is private
  336. (test-assert "output list, combined gexps"
  337. (let* ((exp0 (gexp (mkdir (ungexp output))))
  338. (exp1 (gexp (mkdir (ungexp output "foo"))))
  339. (exp2 (gexp (begin (display "hi!") (ungexp exp0) (ungexp exp1)))))
  340. (and (lset= equal?
  341. (append (gexp-outputs exp0) (gexp-outputs exp1))
  342. (gexp-outputs exp2))
  343. (= 2 (length (gexp-outputs exp2))))))
  344. (test-equal "output list, combined gexps, duplicate output"
  345. 1
  346. (let* ((exp0 (gexp (mkdir (ungexp output))))
  347. (exp1 (gexp (begin (mkdir (ungexp output)) (ungexp exp0))))
  348. (exp2 (gexp (begin (mkdir (ungexp output)) (ungexp exp1)))))
  349. (length (gexp-outputs exp2))))
  350. (test-assert "output list + ungexp-splicing list, combined gexps"
  351. (let* ((exp0 (gexp (mkdir (ungexp output))))
  352. (exp1 (gexp (mkdir (ungexp output "foo"))))
  353. (exp2 (gexp (begin (display "hi!")
  354. (ungexp-splicing (list exp0 exp1))))))
  355. (and (lset= equal?
  356. (append (gexp-outputs exp0) (gexp-outputs exp1))
  357. (gexp-outputs exp2))
  358. (= 2 (length (gexp-outputs exp2))))))
  359. (test-assertm "gexp->file"
  360. (mlet* %store-monad ((exp -> (gexp (display (ungexp %bootstrap-guile))))
  361. (guile (package-file %bootstrap-guile))
  362. (sexp (gexp->sexp exp))
  363. (drv (gexp->file "foo" exp))
  364. (out -> (derivation->output-path drv))
  365. (done (built-derivations (list drv)))
  366. (refs (references* out)))
  367. (return (and (equal? sexp (call-with-input-file out read))
  368. (equal? (list guile) refs)))))
  369. (test-assertm "gexp->file + file-append"
  370. (mlet* %store-monad ((exp -> #~#$(file-append %bootstrap-guile
  371. "/bin/guile"))
  372. (guile (package-file %bootstrap-guile))
  373. (drv (gexp->file "foo" exp))
  374. (out -> (derivation->output-path drv))
  375. (done (built-derivations (list drv)))
  376. (refs (references* out)))
  377. (return (and (equal? (string-append guile "/bin/guile")
  378. (call-with-input-file out read))
  379. (equal? (list guile) refs)))))
  380. (test-assertm "gexp->derivation"
  381. (mlet* %store-monad ((file (text-file "foo" "Hello, world!"))
  382. (exp -> (gexp
  383. (begin
  384. (mkdir (ungexp output))
  385. (chdir (ungexp output))
  386. (symlink
  387. (string-append (ungexp %bootstrap-guile)
  388. "/bin/guile")
  389. "foo")
  390. (symlink (ungexp file)
  391. (ungexp output "2nd")))))
  392. (drv (gexp->derivation "foo" exp))
  393. (out -> (derivation->output-path drv))
  394. (out2 -> (derivation->output-path drv "2nd"))
  395. (done (built-derivations (list drv)))
  396. (refs (references* out))
  397. (refs2 (references* out2))
  398. (guile (package-file %bootstrap-guile "bin/guile")))
  399. (return (and (string=? (readlink (string-append out "/foo")) guile)
  400. (string=? (readlink out2) file)
  401. (equal? refs (list (dirname (dirname guile))))
  402. (equal? refs2 (list file))))))
  403. (test-assertm "gexp->derivation vs. grafts"
  404. (mlet* %store-monad ((graft? (set-grafting #f))
  405. (p0 -> (dummy-package "dummy"
  406. (arguments
  407. '(#:implicit-inputs? #f))))
  408. (r -> (package (inherit p0) (name "DuMMY")))
  409. (p1 -> (package (inherit p0) (replacement r)))
  410. (exp0 -> (gexp (frob (ungexp p0) (ungexp output))))
  411. (exp1 -> (gexp (frob (ungexp p1) (ungexp output))))
  412. (void (set-guile-for-build %bootstrap-guile))
  413. (drv0 (gexp->derivation "t" exp0 #:graft? #t))
  414. (drv1 (gexp->derivation "t" exp1 #:graft? #t))
  415. (drv1* (gexp->derivation "t" exp1 #:graft? #f))
  416. (_ (set-grafting graft?)))
  417. (return (and (not (string=? (derivation->output-path drv0)
  418. (derivation->output-path drv1)))
  419. (string=? (derivation->output-path drv0)
  420. (derivation->output-path drv1*))))))
  421. (test-assertm "gexp->derivation, composed gexps"
  422. (mlet* %store-monad ((exp0 -> (gexp (begin
  423. (mkdir (ungexp output))
  424. (chdir (ungexp output)))))
  425. (exp1 -> (gexp (symlink
  426. (string-append (ungexp %bootstrap-guile)
  427. "/bin/guile")
  428. "foo")))
  429. (exp -> (gexp (begin (ungexp exp0) (ungexp exp1))))
  430. (drv (gexp->derivation "foo" exp))
  431. (out -> (derivation->output-path drv))
  432. (done (built-derivations (list drv)))
  433. (guile (package-file %bootstrap-guile "bin/guile")))
  434. (return (string=? (readlink (string-append out "/foo"))
  435. guile))))
  436. (test-assertm "gexp->derivation, default system"
  437. ;; The default system should be the one at '>>=' time, not the one at
  438. ;; invocation time. See <http://bugs.gnu.org/18002>.
  439. (let ((system (%current-system))
  440. (mdrv (parameterize ((%current-system "foobar64-linux"))
  441. (gexp->derivation "foo"
  442. (gexp
  443. (mkdir (ungexp output)))))))
  444. (mlet %store-monad ((drv mdrv))
  445. (return (string=? system (derivation-system drv))))))
  446. (test-assertm "gexp->derivation, local-file"
  447. (mlet* %store-monad ((file -> (search-path %load-path "guix.scm"))
  448. (intd (interned-file file #:recursive? #f))
  449. (local -> (local-file file))
  450. (exp -> (gexp (begin
  451. (stat (ungexp local))
  452. (symlink (ungexp local)
  453. (ungexp output)))))
  454. (drv (gexp->derivation "local-file" exp)))
  455. (mbegin %store-monad
  456. (built-derivations (list drv))
  457. (return (string=? (readlink (derivation->output-path drv))
  458. intd)))))
  459. (test-assertm "gexp->derivation, cross-compilation"
  460. (mlet* %store-monad ((target -> "mips64el-linux")
  461. (exp -> (gexp (list (ungexp coreutils)
  462. (ungexp output))))
  463. (xdrv (gexp->derivation "foo" exp
  464. #:target target))
  465. (refs (references*
  466. (derivation-file-name xdrv)))
  467. (xcu (package->cross-derivation coreutils
  468. target))
  469. (cu (package->derivation coreutils)))
  470. (return (and (member (derivation-file-name xcu) refs)
  471. (not (member (derivation-file-name cu) refs))))))
  472. (test-assertm "gexp->derivation, ungexp-native"
  473. (mlet* %store-monad ((target -> "mips64el-linux")
  474. (exp -> (gexp (list (ungexp-native coreutils)
  475. (ungexp output))))
  476. (xdrv (gexp->derivation "foo" exp
  477. #:target target))
  478. (drv (gexp->derivation "foo" exp)))
  479. (return (string=? (derivation-file-name drv)
  480. (derivation-file-name xdrv)))))
  481. (test-assertm "gexp->derivation, ungexp + ungexp-native"
  482. (mlet* %store-monad ((target -> "mips64el-linux")
  483. (exp -> (gexp (list (ungexp-native coreutils)
  484. (ungexp glibc)
  485. (ungexp output))))
  486. (xdrv (gexp->derivation "foo" exp
  487. #:target target))
  488. (refs (references*
  489. (derivation-file-name xdrv)))
  490. (xglibc (package->cross-derivation glibc target))
  491. (cu (package->derivation coreutils)))
  492. (return (and (member (derivation-file-name cu) refs)
  493. (member (derivation-file-name xglibc) refs)))))
  494. (test-assertm "gexp->derivation, ungexp-native + composed gexps"
  495. (mlet* %store-monad ((target -> "mips64el-linux")
  496. (exp0 -> (gexp (list 1 2
  497. (ungexp coreutils))))
  498. (exp -> (gexp (list 0 (ungexp-native exp0))))
  499. (xdrv (gexp->derivation "foo" exp
  500. #:target target))
  501. (drv (gexp->derivation "foo" exp)))
  502. (return (string=? (derivation-file-name drv)
  503. (derivation-file-name xdrv)))))
  504. (test-assertm "gexp->derivation, store copy"
  505. (let ((build-one #~(call-with-output-file #$output
  506. (lambda (port)
  507. (display "This is the one." port))))
  508. (build-two (lambda (one)
  509. #~(begin
  510. (mkdir #$output)
  511. (symlink #$one (string-append #$output "/one"))
  512. (call-with-output-file (string-append #$output "/two")
  513. (lambda (port)
  514. (display "This is the second one." port))))))
  515. (build-drv #~(begin
  516. (use-modules (guix build store-copy))
  517. (mkdir #$output)
  518. (populate-store '("graph") #$output))))
  519. (mlet* %store-monad ((one (gexp->derivation "one" build-one))
  520. (two (gexp->derivation "two" (build-two one)))
  521. (drv (gexp->derivation "store-copy" build-drv
  522. #:references-graphs
  523. `(("graph" ,two))
  524. #:modules
  525. '((guix build store-copy)
  526. (guix build utils))))
  527. (ok? (built-derivations (list drv)))
  528. (out -> (derivation->output-path drv)))
  529. (let ((one (derivation->output-path one))
  530. (two (derivation->output-path two)))
  531. (return (and ok?
  532. (file-exists? (string-append out "/" one))
  533. (file-exists? (string-append out "/" two))
  534. (file-exists? (string-append out "/" two "/two"))
  535. (string=? (readlink (string-append out "/" two "/one"))
  536. one)))))))
  537. (test-assertm "imported-files"
  538. (mlet* %store-monad
  539. ((files -> `(("x" . ,(search-path %load-path "ice-9/q.scm"))
  540. ("a/b/c" . ,(search-path %load-path
  541. "guix/derivations.scm"))
  542. ("p/q" . ,(search-path %load-path "guix.scm"))
  543. ("p/z" . ,(search-path %load-path "guix/store.scm"))))
  544. (drv (imported-files files)))
  545. (mbegin %store-monad
  546. (built-derivations (list drv))
  547. (let ((dir (derivation->output-path drv)))
  548. (return
  549. (every (match-lambda
  550. ((path . source)
  551. (equal? (call-with-input-file (string-append dir "/" path)
  552. get-bytevector-all)
  553. (call-with-input-file source
  554. get-bytevector-all))))
  555. files))))))
  556. (test-assertm "imported-files with file-like objects"
  557. (mlet* %store-monad ((plain -> (plain-file "foo" "bar!"))
  558. (q-scm -> (search-path %load-path "ice-9/q.scm"))
  559. (files -> `(("a/b/c" . ,q-scm)
  560. ("p/q" . ,plain)))
  561. (drv (imported-files files)))
  562. (mbegin %store-monad
  563. (built-derivations (list drv))
  564. (mlet %store-monad ((dir -> (derivation->output-path drv))
  565. (plain* (text-file "foo" "bar!"))
  566. (q-scm* (interned-file q-scm "c")))
  567. (return
  568. (and (string=? (readlink (string-append dir "/a/b/c"))
  569. q-scm*)
  570. (string=? (readlink (string-append dir "/p/q"))
  571. plain*)))))))
  572. (test-equal "gexp-modules & ungexp"
  573. '((bar) (foo))
  574. ((@@ (guix gexp) gexp-modules)
  575. #~(foo #$(with-imported-modules '((foo)) #~+)
  576. #+(with-imported-modules '((bar)) #~-))))
  577. (test-equal "gexp-modules & ungexp-splicing"
  578. '((foo) (bar))
  579. ((@@ (guix gexp) gexp-modules)
  580. #~(foo #$@(list (with-imported-modules '((foo)) #~+)
  581. (with-imported-modules '((bar)) #~-)))))
  582. (test-equal "gexp-modules and literal Scheme object"
  583. '()
  584. (gexp-modules #t))
  585. (test-assertm "gexp->derivation #:modules"
  586. (mlet* %store-monad
  587. ((build -> #~(begin
  588. (use-modules (guix build utils))
  589. (mkdir-p (string-append #$output "/guile/guix/nix"))
  590. #t))
  591. (drv (gexp->derivation "test-with-modules" build
  592. #:modules '((guix build utils)))))
  593. (mbegin %store-monad
  594. (built-derivations (list drv))
  595. (let* ((p (derivation->output-path drv))
  596. (s (stat (string-append p "/guile/guix/nix"))))
  597. (return (eq? (stat:type s) 'directory))))))
  598. (test-assertm "gexp->derivation & with-imported-modules"
  599. ;; Same test as above, but using 'with-imported-modules'.
  600. (mlet* %store-monad
  601. ((build -> (with-imported-modules '((guix build utils))
  602. #~(begin
  603. (use-modules (guix build utils))
  604. (mkdir-p (string-append #$output "/guile/guix/nix"))
  605. #t)))
  606. (drv (gexp->derivation "test-with-modules" build)))
  607. (mbegin %store-monad
  608. (built-derivations (list drv))
  609. (let* ((p (derivation->output-path drv))
  610. (s (stat (string-append p "/guile/guix/nix"))))
  611. (return (eq? (stat:type s) 'directory))))))
  612. (test-assertm "gexp->derivation & nested with-imported-modules"
  613. (mlet* %store-monad
  614. ((build1 -> (with-imported-modules '((guix build utils))
  615. #~(begin
  616. (use-modules (guix build utils))
  617. (mkdir-p (string-append #$output "/guile/guix/nix"))
  618. #t)))
  619. (build2 -> (with-imported-modules '((guix build bournish))
  620. #~(begin
  621. (use-modules (guix build bournish)
  622. (system base compile))
  623. #+build1
  624. (call-with-output-file (string-append #$output "/b")
  625. (lambda (port)
  626. (write
  627. (read-and-compile (open-input-string "cd /foo")
  628. #:from %bournish-language
  629. #:to 'scheme)
  630. port))))))
  631. (drv (gexp->derivation "test-with-modules" build2)))
  632. (mbegin %store-monad
  633. (built-derivations (list drv))
  634. (let* ((p (derivation->output-path drv))
  635. (s (stat (string-append p "/guile/guix/nix")))
  636. (b (string-append p "/b")))
  637. (return (and (eq? (stat:type s) 'directory)
  638. (equal? '(chdir "/foo")
  639. (call-with-input-file b read))))))))
  640. (test-assertm "gexp->derivation & with-imported-module & computed module"
  641. (mlet* %store-monad
  642. ((module -> (scheme-file "x" #~(begin
  643. (define-module (foo bar)
  644. #:export (the-answer))
  645. (define the-answer 42))))
  646. (build -> (with-imported-modules `(((foo bar) => ,module)
  647. (guix build utils))
  648. #~(begin
  649. (use-modules (guix build utils)
  650. (foo bar))
  651. mkdir-p
  652. (call-with-output-file #$output
  653. (lambda (port)
  654. (write the-answer port))))))
  655. (drv (gexp->derivation "thing" build))
  656. (out -> (derivation->output-path drv)))
  657. (mbegin %store-monad
  658. (built-derivations (list drv))
  659. (return (= 42 (call-with-input-file out read))))))
  660. (test-assertm "gexp->derivation #:references-graphs"
  661. (mlet* %store-monad
  662. ((one (text-file "one" (random-text)))
  663. (two (gexp->derivation "two"
  664. #~(symlink #$one #$output:chbouib)))
  665. (build -> (with-imported-modules '((guix build store-copy)
  666. (guix build utils))
  667. #~(begin
  668. (use-modules (guix build store-copy))
  669. (with-output-to-file #$output
  670. (lambda ()
  671. (write (call-with-input-file "guile"
  672. read-reference-graph))))
  673. (with-output-to-file #$output:one
  674. (lambda ()
  675. (write (call-with-input-file "one"
  676. read-reference-graph))))
  677. (with-output-to-file #$output:two
  678. (lambda ()
  679. (write (call-with-input-file "two"
  680. read-reference-graph)))))))
  681. (drv (gexp->derivation "ref-graphs" build
  682. #:references-graphs `(("one" ,one)
  683. ("two" ,two "chbouib")
  684. ("guile" ,%bootstrap-guile))))
  685. (ok? (built-derivations (list drv)))
  686. (guile-drv (package->derivation %bootstrap-guile))
  687. (bash (interned-file (search-bootstrap-binary "bash"
  688. (%current-system))
  689. "bash" #:recursive? #t))
  690. (g-one -> (derivation->output-path drv "one"))
  691. (g-two -> (derivation->output-path drv "two"))
  692. (g-guile -> (derivation->output-path drv)))
  693. (return (and ok?
  694. (equal? (call-with-input-file g-one read) (list one))
  695. (lset= string=?
  696. (call-with-input-file g-two read)
  697. (list one (derivation->output-path two "chbouib")))
  698. ;; Note: %BOOTSTRAP-GUILE depends on the bootstrap Bash.
  699. (lset= string=?
  700. (call-with-input-file g-guile read)
  701. (list (derivation->output-path guile-drv) bash))))))
  702. (test-assertm "gexp->derivation #:allowed-references"
  703. (mlet %store-monad ((drv (gexp->derivation "allowed-refs"
  704. #~(begin
  705. (mkdir #$output)
  706. (chdir #$output)
  707. (symlink #$output "self")
  708. (symlink #$%bootstrap-guile
  709. "guile"))
  710. #:allowed-references
  711. (list "out" %bootstrap-guile))))
  712. (built-derivations (list drv))))
  713. (test-assertm "gexp->derivation #:allowed-references, specific output"
  714. (mlet* %store-monad ((in (gexp->derivation "thing"
  715. #~(begin
  716. (mkdir #$output:ok)
  717. (mkdir #$output:not-ok))))
  718. (drv (gexp->derivation "allowed-refs"
  719. #~(begin
  720. (pk #$in:not-ok)
  721. (mkdir #$output)
  722. (chdir #$output)
  723. (symlink #$output "self")
  724. (symlink #$in:ok "ok"))
  725. #:allowed-references
  726. (list "out"
  727. (gexp-input in "ok")))))
  728. (built-derivations (list drv))))
  729. (test-assert "gexp->derivation #:allowed-references, disallowed"
  730. (let ((drv (run-with-store %store
  731. (gexp->derivation "allowed-refs"
  732. #~(begin
  733. (mkdir #$output)
  734. (chdir #$output)
  735. (symlink #$%bootstrap-guile "guile"))
  736. #:allowed-references '()))))
  737. (guard (c ((nix-protocol-error? c) #t))
  738. (build-derivations %store (list drv))
  739. #f)))
  740. (test-assertm "gexp->derivation #:disallowed-references, allowed"
  741. (mlet %store-monad ((drv (gexp->derivation "disallowed-refs"
  742. #~(begin
  743. (mkdir #$output)
  744. (chdir #$output)
  745. (symlink #$output "self")
  746. (symlink #$%bootstrap-guile
  747. "guile"))
  748. #:disallowed-references '())))
  749. (built-derivations (list drv))))
  750. (test-assert "gexp->derivation #:disallowed-references"
  751. (let ((drv (run-with-store %store
  752. (gexp->derivation "disallowed-refs"
  753. #~(begin
  754. (mkdir #$output)
  755. (chdir #$output)
  756. (symlink #$%bootstrap-guile "guile"))
  757. #:disallowed-references (list %bootstrap-guile)))))
  758. (guard (c ((nix-protocol-error? c) #t))
  759. (build-derivations %store (list drv))
  760. #f)))
  761. (define shebang
  762. (string-append "#!" (derivation->output-path (%guile-for-build))
  763. "/bin/guile --no-auto-compile"))
  764. ;; If we're going to hit the silly shebang limit (128 chars on Linux-based
  765. ;; systems), then skip the following test.
  766. (test-skip (if (> (string-length shebang) 127) 2 0))
  767. (test-assertm "gexp->script"
  768. (mlet* %store-monad ((n -> (random (expt 2 50)))
  769. (exp -> (gexp
  770. (system*
  771. (string-append (ungexp %bootstrap-guile)
  772. "/bin/guile")
  773. "-c" (object->string
  774. '(display (expt (ungexp n) 2))))))
  775. (drv (gexp->script "guile-thing" exp
  776. #:guile %bootstrap-guile))
  777. (out -> (derivation->output-path drv))
  778. (done (built-derivations (list drv))))
  779. (let* ((pipe (open-input-pipe out))
  780. (str (get-string-all pipe)))
  781. (return (and (zero? (close-pipe pipe))
  782. (= (expt n 2) (string->number str)))))))
  783. (test-assertm "program-file"
  784. (let* ((n (random (expt 2 50)))
  785. (exp (with-imported-modules '((guix build utils))
  786. (gexp (begin
  787. (use-modules (guix build utils))
  788. (display (ungexp n))))))
  789. (file (program-file "program" exp
  790. #:guile %bootstrap-guile)))
  791. (mlet* %store-monad ((drv (lower-object file))
  792. (out -> (derivation->output-path drv)))
  793. (mbegin %store-monad
  794. (built-derivations (list drv))
  795. (let* ((pipe (open-input-pipe out))
  796. (str (get-string-all pipe)))
  797. (return (and (zero? (close-pipe pipe))
  798. (= n (string->number str)))))))))
  799. (test-assertm "scheme-file"
  800. (let* ((text (plain-file "foo" "Hello, world!"))
  801. (scheme (scheme-file "bar" #~(list "foo" #$text))))
  802. (mlet* %store-monad ((drv (lower-object scheme))
  803. (text (lower-object text))
  804. (out -> (derivation->output-path drv)))
  805. (mbegin %store-monad
  806. (built-derivations (list drv))
  807. (mlet %store-monad ((refs (references* out)))
  808. (return (and (equal? refs (list text))
  809. (equal? `(list "foo" ,text)
  810. (call-with-input-file out read)))))))))
  811. (test-assert "text-file*"
  812. (run-with-store %store
  813. (mlet* %store-monad
  814. ((drv (package->derivation %bootstrap-guile))
  815. (guile -> (derivation->output-path drv))
  816. (file (text-file "bar" "This is bar."))
  817. (text (text-file* "foo"
  818. %bootstrap-guile "/bin/guile "
  819. (gexp-input %bootstrap-guile "out") "/bin/guile "
  820. drv "/bin/guile "
  821. file))
  822. (done (built-derivations (list text)))
  823. (out -> (derivation->output-path text))
  824. (refs (references* out)))
  825. ;; Make sure we get the right references and the right content.
  826. (return (and (lset= string=? refs (list guile file))
  827. (equal? (call-with-input-file out get-string-all)
  828. (string-append guile "/bin/guile "
  829. guile "/bin/guile "
  830. guile "/bin/guile "
  831. file)))))
  832. #:guile-for-build (package-derivation %store %bootstrap-guile)))
  833. (test-assertm "mixed-text-file"
  834. (mlet* %store-monad ((file -> (mixed-text-file "mixed"
  835. "export PATH="
  836. %bootstrap-guile "/bin"))
  837. (drv (lower-object file))
  838. (out -> (derivation->output-path drv))
  839. (guile-drv (package->derivation %bootstrap-guile))
  840. (guile -> (derivation->output-path guile-drv)))
  841. (mbegin %store-monad
  842. (built-derivations (list drv))
  843. (mlet %store-monad ((refs (references* out)))
  844. (return (and (string=? (string-append "export PATH=" guile "/bin")
  845. (call-with-input-file out get-string-all))
  846. (equal? refs (list guile))))))))
  847. (test-assert "gexp->derivation vs. %current-target-system"
  848. (let ((mval (gexp->derivation "foo"
  849. #~(begin
  850. (mkdir #$output)
  851. (foo #+gnu-make))
  852. #:target #f)))
  853. ;; The value of %CURRENT-TARGET-SYSTEM at bind-time should have no
  854. ;; influence.
  855. (parameterize ((%current-target-system "fooooo"))
  856. (derivation? (run-with-store %store mval)))))
  857. (test-assertm "lower-object"
  858. (mlet %store-monad ((drv1 (lower-object %bootstrap-guile))
  859. (drv2 (lower-object (package-source coreutils)))
  860. (item (lower-object (plain-file "foo" "Hello!"))))
  861. (return (and (derivation? drv1) (derivation? drv2)
  862. (store-path? item)))))
  863. (test-assertm "lower-object, computed-file"
  864. (let* ((text (plain-file "foo" "Hello!"))
  865. (exp #~(begin
  866. (mkdir #$output)
  867. (symlink #$%bootstrap-guile
  868. (string-append #$output "/guile"))
  869. (symlink #$text (string-append #$output "/text"))))
  870. (computed (computed-file "computed" exp)))
  871. (mlet* %store-monad ((text (lower-object text))
  872. (guile-drv (lower-object %bootstrap-guile))
  873. (comp-drv (lower-object computed))
  874. (comp -> (derivation->output-path comp-drv)))
  875. (mbegin %store-monad
  876. (built-derivations (list comp-drv))
  877. (return (and (string=? (readlink (string-append comp "/guile"))
  878. (derivation->output-path guile-drv))
  879. (string=? (readlink (string-append comp "/text"))
  880. text)))))))
  881. (test-assert "lower-object & gexp-input-error?"
  882. (guard (c ((gexp-input-error? c)
  883. (gexp-error-invalid-input c)))
  884. (run-with-store %store
  885. (lower-object (current-module))
  886. #:guile-for-build (%guile-for-build))))
  887. (test-assert "printer"
  888. (string-match "^#<gexp \\(string-append .*#<package coreutils.*\
  889. \"/bin/uname\"\\) [[:xdigit:]]+>$"
  890. (with-output-to-string
  891. (lambda ()
  892. (write
  893. (gexp (string-append (ungexp coreutils)
  894. "/bin/uname")))))))
  895. (test-assert "printer vs. ungexp-splicing"
  896. (string-match "^#<gexp .* [[:xdigit:]]+>$"
  897. (with-output-to-string
  898. (lambda ()
  899. ;; #~(begin #$@#~())
  900. (write
  901. (gexp (begin (ungexp-splicing (gexp ())))))))))
  902. (test-equal "sugar"
  903. '(gexp (foo (ungexp bar) (ungexp baz "out")
  904. (ungexp (chbouib 42))
  905. (ungexp-splicing (list x y z))
  906. (ungexp-native foo) (ungexp-native foo "out")
  907. (ungexp-native (chbouib 42))
  908. (ungexp-native-splicing (list x y z))))
  909. '#~(foo #$bar #$baz:out #$(chbouib 42) #$@(list x y z)
  910. #+foo #+foo:out #+(chbouib 42) #+@(list x y z)))
  911. (test-end "gexp")
  912. ;; Local Variables:
  913. ;; eval: (put 'test-assertm 'scheme-indent-function 1)
  914. ;; End: