gexp.scm 72 KB

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