From 600b4801deeeffefd30261215ccf50f72dc30acd Mon Sep 17 00:00:00 2001 From: alex-eg Date: Tue, 20 Oct 2026 11:51:31 +0300 Subject: [PATCH] check sexc's command line on a sexc process The -- split, the -o - spelling and the dependency rule were exported to be unit tested, as lists and strings sexc might never hand on; a stand-in cc and the depfile show what it does hand on. --- Makefile | 5 +- sexc.scm | 6 +- tests/args.scm | 83 --------------------------- tests/command-line/cc | 4 ++ tests/command-line/check.scm | 108 +++++++++++++++++++++++++++++++++++ tests/command-line/hello.sex | 2 + tests/modules/check.scm | 8 +++ tests/run.scm | 1 - 8 files changed, 126 insertions(+), 91 deletions(-) delete mode 100644 tests/args.scm create mode 100755 tests/command-line/cc create mode 100644 tests/command-line/check.scm create mode 100644 tests/command-line/hello.sex diff --git a/Makefile b/Makefile index cc626ad..06e5cb0 100644 --- a/Makefile +++ b/Makefile @@ -90,9 +90,10 @@ check run-tests: sexc sex-tests sextest ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS) @$(MAKE) --no-print-directory check-modules @$(MAKE) --no-print-directory check-exit-code + @$(MAKE) --no-print-directory check-command-line # The end-to-end checks, each on its own: run with the eggs on the path -check-modules check-exit-code: check-%: sexc +check-modules check-exit-code check-command-line: check-%: sexc cd tests/$* && SEXC='$(CURDIR)/sexc' $(CSI) -s check.scm install: all installdirs @@ -116,4 +117,4 @@ clean: rm -f *.link $(PROGRAM_OBJ:.o=.link) rm -f $(PROGRAM_NAMES) -.PHONY: all check run-tests check-modules check-exit-code install install-strip installdirs uninstall clean FORCE +.PHONY: all check run-tests check-modules check-exit-code check-command-line install install-strip installdirs uninstall clean FORCE diff --git a/sexc.scm b/sexc.scm index 80b4e27..001da07 100644 --- a/sexc.scm +++ b/sexc.scm @@ -22,11 +22,7 @@ utils) (export - main - - ;; for the tests - args-before-separator args-after-separator - dependency-rule spell-stdout-output) + main) ;;; Main function facilities diff --git a/tests/args.scm b/tests/args.scm deleted file mode 100644 index a757a1b..0000000 --- a/tests/args.scm +++ /dev/null @@ -1,83 +0,0 @@ -;;; Splitting the command line at `--'. -;;; -;;; getopt-long cannot do this: it consumes the separator and merges -;;; everything after it into `@' alongside the input file. So sexc -;;; splits the raw argv first, and only the head is parsed as options. -;;; Everything else reaches the C compiler exactly as written -- -;;; including the words that do not start with a dash, which a previous -;;; leading-dash heuristic used to drop. - -(import sexc) - -(define full '("foo.sex" "-o" "bar" "--" "-framework" "OpenGL" "-Wall")) - -(test-group "argument separator" - - (test "options and input file stay with sexc" - '("foo.sex" "-o" "bar") - (args-before-separator full)) - - (test "the tail reaches the compiler verbatim" - '("-framework" "OpenGL" "-Wall") - (args-after-separator full)) - - ;; The case that motivated this: `OpenGL' has no leading dash and was - ;; silently dropped, leaving `-framework' to swallow whatever flag - ;; came next. - (test "a word without a dash survives" - '("-framework" "OpenGL") - (args-after-separator '("x.sex" "--" "-framework" "OpenGL"))) - - (test "no separator means nothing for the compiler" - '() - (args-after-separator '("foo.sex" "-o" "bar"))) - - (test "no separator leaves every argument with sexc" - '("foo.sex" "-o" "bar") - (args-before-separator '("foo.sex" "-o" "bar"))) - - (test "a trailing separator is allowed" - '() - (args-after-separator '("foo.sex" "--"))) - - (test "a leading separator leaves no input file" - '() - (args-before-separator '("--" "-lm"))) - - ;; Only the first `--' separates; a later one is an ordinary compiler - ;; argument (ld takes several). - (test "only the first separator counts" - '("-Wl,--as-needed" "--" "-lm") - (args-after-separator '("x.sex" "--" "-Wl,--as-needed" "--" "-lm"))) - - (test "an empty command line is handled" - '() - (args-before-separator '())) - - ;; getopt-long reads a lone `-' as an option and refused `-o -', so - ;; it is handed the one spelling it takes a dash in - (test "-o - reaches getopt-long as a value" - '("x.sex" "--output=-" "-C") - (spell-stdout-output '("x.sex" "-o" "-" "-C"))) - (test "and so does --output -" - '("--output=-") - (spell-stdout-output '("--output" "-"))) - (test "any other output is left alone" - '("-o" "out" "-") - (spell-stdout-output '("-o" "out" "-")))) - -;;; --depfile: an import pastes another file's forms into the output, so -;;; the output depends on that file as a C object does on a header -(test-group "dependency rule" - (test "the target, the source and the modules, then a rule per module" - "app.o: app.sex /m/greet.sex /m/ledger.sex\n\n/m/greet.sex:\n\n/m/ledger.sex:\n" - (dependency-rule "app.o" "app.sex" '("/m/greet.sex" "/m/ledger.sex"))) - (test "no modules, no rules of their own" - "app.o: app.sex\n" - (dependency-rule "app.o" "app.sex" '())) - (test "a space is escaped, the way make reads it" - "a\\ b.o: a\\ b.sex\n" - (dependency-rule "a b.o" "a b.sex" '())) - (test "stdin is no file to depend on" - "app.o: /m/greet.sex\n\n/m/greet.sex:\n" - (dependency-rule "app.o" #f '("/m/greet.sex")))) diff --git a/tests/command-line/cc b/tests/command-line/cc new file mode 100755 index 0000000..643e16b --- /dev/null +++ b/tests/command-line/cc @@ -0,0 +1,4 @@ +#!/bin/sh +# Stands in for the C compiler: writes the arguments it was given, one +# to a line, to $CC_ARGUMENTS +printf '%s\n' "$@" > "$CC_ARGUMENTS" diff --git a/tests/command-line/check.scm b/tests/command-line/check.scm new file mode 100644 index 0000000..28276b1 --- /dev/null +++ b/tests/command-line/check.scm @@ -0,0 +1,108 @@ +;;; What sexc makes of its command line. +;;; +;;; Everything after the first `--' is the C compiler's, word for word; +;;; `-o -' is stdout; --depfile writes a make rule. Each shows only in +;;; what a sexc process hands on, so each is checked on one. `cc' here +;;; stands in for the compiler and writes down the arguments it got. +;;; +;;; make check-command-line + +(import scheme + (chicken base) + (chicken file) + (chicken io) + (chicken pathname) + (chicken process) + (chicken process-context) + (chicken string) + srfi-1 + test) + +(define sexc (or (get-environment-variable "SEXC") "../../sexc")) + +(define scratch (create-temporary-directory)) +(define (built name) (make-pathname scratch name)) + +(define cc-arguments-file (built "cc-arguments")) +(set-environment-variable! "CC_ARGUMENTS" cc-arguments-file) +(define cc-option (string-append "--c-compiler=" (make-pathname (current-directory) "cc"))) + +;;; The lines sexc prints when run with ARGS and INPUT on its stdin, or +;;; #f when it fails +(define (output-with-input input . args) + (let ((proc (process sexc args))) + (display input (process-input-port proc)) + (close-output-port (process-input-port proc)) + (let ((lines (read-lines (process-output-port proc)))) + (process-wait proc) + (and (process-returned-normally? proc) + (zero? (process-exit-status proc)) + lines)))) + +(define (output-of . args) + (apply output-with-input "" args)) + +(define (cc-arguments) + (call-with-input-file cc-arguments-file read-lines)) + +;;; ("-o" "a" "/tmp/x.c" "-lm") -> ("-lm") +(define (past-c-file arguments) + (cdr (find-tail (lambda (a) (equal? (pathname-extension a) "c")) arguments))) + +;;; What the compiler got past sexc's own -o and .c file, when sexc +;;; compiles with ARGS +(define (cc-tail . args) + (and (apply output-of cc-option args) + (past-c-file (cc-arguments)))) + +(define hello (string-intersperse (call-with-input-file "hello.sex" read-lines) "\n")) + +(test-group "--" + ;; `OpenGL' has no dash, and was once dropped for it, leaving + ;; -framework to swallow whatever came next + (test "what follows reaches the compiler as written" + '("-framework" "OpenGL" "-Wall") + (cc-tail "hello.sex" "-o" (built "a") "--" "-framework" "OpenGL" "-Wall")) + (test "what precedes stays with sexc" + (list "-o" (built "a")) + (begin (cc-tail "hello.sex" "-o" (built "a") "--" "-lm") + (take (cc-arguments) 2))) + (test "without one the compiler gets nothing more" + '() + (cc-tail "hello.sex" "-o" (built "a"))) + (test "nor after a trailing one" + '() + (cc-tail "hello.sex" "-o" (built "a") "--")) + ;; ld takes a `--' of its own + (test "only the first one separates" + '("-Wl,--as-needed" "--" "-lm") + (cc-tail "hello.sex" "-o" (built "a") "--" "-Wl,--as-needed" "--" "-lm")) + (test "a file after it is the compiler's, and the source is stdin" + '("extra.o") + (and (output-with-input hello cc-option "-o" (built "a") "--" "extra.o") + (past-c-file (cc-arguments))))) + +;;; getopt-long reads a lone `-' as an option, and refused `-o -' +(test-group "-o -" + (test-assert "writes the C to stdout" + (member "int main (void) {" (or (output-of "hello.sex" "-C" "-o" "-") '()))) + (test-assert "and so does --output -" + (member "int main (void) {" (or (output-of "hello.sex" "-C" "--output" "-") '())))) + +;;; The rules for the modules a unit imports are checked in tests/modules +(test-group "--depfile" + (copy-file "hello.sex" (built "a b.sex")) + (test "a space is escaped, the way make reads it" + (list (string-append (built "a\\ b.c") ": " (built "a\\ b.sex"))) + (and (output-of (built "a b.sex") "-C" "-o" (built "a b.c") + (string-append "--depfile=" (built "a b.d"))) + (call-with-input-file (built "a b.d") read-lines))) + (test "stdin is no file to depend on" + (list (string-append (built "hello.c") ":")) + (and (output-with-input hello "-C" "-o" (built "hello.c") + (string-append "--depfile=" (built "hello.d"))) + (call-with-input-file (built "hello.d") read-lines)))) + +(delete-directory scratch #t) + +(test-exit) diff --git a/tests/command-line/hello.sex b/tests/command-line/hello.sex new file mode 100644 index 0000000..28bf140 --- /dev/null +++ b/tests/command-line/hello.sex @@ -0,0 +1,2 @@ +(pub fn main () int + (return 0)) diff --git a/tests/modules/check.scm b/tests/modules/check.scm index 7f09d72..71e68ae 100644 --- a/tests/modules/check.scm +++ b/tests/modules/check.scm @@ -72,6 +72,14 @@ (irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex" (call-with-input-file (built "greet-app.d") read-line))) +;;; Each module has a rule of its own, so make takes one deleted since as +;;; out of date rather than as a file it has no rule to make +(test "and gives each a rule of its own" + '("greet.sex:" "ledger.sex:") + (filter-map (lambda (line) + (and (irregex-match ".*\\.sex:" line) (pathname-strip-directory line))) + (call-with-input-file (built "greet-app.d") read-lines))) + (test "the app runs through both" '("hello, world" "hello, sex" diff --git a/tests/run.scm b/tests/run.scm index b31b48a..20e72f8 100644 --- a/tests/run.scm +++ b/tests/run.scm @@ -10,7 +10,6 @@ (include-relative "line-directives.scm") (include-relative "codegen.scm") (include-relative "encapsulation.scm") -(include-relative "args.scm") (include-relative "module-path.scm") (include-relative "types.scm") (include-relative "infer.scm")