1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Cyril Roelandt <tipecaml@gmail.com>
3 ;;; Copyright © 2014, 2015 Eric Bavier <bavier@member.fsf.org>
4 ;;; Copyright © 2014, 2015 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2015 Mathieu Lirzin <mthl@openmailbox.org>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (test-lint)
23 #:use-module (guix tests)
24 #:use-module (guix download)
25 #:use-module (guix git-download)
26 #:use-module (guix build-system gnu)
27 #:use-module (guix packages)
28 #:use-module (guix scripts lint)
29 #:use-module (guix ui)
30 #:use-module (gnu packages)
31 #:use-module (gnu packages pkg-config)
32 #:use-module (web server)
33 #:use-module (web server http)
34 #:use-module (web response)
35 #:use-module (ice-9 threads)
36 #:use-module (srfi srfi-9 gnu)
37 #:use-module (srfi srfi-64))
41 (define %http-server-port
42 ;; TCP port to use for the stub HTTP server.
46 ;; URL to use for 'home-page' tests.
47 (string-append "http://localhost:" (number->string %http-server-port)
51 ;; SHA256 of the empty string.
53 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
55 (define %http-server-socket
56 ;; Socket used by the Web server.
59 (let ((sock (socket PF_INET SOCK_STREAM 0)))
60 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
62 (make-socket-address AF_INET INADDR_LOOPBACK
66 (let ((err (system-error-errno args)))
67 (format (current-error-port)
68 "warning: cannot run Web server for tests: ~a~%"
72 (define (http-write server client response body)
74 (let* ((response (write-response response client))
75 (port (response-port response)))
79 (write-response-body response body)))
81 (quit #t) ;exit the server thread
84 ;; Mutex and condition variable to synchronize with the HTTP server.
85 (define %http-server-lock (make-mutex))
86 (define %http-server-ready (make-condition-variable))
88 (define (http-open . args)
89 "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
90 (with-mutex %http-server-lock
91 (let ((result (apply (@@ (web server http) http-open) args)))
92 (signal-condition-variable %http-server-ready)
95 (define-server-impl stub-http-server
96 ;; Stripped-down version of Guile's built-in HTTP server.
98 (@@ (web server http) http-read)
100 (@@ (web server http) http-close))
102 (define (call-with-http-server code thunk)
103 "Call THUNK with an HTTP server running and returning CODE on HTTP
105 (define (server-body)
106 (define (handle request body)
107 (values (build-response #:code code
108 #:reason-phrase "Such is life")
113 (run-server handle stub-http-server
114 `(#:socket ,%http-server-socket)))
117 (with-mutex %http-server-lock
118 (let ((server (make-thread server-body)))
119 (wait-condition-variable %http-server-ready %http-server-lock)
120 ;; Normally SERVER exits automatically once it has received a request.
123 (define-syntax-rule (with-http-server code body ...)
124 (call-with-http-server code (lambda () body ...)))
129 (define (call-with-warnings thunk)
130 (let ((port (open-output-string)))
131 (parameterize ((guix-warning-port port))
133 (get-output-string port)))
135 (define-syntax-rule (with-warnings body ...)
136 (call-with-warnings (lambda () body ...)))
138 (test-assert "description: not empty"
140 (string-contains (with-warnings
141 (let ((pkg (dummy-package "x"
143 (check-description-style pkg)))
144 "description should not be empty")))
146 (test-assert "description: valid Texinfo markup"
150 (check-description-style (dummy-package "x" (description "f{oo}b@r"))))
151 "Texinfo markup in description is invalid")))
153 (test-assert "description: does not start with an upper-case letter"
155 (string-contains (with-warnings
156 (let ((pkg (dummy-package "x"
157 (description "bad description."))))
158 (check-description-style pkg)))
159 "description should start with an upper-case letter")))
161 (test-assert "description: may start with a digit"
164 (let ((pkg (dummy-package "x"
165 (description "2-component library."))))
166 (check-description-style pkg)))))
168 (test-assert "description: may start with lower-case package name"
171 (let ((pkg (dummy-package "x"
172 (description "x is a dummy package."))))
173 (check-description-style pkg)))))
175 (test-assert "description: two spaces after end of sentence"
177 (string-contains (with-warnings
178 (let ((pkg (dummy-package "x"
179 (description "Bad. Quite bad."))))
180 (check-description-style pkg)))
181 "sentences in description should be followed by two spaces")))
183 (test-assert "description: end-of-sentence detection with abbreviations"
186 (let ((pkg (dummy-package "x"
188 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
189 (check-description-style pkg)))))
191 (test-assert "synopsis: not empty"
193 (string-contains (with-warnings
194 (let ((pkg (dummy-package "x"
196 (check-synopsis-style pkg)))
197 "synopsis should not be empty")))
199 (test-assert "synopsis: does not start with an upper-case letter"
201 (string-contains (with-warnings
202 (let ((pkg (dummy-package "x"
203 (synopsis "bad synopsis."))))
204 (check-synopsis-style pkg)))
205 "synopsis should start with an upper-case letter")))
207 (test-assert "synopsis: may start with a digit"
210 (let ((pkg (dummy-package "x"
211 (synopsis "5-dimensional frobnicator"))))
212 (check-synopsis-style pkg)))))
214 (test-assert "synopsis: ends with a period"
216 (string-contains (with-warnings
217 (let ((pkg (dummy-package "x"
218 (synopsis "Bad synopsis."))))
219 (check-synopsis-style pkg)))
220 "no period allowed at the end of the synopsis")))
222 (test-assert "synopsis: ends with 'etc.'"
223 (string-null? (with-warnings
224 (let ((pkg (dummy-package "x"
225 (synopsis "Foo, bar, etc."))))
226 (check-synopsis-style pkg)))))
228 (test-assert "synopsis: starts with 'A'"
230 (string-contains (with-warnings
231 (let ((pkg (dummy-package "x"
232 (synopsis "A bad synopŝis"))))
233 (check-synopsis-style pkg)))
234 "no article allowed at the beginning of the synopsis")))
236 (test-assert "synopsis: starts with 'An'"
238 (string-contains (with-warnings
239 (let ((pkg (dummy-package "x"
240 (synopsis "An awful synopsis"))))
241 (check-synopsis-style pkg)))
242 "no article allowed at the beginning of the synopsis")))
244 (test-assert "synopsis: starts with 'a'"
246 (string-contains (with-warnings
247 (let ((pkg (dummy-package "x"
248 (synopsis "a bad synopsis"))))
249 (check-synopsis-style pkg)))
250 "no article allowed at the beginning of the synopsis")))
252 (test-assert "synopsis: starts with 'an'"
254 (string-contains (with-warnings
255 (let ((pkg (dummy-package "x"
256 (synopsis "an awful synopsis"))))
257 (check-synopsis-style pkg)))
258 "no article allowed at the beginning of the synopsis")))
260 (test-assert "synopsis: too long"
262 (string-contains (with-warnings
263 (let ((pkg (dummy-package "x"
264 (synopsis (make-string 80 #\x)))))
265 (check-synopsis-style pkg)))
266 "synopsis should be less than 80 characters long")))
268 (test-assert "synopsis: start with package name"
270 (string-contains (with-warnings
271 (let ((pkg (dummy-package "x"
273 (synopsis "foo, a nice package"))))
274 (check-synopsis-style pkg)))
275 "synopsis should not start with the package name")))
277 (test-assert "synopsis: start with package name prefix"
280 (let ((pkg (dummy-package "arb"
281 (synopsis "Arbitrary precision"))))
282 (check-synopsis-style pkg)))))
284 (test-assert "synopsis: start with abbreviation"
287 (let ((pkg (dummy-package "uucp"
288 ;; Same problem with "APL interpreter", etc.
289 (synopsis "UUCP implementation")
290 (description "Imagine this is Taylor UUCP."))))
291 (check-synopsis-style pkg)))))
293 (test-assert "inputs: pkg-config is probably a native input"
297 (let ((pkg (dummy-package "x"
298 (inputs `(("pkg-config" ,pkg-config))))))
299 (check-inputs-should-be-native pkg)))
300 "pkg-config should probably be a native input")))
302 (test-assert "patches: file names"
306 (let ((pkg (dummy-package "x"
312 (patches (list "/path/to/y.patch")))))))
313 (check-patch-file-names pkg)))
314 "file names of patches should start with the package name")))
316 (test-assert "patches: not found"
320 (let ((pkg (dummy-package "x"
327 (list (search-patch "this-patch-does-not-exist!"))))))))
328 (check-patch-file-names pkg)))
331 (test-assert "derivation: invalid arguments"
335 (let ((pkg (dummy-package "x"
337 '(#:imported-modules (invalid-module))))))
338 (check-derivation pkg)))
339 "failed to create derivation")))
341 (test-assert "license: invalid license"
344 (check-license (dummy-package "x" (license #f))))
347 (test-assert "home-page: wrong home-page"
352 (inherit (dummy-package "x"))
354 (check-home-page pkg)))
357 (test-assert "home-page: invalid URI"
362 (inherit (dummy-package "x"))
363 (home-page "foobar"))))
364 (check-home-page pkg)))
365 "invalid home page URL")))
367 (test-assert "home-page: host not found"
372 (inherit (dummy-package "x"))
373 (home-page "http://does-not-exist"))))
374 (check-home-page pkg)))
375 "domain not found")))
377 (test-skip (if %http-server-socket 0 1))
378 (test-assert "home-page: Connection refused"
383 (inherit (dummy-package "x"))
384 (home-page %local-url))))
385 (check-home-page pkg)))
386 "Connection refused")))
388 (test-skip (if %http-server-socket 0 1))
389 (test-equal "home-page: 200"
392 (with-http-server 200
394 (inherit (dummy-package "x"))
395 (home-page %local-url))))
396 (check-home-page pkg)))))
398 (test-skip (if %http-server-socket 0 1))
399 (test-assert "home-page: 404"
403 (with-http-server 404
405 (inherit (dummy-package "x"))
406 (home-page %local-url))))
407 (check-home-page pkg))))
408 "not reachable: 404")))
410 (test-assert "source-file-name"
414 (let ((pkg (dummy-package "x"
419 (uri "http://www.example.com/3.2.1.tar.gz")
420 (sha256 %null-sha256))))))
421 (check-source-file-name pkg)))
422 "file name should contain the package name")))
424 (test-assert "source-file-name: v prefix"
428 (let ((pkg (dummy-package "x"
433 (uri "http://www.example.com/v3.2.1.tar.gz")
434 (sha256 %null-sha256))))))
435 (check-source-file-name pkg)))
436 "file name should contain the package name")))
438 (test-assert "source-file-name: bad checkout"
442 (let ((pkg (dummy-package "x"
448 (url "http://www.example.com/x.git")
450 (sha256 %null-sha256))))))
451 (check-source-file-name pkg)))
452 "file name should contain the package name")))
454 (test-assert "source-file-name: good checkout"
459 (let ((pkg (dummy-package "x"
465 (url "http://git.example.com/x.git")
467 (file-name (string-append "x-" version))
468 (sha256 %null-sha256))))))
469 (check-source-file-name pkg)))
470 "file name should contain the package name"))))
472 (test-assert "source-file-name: valid"
477 (let ((pkg (dummy-package "x"
482 (uri "http://www.example.com/x-3.2.1.tar.gz")
483 (sha256 %null-sha256))))))
484 (check-source-file-name pkg)))
485 "file name should contain the package name"))))
487 (test-skip (if %http-server-socket 0 1))
488 (test-equal "source: 200"
491 (with-http-server 200
493 (inherit (dummy-package "x"))
497 (sha256 %null-sha256))))))
498 (check-source pkg)))))
500 (test-skip (if %http-server-socket 0 1))
501 (test-assert "source: 404"
505 (with-http-server 404
507 (inherit (dummy-package "x"))
511 (sha256 %null-sha256))))))
512 (check-source pkg))))
513 "not reachable: 404")))
515 (test-assert "formatting: lonely parentheses"
520 dummy-package "ugly as hell!"
525 (test-assert "formatting: tabulation"
528 (check-formatting (dummy-package "leave the tab here: ")))
531 (test-assert "formatting: trailing white space"
534 ;; Leave the trailing white space on the next line!
535 (check-formatting (dummy-package "x")))
536 "trailing white space"))
538 (test-assert "formatting: long line"
542 (dummy-package "x" ;here is a stupid comment just to make a long line
546 (test-assert "formatting: alright"
549 (check-formatting (dummy-package "x")))))
554 (exit (= (test-runner-fail-count (test-runner-current)) 0))
557 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
558 ;; eval: (put 'with-warnings 'scheme-indent-function 0)