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>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (test-lint)
22 #:use-module (guix tests)
23 #:use-module (guix download)
24 #:use-module (guix git-download)
25 #:use-module (guix build-system gnu)
26 #:use-module (guix packages)
27 #:use-module (guix scripts lint)
28 #:use-module (guix ui)
29 #:use-module (gnu packages)
30 #:use-module (gnu packages pkg-config)
31 #:use-module (web server)
32 #:use-module (web server http)
33 #:use-module (web response)
34 #:use-module (ice-9 threads)
35 #:use-module (srfi srfi-9 gnu)
36 #:use-module (srfi srfi-64))
40 (define %http-server-port
41 ;; TCP port to use for the stub HTTP server.
45 ;; URL to use for 'home-page' tests.
46 (string-append "http://localhost:" (number->string %http-server-port)
50 ;; SHA256 of the empty string.
52 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
54 (define %http-server-socket
55 ;; Socket used by the Web server.
58 (let ((sock (socket PF_INET SOCK_STREAM 0)))
59 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
61 (make-socket-address AF_INET INADDR_LOOPBACK
65 (let ((err (system-error-errno args)))
66 (format (current-error-port)
67 "warning: cannot run Web server for tests: ~a~%"
71 (define (http-write server client response body)
73 (let* ((response (write-response response client))
74 (port (response-port response)))
78 (write-response-body response body)))
80 (quit #t) ;exit the server thread
83 ;; Mutex and condition variable to synchronize with the HTTP server.
84 (define %http-server-lock (make-mutex))
85 (define %http-server-ready (make-condition-variable))
87 (define (http-open . args)
88 "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
89 (with-mutex %http-server-lock
90 (let ((result (apply (@@ (web server http) http-open) args)))
91 (signal-condition-variable %http-server-ready)
94 (define-server-impl stub-http-server
95 ;; Stripped-down version of Guile's built-in HTTP server.
97 (@@ (web server http) http-read)
99 (@@ (web server http) http-close))
101 (define (call-with-http-server code thunk)
102 "Call THUNK with an HTTP server running and returning CODE on HTTP
104 (define (server-body)
105 (define (handle request body)
106 (values (build-response #:code code
107 #:reason-phrase "Such is life")
112 (run-server handle stub-http-server
113 `(#:socket ,%http-server-socket)))
116 (with-mutex %http-server-lock
117 (let ((server (make-thread server-body)))
118 (wait-condition-variable %http-server-ready %http-server-lock)
119 ;; Normally SERVER exits automatically once it has received a request.
122 (define-syntax-rule (with-http-server code body ...)
123 (call-with-http-server code (lambda () body ...)))
128 (define (call-with-warnings thunk)
129 (let ((port (open-output-string)))
130 (parameterize ((guix-warning-port port))
132 (get-output-string port)))
134 (define-syntax-rule (with-warnings body ...)
135 (call-with-warnings (lambda () body ...)))
137 (test-assert "description: not empty"
139 (string-contains (with-warnings
140 (let ((pkg (dummy-package "x"
142 (check-description-style pkg)))
143 "description should not be empty")))
145 (test-assert "description: does not start with an upper-case letter"
147 (string-contains (with-warnings
148 (let ((pkg (dummy-package "x"
149 (description "bad description."))))
150 (check-description-style pkg)))
151 "description should start with an upper-case letter")))
153 (test-assert "description: may start with a digit"
156 (let ((pkg (dummy-package "x"
157 (description "2-component library."))))
158 (check-description-style pkg)))))
160 (test-assert "description: may start with lower-case package name"
163 (let ((pkg (dummy-package "x"
164 (description "x is a dummy package."))))
165 (check-description-style pkg)))))
167 (test-assert "description: two spaces after end of sentence"
169 (string-contains (with-warnings
170 (let ((pkg (dummy-package "x"
171 (description "Bad. Quite bad."))))
172 (check-description-style pkg)))
173 "sentences in description should be followed by two spaces")))
175 (test-assert "description: end-of-sentence detection with abbreviations"
178 (let ((pkg (dummy-package "x"
180 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
181 (check-description-style pkg)))))
183 (test-assert "synopsis: not empty"
185 (string-contains (with-warnings
186 (let ((pkg (dummy-package "x"
188 (check-synopsis-style pkg)))
189 "synopsis should not be empty")))
191 (test-assert "synopsis: does not start with an upper-case letter"
193 (string-contains (with-warnings
194 (let ((pkg (dummy-package "x"
195 (synopsis "bad synopsis."))))
196 (check-synopsis-style pkg)))
197 "synopsis should start with an upper-case letter")))
199 (test-assert "synopsis: may start with a digit"
202 (let ((pkg (dummy-package "x"
203 (synopsis "5-dimensional frobnicator"))))
204 (check-synopsis-style pkg)))))
206 (test-assert "synopsis: ends with a period"
208 (string-contains (with-warnings
209 (let ((pkg (dummy-package "x"
210 (synopsis "Bad synopsis."))))
211 (check-synopsis-style pkg)))
212 "no period allowed at the end of the synopsis")))
214 (test-assert "synopsis: ends with 'etc.'"
215 (string-null? (with-warnings
216 (let ((pkg (dummy-package "x"
217 (synopsis "Foo, bar, etc."))))
218 (check-synopsis-style pkg)))))
220 (test-assert "synopsis: starts with 'A'"
222 (string-contains (with-warnings
223 (let ((pkg (dummy-package "x"
224 (synopsis "A bad synopŝis"))))
225 (check-synopsis-style pkg)))
226 "no article allowed at the beginning of the synopsis")))
228 (test-assert "synopsis: starts with 'An'"
230 (string-contains (with-warnings
231 (let ((pkg (dummy-package "x"
232 (synopsis "An awful synopsis"))))
233 (check-synopsis-style pkg)))
234 "no article allowed at the beginning of the synopsis")))
236 (test-assert "synopsis: starts with 'a'"
238 (string-contains (with-warnings
239 (let ((pkg (dummy-package "x"
240 (synopsis "a bad synopsis"))))
241 (check-synopsis-style pkg)))
242 "no article allowed at the beginning of the synopsis")))
244 (test-assert "synopsis: starts with 'an'"
246 (string-contains (with-warnings
247 (let ((pkg (dummy-package "x"
248 (synopsis "an awful synopsis"))))
249 (check-synopsis-style pkg)))
250 "no article allowed at the beginning of the synopsis")))
252 (test-assert "synopsis: too long"
254 (string-contains (with-warnings
255 (let ((pkg (dummy-package "x"
256 (synopsis (make-string 80 #\x)))))
257 (check-synopsis-style pkg)))
258 "synopsis should be less than 80 characters long")))
260 (test-assert "synopsis: start with package name"
262 (string-contains (with-warnings
263 (let ((pkg (dummy-package "x"
265 (synopsis "foo, a nice package"))))
266 (check-synopsis-style pkg)))
267 "synopsis should not start with the package name")))
269 (test-assert "synopsis: start with package name prefix"
272 (let ((pkg (dummy-package "arb"
273 (synopsis "Arbitrary precision"))))
274 (check-synopsis-style pkg)))))
276 (test-assert "synopsis: start with abbreviation"
279 (let ((pkg (dummy-package "uucp"
280 ;; Same problem with "APL interpreter", etc.
281 (synopsis "UUCP implementation")
282 (description "Imagine this is Taylor UUCP."))))
283 (check-synopsis-style pkg)))))
285 (test-assert "inputs: pkg-config is probably a native input"
289 (let ((pkg (dummy-package "x"
290 (inputs `(("pkg-config" ,pkg-config))))))
291 (check-inputs-should-be-native pkg)))
292 "pkg-config should probably be a native input")))
294 (test-assert "patches: file names"
298 (let ((pkg (dummy-package "x"
304 (patches (list "/path/to/y.patch")))))))
305 (check-patch-file-names pkg)))
306 "file names of patches should start with the package name")))
308 (test-assert "patches: not found"
312 (let ((pkg (dummy-package "x"
319 (list (search-patch "this-patch-does-not-exist!"))))))))
320 (check-patch-file-names pkg)))
323 (test-assert "derivation: invalid arguments"
327 (let ((pkg (dummy-package "x"
329 '(#:imported-modules (invalid-module))))))
330 (check-derivation pkg)))
331 "failed to create derivation")))
333 (test-assert "license: invalid license"
336 (check-license (dummy-package "x" (license #f))))
339 (test-assert "home-page: wrong home-page"
344 (inherit (dummy-package "x"))
346 (check-home-page pkg)))
349 (test-assert "home-page: invalid URI"
354 (inherit (dummy-package "x"))
355 (home-page "foobar"))))
356 (check-home-page pkg)))
357 "invalid home page URL")))
359 (test-assert "home-page: host not found"
364 (inherit (dummy-package "x"))
365 (home-page "http://does-not-exist"))))
366 (check-home-page pkg)))
367 "domain not found")))
369 (test-skip (if %http-server-socket 0 1))
370 (test-assert "home-page: Connection refused"
375 (inherit (dummy-package "x"))
376 (home-page %local-url))))
377 (check-home-page pkg)))
378 "Connection refused")))
380 (test-skip (if %http-server-socket 0 1))
381 (test-equal "home-page: 200"
384 (with-http-server 200
386 (inherit (dummy-package "x"))
387 (home-page %local-url))))
388 (check-home-page pkg)))))
390 (test-skip (if %http-server-socket 0 1))
391 (test-assert "home-page: 404"
395 (with-http-server 404
397 (inherit (dummy-package "x"))
398 (home-page %local-url))))
399 (check-home-page pkg))))
400 "not reachable: 404")))
402 (test-assert "source-file-name"
406 (let ((pkg (dummy-package "x"
411 (uri "http://www.example.com/3.2.1.tar.gz")
412 (sha256 %null-sha256))))))
413 (check-source-file-name pkg)))
414 "file name should contain the package name")))
416 (test-assert "source-file-name: v prefix"
420 (let ((pkg (dummy-package "x"
425 (uri "http://www.example.com/v3.2.1.tar.gz")
426 (sha256 %null-sha256))))))
427 (check-source-file-name pkg)))
428 "file name should contain the package name")))
430 (test-assert "source-file-name: bad checkout"
434 (let ((pkg (dummy-package "x"
440 (url "http://www.example.com/x.git")
442 (sha256 %null-sha256))))))
443 (check-source-file-name pkg)))
444 "file name should contain the package name")))
446 (test-assert "source-file-name: good checkout"
451 (let ((pkg (dummy-package "x"
457 (url "http://git.example.com/x.git")
459 (file-name (string-append "x-" version))
460 (sha256 %null-sha256))))))
461 (check-source-file-name pkg)))
462 "file name should contain the package name"))))
464 (test-assert "source-file-name: valid"
469 (let ((pkg (dummy-package "x"
474 (uri "http://www.example.com/x-3.2.1.tar.gz")
475 (sha256 %null-sha256))))))
476 (check-source-file-name pkg)))
477 "file name should contain the package name"))))
479 (test-skip (if %http-server-socket 0 1))
480 (test-equal "source: 200"
483 (with-http-server 200
485 (inherit (dummy-package "x"))
489 (sha256 %null-sha256))))))
490 (check-source pkg)))))
492 (test-skip (if %http-server-socket 0 1))
493 (test-assert "source: 404"
497 (with-http-server 404
499 (inherit (dummy-package "x"))
503 (sha256 %null-sha256))))))
504 (check-source pkg))))
505 "not reachable: 404")))
507 (test-assert "formatting: tabulation"
510 (check-formatting (dummy-package "leave the tab here: ")))
513 (test-assert "formatting: trailing white space"
516 ;; Leave the trailing white space on the next line!
517 (check-formatting (dummy-package "x")))
518 "trailing white space"))
520 (test-assert "formatting: long line"
524 (dummy-package "x" ;here is a stupid comment just to make a long line
528 (test-assert "formatting: alright"
531 (check-formatting (dummy-package "x")))))
536 (exit (= (test-runner-fail-count (test-runner-current)) 0))
539 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
540 ;; eval: (put 'with-warnings 'scheme-indent-function 0)