style.scm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021-2022 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 (tests-style)
  19. #:use-module (guix packages)
  20. #:use-module (guix scripts style)
  21. #:use-module ((guix utils) #:select (call-with-temporary-directory))
  22. #:use-module ((guix build utils) #:select (substitute*))
  23. #:use-module (guix gexp) ;for the reader extension
  24. #:use-module (guix diagnostics)
  25. #:use-module (gnu packages acl)
  26. #:use-module (gnu packages multiprecision)
  27. #:use-module (srfi srfi-1)
  28. #:use-module (srfi srfi-64)
  29. #:use-module (ice-9 match)
  30. #:use-module (ice-9 rdelim)
  31. #:use-module (ice-9 pretty-print))
  32. (define (call-with-test-package inputs proc)
  33. (call-with-temporary-directory
  34. (lambda (directory)
  35. (call-with-output-file (string-append directory "/my-packages.scm")
  36. (lambda (port)
  37. (pretty-print
  38. `(begin
  39. (define-module (my-packages)
  40. #:use-module (guix)
  41. #:use-module (guix licenses)
  42. #:use-module (gnu packages acl)
  43. #:use-module (gnu packages base)
  44. #:use-module (gnu packages multiprecision)
  45. #:use-module (srfi srfi-1))
  46. (define base
  47. (package
  48. (inherit coreutils)
  49. (inputs '())
  50. (native-inputs '())
  51. (propagated-inputs '())))
  52. (define (sdl-union . lst)
  53. (package
  54. (inherit base)
  55. (name "sdl-union")))
  56. (define-public my-coreutils
  57. (package
  58. (inherit base)
  59. ,@inputs
  60. (name "my-coreutils"))))
  61. port)))
  62. (proc directory))))
  63. (define test-directory
  64. ;; Directory where the package definition lives.
  65. (make-parameter #f))
  66. (define-syntax-rule (with-test-package fields exp ...)
  67. (call-with-test-package fields
  68. (lambda (directory)
  69. (define file
  70. (string-append directory "/my-packages.scm"))
  71. ;; Run as a separate process to make sure FILE is reloaded.
  72. (system* "guix" "style" "-L" directory "-S" "inputs"
  73. "my-coreutils")
  74. (system* "cat" file)
  75. (load file)
  76. (parameterize ((test-directory directory))
  77. exp ...))))
  78. (define* (read-lines port line #:optional (count 1))
  79. "Read COUNT lines from PORT, starting from LINE."
  80. (let loop ((lines '())
  81. (count count))
  82. (cond ((< (port-line port) (- line 1))
  83. (read-char port)
  84. (loop lines count))
  85. ((zero? count)
  86. (string-concatenate-reverse lines))
  87. (else
  88. (match (read-line port 'concat)
  89. ((? eof-object?)
  90. (loop lines 0))
  91. (line
  92. (loop (cons line lines) (- count 1))))))))
  93. (define* (read-package-field package field #:optional (count 1))
  94. (let* ((location (package-field-location package field))
  95. (file (location-file location))
  96. (line (location-line location)))
  97. (call-with-input-file (if (string-prefix? "/" file)
  98. file
  99. (string-append (test-directory) "/"
  100. file))
  101. (lambda (port)
  102. (read-lines port line count)))))
  103. (define-syntax-rule (test-pretty-print str args ...)
  104. "Test equality after a round-trip where STR is passed to
  105. 'read-with-comments' and the resulting sexp is then passed to
  106. 'pretty-print-with-comments'."
  107. (test-equal str
  108. (call-with-output-string
  109. (lambda (port)
  110. (let ((exp (call-with-input-string str
  111. read-with-comments)))
  112. (pretty-print-with-comments port exp args ...))))))
  113. (test-begin "style")
  114. (test-equal "nothing to rewrite"
  115. '()
  116. (with-test-package '()
  117. (package-direct-inputs (@ (my-packages) my-coreutils))))
  118. (test-equal "input labels, mismatch"
  119. (list `(("foo" ,gmp) ("bar" ,acl))
  120. " (inputs `((\"foo\" ,gmp) (\"bar\" ,acl)))\n")
  121. (with-test-package '((inputs `(("foo" ,gmp) ("bar" ,acl))))
  122. (list (package-direct-inputs (@ (my-packages) my-coreutils))
  123. (read-package-field (@ (my-packages) my-coreutils) 'inputs))))
  124. (test-equal "input labels, simple"
  125. (list `(("gmp" ,gmp) ("acl" ,acl))
  126. " (inputs (list gmp acl))\n")
  127. (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl))))
  128. (list (package-direct-inputs (@ (my-packages) my-coreutils))
  129. (read-package-field (@ (my-packages) my-coreutils) 'inputs))))
  130. (test-equal "input labels, long list with one item per line"
  131. (list (concatenate (make-list 4 `(("gmp" ,gmp) ("acl" ,acl))))
  132. "\
  133. (list gmp
  134. acl
  135. gmp
  136. acl
  137. gmp
  138. acl
  139. gmp
  140. acl))\n")
  141. (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
  142. ("gmp" ,gmp) ("acl" ,acl)
  143. ("gmp" ,gmp) ("acl" ,acl)
  144. ("gmp" ,gmp) ("acl" ,acl))))
  145. (list (package-direct-inputs (@ (my-packages) my-coreutils))
  146. (read-package-field (@ (my-packages) my-coreutils) 'inputs 8))))
  147. (test-equal "input labels, sdl-union"
  148. "\
  149. (list gmp acl
  150. (sdl-union 1 2 3 4)))\n"
  151. (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
  152. ("sdl-union" ,(sdl-union 1 2 3 4)))))
  153. (read-package-field (@ (my-packages) my-coreutils) 'inputs 2)))
  154. (test-equal "input labels, output"
  155. (list `(("gmp" ,gmp "debug") ("acl" ,acl))
  156. " (inputs (list `(,gmp \"debug\") acl))\n")
  157. (with-test-package '((inputs `(("gmp" ,gmp "debug") ("acl" ,acl))))
  158. (list (package-direct-inputs (@ (my-packages) my-coreutils))
  159. (read-package-field (@ (my-packages) my-coreutils) 'inputs))))
  160. (test-equal "input labels, prepend"
  161. (list `(("gmp" ,gmp) ("acl" ,acl))
  162. "\
  163. (modify-inputs (package-propagated-inputs coreutils)
  164. (prepend gmp acl)))\n")
  165. (with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
  166. ,@(package-propagated-inputs coreutils))))
  167. (list (package-inputs (@ (my-packages) my-coreutils))
  168. (read-package-field (@ (my-packages) my-coreutils) 'inputs 2))))
  169. (test-equal "input labels, prepend + delete"
  170. (list `(("gmp" ,gmp) ("acl" ,acl))
  171. "\
  172. (modify-inputs (package-propagated-inputs coreutils)
  173. (delete \"gmp\")
  174. (prepend gmp acl)))\n")
  175. (with-test-package '((inputs `(("gmp" ,gmp)
  176. ("acl" ,acl)
  177. ,@(alist-delete "gmp"
  178. (package-propagated-inputs coreutils)))))
  179. (list (package-inputs (@ (my-packages) my-coreutils))
  180. (read-package-field (@ (my-packages) my-coreutils) 'inputs 3))))
  181. (test-equal "input labels, prepend + delete multiple"
  182. (list `(("gmp" ,gmp) ("acl" ,acl))
  183. "\
  184. (modify-inputs (package-propagated-inputs coreutils)
  185. (delete \"foo\" \"bar\" \"baz\")
  186. (prepend gmp acl)))\n")
  187. (with-test-package '((inputs `(("gmp" ,gmp)
  188. ("acl" ,acl)
  189. ,@(fold alist-delete
  190. (package-propagated-inputs coreutils)
  191. '("foo" "bar" "baz")))))
  192. (list (package-inputs (@ (my-packages) my-coreutils))
  193. (read-package-field (@ (my-packages) my-coreutils) 'inputs 3))))
  194. (test-equal "input labels, replace"
  195. (list '() ;there's no "gmp" input to replace
  196. "\
  197. (modify-inputs (package-propagated-inputs coreutils)
  198. (replace \"gmp\" gmp)))\n")
  199. (with-test-package '((inputs `(("gmp" ,gmp)
  200. ,@(alist-delete "gmp"
  201. (package-propagated-inputs coreutils)))))
  202. (list (package-inputs (@ (my-packages) my-coreutils))
  203. (read-package-field (@ (my-packages) my-coreutils) 'inputs 2))))
  204. (test-equal "input labels, 'safe' policy"
  205. (list `(("gmp" ,gmp) ("acl" ,acl))
  206. "\
  207. (inputs (list gmp acl))\n")
  208. (call-with-test-package '((inputs `(("GMP" ,gmp) ("ACL" ,acl)))
  209. (arguments '())) ;no build system arguments
  210. (lambda (directory)
  211. (define file
  212. (string-append directory "/my-packages.scm"))
  213. (system* "guix" "style" "-L" directory "my-coreutils"
  214. "-S" "inputs"
  215. "--input-simplification=safe")
  216. (load file)
  217. (list (package-inputs (@ (my-packages) my-coreutils))
  218. (read-package-field (@ (my-packages) my-coreutils) 'inputs)))))
  219. (test-equal "input labels, 'safe' policy, nothing changed"
  220. (list `(("GMP" ,gmp) ("ACL" ,acl))
  221. "\
  222. (inputs `((\"GMP\" ,gmp) (\"ACL\" ,acl)))\n")
  223. (call-with-test-package '((inputs `(("GMP" ,gmp) ("ACL" ,acl)))
  224. ;; Non-empty argument list, so potentially unsafe
  225. ;; input simplification.
  226. (arguments
  227. '(#:configure-flags
  228. (assoc-ref %build-inputs "GMP"))))
  229. (lambda (directory)
  230. (define file
  231. (string-append directory "/my-packages.scm"))
  232. (system* "guix" "style" "-L" directory "my-coreutils"
  233. "-S" "inputs"
  234. "--input-simplification=safe")
  235. (load file)
  236. (list (package-inputs (@ (my-packages) my-coreutils))
  237. (read-package-field (@ (my-packages) my-coreutils) 'inputs)))))
  238. (test-equal "input labels, margin comment"
  239. (list `(("gmp" ,gmp))
  240. `(("acl" ,acl))
  241. " (inputs (list gmp)) ;margin comment\n"
  242. " (native-inputs (list acl)) ;another one\n")
  243. (call-with-test-package '((inputs `(("gmp" ,gmp)))
  244. (native-inputs `(("acl" ,acl))))
  245. (lambda (directory)
  246. (define file
  247. (string-append directory "/my-packages.scm"))
  248. (substitute* file
  249. (("\"gmp\"(.*)$" _ rest)
  250. (string-append "\"gmp\"" (string-trim-right rest)
  251. " ;margin comment\n"))
  252. (("\"acl\"(.*)$" _ rest)
  253. (string-append "\"acl\"" (string-trim-right rest)
  254. " ;another one\n")))
  255. (system* "cat" file)
  256. (system* "guix" "style" "-L" directory "-S" "inputs"
  257. "my-coreutils")
  258. (load file)
  259. (list (package-inputs (@ (my-packages) my-coreutils))
  260. (package-native-inputs (@ (my-packages) my-coreutils))
  261. (read-package-field (@ (my-packages) my-coreutils) 'inputs)
  262. (read-package-field (@ (my-packages) my-coreutils) 'native-inputs)))))
  263. (test-equal "input labels, margin comment on long list"
  264. (list (concatenate (make-list 4 `(("gmp" ,gmp) ("acl" ,acl))))
  265. "\
  266. (list gmp ;margin comment
  267. acl
  268. gmp ;margin comment
  269. acl
  270. gmp ;margin comment
  271. acl
  272. gmp ;margin comment
  273. acl))\n")
  274. (call-with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl)
  275. ("gmp" ,gmp) ("acl" ,acl)
  276. ("gmp" ,gmp) ("acl" ,acl)
  277. ("gmp" ,gmp) ("acl" ,acl))))
  278. (lambda (directory)
  279. (define file
  280. (string-append directory "/my-packages.scm"))
  281. (substitute* file
  282. (("\"gmp\"(.*)$" _ rest)
  283. (string-append "\"gmp\"" (string-trim-right rest)
  284. " ;margin comment\n")))
  285. (system* "cat" file)
  286. (system* "guix" "style" "-L" directory "-S" "inputs"
  287. "my-coreutils")
  288. (load file)
  289. (list (package-inputs (@ (my-packages) my-coreutils))
  290. (read-package-field (@ (my-packages) my-coreutils) 'inputs 8)))))
  291. (test-equal "input labels, line comment"
  292. (list `(("gmp" ,gmp) ("acl" ,acl))
  293. "\
  294. (inputs (list gmp
  295. ;; line comment!
  296. acl))\n")
  297. (call-with-test-package '((inputs `(("gmp" ,gmp) ("acl" ,acl))))
  298. (lambda (directory)
  299. (define file
  300. (string-append directory "/my-packages.scm"))
  301. (substitute* file
  302. ((",gmp\\)(.*)$" _ rest)
  303. (string-append ",gmp)\n ;; line comment!\n" rest)))
  304. (system* "guix" "style" "-L" directory "-S" "inputs"
  305. "my-coreutils")
  306. (load file)
  307. (list (package-inputs (@ (my-packages) my-coreutils))
  308. (read-package-field (@ (my-packages) my-coreutils) 'inputs 3)))))
  309. (test-equal "input labels, modify-inputs and margin comment"
  310. (list `(("gmp" ,gmp) ("acl" ,acl) ("mpfr" ,mpfr))
  311. "\
  312. (modify-inputs (package-propagated-inputs coreutils)
  313. (prepend gmp ;margin comment
  314. acl ;another one
  315. mpfr)))\n")
  316. (call-with-test-package '((inputs
  317. `(("gmp" ,gmp) ("acl" ,acl) ("mpfr" ,mpfr)
  318. ,@(package-propagated-inputs coreutils))))
  319. (lambda (directory)
  320. (define file
  321. (string-append directory "/my-packages.scm"))
  322. (substitute* file
  323. ((",gmp\\)(.*)$" _ rest)
  324. (string-append ",gmp) ;margin comment\n" rest))
  325. ((",acl\\)(.*)$" _ rest)
  326. (string-append ",acl) ;another one\n" rest)))
  327. (system* "guix" "style" "-L" directory "-S" "inputs"
  328. "my-coreutils")
  329. (load file)
  330. (list (package-inputs (@ (my-packages) my-coreutils))
  331. (read-package-field (@ (my-packages) my-coreutils) 'inputs 4)))))
  332. (test-equal "read-with-comments: dot notation"
  333. (cons 'a 'b)
  334. (call-with-input-string "(a . b)"
  335. read-with-comments))
  336. (test-pretty-print "(list 1 2 3 4)")
  337. (test-pretty-print "((a . 1) (b . 2))")
  338. (test-pretty-print "(a b c . boom)")
  339. (test-pretty-print "(list 1
  340. 2
  341. 3
  342. 4)"
  343. #:long-list 3
  344. #:indent 20)
  345. (test-pretty-print "\
  346. (list abc
  347. def)"
  348. #:max-width 11)
  349. (test-pretty-print "\
  350. (#:foo
  351. #:bar)"
  352. #:max-width 10)
  353. (test-pretty-print "\
  354. (#:first 1
  355. #:second 2
  356. #:third 3)")
  357. (test-pretty-print "\
  358. ((x
  359. 1)
  360. (y
  361. 2)
  362. (z
  363. 3))"
  364. #:max-width 3)
  365. (test-pretty-print "\
  366. (let ((x 1)
  367. (y 2)
  368. (z 3)
  369. (p 4))
  370. (+ x y))"
  371. #:max-width 11)
  372. (test-pretty-print "\
  373. (lambda (x y)
  374. ;; This is a procedure.
  375. (let ((z (+ x y)))
  376. (* z z)))")
  377. (test-pretty-print "\
  378. #~(string-append #$coreutils \"/bin/uname\")")
  379. (test-pretty-print "\
  380. (package
  381. (inherit coreutils)
  382. (version \"42\"))")
  383. (test-pretty-print "\
  384. (modify-phases %standard-phases
  385. (add-after 'unpack 'post-unpack
  386. (lambda _
  387. #t))
  388. (add-before 'check 'pre-check
  389. (lambda* (#:key inputs #:allow-other-keys)
  390. do things ...)))")
  391. (test-pretty-print "\
  392. (#:phases (modify-phases sdfsdf
  393. (add-before 'x 'y
  394. (lambda _
  395. xyz))))")
  396. (test-pretty-print "\
  397. (description \"abcdefghijkl
  398. mnopqrstuvwxyz.\")"
  399. #:max-width 30)
  400. (test-pretty-print "\
  401. (description
  402. \"abcdefghijkl
  403. mnopqrstuvwxyz.\")"
  404. #:max-width 12)
  405. (test-pretty-print "\
  406. (description
  407. \"abcdefghijklmnopqrstuvwxyz\")"
  408. #:max-width 33)
  409. (test-pretty-print "\
  410. (modify-phases %standard-phases
  411. (replace 'build
  412. ;; Nicely indented in 'modify-phases' context.
  413. (lambda _
  414. #t)))")
  415. (test-pretty-print "\
  416. (modify-inputs inputs
  417. ;; Regular indentation for 'replace' here.
  418. (replace \"gmp\" gmp))")
  419. (test-pretty-print "\
  420. (package
  421. ;; Here 'sha256', 'base32', and 'arguments' must be
  422. ;; immediately followed by a newline.
  423. (source (origin
  424. (method url-fetch)
  425. (sha256
  426. (base32
  427. \"not a real base32 string\"))))
  428. (arguments
  429. '(#:phases %standard-phases
  430. #:tests? #f)))")
  431. (test-equal "pretty-print-with-comments, canonicalize-comment"
  432. "\
  433. (list abc
  434. ;; Not a margin comment.
  435. ;; Ditto.
  436. ;;
  437. ;; There's a blank line above.
  438. def ;margin comment
  439. ghi)"
  440. (let ((sexp (call-with-input-string
  441. "\
  442. (list abc
  443. ;Not a margin comment.
  444. ;;; Ditto.
  445. ;;;;;
  446. ; There's a blank line above.
  447. def ;; margin comment
  448. ghi)"
  449. read-with-comments)))
  450. (call-with-output-string
  451. (lambda (port)
  452. (pretty-print-with-comments port sexp
  453. #:format-comment
  454. canonicalize-comment)))))
  455. (test-end)
  456. ;; Local Variables:
  457. ;; eval: (put 'with-test-package 'scheme-indent-function 1)
  458. ;; eval: (put 'call-with-test-package 'scheme-indent-function 1)
  459. ;; End: