gexp.scm 71 KB

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