transformations.scm 21 KB

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