diff options
Diffstat (limited to 'guix/tests')
-rw-r--r-- | guix/tests/git.scm | 105 | ||||
-rw-r--r-- | guix/tests/http.scm | 39 |
2 files changed, 129 insertions, 15 deletions
diff --git a/guix/tests/git.scm b/guix/tests/git.scm new file mode 100644 index 0000000000..21573ac14e --- /dev/null +++ b/guix/tests/git.scm @@ -0,0 +1,105 @@ +;;; GNU Guix --- Functional package management for GNU +;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org> +;;; +;;; This file is part of GNU Guix. +;;; +;;; GNU Guix is free software; you can redistribute it and/or modify it +;;; under the terms of the GNU General Public License as published by +;;; the Free Software Foundation; either version 3 of the License, or (at +;;; your option) any later version. +;;; +;;; GNU Guix is distributed in the hope that it will be useful, but +;;; WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. + +(define-module (guix tests git) + #:use-module (git) + #:use-module ((guix git) #:select (with-repository)) + #:use-module (guix utils) + #:use-module (guix build utils) + #:use-module (ice-9 match) + #:use-module (ice-9 control) + #:export (git-command + with-temporary-git-repository + find-commit)) + +(define git-command + (make-parameter "git")) + +(define (populate-git-repository directory directives) + "Initialize a new Git checkout and repository in DIRECTORY and apply +DIRECTIVES. Each element of DIRECTIVES is an sexp like: + + (add \"foo.txt\" \"hi!\") + +Return DIRECTORY on success." + + ;; Note: As of version 0.2.0, Guile-Git lacks the necessary bindings to do + ;; all this, so resort to the "git" command. + (define (git command . args) + (apply invoke (git-command) "-C" directory + command args)) + + (mkdir-p directory) + (git "init") + + (let loop ((directives directives)) + (match directives + (() + directory) + ((('add file contents) rest ...) + (let ((file (string-append directory "/" file))) + (mkdir-p (dirname file)) + (call-with-output-file file + (lambda (port) + (display (if (string? contents) + contents + (with-repository directory repository + (contents repository))) + port))) + (git "add" file) + (loop rest))) + ((('commit text) rest ...) + (git "commit" "-m" text) + (loop rest)) + ((('tag name) rest ...) + (git "tag" name) + (loop rest)) + ((('branch name) rest ...) + (git "branch" name) + (loop rest)) + ((('checkout branch) rest ...) + (git "checkout" branch) + (loop rest)) + ((('merge branch message) rest ...) + (git "merge" branch "-m" message) + (loop rest))))) + +(define (call-with-temporary-git-repository directives proc) + (call-with-temporary-directory + (lambda (directory) + (populate-git-repository directory directives) + (proc directory)))) + +(define-syntax-rule (with-temporary-git-repository directory + directives exp ...) + "Evaluate EXP in a context where DIRECTORY contains a checkout populated as +per DIRECTIVES." + (call-with-temporary-git-repository directives + (lambda (directory) + exp ...))) + +(define (find-commit repository message) + "Return the commit in REPOSITORY whose message includes MESSAGE, a string." + (let/ec return + (fold-commits (lambda (commit _) + (and (string-contains (commit-message commit) + message) + (return commit))) + #f + repository) + (error "commit not found" message))) diff --git a/guix/tests/http.scm b/guix/tests/http.scm index a56d6f213d..05ce39bca2 100644 --- a/guix/tests/http.scm +++ b/guix/tests/http.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> +;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org> ;;; ;;; This file is part of GNU Guix. ;;; @@ -22,6 +22,7 @@ #:use-module (web server http) #:use-module (web response) #:use-module (srfi srfi-39) + #:use-module (ice-9 match) #:export (with-http-server call-with-http-server %http-server-port @@ -69,10 +70,20 @@ needed." (string-append "http://localhost:" (number->string (%http-server-port)) "/foo/bar")) -(define* (call-with-http-server code data thunk - #:key (headers '())) - "Call THUNK with an HTTP server running and returning CODE and DATA (a -string) on HTTP requests." +(define* (call-with-http-server responses+data thunk) + "Call THUNK with an HTTP server running and returning RESPONSES+DATA on HTTP +requests. Each elements of RESPONSES+DATA must be a tuple containing a +response and a string, or an HTTP response code and a string." + (define responses + (map (match-lambda + (((? response? response) data) + (list response data)) + (((? integer? code) data) + (list (build-response #:code code + #:reason-phrase "Such is life") + data))) + responses+data)) + (define (http-write server client response body) "Write RESPONSE." (let* ((response (write-response response client)) @@ -82,7 +93,8 @@ string) on HTTP requests." (else (write-response-body response body))) (close-port port) - (quit #t) ;exit the server thread + (when (null? responses) + (quit #t)) ;exit the server thread (values))) ;; Mutex and condition variable to synchronize with the HTTP server. @@ -105,10 +117,10 @@ string) on HTTP requests." (define (server-body) (define (handle request body) - (values (build-response #:code code - #:reason-phrase "Such is life" - #:headers headers) - data)) + (match responses + (((response data) rest ...) + (set! responses rest) + (values response data)))) (let ((socket (open-http-server-socket))) (catch 'quit @@ -126,10 +138,7 @@ string) on HTTP requests." (define-syntax with-http-server (syntax-rules () - ((_ (code headers) data body ...) - (call-with-http-server code data (lambda () body ...) - #:headers headers)) - ((_ code data body ...) - (call-with-http-server code data (lambda () body ...))))) + ((_ responses+data body ...) + (call-with-http-server responses+data (lambda () body ...))))) ;;; http.scm ends here |