transformations.scm 19 KB

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