transformations.scm 19 KB

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