Files
sex/tests/exit-code/check.scm
alex-eg 6da6bb89fb refuse an output naming the source
-o naming the input wrote over it, whatever was asked for.
2026-10-19 21:56:28 +03:00

114 lines
4.2 KiB
Scheme

;;; The failure paths.
;;;
;;; Each needs a process to show itself, so none fits the unit suite:
;;; sexc has to fail when cc fails -- exiting 0 after a failed compile
;;; makes every driver, sextest included, read it as success -- it has to
;;; clean up its temporary .c, and any output it began, when its own
;;; emission throws, and it has to
;;; refuse `-o -' for an executable, which cc would write to a file named
;;; `-'.
;;;
;;; Everything is built inside a scratch directory, so nothing is left
;;; here to clean up.
;;;
;;; make check-exit-code
(import scheme
(chicken base)
(chicken file)
(chicken io)
(chicken pathname)
(chicken process)
(chicken process-context)
(chicken string)
test)
;;; A path stays good past the change of directory below; a bare name
;;; is looked up on PATH
(define sexc
(let ((path (or (get-environment-variable "SEXC") "../../sexc")))
(if (or (absolute-pathname? path) (not (substring-index "/" path)))
path
(make-pathname (current-directory) path))))
(define scratch (create-temporary-directory))
(define out (make-pathname scratch "out"))
(set-environment-variable! "TMPDIR" scratch)
;;; (status stdout stderr) of sexc run with ARGS, and INPUT, when one
;;; is given, on its stdin
(define (sexc-run input . args)
(let ((proc (process* sexc args)))
(when input (display input (process-input-port proc)))
(close-output-port (process-input-port proc))
(let* ((text (lambda (s) (if (eof-object? s) "" s)))
(out (text (read-string #f (process-output-port proc))))
(err (text (read-string #f (process-error-port proc)))))
(process-wait proc)
(list (process-exit-status proc) out err))))
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
(define (sexc-succeeds? . args)
(zero? (system (string-append (string-intersperse (map qs (cons sexc args)))
" >/dev/null 2>&1"))))
;;; Every check below expects a failure, which a sexc that cannot run
;;; gives too
(test "sexc compiles a program" #t
(sexc-succeeds? "hello.sex" "-o" out))
(test "a failed emission leaves no temporary .c" '()
(begin
(sexc-succeeds? "nested-pointer.sex" "-o" out)
(glob (make-pathname scratch "*.c"))))
;; and none of a file it was writing out, which make would take for done
(test "a failed emission leaves no part of its output" #f
(begin
(sexc-succeeds? "nested-pointer.sex" "-C" "-o" (make-pathname scratch "out.c"))
(file-exists? (make-pathname scratch "out.c"))))
(test "a failed cc fails sexc" #f
(sexc-succeeds? "hello.sex" "-o" out "--" "-no-such-cc-flag"))
(define hello (make-pathname (current-directory) "hello.sex"))
(define hello-source (call-with-input-file hello (lambda (p) (read-string #f p))))
;;; getopt-long read a lone `-' as an option it did not know, and died
(test-assert "- is stdin"
(let ((run (sexc-run hello-source "-" "-C" "-o" "-")))
(and (zero? (car run)) (substring-index "main" (cadr run)) #t)))
(change-directory scratch)
;;; the second was never read, and nothing said so
(test-assert "a second input is refused"
(let ((run (sexc-run #f hello hello "-C" "-o" "-")))
(and (not (zero? (car run)))
(substring-index "one input file at a time" (caddr run))
#t)))
(test-assert "stdin besides a file too"
(let ((run (sexc-run hello-source "-" hello "-C" "-o" "-")))
(and (not (zero? (car run)))
(substring-index "one input file at a time" (caddr run))
#t)))
(test "-o - is refused for an executable" #f
(sexc-succeeds? hello "-o" "-"))
(test "and leaves no file named -" #f
(file-exists? "-"))
;;; -o naming the source overwrote it, whatever was asked for
(call-with-output-file "victim.sex" (lambda (p) (display hello-source p)))
(test-assert "an output naming the source is refused"
(let ((run (sexc-run #f "victim.sex" "-C" "-o" "./victim.sex")))
(and (not (zero? (car run)))
(substring-index "would overwrite its source" (caddr run))
#t)))
(test "and the source is as it was" hello-source
(call-with-input-file "victim.sex" (lambda (p) (read-string #f p))))
(delete-directory scratch #t)
(test-exit)