gexp.scm 68 KB

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