transformations.scm 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 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-transformations)
  19. #:use-module (guix tests)
  20. #:use-module (guix store)
  21. #:use-module ((guix gexp) #:select (lower-object))
  22. #:use-module ((guix profiles)
  23. #:select (package->manifest-entry
  24. manifest-entry-properties))
  25. #:use-module (guix derivations)
  26. #:use-module (guix packages)
  27. #:use-module (guix git-download)
  28. #:use-module (guix build-system)
  29. #:use-module (guix build-system gnu)
  30. #:use-module (guix transformations)
  31. #:use-module ((guix gexp)
  32. #:select (local-file? local-file-file
  33. computed-file? computed-file-gexp
  34. gexp-input-thing))
  35. #:use-module (guix ui)
  36. #:use-module (guix utils)
  37. #:use-module (guix git)
  38. #:use-module (guix upstream)
  39. #:use-module (gnu packages)
  40. #:use-module (gnu packages base)
  41. #:use-module (gnu packages busybox)
  42. #:use-module (ice-9 match)
  43. #:use-module (srfi srfi-1)
  44. #:use-module (srfi srfi-26)
  45. #:use-module (srfi srfi-64))
  46. (test-begin "transformations")
  47. (test-assert "options->transformation, no transformations"
  48. (let ((p (dummy-package "foo"))
  49. (t (options->transformation '())))
  50. (eq? (t p) p)))
  51. (test-assert "options->transformation, with-source"
  52. ;; Our pseudo-package is called 'guix.scm' so the 'guix.scm' source should
  53. ;; be applicable.
  54. (let* ((p (dummy-package "guix.scm"))
  55. (s (search-path %load-path "guix.scm"))
  56. (t (options->transformation `((with-source . ,s)))))
  57. (with-store store
  58. (let* ((new (t p))
  59. (source (run-with-store store
  60. (lower-object (package-source new)))))
  61. (and (not (eq? new p))
  62. (string=? source
  63. (add-to-store store "guix.scm" #t
  64. "sha256" s)))))))
  65. (test-assert "options->transformation, with-source, replacement"
  66. ;; Same, but this time the original package has a 'replacement' field. We
  67. ;; expect that replacement to be set to #f in the new package.
  68. (let* ((p (dummy-package "guix.scm" (replacement coreutils)))
  69. (s (search-path %load-path "guix.scm"))
  70. (t (options->transformation `((with-source . ,s)))))
  71. (let ((new (t p)))
  72. (and (not (eq? new p))
  73. (not (package-replacement new))))))
  74. (test-assert "options->transformation, with-source, with version"
  75. ;; Our pseudo-package is called 'guix.scm' so the 'guix.scm-2.0' source
  76. ;; should be applicable, and its version should be extracted.
  77. (let ((p (dummy-package "foo"))
  78. (s (search-path %load-path "guix.scm")))
  79. (call-with-temporary-directory
  80. (lambda (directory)
  81. (let* ((f (string-append directory "/foo-42.0.tar.gz"))
  82. (t (options->transformation `((with-source . ,f)))))
  83. (copy-file s f)
  84. (with-store store
  85. (let* ((new (t p))
  86. (source (run-with-store store
  87. (lower-object (package-source new)))))
  88. (and (not (eq? new p))
  89. (string=? (package-name new) (package-name p))
  90. (string=? (package-version new) "42.0")
  91. (string=? source
  92. (add-to-store store (basename f) #t
  93. "sha256" f))))))))))
  94. (test-assert "options->transformation, with-source, no matches"
  95. ;; When a transformation in not applicable, a warning must be raised.
  96. (let* ((p (dummy-package "foobar"))
  97. (s (search-path %load-path "guix.scm"))
  98. (t (options->transformation `((with-source . ,s)))))
  99. (let* ((port (open-output-string))
  100. (new (parameterize ((guix-warning-port port))
  101. (t p))))
  102. (and (eq? new p)
  103. (string-contains (get-output-string port)
  104. "had no effect")))))
  105. (test-assert "options->transformation, with-source, PKG=URI"
  106. (let* ((p (dummy-package "foo"))
  107. (s (search-path %load-path "guix.scm"))
  108. (f (string-append "foo=" s))
  109. (t (options->transformation `((with-source . ,f)))))
  110. (with-store store
  111. (let* ((new (t p))
  112. (source (run-with-store store
  113. (lower-object (package-source new)))))
  114. (and (not (eq? new p))
  115. (string=? (package-name new) (package-name p))
  116. (string=? (package-version new)
  117. (package-version p))
  118. (string=? source
  119. (add-to-store store (basename s) #t
  120. "sha256" s)))))))
  121. (test-assert "options->transformation, with-source, PKG@VER=URI"
  122. (let* ((p (dummy-package "foo"))
  123. (s (search-path %load-path "guix.scm"))
  124. (f (string-append "foo@42.0=" s))
  125. (t (options->transformation `((with-source . ,f)))))
  126. (with-store store
  127. (let* ((new (t p))
  128. (source (run-with-store store
  129. (lower-object (package-source new)))))
  130. (and (not (eq? new p))
  131. (string=? (package-name new) (package-name p))
  132. (string=? (package-version new) "42.0")
  133. (string=? source
  134. (add-to-store store (basename s) #t
  135. "sha256" s)))))))
  136. (test-assert "options->transformation, with-input"
  137. (let* ((p (dummy-package "guix.scm"
  138. (inputs `(("foo" ,(specification->package "coreutils"))
  139. ("bar" ,(specification->package "grep"))
  140. ("baz" ,(dummy-package "chbouib"
  141. (native-inputs `(("x" ,grep)))))))))
  142. (t (options->transformation '((with-input . "coreutils=busybox")
  143. (with-input . "grep=findutils")))))
  144. (let ((new (t p)))
  145. (and (not (eq? new p))
  146. (match (package-inputs new)
  147. ((("foo" dep1) ("bar" dep2) ("baz" dep3))
  148. (and (string=? (package-full-name dep1)
  149. (package-full-name busybox))
  150. (string=? (package-full-name dep2)
  151. (package-full-name findutils))
  152. (string=? (package-name dep3) "chbouib")
  153. (match (package-native-inputs dep3)
  154. ((("x" dep))
  155. (string=? (package-full-name dep)
  156. (package-full-name findutils)))))))))))
  157. (test-assert "options->transformation, with-graft"
  158. (let* ((p (dummy-package "guix.scm"
  159. (inputs `(("foo" ,grep)
  160. ("bar" ,(dummy-package "chbouib"
  161. (native-inputs `(("x" ,grep)))))))))
  162. (t (options->transformation '((with-graft . "grep=findutils")))))
  163. (let ((new (t p)))
  164. (and (not (eq? new p))
  165. (match (package-inputs new)
  166. ((("foo" dep1) ("bar" dep2))
  167. (and (string=? (package-full-name dep1)
  168. (package-full-name grep))
  169. (string=? (package-full-name (package-replacement dep1))
  170. (package-full-name findutils))
  171. (string=? (package-name dep2) "chbouib")
  172. (match (package-native-inputs dep2)
  173. ((("x" dep))
  174. (with-store store
  175. (string=? (derivation-file-name
  176. (package-derivation store findutils))
  177. (derivation-file-name
  178. (package-derivation store dep)))))))))))))
  179. (test-equal "options->transformation, with-branch"
  180. (git-checkout (url "https://example.org")
  181. (branch "devel")
  182. (recursive? #t))
  183. (let* ((p (dummy-package "guix.scm"
  184. (inputs `(("foo" ,grep)
  185. ("bar" ,(dummy-package "chbouib"
  186. (source (origin
  187. (method git-fetch)
  188. (uri (git-reference
  189. (url "https://example.org")
  190. (commit "cabba9e")))
  191. (sha256 #f)))))))))
  192. (t (options->transformation '((with-branch . "chbouib=devel")))))
  193. (let ((new (t p)))
  194. (and (not (eq? new p))
  195. (match (package-inputs new)
  196. ((("foo" dep1) ("bar" dep2))
  197. (and (string=? (package-full-name dep1)
  198. (package-full-name grep))
  199. (string=? (package-name dep2) "chbouib")
  200. (package-source dep2))))))))
  201. (test-equal "options->transformation, with-commit"
  202. (git-checkout (url "https://example.org")
  203. (commit "abcdef")
  204. (recursive? #t))
  205. (let* ((p (dummy-package "guix.scm"
  206. (inputs `(("foo" ,grep)
  207. ("bar" ,(dummy-package "chbouib"
  208. (source (origin
  209. (method git-fetch)
  210. (uri (git-reference
  211. (url "https://example.org")
  212. (commit "cabba9e")))
  213. (sha256 #f)))))))))
  214. (t (options->transformation '((with-commit . "chbouib=abcdef")))))
  215. (let ((new (t p)))
  216. (and (not (eq? new p))
  217. (match (package-inputs new)
  218. ((("foo" dep1) ("bar" dep2))
  219. (and (string=? (package-full-name dep1)
  220. (package-full-name grep))
  221. (string=? (package-name dep2) "chbouib")
  222. (package-source dep2))))))))
  223. (test-equal "options->transformation, with-git-url"
  224. (let ((source (git-checkout (url "https://example.org")
  225. (recursive? #t))))
  226. (list source source))
  227. (let* ((p (dummy-package "guix.scm"
  228. (inputs `(("foo" ,grep)
  229. ("bar" ,(dummy-package "chbouib"
  230. (native-inputs `(("x" ,grep)))))))))
  231. (t (options->transformation '((with-git-url . "grep=https://example.org")))))
  232. (let ((new (t p)))
  233. (and (not (eq? new p))
  234. (match (package-inputs new)
  235. ((("foo" dep1) ("bar" dep2))
  236. (and (string=? (package-full-name dep1)
  237. (package-full-name grep))
  238. (string=? (package-name dep2) "chbouib")
  239. (match (package-native-inputs dep2)
  240. ((("x" dep3))
  241. (map package-source (list dep1 dep3)))))))))))
  242. (test-equal "options->transformation, with-git-url + with-branch"
  243. ;; Combine the two options and make sure the 'with-branch' transformation
  244. ;; comes after the 'with-git-url' transformation.
  245. (let ((source (git-checkout (url "https://example.org")
  246. (branch "BRANCH")
  247. (recursive? #t))))
  248. (list source source))
  249. (let* ((p (dummy-package "guix.scm"
  250. (inputs `(("foo" ,grep)
  251. ("bar" ,(dummy-package "chbouib"
  252. (native-inputs `(("x" ,grep)))))))))
  253. (t (options->transformation
  254. (reverse '((with-git-url
  255. . "grep=https://example.org")
  256. (with-branch . "grep=BRANCH"))))))
  257. (let ((new (t p)))
  258. (and (not (eq? new p))
  259. (match (package-inputs new)
  260. ((("foo" dep1) ("bar" dep2))
  261. (and (string=? (package-name dep1) "grep")
  262. (string=? (package-name dep2) "chbouib")
  263. (match (package-native-inputs dep2)
  264. ((("x" dep3))
  265. (map package-source (list dep1 dep3)))))))))))
  266. (define* (depends-on-toolchain? p #:optional (toolchain "gcc-toolchain"))
  267. "Return true if P depends on TOOLCHAIN instead of the default tool chain."
  268. (define toolchain-packages
  269. '("gcc" "binutils" "glibc" "ld-wrapper"))
  270. (define (package-name* obj)
  271. (and (package? obj) (package-name obj)))
  272. (match (bag-build-inputs (package->bag p))
  273. (((_ (= package-name* packages) . _) ...)
  274. (and (not (any (cut member <> packages) toolchain-packages))
  275. (member toolchain packages)))))
  276. (test-assert "options->transformation, with-c-toolchain"
  277. (let* ((dep0 (dummy-package "chbouib"
  278. (build-system gnu-build-system)
  279. (native-inputs `(("y" ,grep)))))
  280. (dep1 (dummy-package "stuff"
  281. (native-inputs `(("x" ,dep0)))))
  282. (p (dummy-package "thingie"
  283. (build-system gnu-build-system)
  284. (inputs `(("foo" ,grep)
  285. ("bar" ,dep1)))))
  286. (t (options->transformation
  287. '((with-c-toolchain . "chbouib=gcc-toolchain")))))
  288. ;; Here we check that the transformation applies to DEP0 and all its
  289. ;; dependents: DEP0 must use GCC-TOOLCHAIN, DEP1 must use GCC-TOOLCHAIN
  290. ;; and the DEP0 that uses GCC-TOOLCHAIN, and so on.
  291. (let ((new (t p)))
  292. (and (depends-on-toolchain? new "gcc-toolchain")
  293. (match (bag-build-inputs (package->bag new))
  294. ((("foo" dep0) ("bar" dep1) _ ...)
  295. (and (depends-on-toolchain? dep1 "gcc-toolchain")
  296. (not (depends-on-toolchain? dep0 "gcc-toolchain"))
  297. (string=? (package-full-name dep0)
  298. (package-full-name grep))
  299. (match (bag-build-inputs (package->bag dep1))
  300. ((("x" dep) _ ...)
  301. (and (depends-on-toolchain? dep "gcc-toolchain")
  302. (match (bag-build-inputs (package->bag dep))
  303. ((("y" dep) _ ...) ;this one is unchanged
  304. (eq? dep grep)))))))))))))
  305. (test-equal "options->transformation, with-c-toolchain twice"
  306. (package-full-name grep)
  307. (let* ((dep0 (dummy-package "chbouib"))
  308. (dep1 (dummy-package "stuff"))
  309. (p (dummy-package "thingie"
  310. (build-system gnu-build-system)
  311. (inputs `(("foo" ,dep0)
  312. ("bar" ,dep1)
  313. ("baz" ,grep)))))
  314. (t (options->transformation
  315. '((with-c-toolchain . "chbouib=clang-toolchain")
  316. (with-c-toolchain . "stuff=clang-toolchain")))))
  317. (let ((new (t p)))
  318. (and (depends-on-toolchain? new "clang-toolchain")
  319. (match (bag-build-inputs (package->bag new))
  320. ((("foo" dep0) ("bar" dep1) ("baz" dep2) _ ...)
  321. (and (depends-on-toolchain? dep0 "clang-toolchain")
  322. (depends-on-toolchain? dep1 "clang-toolchain")
  323. (not (depends-on-toolchain? dep2 "clang-toolchain"))
  324. (package-full-name dep2))))))))
  325. (test-assert "options->transformation, with-c-toolchain, no effect"
  326. (let ((p (dummy-package "thingie"))
  327. (t (options->transformation
  328. '((with-c-toolchain . "does-not-exist=gcc-toolchain")))))
  329. ;; When it has no effect, '--with-c-toolchain' returns P.
  330. (eq? (t p) p)))
  331. (test-equal "options->transformation, with-debug-info"
  332. '(#:strip-binaries? #f)
  333. (let* ((dep (dummy-package "chbouib"))
  334. (p (dummy-package "thingie"
  335. (build-system gnu-build-system)
  336. (inputs `(("foo" ,dep)
  337. ("bar" ,grep)))))
  338. (t (options->transformation
  339. '((with-debug-info . "chbouib")))))
  340. (let ((new (t p)))
  341. (match (package-inputs new)
  342. ((("foo" dep0) ("bar" dep1))
  343. (and (string=? (package-full-name dep1)
  344. (package-full-name grep))
  345. (package-arguments (package-replacement dep0))))))))
  346. (test-assert "options->transformation, without-tests"
  347. (let* ((dep (dummy-package "dep"))
  348. (p (dummy-package "foo"
  349. (inputs `(("dep" ,dep)))))
  350. (t (options->transformation '((without-tests . "dep")
  351. (without-tests . "tar")))))
  352. (let ((new (t p)))
  353. (match (bag-direct-inputs (package->bag new))
  354. ((("dep" dep) ("tar" tar) _ ...)
  355. (and (equal? (package-arguments dep) '(#:tests? #f))
  356. (match (memq #:tests? (package-arguments tar))
  357. ((#:tests? #f _ ...) #t))))))))
  358. (test-equal "options->transformation, with-patch"
  359. (search-patches "glibc-locales.patch" "guile-relocatable.patch")
  360. (let* ((dep (dummy-package "dep"
  361. (source (dummy-origin))))
  362. (p (dummy-package "foo"
  363. (inputs `(("dep" ,dep)))))
  364. (patch1 (search-patch "glibc-locales.patch"))
  365. (patch2 (search-patch "guile-relocatable.patch"))
  366. (t (options->transformation
  367. `((with-patch . ,(string-append "dep=" patch1))
  368. (with-patch . ,(string-append "dep=" patch2))
  369. (with-patch . ,(string-append "tar=" patch1))))))
  370. (let ((new (t p)))
  371. (match (bag-direct-inputs (package->bag new))
  372. ((("dep" dep) ("tar" tar) _ ...)
  373. (and (member patch1
  374. (filter-map (lambda (patch)
  375. (and (local-file? patch)
  376. (local-file-file patch)))
  377. (origin-patches (package-source tar))))
  378. (map local-file-file
  379. (origin-patches (package-source dep)))))))))
  380. (test-equal "options->transformation, with-commit + with-patch"
  381. '(#t #t)
  382. (let* ((patch (search-patch "glibc-locales.patch"))
  383. (commit "f8934ec94df5868ee8baf1fb0f8ed0f24e7e91eb")
  384. (t (options->transformation
  385. ;; Note: options are applied in reverse order, so
  386. ;; 'with-patch' comes on top.
  387. `((with-patch . ,(string-append "guile-gcrypt=" patch))
  388. (with-commit
  389. . ,(string-append "guile-gcrypt=" commit))))))
  390. (let ((new (t (@ (gnu packages gnupg) guile-gcrypt))))
  391. (match (package-source new)
  392. ((? computed-file? source)
  393. (let* ((gexp (computed-file-gexp source))
  394. (inputs (map gexp-input-thing
  395. ((@@ (guix gexp) gexp-inputs) gexp))))
  396. (list (any (lambda (input)
  397. (and (git-checkout? input)
  398. (string=? commit (git-checkout-commit input))))
  399. inputs)
  400. (any (lambda (input)
  401. (and (local-file? input)
  402. (string=? (local-file-file input) patch)))
  403. inputs))))))))
  404. (test-equal "options->transformation, with-latest"
  405. "42.0"
  406. (mock ((guix upstream) %updaters
  407. (delay (list (upstream-updater
  408. (name 'dummy)
  409. (pred (const #t))
  410. (description "")
  411. (latest (const (upstream-source
  412. (package "foo")
  413. (version "42.0")
  414. (urls '("http://example.org")))))))))
  415. (let* ((p (dummy-package "foo" (version "1.0")))
  416. (t (options->transformation
  417. `((with-latest . "foo")))))
  418. (package-version (t p)))))
  419. (test-equal "options->transformation + package->manifest-entry"
  420. '((transformations . ((without-tests . "foo"))))
  421. (let* ((p (dummy-package "foo"))
  422. (t (options->transformation '((without-tests . "foo"))))
  423. (e (package->manifest-entry (t p))))
  424. (manifest-entry-properties e)))
  425. (test-end)
  426. ;;; Local Variables:
  427. ;;; eval: (put 'dummy-package 'scheme-indent-function 1)
  428. ;;; End: