1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013 Cyril Roelandt <tipecaml@gmail.com>
3 ;;; Copyright © 2014 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 build-system gnu)
25 #:use-module (guix packages)
26 #:use-module (guix scripts lint)
27 #:use-module (guix ui)
28 #:use-module (gnu packages)
29 #:use-module (gnu packages pkg-config)
30 #:use-module (web server)
31 #:use-module (web server http)
32 #:use-module (web response)
33 #:use-module (ice-9 threads)
34 #:use-module (srfi srfi-9 gnu)
35 #:use-module (srfi srfi-64))
39 (define %http-server-port
40 ;; TCP port to use for the stub HTTP server.
44 ;; URL to use for 'home-page' tests.
45 (string-append "http://localhost:" (number->string %http-server-port)
49 ;; SHA256 of the empty string.
51 "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73"))
53 (define %http-server-socket
54 ;; Socket used by the Web server.
57 (let ((sock (socket PF_INET SOCK_STREAM 0)))
58 (setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
60 (make-socket-address AF_INET INADDR_LOOPBACK
64 (let ((err (system-error-errno args)))
65 (format (current-error-port)
66 "warning: cannot run Web server for tests: ~a~%"
70 (define (http-write server client response body)
72 (let* ((response (write-response response client))
73 (port (response-port response)))
77 (write-response-body response body)))
79 (quit #t) ;exit the server thread
82 ;; Mutex and condition variable to synchronize with the HTTP server.
83 (define %http-server-lock (make-mutex))
84 (define %http-server-ready (make-condition-variable))
86 (define (http-open . args)
87 "Start listening for HTTP requests and signal %HTTP-SERVER-READY."
88 (with-mutex %http-server-lock
89 (let ((result (apply (@@ (web server http) http-open) args)))
90 (signal-condition-variable %http-server-ready)
93 (define-server-impl stub-http-server
94 ;; Stripped-down version of Guile's built-in HTTP server.
96 (@@ (web server http) http-read)
98 (@@ (web server http) http-close))
100 (define (call-with-http-server code thunk)
101 "Call THUNK with an HTTP server running and returning CODE on HTTP
103 (define (server-body)
104 (define (handle request body)
105 (values (build-response #:code code
106 #:reason-phrase "Such is life")
111 (run-server handle stub-http-server
112 `(#:socket ,%http-server-socket)))
115 (with-mutex %http-server-lock
116 (let ((server (make-thread server-body)))
117 (wait-condition-variable %http-server-ready %http-server-lock)
118 ;; Normally SERVER exits automatically once it has received a request.
121 (define-syntax-rule (with-http-server code body ...)
122 (call-with-http-server code (lambda () body ...)))
127 (define (call-with-warnings thunk)
128 (let ((port (open-output-string)))
129 (parameterize ((guix-warning-port port))
131 (get-output-string port)))
133 (define-syntax-rule (with-warnings body ...)
134 (call-with-warnings (lambda () body ...)))
136 (test-assert "description: not empty"
138 (string-contains (with-warnings
139 (let ((pkg (dummy-package "x"
141 (check-description-style pkg)))
142 "description should not be empty")))
144 (test-assert "description: does not start with an upper-case letter"
146 (string-contains (with-warnings
147 (let ((pkg (dummy-package "x"
148 (description "bad description."))))
149 (check-description-style pkg)))
150 "description should start with an upper-case letter")))
152 (test-assert "description: may start with a digit"
155 (let ((pkg (dummy-package "x"
156 (description "2-component library."))))
157 (check-description-style pkg)))))
159 (test-assert "description: may start with lower-case package name"
162 (let ((pkg (dummy-package "x"
163 (description "x is a dummy package."))))
164 (check-description-style pkg)))))
166 (test-assert "description: two spaces after end of sentence"
168 (string-contains (with-warnings
169 (let ((pkg (dummy-package "x"
170 (description "Bad. Quite bad."))))
171 (check-description-style pkg)))
172 "sentences in description should be followed by two spaces")))
174 (test-assert "description: end-of-sentence detection with abbreviations"
177 (let ((pkg (dummy-package "x"
179 "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD)."))))
180 (check-description-style pkg)))))
182 (test-assert "synopsis: not empty"
184 (string-contains (with-warnings
185 (let ((pkg (dummy-package "x"
187 (check-synopsis-style pkg)))
188 "synopsis should not be empty")))
190 (test-assert "synopsis: does not start with an upper-case letter"
192 (string-contains (with-warnings
193 (let ((pkg (dummy-package "x"
194 (synopsis "bad synopsis."))))
195 (check-synopsis-style pkg)))
196 "synopsis should start with an upper-case letter")))
198 (test-assert "synopsis: may start with a digit"
201 (let ((pkg (dummy-package "x"
202 (synopsis "5-dimensional frobnicator"))))
203 (check-synopsis-style pkg)))))
205 (test-assert "synopsis: ends with a period"
207 (string-contains (with-warnings
208 (let ((pkg (dummy-package "x"
209 (synopsis "Bad synopsis."))))
210 (check-synopsis-style pkg)))
211 "no period allowed at the end of the synopsis")))
213 (test-assert "synopsis: ends with 'etc.'"
214 (string-null? (with-warnings
215 (let ((pkg (dummy-package "x"
216 (synopsis "Foo, bar, etc."))))
217 (check-synopsis-style pkg)))))
219 (test-assert "synopsis: starts with 'A'"
221 (string-contains (with-warnings
222 (let ((pkg (dummy-package "x"
223 (synopsis "A bad synopŝis"))))
224 (check-synopsis-style pkg)))
225 "no article allowed at the beginning of the synopsis")))
227 (test-assert "synopsis: starts with 'An'"
229 (string-contains (with-warnings
230 (let ((pkg (dummy-package "x"
231 (synopsis "An awful synopsis"))))
232 (check-synopsis-style pkg)))
233 "no article allowed at the beginning of the synopsis")))
235 (test-assert "synopsis: starts with 'a'"
237 (string-contains (with-warnings
238 (let ((pkg (dummy-package "x"
239 (synopsis "a bad synopsis"))))
240 (check-synopsis-style pkg)))
241 "no article allowed at the beginning of the synopsis")))
243 (test-assert "synopsis: starts with 'an'"
245 (string-contains (with-warnings
246 (let ((pkg (dummy-package "x"
247 (synopsis "an awful synopsis"))))
248 (check-synopsis-style pkg)))
249 "no article allowed at the beginning of the synopsis")))
251 (test-assert "synopsis: too long"
253 (string-contains (with-warnings
254 (let ((pkg (dummy-package "x"
255 (synopsis (make-string 80 #\x)))))
256 (check-synopsis-style pkg)))
257 "synopsis should be less than 80 characters long")))
259 (test-assert "synopsis: start with package name"
261 (string-contains (with-warnings
262 (let ((pkg (dummy-package "x"
264 (synopsis "foo, a nice package"))))
265 (check-synopsis-style pkg)))
266 "synopsis should not start with the package name")))
268 (test-assert "synopsis: start with package name prefix"
271 (let ((pkg (dummy-package "arb"
272 (synopsis "Arbitrary precision"))))
273 (check-synopsis-style pkg)))))
275 (test-assert "synopsis: start with abbreviation"
278 (let ((pkg (dummy-package "uucp"
279 ;; Same problem with "APL interpreter", etc.
280 (synopsis "UUCP implementation")
281 (description "Imagine this is Taylor UUCP."))))
282 (check-synopsis-style pkg)))))
284 (test-assert "inputs: pkg-config is probably a native input"
288 (let ((pkg (dummy-package "x"
289 (inputs `(("pkg-config" ,pkg-config))))))
290 (check-inputs-should-be-native pkg)))
291 "pkg-config should probably be a native input")))
293 (test-assert "patches: file names"
297 (let ((pkg (dummy-package "x"
303 (patches (list "/path/to/y.patch")))))))
304 (check-patch-file-names pkg)))
305 "file names of patches should start with the package name")))
307 (test-assert "patches: not found"
311 (let ((pkg (dummy-package "x"
318 (list (search-patch "this-patch-does-not-exist!"))))))))
319 (check-patch-file-names pkg)))
322 (test-assert "derivation: invalid arguments"
326 (let ((pkg (dummy-package "x"
328 '(#:imported-modules (invalid-module))))))
329 (check-derivation pkg)))
330 "failed to create derivation")))
332 (test-assert "home-page: wrong home-page"
337 (inherit (dummy-package "x"))
339 (check-home-page pkg)))
342 (test-assert "home-page: invalid URI"
347 (inherit (dummy-package "x"))
348 (home-page "foobar"))))
349 (check-home-page pkg)))
350 "invalid home page URL")))
352 (test-assert "home-page: host not found"
357 (inherit (dummy-package "x"))
358 (home-page "http://does-not-exist"))))
359 (check-home-page pkg)))
360 "domain not found")))
362 (test-skip (if %http-server-socket 0 1))
363 (test-assert "home-page: Connection refused"
368 (inherit (dummy-package "x"))
369 (home-page %local-url))))
370 (check-home-page pkg)))
371 "Connection refused")))
373 (test-skip (if %http-server-socket 0 1))
374 (test-equal "home-page: 200"
377 (with-http-server 200
379 (inherit (dummy-package "x"))
380 (home-page %local-url))))
381 (check-home-page pkg)))))
383 (test-skip (if %http-server-socket 0 1))
384 (test-assert "home-page: 404"
388 (with-http-server 404
390 (inherit (dummy-package "x"))
391 (home-page %local-url))))
392 (check-home-page pkg))))
393 "not reachable: 404")))
395 (test-skip (if %http-server-socket 0 1))
396 (test-equal "source: 200"
399 (with-http-server 200
401 (inherit (dummy-package "x"))
405 (sha256 %null-sha256))))))
406 (check-source pkg)))))
408 (test-skip (if %http-server-socket 0 1))
409 (test-assert "source: 404"
413 (with-http-server 404
415 (inherit (dummy-package "x"))
419 (sha256 %null-sha256))))))
420 (check-source pkg))))
421 "not reachable: 404")))
423 (test-assert "formatting: tabulation"
426 (check-formatting (dummy-package "leave the tab here: ")))
429 (test-assert "formatting: trailing white space"
432 ;; Leave the trailing white space on the next line!
433 (check-formatting (dummy-package "x")))
434 "trailing white space"))
436 (test-assert "formatting: long line"
440 (dummy-package "x" ;here is a stupid comment just to make a long line
444 (test-assert "formatting: alright"
447 (check-formatting (dummy-package "x")))))
452 (exit (= (test-runner-fail-count (test-runner-current)) 0))
455 ;; eval: (put 'with-http-server 'scheme-indent-function 1)
456 ;; eval: (put 'with-warnings 'scheme-indent-function 0)