gexp.scm 71 KB

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