diff --git a/Makefile b/Makefile index 431abc5..ffa51b9 100644 --- a/Makefile +++ b/Makefile @@ -114,16 +114,15 @@ SEX_TEST_PROGRAMS = c11 \ unnamed-params \ wildcards -# Multi-module linking is checked end to end; see tests/modules/Makefile. -check-modules: sexc - @$(MAKE) --no-print-directory -C ./tests/modules check SEXC=../../sexc - -# The failure paths are checked end to end; see tests/exit-code/Makefile. -check-exit-code: sexc - @$(MAKE) --no-print-directory -C ./tests/exit-code check SEXC=../../sexc - check run-tests: sexc sex-tests sextest - ./sex-tests && ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex) && $(MAKE) check-modules && $(MAKE) check-exit-code + ./sex-tests + ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex) + @$(MAKE) --no-print-directory check-modules + @$(MAKE) --no-print-directory check-exit-code + +# The end-to-end checks, each on its own: run with the eggs on the path +check-modules check-exit-code: check-%: sexc + cd tests/$* && SEXC='$(CURDIR)/sexc' $(CSI) -s check.scm install: all installdirs $(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc @@ -145,8 +144,5 @@ clean: rm -f *.import.scm rm -f *.link rm -f sexc sex-tests sextest - $(MAKE) -C ./tests/modules clean -.PHONY: all check run-tests check-modules check-exit-code \ - install install-strip installdirs uninstall \ - clean +.PHONY: all check run-tests check-modules check-exit-code install install-strip installdirs uninstall clean diff --git a/tests/exit-code/Makefile b/tests/exit-code/Makefile deleted file mode 100644 index 79b8837..0000000 --- a/tests/exit-code/Makefile +++ /dev/null @@ -1,32 +0,0 @@ -# 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 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. - -SEXC ?= ../../sexc - -check: - @d=`mktemp -d`; \ - TMPDIR=$$d $(SEXC) nested-pointer.sex -o $$d/out >/dev/null 2>&1; \ - if [ -n "`find $$d -name '*.c'`" ]; then \ - echo "exit code FAILED: the temporary .c survived a failed emission"; \ - rm -rf $$d; exit 1; \ - fi; \ - if $(SEXC) hello.sex -o $$d/out -- -no-such-cc-flag >/dev/null 2>&1; then \ - echo "exit code FAILED: sexc reported success after cc failed"; \ - rm -rf $$d; exit 1; \ - fi; \ - if (cd $$d && $(abspath $(SEXC)) $(CURDIR)/hello.sex -o - >/dev/null 2>&1) || [ -e $$d/- ]; then \ - echo "exit code FAILED: -o - built an executable"; \ - rm -rf $$d; exit 1; \ - fi; \ - rm -rf $$d; echo "exit code ok" - -.PHONY: check diff --git a/tests/exit-code/check.scm b/tests/exit-code/check.scm new file mode 100644 index 0000000..b3b31e4 --- /dev/null +++ b/tests/exit-code/check.scm @@ -0,0 +1,65 @@ +;;; 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 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 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) + +;;; 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")))) + +(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")) +(change-directory scratch) + +(test "-o - is refused for an executable" #f + (sexc-succeeds? hello "-o" "-")) + +(test "and leaves no file named -" #f + (file-exists? "-")) + +(delete-directory scratch #t) + +(test-exit) diff --git a/tests/modules/Makefile b/tests/modules/Makefile deleted file mode 100644 index d43c2f1..0000000 --- a/tests/modules/Makefile +++ /dev/null @@ -1,56 +0,0 @@ -# Multi-module linking. -# -# Modules are only testable end to end, and nothing else in the suite -# links more than one translation unit. Three things have to hold at -# once: an imported `pub fn' comes out as a prototype with external -# linkage, an imported `pub var' as an extern declaration, and the -# module's object survives being passed after `--'. Get any of them -# wrong and this fails to link -- or, in the `pub var' case, links and -# quietly counts into a private copy. -# -# It also checks what only a second translation unit can check: that an -# imported type reaches the type database, by expanding a macro that -# reads the imported struct's fields; and that a closure type crossing -# the boundary works both ways -- one built in the module and called -# here, one built here and called there, through a code pointer that is -# static in the other object. And that a record can cross as a handle -# alone, its fields left behind and its methods still found. -# -# The public forms carry comments in their headers, which the reduction -# to a prototype and to an extern both have to look past. And a type the -# app cannot see -- private to greet, or from a module greet imports -# without `pub' -- crosses behind a pointer, declared and no more. -# -# The interface prints as importers get it: the declarations first, then -# the pub forms in the order they were written. -# -# The app's dependency rule names both modules: greet, which it imports, -# and ledger, which only greet does but which decides what the app is -# told of it. - -SEXC ?= ../../sexc - -EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\nboard 42\nledger 2\ncolour 123 - -check: - @head="`$(SEXC) --public-interface greet.sex | head -3`"; \ - if [ "$$head" != "`printf '(record cell)\n(record ledger)\n(include stdio.h)'`" ]; then \ - echo "modules FAILED, the interface begins:"; echo "$$head"; exit 1; \ - fi - @$(SEXC) ledger.sex -c -o ledger.o - @$(SEXC) greet.sex -c -o greet.o - @$(SEXC) greet-app.sex -o greet-app --depfile=greet-app.d -- greet.o ledger.o - @if ! head -1 greet-app.d | grep -q '^greet-app: greet-app.sex .*/greet.sex .*/ledger.sex$$'; then \ - echo "modules FAILED, the dependency rule is:"; cat greet-app.d; $(MAKE) clean; exit 1; \ - fi - @if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \ - echo "modules ok"; \ - else \ - echo "modules FAILED, got:"; ./greet-app; $(MAKE) clean; exit 1; \ - fi - @$(MAKE) --no-print-directory clean - -clean: - @rm -f greet.o ledger.o greet-app greet-app.d - -.PHONY: check clean diff --git a/tests/modules/check.scm b/tests/modules/check.scm new file mode 100644 index 0000000..7f09d72 --- /dev/null +++ b/tests/modules/check.scm @@ -0,0 +1,93 @@ +;;; Multi-module linking. +;;; +;;; Modules are only testable end to end, and nothing else in the suite +;;; links more than one translation unit. Three things have to hold at +;;; once: an imported `pub fn' comes out as a prototype with external +;;; linkage, an imported `pub var' as an extern declaration, and the +;;; module's object survives being passed after `--'. Get any of them +;;; wrong and this fails to link -- or, in the `pub var' case, links and +;;; quietly counts into a private copy. +;;; +;;; It also checks what only a second translation unit can check: that an +;;; imported type reaches the type database, by expanding a macro that +;;; reads the imported struct's fields; and that a closure type crossing +;;; the boundary works both ways -- one built in the module and called +;;; here, one built here and called there, through a code pointer that is +;;; static in the other object. And that a record can cross as a handle +;;; alone, its fields left behind and its methods still found. +;;; +;;; The public forms carry comments in their headers, which the reduction +;;; to a prototype and to an extern both have to look past. And a type the +;;; app cannot see -- private to greet, or from a module greet imports +;;; without `pub' -- crosses behind a pointer, declared and no more. +;;; +;;; The interface prints as importers get it: the declarations first, then +;;; the pub forms in the order they were written. +;;; +;;; The app's dependency rule names both modules: greet, which it imports, +;;; and ledger, which only greet does but which decides what the app is +;;; told of it. +;;; +;;; make check-modules + +(import scheme + (chicken base) + (chicken file) + (chicken io) + (chicken irregex) + (chicken pathname) + (chicken process) + (chicken process-context) + srfi-1 + test) + +(define sexc (or (get-environment-variable "SEXC") "../../sexc")) + +;;; Where the objects and the app go: not beside the sources, where what +;;; an earlier run left would pass for what this one did not build +(define scratch (create-temporary-directory)) +(define (built name) (make-pathname scratch name)) + +;;; The lines PROGRAM prints when run with ARGS, or #f when it fails +(define (output-of program . args) + (let* ((proc (process program args)) + (lines (read-lines (process-output-port proc)))) + (process-wait proc) + (and (process-returned-normally? proc) + (zero? (process-exit-status proc)) + lines))) + +(test "the interface begins with the declarations" + '("(record cell)" "(record ledger)" "(include stdio.h)") + (take (or (output-of sexc "--public-interface" "greet.sex") '()) 3)) + +(test-assert "the app links with both modules" + (and (output-of sexc "ledger.sex" "-c" "-o" (built "ledger.o")) + (output-of sexc "greet.sex" "-c" "-o" (built "greet.o")) + (output-of sexc "greet-app.sex" "-o" (built "greet-app") + (string-append "--depfile=" (built "greet-app.d")) + "--" (built "greet.o") (built "ledger.o")))) + +(test-assert "the dependency rule names both modules" + (irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex" + (call-with-input-file (built "greet-app.d") read-line))) + +(test "the app runs through both" + '("hello, world" + "hello, sex" + "2 greetings" + "text times " + "text times " + "mood 1" + "closure 15 21 201" + "vec2 7 7" + "vec2(3 4)" + "tally 2" + "board 42" + "ledger 2" + "colour 123") + (output-of (built "greet-app"))) + +(delete-directory scratch #t) + +(test-exit)