| 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2012, 2013 Cyril Roelandt <tipecaml@gmail.com> |
| 3 | ;;; Copyright © 2014, 2015, 2016 Eric Bavier <bavier@member.fsf.org> |
| 4 | ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org> |
| 5 | ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org> |
| 6 | ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com> |
| 7 | ;;; Copyright © 2017 Alex Kost <alezost@gmail.com> |
| 8 | ;;; Copyright © 2017 Efraim Flashner <efraim@flashner.co.il> |
| 9 | ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net> |
| 10 | ;;; |
| 11 | ;;; This file is part of GNU Guix. |
| 12 | ;;; |
| 13 | ;;; GNU Guix is free software; you can redistribute it and/or modify it |
| 14 | ;;; under the terms of the GNU General Public License as published by |
| 15 | ;;; the Free Software Foundation; either version 3 of the License, or (at |
| 16 | ;;; your option) any later version. |
| 17 | ;;; |
| 18 | ;;; GNU Guix is distributed in the hope that it will be useful, but |
| 19 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | ;;; GNU General Public License for more details. |
| 22 | ;;; |
| 23 | ;;; You should have received a copy of the GNU General Public License |
| 24 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | ;; Avoid interference. |
| 27 | (unsetenv "http_proxy") |
| 28 | |
| 29 | (define-module (test-lint) |
| 30 | #:use-module (guix tests) |
| 31 | #:use-module (guix tests http) |
| 32 | #:use-module (guix download) |
| 33 | #:use-module (guix git-download) |
| 34 | #:use-module (guix build-system gnu) |
| 35 | #:use-module (guix packages) |
| 36 | #:use-module (guix lint) |
| 37 | #:use-module (guix ui) |
| 38 | #:use-module (guix swh) |
| 39 | #:use-module (gnu packages) |
| 40 | #:use-module (gnu packages glib) |
| 41 | #:use-module (gnu packages pkg-config) |
| 42 | #:use-module (gnu packages python-xyz) |
| 43 | #:use-module (web uri) |
| 44 | #:use-module (web server) |
| 45 | #:use-module (web server http) |
| 46 | #:use-module (web response) |
| 47 | #:use-module (ice-9 match) |
| 48 | #:use-module (ice-9 regex) |
| 49 | #:use-module (ice-9 getopt-long) |
| 50 | #:use-module (ice-9 pretty-print) |
| 51 | #:use-module (rnrs bytevectors) |
| 52 | #:use-module (srfi srfi-1) |
| 53 | #:use-module (srfi srfi-9 gnu) |
| 54 | #:use-module (srfi srfi-26) |
| 55 | #:use-module (srfi srfi-64)) |
| 56 | |
| 57 | ;; Test the linter. |
| 58 | |
| 59 | ;; Avoid collisions with other tests. |
| 60 | (%http-server-port 9999) |
| 61 | |
| 62 | (define %null-sha256 |
| 63 | ;; SHA256 of the empty string. |
| 64 | (base32 |
| 65 | "0mdqa9w1p6cmli6976v4wi0sw9r4p5prkj7lzfd1877wk11c9c73")) |
| 66 | |
| 67 | (define %long-string |
| 68 | (make-string 2000 #\a)) |
| 69 | |
| 70 | (define (string-match-or-error pattern str) |
| 71 | (or (string-match pattern str) |
| 72 | (error str "did not match" pattern))) |
| 73 | |
| 74 | (define single-lint-warning-message |
| 75 | (match-lambda |
| 76 | (((and (? lint-warning?) warning)) |
| 77 | (lint-warning-message warning)))) |
| 78 | |
| 79 | (define (warning-contains? str warnings) |
| 80 | "Return true if WARNINGS is a singleton with a warning that contains STR." |
| 81 | (match warnings |
| 82 | (((? lint-warning? warning)) |
| 83 | (string-contains (lint-warning-message warning) str)))) |
| 84 | |
| 85 | \f |
| 86 | (test-begin "lint") |
| 87 | |
| 88 | (test-equal "description: not a string" |
| 89 | "invalid description: foobar" |
| 90 | (single-lint-warning-message |
| 91 | (check-description-style |
| 92 | (dummy-package "x" (description 'foobar))))) |
| 93 | |
| 94 | (test-equal "description: not empty" |
| 95 | "description should not be empty" |
| 96 | (single-lint-warning-message |
| 97 | (check-description-style |
| 98 | (dummy-package "x" (description ""))))) |
| 99 | |
| 100 | (test-equal "description: invalid Texinfo markup" |
| 101 | "Texinfo markup in description is invalid" |
| 102 | (single-lint-warning-message |
| 103 | (check-description-style |
| 104 | (dummy-package "x" (description "f{oo}b@r"))))) |
| 105 | |
| 106 | (test-equal "description: does not start with an upper-case letter" |
| 107 | "description should start with an upper-case letter or digit" |
| 108 | (single-lint-warning-message |
| 109 | (let ((pkg (dummy-package "x" |
| 110 | (description "bad description.")))) |
| 111 | (check-description-style pkg)))) |
| 112 | |
| 113 | (test-equal "description: may start with a digit" |
| 114 | '() |
| 115 | (let ((pkg (dummy-package "x" |
| 116 | (description "2-component library.")))) |
| 117 | (check-description-style pkg))) |
| 118 | |
| 119 | (test-equal "description: may start with lower-case package name" |
| 120 | '() |
| 121 | (let ((pkg (dummy-package "x" |
| 122 | (description "x is a dummy package.")))) |
| 123 | (check-description-style pkg))) |
| 124 | |
| 125 | (test-equal "description: two spaces after end of sentence" |
| 126 | "sentences in description should be followed by two spaces; possible infraction at 3" |
| 127 | (single-lint-warning-message |
| 128 | (let ((pkg (dummy-package "x" |
| 129 | (description "Bad. Quite bad.")))) |
| 130 | (check-description-style pkg)))) |
| 131 | |
| 132 | (test-equal "description: end-of-sentence detection with abbreviations" |
| 133 | '() |
| 134 | (let ((pkg (dummy-package "x" |
| 135 | (description |
| 136 | "E.g. Foo, i.e. Bar resp. Baz (a.k.a. DVD).")))) |
| 137 | (check-description-style pkg))) |
| 138 | |
| 139 | (test-equal "description: may not contain trademark signs: ™" |
| 140 | "description should not contain trademark sign '™' at 20" |
| 141 | (single-lint-warning-message |
| 142 | (let ((pkg (dummy-package "x" |
| 143 | (description "Does The Right Thing™")))) |
| 144 | (check-description-style pkg)))) |
| 145 | |
| 146 | (test-equal "description: may not contain trademark signs: ®" |
| 147 | "description should not contain trademark sign '®' at 17" |
| 148 | (single-lint-warning-message |
| 149 | (let ((pkg (dummy-package "x" |
| 150 | (description "Works with Format®")))) |
| 151 | (check-description-style pkg)))) |
| 152 | |
| 153 | (test-equal "description: suggest ornament instead of quotes" |
| 154 | "use @code or similar ornament instead of quotes" |
| 155 | (single-lint-warning-message |
| 156 | (let ((pkg (dummy-package "x" |
| 157 | (description "This is a 'quoted' thing.")))) |
| 158 | (check-description-style pkg)))) |
| 159 | |
| 160 | (test-equal "synopsis: not a string" |
| 161 | "invalid synopsis: #f" |
| 162 | (single-lint-warning-message |
| 163 | (let ((pkg (dummy-package "x" |
| 164 | (synopsis #f)))) |
| 165 | (check-synopsis-style pkg)))) |
| 166 | |
| 167 | (test-equal "synopsis: not empty" |
| 168 | "synopsis should not be empty" |
| 169 | (single-lint-warning-message |
| 170 | (let ((pkg (dummy-package "x" |
| 171 | (synopsis "")))) |
| 172 | (check-synopsis-style pkg)))) |
| 173 | |
| 174 | (test-equal "synopsis: valid Texinfo markup" |
| 175 | "Texinfo markup in synopsis is invalid" |
| 176 | (single-lint-warning-message |
| 177 | (check-synopsis-style |
| 178 | (dummy-package "x" (synopsis "Bad $@ texinfo"))))) |
| 179 | |
| 180 | (test-equal "synopsis: does not start with an upper-case letter" |
| 181 | "synopsis should start with an upper-case letter or digit" |
| 182 | (single-lint-warning-message |
| 183 | (let ((pkg (dummy-package "x" |
| 184 | (synopsis "bad synopsis")))) |
| 185 | (check-synopsis-style pkg)))) |
| 186 | |
| 187 | (test-equal "synopsis: may start with a digit" |
| 188 | '() |
| 189 | (let ((pkg (dummy-package "x" |
| 190 | (synopsis "5-dimensional frobnicator")))) |
| 191 | (check-synopsis-style pkg))) |
| 192 | |
| 193 | (test-equal "synopsis: ends with a period" |
| 194 | "no period allowed at the end of the synopsis" |
| 195 | (single-lint-warning-message |
| 196 | (let ((pkg (dummy-package "x" |
| 197 | (synopsis "Bad synopsis.")))) |
| 198 | (check-synopsis-style pkg)))) |
| 199 | |
| 200 | (test-equal "synopsis: ends with 'etc.'" |
| 201 | '() |
| 202 | (let ((pkg (dummy-package "x" |
| 203 | (synopsis "Foo, bar, etc.")))) |
| 204 | (check-synopsis-style pkg))) |
| 205 | |
| 206 | (test-equal "synopsis: starts with 'A'" |
| 207 | "no article allowed at the beginning of the synopsis" |
| 208 | (single-lint-warning-message |
| 209 | (let ((pkg (dummy-package "x" |
| 210 | (synopsis "A bad synopŝis")))) |
| 211 | (check-synopsis-style pkg)))) |
| 212 | |
| 213 | (test-equal "synopsis: starts with 'An'" |
| 214 | "no article allowed at the beginning of the synopsis" |
| 215 | (single-lint-warning-message |
| 216 | (let ((pkg (dummy-package "x" |
| 217 | (synopsis "An awful synopsis")))) |
| 218 | (check-synopsis-style pkg)))) |
| 219 | |
| 220 | (test-equal "synopsis: starts with 'a'" |
| 221 | '("no article allowed at the beginning of the synopsis" |
| 222 | "synopsis should start with an upper-case letter or digit") |
| 223 | (sort |
| 224 | (map |
| 225 | lint-warning-message |
| 226 | (let ((pkg (dummy-package "x" |
| 227 | (synopsis "a bad synopsis")))) |
| 228 | (check-synopsis-style pkg))) |
| 229 | string<?)) |
| 230 | |
| 231 | (test-equal "synopsis: starts with 'an'" |
| 232 | '("no article allowed at the beginning of the synopsis" |
| 233 | "synopsis should start with an upper-case letter or digit") |
| 234 | (sort |
| 235 | (map |
| 236 | lint-warning-message |
| 237 | (let ((pkg (dummy-package "x" |
| 238 | (synopsis "an awful synopsis")))) |
| 239 | (check-synopsis-style pkg))) |
| 240 | string<?)) |
| 241 | |
| 242 | (test-equal "synopsis: too long" |
| 243 | "synopsis should be less than 80 characters long" |
| 244 | (single-lint-warning-message |
| 245 | (let ((pkg (dummy-package "x" |
| 246 | (synopsis (make-string 80 #\X))))) |
| 247 | (check-synopsis-style pkg)))) |
| 248 | |
| 249 | (test-equal "synopsis: start with package name" |
| 250 | "synopsis should not start with the package name" |
| 251 | (single-lint-warning-message |
| 252 | (let ((pkg (dummy-package "x" |
| 253 | (name "Foo") |
| 254 | (synopsis "Foo, a nice package")))) |
| 255 | (check-synopsis-style pkg)))) |
| 256 | |
| 257 | (test-equal "synopsis: start with package name prefix" |
| 258 | '() |
| 259 | (let ((pkg (dummy-package "arb" |
| 260 | (synopsis "Arbitrary precision")))) |
| 261 | (check-synopsis-style pkg))) |
| 262 | |
| 263 | (test-equal "synopsis: start with abbreviation" |
| 264 | '() |
| 265 | (let ((pkg (dummy-package "uucp" |
| 266 | ;; Same problem with "APL interpreter", etc. |
| 267 | (synopsis "UUCP implementation") |
| 268 | (description "Imagine this is Taylor UUCP.")))) |
| 269 | (check-synopsis-style pkg))) |
| 270 | |
| 271 | (test-equal "inputs: pkg-config is probably a native input" |
| 272 | "'pkg-config' should probably be a native input" |
| 273 | (single-lint-warning-message |
| 274 | (let ((pkg (dummy-package "x" |
| 275 | (inputs `(("pkg-config" ,pkg-config)))))) |
| 276 | (check-inputs-should-be-native pkg)))) |
| 277 | |
| 278 | (test-equal "inputs: glib:bin is probably a native input" |
| 279 | "'glib:bin' should probably be a native input" |
| 280 | (single-lint-warning-message |
| 281 | (let ((pkg (dummy-package "x" |
| 282 | (inputs `(("glib" ,glib "bin")))))) |
| 283 | (check-inputs-should-be-native pkg)))) |
| 284 | |
| 285 | (test-equal |
| 286 | "inputs: python-setuptools should not be an input at all (input)" |
| 287 | "'python-setuptools' should probably not be an input at all" |
| 288 | (single-lint-warning-message |
| 289 | (let ((pkg (dummy-package "x" |
| 290 | (inputs `(("python-setuptools" |
| 291 | ,python-setuptools)))))) |
| 292 | (check-inputs-should-not-be-an-input-at-all pkg)))) |
| 293 | |
| 294 | (test-equal |
| 295 | "inputs: python-setuptools should not be an input at all (native-input)" |
| 296 | "'python-setuptools' should probably not be an input at all" |
| 297 | (single-lint-warning-message |
| 298 | (let ((pkg (dummy-package "x" |
| 299 | (native-inputs |
| 300 | `(("python-setuptools" |
| 301 | ,python-setuptools)))))) |
| 302 | (check-inputs-should-not-be-an-input-at-all pkg)))) |
| 303 | |
| 304 | (test-equal |
| 305 | "inputs: python-setuptools should not be an input at all (propagated-input)" |
| 306 | "'python-setuptools' should probably not be an input at all" |
| 307 | (single-lint-warning-message |
| 308 | (let ((pkg (dummy-package "x" |
| 309 | (propagated-inputs |
| 310 | `(("python-setuptools" ,python-setuptools)))))) |
| 311 | (check-inputs-should-not-be-an-input-at-all pkg)))) |
| 312 | |
| 313 | (test-equal "patches: file names" |
| 314 | "file names of patches should start with the package name" |
| 315 | (single-lint-warning-message |
| 316 | (let ((pkg (dummy-package "x" |
| 317 | (source |
| 318 | (dummy-origin |
| 319 | (patches (list "/path/to/y.patch"))))))) |
| 320 | (check-patch-file-names pkg)))) |
| 321 | |
| 322 | (test-equal "patches: file name too long" |
| 323 | (string-append "x-" |
| 324 | (make-string 100 #\a) |
| 325 | ".patch: file name is too long") |
| 326 | (single-lint-warning-message |
| 327 | (let ((pkg (dummy-package |
| 328 | "x" |
| 329 | (source |
| 330 | (dummy-origin |
| 331 | (patches (list (string-append "x-" |
| 332 | (make-string 100 #\a) |
| 333 | ".patch")))))))) |
| 334 | (check-patch-file-names pkg)))) |
| 335 | |
| 336 | (test-equal "patches: not found" |
| 337 | "this-patch-does-not-exist!: patch not found" |
| 338 | (single-lint-warning-message |
| 339 | (let ((pkg (dummy-package |
| 340 | "x" |
| 341 | (source |
| 342 | (dummy-origin |
| 343 | (patches |
| 344 | (list (search-patch "this-patch-does-not-exist!")))))))) |
| 345 | (check-patch-file-names pkg)))) |
| 346 | |
| 347 | (test-equal "derivation: invalid arguments" |
| 348 | "failed to create x86_64-linux derivation: (wrong-type-arg \"map\" \"Wrong type argument: ~S\" (invalid-module) ())" |
| 349 | (match (let ((pkg (dummy-package "x" |
| 350 | (arguments |
| 351 | '(#:imported-modules (invalid-module)))))) |
| 352 | (check-derivation pkg)) |
| 353 | (((and (? lint-warning?) first-warning) others ...) |
| 354 | (lint-warning-message first-warning)))) |
| 355 | |
| 356 | (test-equal "license: invalid license" |
| 357 | "invalid license field" |
| 358 | (single-lint-warning-message |
| 359 | (check-license (dummy-package "x" (license #f))))) |
| 360 | |
| 361 | (test-equal "home-page: wrong home-page" |
| 362 | "invalid value for home page" |
| 363 | (let ((pkg (package |
| 364 | (inherit (dummy-package "x")) |
| 365 | (home-page #f)))) |
| 366 | (single-lint-warning-message |
| 367 | (check-home-page pkg)))) |
| 368 | |
| 369 | (test-equal "home-page: invalid URI" |
| 370 | "invalid home page URL: \"foobar\"" |
| 371 | (let ((pkg (package |
| 372 | (inherit (dummy-package "x")) |
| 373 | (home-page "foobar")))) |
| 374 | (single-lint-warning-message |
| 375 | (check-home-page pkg)))) |
| 376 | |
| 377 | (test-assert "home-page: host not found" |
| 378 | (let ((pkg (package |
| 379 | (inherit (dummy-package "x")) |
| 380 | (home-page "http://does-not-exist")))) |
| 381 | (warning-contains? "domain not found" (check-home-page pkg)))) |
| 382 | |
| 383 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 384 | (test-equal "home-page: Connection refused" |
| 385 | "URI http://localhost:9999/foo/bar unreachable: Connection refused" |
| 386 | (let ((pkg (package |
| 387 | (inherit (dummy-package "x")) |
| 388 | (home-page (%local-url))))) |
| 389 | (single-lint-warning-message |
| 390 | (check-home-page pkg)))) |
| 391 | |
| 392 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 393 | (test-equal "home-page: 200" |
| 394 | '() |
| 395 | (with-http-server `((200 ,%long-string)) |
| 396 | (let ((pkg (package |
| 397 | (inherit (dummy-package "x")) |
| 398 | (home-page (%local-url))))) |
| 399 | (check-home-page pkg)))) |
| 400 | |
| 401 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 402 | (test-equal "home-page: 200 but short length" |
| 403 | "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)" |
| 404 | (with-http-server `((200 "This is too small.")) |
| 405 | (let ((pkg (package |
| 406 | (inherit (dummy-package "x")) |
| 407 | (home-page (%local-url))))) |
| 408 | |
| 409 | (single-lint-warning-message |
| 410 | (check-home-page pkg))))) |
| 411 | |
| 412 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 413 | (test-equal "home-page: 404" |
| 414 | "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")" |
| 415 | (with-http-server `((404 ,%long-string)) |
| 416 | (let ((pkg (package |
| 417 | (inherit (dummy-package "x")) |
| 418 | (home-page (%local-url))))) |
| 419 | (single-lint-warning-message |
| 420 | (check-home-page pkg))))) |
| 421 | |
| 422 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 423 | (test-equal "home-page: 301, invalid" |
| 424 | "invalid permanent redirect from http://localhost:9999/foo/bar" |
| 425 | (with-http-server `((301 ,%long-string)) |
| 426 | (let ((pkg (package |
| 427 | (inherit (dummy-package "x")) |
| 428 | (home-page (%local-url))))) |
| 429 | (single-lint-warning-message |
| 430 | (check-home-page pkg))))) |
| 431 | |
| 432 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 433 | (test-equal "home-page: 301 -> 200" |
| 434 | "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar" |
| 435 | (with-http-server `((200 ,%long-string)) |
| 436 | (let* ((initial-url (%local-url)) |
| 437 | (redirect (build-response #:code 301 |
| 438 | #:headers |
| 439 | `((location |
| 440 | . ,(string->uri initial-url)))))) |
| 441 | (parameterize ((%http-server-port (+ 1 (%http-server-port)))) |
| 442 | (with-http-server `((,redirect "")) |
| 443 | (let ((pkg (package |
| 444 | (inherit (dummy-package "x")) |
| 445 | (home-page (%local-url))))) |
| 446 | (single-lint-warning-message |
| 447 | (check-home-page pkg)))))))) |
| 448 | |
| 449 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 450 | (test-equal "home-page: 301 -> 404" |
| 451 | "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")" |
| 452 | (with-http-server '((404 "booh!")) |
| 453 | (let* ((initial-url (%local-url)) |
| 454 | (redirect (build-response #:code 301 |
| 455 | #:headers |
| 456 | `((location |
| 457 | . ,(string->uri initial-url)))))) |
| 458 | (parameterize ((%http-server-port (+ 1 (%http-server-port)))) |
| 459 | (with-http-server `((,redirect "")) |
| 460 | (let ((pkg (package |
| 461 | (inherit (dummy-package "x")) |
| 462 | (home-page (%local-url))))) |
| 463 | (single-lint-warning-message |
| 464 | (check-home-page pkg)))))))) |
| 465 | |
| 466 | |
| 467 | (test-equal "source-file-name" |
| 468 | "the source file name should contain the package name" |
| 469 | (let ((pkg (dummy-package "x" |
| 470 | (version "3.2.1") |
| 471 | (source |
| 472 | (origin |
| 473 | (method url-fetch) |
| 474 | (uri "http://www.example.com/3.2.1.tar.gz") |
| 475 | (sha256 %null-sha256)))))) |
| 476 | (single-lint-warning-message |
| 477 | (check-source-file-name pkg)))) |
| 478 | |
| 479 | (test-equal "source-file-name: v prefix" |
| 480 | "the source file name should contain the package name" |
| 481 | (let ((pkg (dummy-package "x" |
| 482 | (version "3.2.1") |
| 483 | (source |
| 484 | (origin |
| 485 | (method url-fetch) |
| 486 | (uri "http://www.example.com/v3.2.1.tar.gz") |
| 487 | (sha256 %null-sha256)))))) |
| 488 | (single-lint-warning-message |
| 489 | (check-source-file-name pkg)))) |
| 490 | |
| 491 | (test-equal "source-file-name: bad checkout" |
| 492 | "the source file name should contain the package name" |
| 493 | (let ((pkg (dummy-package "x" |
| 494 | (version "3.2.1") |
| 495 | (source |
| 496 | (origin |
| 497 | (method git-fetch) |
| 498 | (uri (git-reference |
| 499 | (url "http://www.example.com/x.git") |
| 500 | (commit "0"))) |
| 501 | (sha256 %null-sha256)))))) |
| 502 | (single-lint-warning-message |
| 503 | (check-source-file-name pkg)))) |
| 504 | |
| 505 | (test-equal "source-file-name: good checkout" |
| 506 | '() |
| 507 | (let ((pkg (dummy-package "x" |
| 508 | (version "3.2.1") |
| 509 | (source |
| 510 | (origin |
| 511 | (method git-fetch) |
| 512 | (uri (git-reference |
| 513 | (url "http://git.example.com/x.git") |
| 514 | (commit "0"))) |
| 515 | (file-name (string-append "x-" version)) |
| 516 | (sha256 %null-sha256)))))) |
| 517 | (check-source-file-name pkg))) |
| 518 | |
| 519 | (test-equal "source-file-name: valid" |
| 520 | '() |
| 521 | (let ((pkg (dummy-package "x" |
| 522 | (version "3.2.1") |
| 523 | (source |
| 524 | (origin |
| 525 | (method url-fetch) |
| 526 | (uri "http://www.example.com/x-3.2.1.tar.gz") |
| 527 | (sha256 %null-sha256)))))) |
| 528 | (check-source-file-name pkg))) |
| 529 | |
| 530 | (test-equal "source-unstable-tarball" |
| 531 | "the source URI should not be an autogenerated tarball" |
| 532 | (let ((pkg (dummy-package "x" |
| 533 | (source |
| 534 | (origin |
| 535 | (method url-fetch) |
| 536 | (uri "https://github.com/example/example/archive/v0.0.tar.gz") |
| 537 | (sha256 %null-sha256)))))) |
| 538 | (single-lint-warning-message |
| 539 | (check-source-unstable-tarball pkg)))) |
| 540 | |
| 541 | (test-equal "source-unstable-tarball: source #f" |
| 542 | '() |
| 543 | (let ((pkg (dummy-package "x" |
| 544 | (source #f)))) |
| 545 | (check-source-unstable-tarball pkg))) |
| 546 | |
| 547 | (test-equal "source-unstable-tarball: valid" |
| 548 | '() |
| 549 | (let ((pkg (dummy-package "x" |
| 550 | (source |
| 551 | (origin |
| 552 | (method url-fetch) |
| 553 | (uri "https://github.com/example/example/releases/download/x-0.0/x-0.0.tar.gz") |
| 554 | (sha256 %null-sha256)))))) |
| 555 | (check-source-unstable-tarball pkg))) |
| 556 | |
| 557 | (test-equal "source-unstable-tarball: package named archive" |
| 558 | '() |
| 559 | (let ((pkg (dummy-package "x" |
| 560 | (source |
| 561 | (origin |
| 562 | (method url-fetch) |
| 563 | (uri "https://github.com/example/archive/releases/download/x-0.0/x-0.0.tar.gz") |
| 564 | (sha256 %null-sha256)))))) |
| 565 | (check-source-unstable-tarball pkg))) |
| 566 | |
| 567 | (test-equal "source-unstable-tarball: not-github" |
| 568 | '() |
| 569 | (let ((pkg (dummy-package "x" |
| 570 | (source |
| 571 | (origin |
| 572 | (method url-fetch) |
| 573 | (uri "https://bitbucket.org/archive/example/download/x-0.0.tar.gz") |
| 574 | (sha256 %null-sha256)))))) |
| 575 | (check-source-unstable-tarball pkg))) |
| 576 | |
| 577 | (test-equal "source-unstable-tarball: git-fetch" |
| 578 | '() |
| 579 | (let ((pkg (dummy-package "x" |
| 580 | (source |
| 581 | (origin |
| 582 | (method git-fetch) |
| 583 | (uri (git-reference |
| 584 | (url "https://github.com/archive/example.git") |
| 585 | (commit "0"))) |
| 586 | (sha256 %null-sha256)))))) |
| 587 | (check-source-unstable-tarball pkg))) |
| 588 | |
| 589 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 590 | (test-equal "source: 200" |
| 591 | '() |
| 592 | (with-http-server `((200 ,%long-string)) |
| 593 | (let ((pkg (package |
| 594 | (inherit (dummy-package "x")) |
| 595 | (source (origin |
| 596 | (method url-fetch) |
| 597 | (uri (%local-url)) |
| 598 | (sha256 %null-sha256)))))) |
| 599 | (check-source pkg)))) |
| 600 | |
| 601 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 602 | (test-equal "source: 200 but short length" |
| 603 | "URI http://localhost:9999/foo/bar returned suspiciously small file (18 bytes)" |
| 604 | (with-http-server '((200 "This is too small.")) |
| 605 | (let ((pkg (package |
| 606 | (inherit (dummy-package "x")) |
| 607 | (source (origin |
| 608 | (method url-fetch) |
| 609 | (uri (%local-url)) |
| 610 | (sha256 %null-sha256)))))) |
| 611 | (match (check-source pkg) |
| 612 | ((first-warning ; All source URIs are unreachable |
| 613 | (and (? lint-warning?) second-warning)) |
| 614 | (lint-warning-message second-warning)))))) |
| 615 | |
| 616 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 617 | (test-equal "source: 404" |
| 618 | "URI http://localhost:9999/foo/bar not reachable: 404 (\"Such is life\")" |
| 619 | (with-http-server `((404 ,%long-string)) |
| 620 | (let ((pkg (package |
| 621 | (inherit (dummy-package "x")) |
| 622 | (source (origin |
| 623 | (method url-fetch) |
| 624 | (uri (%local-url)) |
| 625 | (sha256 %null-sha256)))))) |
| 626 | (match (check-source pkg) |
| 627 | ((first-warning ; All source URIs are unreachable |
| 628 | (and (? lint-warning?) second-warning)) |
| 629 | (lint-warning-message second-warning)))))) |
| 630 | |
| 631 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 632 | (test-equal "source: 404 and 200" |
| 633 | '() |
| 634 | (with-http-server `((404 ,%long-string)) |
| 635 | (let ((bad-url (%local-url))) |
| 636 | (parameterize ((%http-server-port (+ 1 (%http-server-port)))) |
| 637 | (with-http-server `((200 ,%long-string)) |
| 638 | (let ((pkg (package |
| 639 | (inherit (dummy-package "x")) |
| 640 | (source (origin |
| 641 | (method url-fetch) |
| 642 | (uri (list bad-url (%local-url))) |
| 643 | (sha256 %null-sha256)))))) |
| 644 | ;; Since one of the two URLs is good, this should return the empty |
| 645 | ;; list. |
| 646 | (check-source pkg))))))) |
| 647 | |
| 648 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 649 | (test-equal "source: 301 -> 200" |
| 650 | "permanent redirect from http://localhost:10000/foo/bar to http://localhost:9999/foo/bar" |
| 651 | (with-http-server `((200 ,%long-string)) |
| 652 | (let* ((initial-url (%local-url)) |
| 653 | (redirect (build-response #:code 301 |
| 654 | #:headers |
| 655 | `((location |
| 656 | . ,(string->uri initial-url)))))) |
| 657 | (parameterize ((%http-server-port (+ 1 (%http-server-port)))) |
| 658 | (with-http-server `((,redirect "")) |
| 659 | (let ((pkg (package |
| 660 | (inherit (dummy-package "x")) |
| 661 | (source (origin |
| 662 | (method url-fetch) |
| 663 | (uri (%local-url)) |
| 664 | (sha256 %null-sha256)))))) |
| 665 | (match (check-source pkg) |
| 666 | ((first-warning ; All source URIs are unreachable |
| 667 | (and (? lint-warning?) second-warning)) |
| 668 | (lint-warning-message second-warning))))))))) |
| 669 | |
| 670 | (test-skip (if (http-server-can-listen?) 0 1)) |
| 671 | (test-equal "source: 301 -> 404" |
| 672 | "URI http://localhost:10000/foo/bar not reachable: 404 (\"Such is life\")" |
| 673 | (with-http-server '((404 "booh!")) |
| 674 | (let* ((initial-url (%local-url)) |
| 675 | (redirect (build-response #:code 301 |
| 676 | #:headers |
| 677 | `((location |
| 678 | . ,(string->uri initial-url)))))) |
| 679 | (parameterize ((%http-server-port (+ 1 (%http-server-port)))) |
| 680 | (with-http-server `((,redirect "")) |
| 681 | (let ((pkg (package |
| 682 | (inherit (dummy-package "x")) |
| 683 | (source (origin |
| 684 | (method url-fetch) |
| 685 | (uri (%local-url)) |
| 686 | (sha256 %null-sha256)))))) |
| 687 | (match (check-source pkg) |
| 688 | ((first-warning ; The first warning says that all URI's are |
| 689 | ; unreachable |
| 690 | (and (? lint-warning?) second-warning)) |
| 691 | (lint-warning-message second-warning))))))))) |
| 692 | |
| 693 | (test-equal "mirror-url" |
| 694 | '() |
| 695 | (let ((source (origin |
| 696 | (method url-fetch) |
| 697 | (uri "http://example.org/foo/bar.tar.gz") |
| 698 | (sha256 %null-sha256)))) |
| 699 | (check-mirror-url (dummy-package "x" (source source))))) |
| 700 | |
| 701 | (test-equal "mirror-url: one suggestion" |
| 702 | "URL should be 'mirror://gnu/foo/foo.tar.gz'" |
| 703 | (let ((source (origin |
| 704 | (method url-fetch) |
| 705 | (uri "http://ftp.gnu.org/pub/gnu/foo/foo.tar.gz") |
| 706 | (sha256 %null-sha256)))) |
| 707 | (single-lint-warning-message |
| 708 | (check-mirror-url (dummy-package "x" (source source)))))) |
| 709 | |
| 710 | (test-equal "github-url" |
| 711 | '() |
| 712 | (with-http-server `((200 ,%long-string)) |
| 713 | (check-github-url |
| 714 | (dummy-package "x" (source |
| 715 | (origin |
| 716 | (method url-fetch) |
| 717 | (uri (%local-url)) |
| 718 | (sha256 %null-sha256))))))) |
| 719 | |
| 720 | (let ((github-url "https://github.com/foo/bar/bar-1.0.tar.gz")) |
| 721 | (test-equal "github-url: one suggestion" |
| 722 | (string-append |
| 723 | "URL should be '" github-url "'") |
| 724 | (let ((redirect (build-response #:code 301 |
| 725 | #:headers |
| 726 | `((location |
| 727 | . ,(string->uri github-url)))))) |
| 728 | (with-http-server `((,redirect "")) |
| 729 | (let* ((initial-url (%local-url)) |
| 730 | (redirect (build-response #:code 302 |
| 731 | #:headers |
| 732 | `((location |
| 733 | . ,(string->uri initial-url)))))) |
| 734 | (parameterize ((%http-server-port (+ 1 (%http-server-port)))) |
| 735 | (with-http-server `((,redirect "")) |
| 736 | (single-lint-warning-message |
| 737 | (check-github-url |
| 738 | (dummy-package "x" (source |
| 739 | (origin |
| 740 | (method url-fetch) |
| 741 | (uri (%local-url)) |
| 742 | (sha256 %null-sha256)))))))))))) |
| 743 | (test-equal "github-url: already the correct github url" |
| 744 | '() |
| 745 | (check-github-url |
| 746 | (dummy-package "x" (source |
| 747 | (origin |
| 748 | (method url-fetch) |
| 749 | (uri github-url) |
| 750 | (sha256 %null-sha256))))))) |
| 751 | |
| 752 | (test-equal "cve" |
| 753 | '() |
| 754 | (mock ((guix lint) package-vulnerabilities (const '())) |
| 755 | (check-vulnerabilities (dummy-package "x")))) |
| 756 | |
| 757 | (test-equal "cve: one vulnerability" |
| 758 | "probably vulnerable to CVE-2015-1234" |
| 759 | (let ((dummy-vulnerabilities |
| 760 | (lambda (package) |
| 761 | (list (make-struct/no-tail |
| 762 | (@@ (guix cve) <vulnerability>) |
| 763 | "CVE-2015-1234" |
| 764 | (list (cons (package-name package) |
| 765 | (package-version package)))))))) |
| 766 | (single-lint-warning-message |
| 767 | (check-vulnerabilities (dummy-package "pi" (version "3.14")) |
| 768 | dummy-vulnerabilities)))) |
| 769 | |
| 770 | (test-equal "cve: one patched vulnerability" |
| 771 | '() |
| 772 | (mock ((guix lint) package-vulnerabilities |
| 773 | (lambda (package) |
| 774 | (list (make-struct/no-tail (@@ (guix cve) <vulnerability>) |
| 775 | "CVE-2015-1234" |
| 776 | (list (cons (package-name package) |
| 777 | (package-version package))))))) |
| 778 | (check-vulnerabilities |
| 779 | (dummy-package "pi" |
| 780 | (version "3.14") |
| 781 | (source |
| 782 | (dummy-origin |
| 783 | (patches |
| 784 | (list "/a/b/pi-CVE-2015-1234.patch")))))))) |
| 785 | |
| 786 | (test-equal "cve: known safe from vulnerability" |
| 787 | '() |
| 788 | (mock ((guix lint) package-vulnerabilities |
| 789 | (lambda (package) |
| 790 | (list (make-struct/no-tail (@@ (guix cve) <vulnerability>) |
| 791 | "CVE-2015-1234" |
| 792 | (list (cons (package-name package) |
| 793 | (package-version package))))))) |
| 794 | (check-vulnerabilities |
| 795 | (dummy-package "pi" |
| 796 | (version "3.14") |
| 797 | (properties `((lint-hidden-cve . ("CVE-2015-1234")))))))) |
| 798 | |
| 799 | (test-equal "cve: vulnerability fixed in replacement version" |
| 800 | '() |
| 801 | (mock ((guix lint) package-vulnerabilities |
| 802 | (lambda (package) |
| 803 | (match (package-version package) |
| 804 | ("0" |
| 805 | (list (make-struct/no-tail (@@ (guix cve) <vulnerability>) |
| 806 | "CVE-2015-1234" |
| 807 | (list (cons (package-name package) |
| 808 | (package-version package)))))) |
| 809 | ("1" |
| 810 | '())))) |
| 811 | (check-vulnerabilities |
| 812 | (dummy-package |
| 813 | "foo" (version "0") |
| 814 | (replacement (dummy-package "foo" (version "1"))))))) |
| 815 | |
| 816 | (test-equal "cve: patched vulnerability in replacement" |
| 817 | '() |
| 818 | (mock ((guix lint) package-vulnerabilities |
| 819 | (lambda (package) |
| 820 | (list (make-struct/no-tail (@@ (guix cve) <vulnerability>) |
| 821 | "CVE-2015-1234" |
| 822 | (list (cons (package-name package) |
| 823 | (package-version package))))))) |
| 824 | (check-vulnerabilities |
| 825 | (dummy-package |
| 826 | "pi" (version "3.14") (source (dummy-origin)) |
| 827 | (replacement (dummy-package |
| 828 | "pi" (version "3.14") |
| 829 | (source |
| 830 | (dummy-origin |
| 831 | (patches |
| 832 | (list "/a/b/pi-CVE-2015-1234.patch")))))))))) |
| 833 | |
| 834 | (test-equal "formatting: lonely parentheses" |
| 835 | "parentheses feel lonely, move to the previous or next line" |
| 836 | (single-lint-warning-message |
| 837 | (check-formatting |
| 838 | (dummy-package "ugly as hell!" |
| 839 | ) |
| 840 | ))) |
| 841 | |
| 842 | (test-assert "formatting: tabulation" |
| 843 | (string-match-or-error |
| 844 | "tabulation on line [0-9]+, column [0-9]+" |
| 845 | (single-lint-warning-message |
| 846 | (check-formatting (dummy-package "leave the tab here: "))))) |
| 847 | |
| 848 | (test-assert "formatting: trailing white space" |
| 849 | (string-match-or-error |
| 850 | "trailing white space .*" |
| 851 | ;; Leave the trailing white space on the next line! |
| 852 | (single-lint-warning-message |
| 853 | (check-formatting (dummy-package "x"))))) |
| 854 | |
| 855 | (test-assert "formatting: long line" |
| 856 | (string-match-or-error |
| 857 | "line [0-9]+ is way too long \\([0-9]+ characters\\)" |
| 858 | (single-lint-warning-message (check-formatting |
| 859 | (dummy-package "x")) ;here is a stupid comment just to make a long line |
| 860 | ))) |
| 861 | |
| 862 | (test-equal "formatting: alright" |
| 863 | '() |
| 864 | (check-formatting (dummy-package "x"))) |
| 865 | |
| 866 | (test-assert "archival: missing content" |
| 867 | (let* ((origin (origin |
| 868 | (method url-fetch) |
| 869 | (uri "http://example.org/foo.tgz") |
| 870 | (sha256 (make-bytevector 32)))) |
| 871 | (warnings (with-http-server '((404 "Not archived.")) |
| 872 | (parameterize ((%swh-base-url (%local-url))) |
| 873 | (check-archival (dummy-package "x" |
| 874 | (source origin))))))) |
| 875 | (warning-contains? "not archived" warnings))) |
| 876 | |
| 877 | (test-equal "archival: content available" |
| 878 | '() |
| 879 | (let* ((origin (origin |
| 880 | (method url-fetch) |
| 881 | (uri "http://example.org/foo.tgz") |
| 882 | (sha256 (make-bytevector 32)))) |
| 883 | ;; https://archive.softwareheritage.org/api/1/content/ |
| 884 | (content "{ \"checksums\": {}, \"data_url\": \"xyz\", |
| 885 | \"length\": 42 }")) |
| 886 | (with-http-server `((200 ,content)) |
| 887 | (parameterize ((%swh-base-url (%local-url))) |
| 888 | (check-archival (dummy-package "x" (source origin))))))) |
| 889 | |
| 890 | (test-assert "archival: missing revision" |
| 891 | (let* ((origin (origin |
| 892 | (method git-fetch) |
| 893 | (uri (git-reference |
| 894 | (url "http://example.org/foo.git") |
| 895 | (commit "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))) |
| 896 | (sha256 (make-bytevector 32)))) |
| 897 | ;; https://archive.softwareheritage.org/api/1/origin/save/ |
| 898 | (save "{ \"origin_url\": \"http://example.org/foo.git\", |
| 899 | \"save_request_date\": \"2014-11-17T22:09:38+01:00\", |
| 900 | \"save_request_status\": \"accepted\", |
| 901 | \"save_task_status\": \"scheduled\" }") |
| 902 | (warnings (with-http-server `((404 "No revision.") ;lookup-revision |
| 903 | (404 "No origin.") ;lookup-origin |
| 904 | (200 ,save)) ;save-origin |
| 905 | (parameterize ((%swh-base-url (%local-url))) |
| 906 | (check-archival (dummy-package "x" (source origin))))))) |
| 907 | (warning-contains? "scheduled" warnings))) |
| 908 | |
| 909 | (test-equal "archival: revision available" |
| 910 | '() |
| 911 | (let* ((origin (origin |
| 912 | (method git-fetch) |
| 913 | (uri (git-reference |
| 914 | (url "http://example.org/foo.git") |
| 915 | (commit "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))) |
| 916 | (sha256 (make-bytevector 32)))) |
| 917 | ;; https://archive.softwareheritage.org/api/1/revision/ |
| 918 | (revision "{ \"author\": {}, \"parents\": [], |
| 919 | \"date\": \"2014-11-17T22:09:38+01:00\" }")) |
| 920 | (with-http-server `((200 ,revision)) |
| 921 | (parameterize ((%swh-base-url (%local-url))) |
| 922 | (check-archival (dummy-package "x" (source origin))))))) |
| 923 | |
| 924 | (test-assert "archival: rate limit reached" |
| 925 | ;; We should get a single warning stating that the rate limit was reached, |
| 926 | ;; and nothing more, in particular no other HTTP requests. |
| 927 | (let* ((origin (origin |
| 928 | (method url-fetch) |
| 929 | (uri "http://example.org/foo.tgz") |
| 930 | (sha256 (make-bytevector 32)))) |
| 931 | (too-many (build-response |
| 932 | #:code 429 |
| 933 | #:reason-phrase "Too many requests" |
| 934 | #:headers '((x-ratelimit-remaining . "0") |
| 935 | (x-ratelimit-reset . "3000000000")))) |
| 936 | (warnings (with-http-server `((,too-many "Rate limit reached.")) |
| 937 | (parameterize ((%swh-base-url (%local-url))) |
| 938 | (append-map (lambda (name) |
| 939 | (check-archival |
| 940 | (dummy-package name (source origin)))) |
| 941 | '("x" "y" "z")))))) |
| 942 | (string-contains (single-lint-warning-message warnings) |
| 943 | "rate limit reached"))) |
| 944 | |
| 945 | (test-end "lint") |
| 946 | |
| 947 | ;; Local Variables: |
| 948 | ;; eval: (put 'with-http-server 'scheme-indent-function 1) |
| 949 | ;; eval: (put 'with-warnings 'scheme-indent-function 0) |
| 950 | ;; End: |