Debian Codegraph @

test/scripts.scm

raw
1 ;; Test utility scripts
2
3 (use gauche.test)
4 (use gauche.process)
5 (use srfi.13)
6 (use file.util)
7 (use file.filter)
8 (use gauche.config)
9 (use gauche.threads)
10
11 (test-start "utility scripts")
12
13 (define *top-srcdir*
14 (sys-normalize-pathname (or (sys-getenv "top_srcdir") "..")
15 :absolute #t :canonicalize #t))
16 (define *top-builddir*
17 (sys-normalize-pathname (or (sys-getenv "top_builddir") "..")
18 :absolute #t :canonicalize #t))
19
20 (define *executable-suffix* (gauche-config "--executable-suffix"))
21
22 (define (fix-path path)
23 (cond-expand
24 [gauche.os.windows (regexp-replace-all #/\\/ path "/")]
25 [else path]))
26
27 (define (wrap-with-test-directory thunk dirs :optional (mkdir? #t))
28 ;; cleanup may fail due to the timing (exp. on mingw), so we retry
29 ;; with delay.
30 (define (remove-dirs dirs)
31 (let loop ([retry 0])
32 (if (< retry 3)
33 (or (guard (e [<system-error> #f])
34 (apply remove-files dirs)
35 #t)
36 (begin (sys-sleep 1) (loop (+ retry 1))))
37 (apply remove-files dirs))))
38 (remove-dirs dirs)
39 (when mkdir? (for-each make-directory* dirs))
40 (unwind-protect (thunk) (remove-dirs dirs)))
41
42 ;;=======================================================================
43 (test-section "gosh")
44
45 (test* "'main' in an alternative module" "foo"
46 (unwind-protect
47 (begin
48 (delete-files "test.o")
49 (with-output-to-file "test.o"
50 (^[]
51 (write
52 '(define-module foo (define (main args) (print "foo") 0)))
53 (write
54 '(define (main args) (print "bar") 0))))
55 (process-output->string '("./gosh" "-ftest" "-mfoo" "test.o")))
56 (delete-files "test.o")))
57
58 (test* "r7rs script support" "bar"
59 (unwind-protect
60 (begin
61 (delete-files "test.o")
62 (with-output-to-file "test.o"
63 (^[]
64 (write
65 '(import (scheme base) (scheme write)))
66 (write
67 '(display "bar\n"))))
68 (process-output->string '("./gosh" "-ftest" "test.o")))
69 (delete-files "test.o")))
70
71 (test* "r7rs script support (don't call main automatically)" "baz"
72 (unwind-protect
73 (begin
74 (delete-files "test.o")
75 (with-output-to-file "test.o"
76 (^[]
77 (write
78 '(import (scheme base) (scheme write)))
79 (write
80 '(define (main args) (display "bar\n")))
81 (write
82 '(display "baz\n"))))
83 (process-output->string '("./gosh" "-ftest" "test.o")))
84 (delete-files "test.o")))
85
86 (test* "r7rs + SRFI-22 by interpreter name" "baz bar"
87 (unwind-protect
88 (begin
89 (delete-files "test.o" "scheme-r7rs")
90 (copy-file #"./gosh~*executable-suffix*" "./scheme-r7rs")
91 (sys-chmod "./scheme-r7rs" #o755)
92 (with-output-to-file "test.o"
93 (^[]
94 (write
95 '(import (scheme base) (scheme write)))
96 (write
97 '(define (main args) (display "bar\n") 0))
98 (write
99 '(display "baz\n"))))
100 (process-output->string '("./scheme-r7rs" "-ftest" "test.o")
101 :on-abnormal-exit :ignore))
102 (delete-files "test.o" "scheme-r7rs")))
103
104 ;; This caused assertion failure in 0.9.5, because 'main' was called
105 ;; via Scm_ApplyRec without base VM running.
106 ;; See https://github.com/shirok/Gauche/issues/244
107 (test* "proper error handling of 'main'" "ok"
108 (unwind-protect
109 (begin
110 (delete-files "test.o")
111 (with-output-to-file "test.o"
112 (^[]
113 (write
114 '(use gauche.partcont))
115 (write
116 `(define (main args)
117 (reset (shift k (call-with-input-file "test.o" k)))
118 (print 'ok)
119 0))))
120 (process-output->string '("./gosh" "-ftest" "test.o")))
121 (delete-files "test.o")))
122
123 ;;=======================================================================
124 (test-section "gauche-config")
125
126 (define (run-gauche-config . opts)
127 (let1 p (run-process `("./gauche-config" ,@opts)
128 :redirects '((>& 2 1) (> 1 out)))
129 (begin0 (port->string (process-output p 'out))
130 (process-wait p))))
131
132 (define *config-options*
133 '("-V" "-I" "-L" "-l" "--cc" "--ac" "--reconfigure" "--arch"
134 "--syslibdir" "--sysarchdir" "--sysincdir"
135 "--sitelibdir" "--sitearchdir" "--siteincdir"
136 "--pkglibdir" "--pkgarchdir" "--pkgincdir"
137 "--mandir" "--infodir"
138 "--object-suffix" "--executable-suffix" "--so-suffix" "--so-ldflags"
139 "--so-libs" "--dylib-suffix" "--dylib-ldflags" "--rpath-flag"))
140
141 (dolist [opt *config-options*]
142 (test* #"gauche-config ~opt" (gauche-config opt)
143 (car (string-split (run-gauche-config opt) #[\r\n]))))
144
145 (test*/diff
146 "gauche-config --help"
147 '("Usage: gauche-config [option ...]"
148 "Multiple options can be given."
149 ""
150 "General parameter"
151 " -V"
152 " The current Gauche version."
153 ""
154 "Parameters to compile applications using Gauche"
155 " -I"
156 " Include path options required to compile programs using Gauche"
157 " (Note: This doesn't work if Gauche installation directory"
158 " path contains whitespaces. See --incdirs below.)"
159 " -L"
160 " Library path options required to link programs using Gauche"
161 " (Note: This doesn't work if Gauche installation directory"
162 " path contains whitespaces. See --archdirs below.)"
163 " -l"
164 " Link library options required to link programs using Gauche."
165 " --cc"
166 " The name of the C compiler used to compile this Gauche."
167 " --cpp"
168 " The command to run the C preprocessor."
169 " --ac"
170 " The directory that contains Gauche-specific autoconf macros."
171 " --reconfigure"
172 " The command line used to configure the current installation."
173 " --arch"
174 " The autoconf-style architecture signature (cpu-vendor-kernel-os)."
175 " --incdirs"
176 " --archdirs"
177 " The list of directory names to be looked for include files and"
178 " libraries, respectively. Each directory name may be quoted if"
179 " it contains whitespaces, and separated by ':' on Unix platforms, or"
180 " by ';' on Windows platforms."
181 " --local-incdir"
182 " --local-libdir"
183 " These are '-I' and '-L' flags for additional local headers/libraries"
184 " to search, given by '--with-local' configure flags. Note that those"
185 " are also included in '-I', '-L', '--incdirs', and '--archdirs'."
186 ""
187 "Parameters to install files"
188 " --prefix"
189 " The directory prefix set by configure."
190 " --sysincdir"
191 " --siteincdir"
192 " --pkgincdir"
193 " Directories where system|site|package header files of extensions go."
194 " --syslibdir"
195 " --sitelibdir"
196 " --pkglibdir"
197 " Directories where system|site|package scheme files go."
198 " --sysarchdir"
199 " --sitearchdir"
200 " --pkgarchdir"
201 " Directories where system|site|package DSO files go."
202 " --mandir"
203 " --infodir"
204 " Directories where gauche manpage and info docs are installed."
205 ""
206 "Parameters to help building extensions"
207 " --object-suffix"
208 " The extension of the compiled objects (e.g. 'o' or 'obj')."
209 " --executable-suffix"
210 " The extension of the executable including a period (empty on Unix systems,"
211 " '.exe' on Windows."
212 " --so-suffix"
213 " The extension for dynamically loadable (dlopen-able) modules (e.g. 'so')."
214 " --so-cflags"
215 " Additional CFLAGS to create dynamically loadable modules."
216 " --so-ldflags"
217 " Additional LDFLAGS to create dynamically loadable modules."
218 " --so-libs"
219 " Additional libraries required to create dynamically loadable modules."
220 " --dylib-suffix"
221 " The extension for dynamically linked libraries (as opposed to dlopen()ed)"
222 " Usually the same as --so-suffix, but OSX wants 'dylib'."
223 " --dylib-ldflags"
224 " LDFLAGS to create dynamically linked libraries."
225 " --rpath-flag"
226 " Compiler flag(s) to embed RPATH"
227 " --default-cflags"
228 " This is the CFLAGS used to compile Gauche, and to be used to compile"
229 " extensions as well."
230 " --cppflags"
231 " Compiler flag(s) passed to C preprocessor"
232 " --static-libs"
233 " List of library link flags ('-llib') required to link Gauche statically."
234 " Similar to '-l', but this includes the libraries that are used"
235 " for extension modules, and also the static library itself,"
236 " that is -lgauche-static-X.X."
237 " --libgauche-so"
238 " The base name of dynamically linked libgauche."
239 "")
240 (string-split (run-gauche-config "--help")
241 (cond-expand
242 [gauche.os.windows "\r\n"]
243 [else "\n"])))
244
245 ;;=======================================================================
246 (test-section "configure")
247
248 (use gauche.configure)
249 (test-module 'gauche.configure)
250 (test-module 'gauche.configure.base)
251 (test-module 'gauche.configure.lang)
252 (test-module 'gauche.configure.prog)
253 (test-module 'gauche.configure.output)
254 (test-module 'gauche.configure.compile)
255 (test-module 'gauche.configure.init)
256
257 ;; cf-help-string test
258 (test* "cf-help-string (run-in)"
259 " -C ignored for the compatibility to autoconf-generated\
260 \n configure\n"
261 ($ cf-help-string "-C"
262 "ignored for the compatibility to autoconf-generated configure"))
263 (test* "cf-help-string (description next line)"
264 " -C, --config-cache,--cache-file=FILE\
265 \n ignored for the compatibility to autoconf-generated\
266 \n configure\n"
267 ($ cf-help-string "-C, --config-cache,--cache-file=FILE"
268 "ignored for the compatibility to autoconf-generated configure"))
269
270 ;; When we run configure, we need to include directories of gosh and
271 ;; other scripts in PATH. Before installing Gauche, where we find them
272 ;; is "..".
273 (define (run-with-parent-directory-in-paths cmd . args)
274 (let* ([separ (cond-expand [gauche.os.windows ";"] [else ":"])]
275 [paths-old (sys-getenv "PATH")]
276 [paths-new #"..~|separ|~|paths-old|"])
277 (cond-expand
278 [gauche.os.windows
279 (sys-setenv "PATH" paths-new #t)
280 (unwind-protect (apply run-process cmd args)
281 (sys-setenv "PATH" paths-old #t))]
282 [else
283 (apply run-process `("env" ,#"PATH=~|paths-new|" ,@cmd) args)])))
284
285 (define (with-cf-subst-test)
286 (make-directory* "test.o")
287 (with-output-to-file "test.o/configure"
288 (^[]
289 (write '(use gauche.configure))
290 (write '(cf-init "foo" "1.0"))
291 (write '(cf-subst 'A "1"))
292 (write '(cf-subst 'B "2"))
293 (write '(cf-subst 'C "3"))
294 (write '(define (dump)
295 (for-each (^v (cf-msg-notice "~a=~a" v (cf$ v)))
296 '(A B C D E F))))
297 (write '(with-cf-subst
298 ((A "11") (B + "zz") (C "nn" +) (D "44") (E + "55") (F "66" +))
299 (dump)))
300 (write '(dump))))
301
302 (test* "running `configure' script for with-cf-subst" 0
303 (process-exit-status
304 (run-with-parent-directory-in-paths
305 `("../gosh" "-ftest" "./configure")
306 :output :null :wait #t :directory "test.o")))
307 (test*/diff "check result of with-cf-subst"
308 '("Configuring foo 1.0"
309 "A=11"
310 "B=2 zz"
311 "C=nn 3"
312 "D=44"
313 "E=55"
314 "F=66"
315 "A=1"
316 "B=2"
317 "C=3"
318 "D="
319 "E="
320 "F=")
321 (file->string "test.o/config.log")))
322
323 (define (configure-test-1)
324 (make-directory* "test.o/src")
325 (make-directory* "test2.o")
326 (let ([extdir (build-path (or (sys-getenv "top_srcdir") "..") "ext")])
327 (define (filter-copy infile outfile)
328 (file-filter (^[in out]
329 (dolist [line (port->string-list in)]
330 (display ($ regexp-replace-all* line
331 #/@@author@@/ ""
332 #/@@license@@/ ""
333 #/@@/ ""
334 #/\(cf-output-default\)/
335 "(cf-define 'HAVE_STDIO_H \"1\")\n\
336 (cf-config-headers \"config.h\")\n\
337 (cf-output-default)")
338 out)
339 (newline out)))
340 :input infile
341 :output outfile))
342 (filter-copy (build-path extdir "package-templates" "configure")
343 "test.o/configure")
344 (filter-copy (build-path extdir "package-templates" "package.scm")
345 "test.o/package.scm")
346 (filter-copy (build-path extdir "package-templates" "Makefile.in")
347 "test.o/Makefile.in")
348
349 (with-output-to-file "test.o/src/Makefile.in"
350 (^[]
351 (print "srcdir = @srcdir@")
352 (print "top_srcdir = @top_srcdir@")
353 (print "builddir = @builddir@")
354 (print "top_builddir = @top_builddir@")))
355
356 (with-output-to-file "test.o/config.h.in"
357 (^[]
358 (print "/* Define to 1 if you have the <foo.h> header file */")
359 (print "#undef HAVE_FOO_H")
360 (print)
361 (print "this is literal line")
362 (print "/* Define to 1 if you have the <stdio.h> header file */")
363 (print "#undef HAVE_STDIO_H")
364 ))
365 )
366
367 (test* "running `configure' script" 0
368 (process-exit-status
369 (run-with-parent-directory-in-paths
370 `("../gosh" "-ftest" "./configure")
371 :output :null :wait #t :directory "test.o")))
372 (test* "Makefile substitution" '()
373 (and (file-exists? "test.o/Makefile")
374 (filter #/@\w+@/ (file->string-list "test.o/Makefile"))))
375 (test* "VERSION generation" "0.1\n"
376 (and (file-exists? "test.o/VERSION")
377 (file->string "test.o/VERSION")))
378 (test* "srcdir etc."
379 '("srcdir = ." "top_srcdir = ../" "builddir = ." "top_builddir = ../")
380 (file->string-list "test.o/src/Makefile"))
381
382 (test* "configure --version" "package configure 0.1"
383 (read-line
384 (process-output
385 (run-with-parent-directory-in-paths
386 `("../gosh" "-ftest" "./configure" "--version")
387 :output :pipe :directory "test.o"))))
388
389 (test* "running `configure' script in different directory" 0
390 (process-exit-status
391 (run-with-parent-directory-in-paths
392 `("../gosh" "-ftest" "../test.o/configure")
393 :output :null :wait #t :directory "test2.o")))
394
395 (test* "Makefiles in proper builddir" '(#t #t)
396 (list (file-exists? "test2.o/Makefile")
397 (file-exists? "test2.o/src/Makefile")))
398
399 (test* "srcdir etc."
400 '("srcdir = ../../test.o/src" "top_srcdir = ../../test.o" "builddir = ." "top_builddir = ../")
401 (file->string-list "test2.o/src/Makefile"))
402
403 (dolist [d '("test.o" "test2.o")]
404 (let1 config.h (build-path d "config.h")
405 (test* config.h
406 '("/* Define to 1 if you have the <foo.h> header file */"
407 "/* #undef HAVE_FOO_H */"
408 ""
409 "this is literal line"
410 "/* Define to 1 if you have the <stdio.h> header file */"
411 "#define HAVE_STDIO_H 1")
412 (and (file-exists? config.h)
413 (file->string-list config.h)))))
414
415 (remove-files "test2.o/package.gpd")
416
417 (test* "gpd"
418 (cond-expand
419 [gauche.os.windows "./configure \"--with-local=/a/b:/c/d\""]
420 [else "./configure --with-local=/a/b:/c/d"])
421 (and (zero?
422 (process-exit-status
423 (run-with-parent-directory-in-paths
424 `("../gosh" "-ftest" "../test.o/configure"
425 "--with-local=/a/b:/c/d")
426 :output :null :wait #t :directory "test2.o")))
427 (cadr (memv :configure
428 (car (file->sexp-list "test2.o/package.gpd"))))))
429 )
430
431 (define (configure-test-2)
432 (make-directory* "test.o/src")
433 (make-directory* "test2.o")
434 (let ([extdir (build-path (or (sys-getenv "top_srcdir") "..") "ext")])
435 (define (filter-copy infile outfile)
436 (file-filter (^[in out]
437 (dolist [line (port->string-list in)]
438 (display (regexp-replace-all* line
439 #/@@author@@/ ""
440 #/@@license@@/ ""
441 #/@@/ "")
442 out)
443 (newline out)))
444 :input infile
445 :output outfile))
446 (filter-copy (build-path extdir "package-templates" "package.scm")
447 "test.o/package.scm")
448 (filter-copy (build-path extdir "package-templates" "Makefile.in")
449 "test.o/Makefile.in")
450
451 (with-output-to-file "test.o/configure"
452 (^[]
453 ($ for-each write
454 '((use gauche.configure)
455 (cf-init-gauche-extension)
456 (cf-check-headers '("stdio.h" "stdlib.h"
457 "no-such-header-should-exist.h"))
458 (cf-check-types '("a_t" "b_t" "struct c_t" "d_t")
459 :includes '("typedef int a_t;"
460 "int b_t;"
461 "struct c_t { int i; };"))
462 (cf-check-decls '("a" "b" "c" "d" "e" "f")
463 :includes '("#define a 1"
464 "int b;"
465 "const int c = 1;"
466 "extern int d();"
467 "struct foo { int i; } e;"))
468 (cf-check-members '("struct foo.a" "struct foo.b")
469 :includes '("struct foo { int b; };"))
470 (cf-check-funcs '("printf" "nonexistent_weird_function"))
471 (unless (#/darwin/ (gauche-architecture))
472 ;; this test fails on OSX since 'sin' is recognized as built-in
473 (cf-check-lib "m" "sin"))
474 (cf-check-lib "no-such-library-should-exist" "sin")
475 (cf-config-headers "config.h")
476 (cf-output-default)))))
477
478 (with-output-to-file "test.o/src/Makefile.in"
479 (^[]
480 (print "LIBS = @LIBS@")))
481
482 (with-output-to-file "test.o/config.h.in"
483 (^[]
484 (print "#undef HAVE_STDIO_H")
485 (print "#undef HAVE_STDLIB_H")
486 (print "#undef HAVE_NO_SUCH_HEADER_SHOILD_EXIST_H")
487 (print "#undef HAVE_A_T")
488 (print "#undef HAVE_B_T")
489 (print "#undef HAVE_STRUCT_C_T")
490 (print "#undef HAVE_D_T")
491 (print "#undef HAVE_DECL_A")
492 (print "#undef HAVE_DECL_B")
493 (print "#undef HAVE_DECL_C")
494 (print "#undef HAVE_DECL_D")
495 (print "#undef HAVE_DECL_E")
496 (print "#undef HAVE_DECL_F")
497 (print "#undef HAVE_STRUCT_FOO_A")
498 (print "#undef HAVE_STRUCT_FOO_B")
499 (print "#undef HAVE_PRINTF")
500 (print "#undef HAVE_NONEXISTENT_WEIRD_FUNCTION")
501 (print "#undef HAVE_LIBM")
502 (print "#undef HAVE_LIBNO_SUCH_LIBRARY_SHOULD_EXIST")
503 ))
504 )
505
506 (test* "running `configure' script with actual checks" 0
507 (process-exit-status
508 (run-with-parent-directory-in-paths
509 `("../gosh" "-ftest" "./configure")
510 :output :null :wait #t :directory "test.o")))
511
512 (test* "cf-check-lib set LIBS"
513 (if (#/darwin/ (gauche-architecture))
514 '("LIBS = ")
515 '("LIBS = -lm"))
516 (file->string-list "test.o/src/Makefile"))
517
518 (test*/diff "cf-check-headers and cf-check-lib to set defines"
519 `("#define HAVE_STDIO_H 1"
520 "#define HAVE_STDLIB_H 1"
521 "/* #undef HAVE_NO_SUCH_HEADER_SHOILD_EXIST_H */"
522 "#define HAVE_A_T 1"
523 "/* #undef HAVE_B_T */"
524 "#define HAVE_STRUCT_C_T 1"
525 "/* #undef HAVE_D_T */"
526 "#define HAVE_DECL_A 1"
527 "#define HAVE_DECL_B 1"
528 "#define HAVE_DECL_C 1"
529 "#define HAVE_DECL_D 1"
530 "#define HAVE_DECL_E 1"
531 "#define HAVE_DECL_F 0"
532 "/* #undef HAVE_STRUCT_FOO_A */"
533 "#define HAVE_STRUCT_FOO_B 1"
534 "#define HAVE_PRINTF 1"
535 "/* #undef HAVE_NONEXISTENT_WEIRD_FUNCTION */"
536 ,(if (#/darwin/ (gauche-architecture))
537 "/* #undef HAVE_LIBM */"
538 "#define HAVE_LIBM 1")
539 "/* #undef HAVE_LIBNO_SUCH_LIBRARY_SHOULD_EXIST */")
540 ($ filter #/HAVE_/
541 $ file->string-list "test.o/config.h"))
542 )
543
544 (wrap-with-test-directory with-cf-subst-test '("test.o") #f)
545 (wrap-with-test-directory configure-test-1 '("test.o" "test2.o") #f)
546 (wrap-with-test-directory configure-test-2 '("test.o" "test2.o") #f)
547
548 ;;=======================================================================
549 (test-section "gauche-install")
550
551 (define *gauche-install-script*
552 (build-path (or (sys-getenv "top_srcdir") "..") "src" "gauche-install.in"))
553
554 (test-script *gauche-install-script*)
555
556 (define (run-install . args)
557 (run-process `("./gosh" "-ftest" ,*gauche-install-script* ,@args)
558 :output :null :wait #t))
559
560 (define (test-install)
561 (test* "-d" #t
562 (begin (run-install "-d" "test1.o/dest")
563 (file-is-directory? "test1.o/dest")))
564
565 (create-directory-tree "."
566 `(test.o ((bin ((command1 ,(make-string 20000))
567 (command2 ,(make-string 20000))))
568 (lib ((lib1 ,(make-string 1000000))
569 (lib2 ,(make-string 500000))))
570 (etc ((conf1 ,(make-string 1000)))))))
571 (test* "file -> file" #t
572 (begin (run-install "-m" "444" "test.o/lib/lib1" "test1.o/dest/lib1")
573 (and (file-is-regular? "test1.o/dest/lib1")
574 (= (file-perm "test1.o/dest/lib1") #o444)
575 (file-equal? "test.o/lib/lib1" "test1.o/dest/lib1"))))
576
577 (remove-files "test1.o/dest/lib1")
578 (test* "files -> dir" #t
579 (begin (run-install "-m" "444" "test.o/lib/lib1" "test.o/lib/lib2"
580 "test1.o/dest")
581 (and (file-is-regular? "test1.o/dest/lib1")
582 (file-is-regular? "test1.o/dest/lib2")
583 (= (file-perm "test1.o/dest/lib1")
584 (file-perm "test1.o/dest/lib1")
585 #o444)
586 (file-equal? "test.o/lib/lib1" "test1.o/dest/lib1")
587 (file-equal? "test.o/lib/lib2" "test1.o/dest/lib2"))))
588
589 (remove-files "test1.o/dest/lib1" "test1.o/dest/lib2")
590 (test* "-T" #t
591 (begin (run-install "-T" "test1.o/dest" "-m" "555"
592 "test.o/bin/command1"
593 "test.o/bin/command2")
594 (and (= (file-perm "test1.o/dest/test.o/bin/command1")
595 (file-perm "test1.o/dest/test.o/bin/command2")
596 (cond-expand
597 [gauche.os.windows #o444]
598 [else #o555]))
599 (file-equal? "test.o/bin/command1"
600 "test1.o/dest/test.o/bin/command1")
601 (file-equal? "test.o/bin/command2"
602 "test1.o/dest/test.o/bin/command2"))))
603
604 (test* "-U" #t
605 (begin (run-install "-U" "test1.o/dest" "-m" "555"
606 "test.o/bin/command1"
607 "test.o/bin/command2")
608 (and (not (file-exists? "test1.o/dest/test.o/bin/command1"))
609 (not (file-exists? "test1.o/dest/test.o/bin/command2")))))
610
611 (test* "-T -p" #t
612 (begin (run-install "-T" "test1.o/dest" "-m" "555" "-p" "test.o"
613 "test.o/bin/command1"
614 "test.o/bin/command2")
615 (and (= (file-perm "test1.o/dest/bin/command1")
616 (file-perm "test1.o/dest/bin/command2")
617 (cond-expand
618 [gauche.os.windows #o444]
619 [else #o555]))
620 (file-equal? "test.o/bin/command1"
621 "test1.o/dest/bin/command1")
622 (file-equal? "test.o/bin/command2"
623 "test1.o/dest/bin/command2"))))
624
625 (test* "-U -p" #t
626 (begin (run-install "-U" "test1.o/dest" "-m" "555" "-p" "test.o"
627 "test.o/bin/command1"
628 "test.o/bin/command2")
629 (and (not (file-exists? "test1.o/dest/bin/command1"))
630 (not (file-exists? "test1.o/dest/bin/command2")))))
631 )
632
633 (wrap-with-test-directory test-install '("test.o" "test1.o") #f)
634
635 ;;=======================================================================
636 (test-section "gauche-package")
637
638 (test-script
639 (build-path (or (sys-getenv "top_srcdir") "..") "src" "gauche-package.in"))
640
641 (define (package-generate-tests scheme-only?)
642 (define (file-check name)
643 (test* #"checking existence of ~name" #t
644 (file-exists? #"test.o/Test/~name")))
645 (define pwd (sys-getcwd))
646
647 ;; When we run the test before installation, we need to tweak
648 ;; some directories. We can distinguish it, for install-check
649 ;; passes an argument "install-check" to the test script.
650 (define in-place? (not (equal? (cdr (command-line)) '("install-check"))))
651
652 (define gauche-package
653 (build-path (gauche-architecture-directory) "gauche-package"))
654 (define generate-command
655 (if in-place?
656 `(../gosh -ftest
657 ,(build-path *top-srcdir* "src" "gauche-package.in")
658 generate
659 ,@(cond-list [scheme-only? "--scheme-only"])
660 --template-dir ,(build-path *top-srcdir* "ext"
661 "package-templates")
662 Test test.tester)
663 `(,gauche-package generate
664 ,@(cond-list [scheme-only? "--scheme-only"])
665 Test test.tester)))
666 (define compile-command
667 (if in-place?
668 `(../../gosh -q -I../../../src -I../../../lib
669 ../run compile
670 --verbose test test.c testlib.scm)
671 `(,gauche-package compile --verbose test test.c testlib.scm)))
672 (define test-command
673 (if in-place?
674 `(../../gosh -ftest -I. ./test.scm)
675 `(,(build-path (gauche-architecture-directory) "gosh") -I. ./test.scm)))
676
677 (when in-place?
678 (with-output-to-file "test.o/run"
679 (cut for-each write
680 `((use gauche.config)
681 (define prefix (if (equal? (sys-basename (sys-getcwd)) "Test")
682 "../" ""))
683 (define sep (cond-expand [gauche.os.windows ";"][else ":"]))
684 (define top-srcdir ,*top-srcdir*)
685 ;; This is used to copy templates from.
686 (define (gauche-library-directory) #"~|top-srcdir|/ext/templates/x")
687 ;; Intercept gauche-config to override compiler flags
688 (define gauche-config-orig
689 (with-module gauche.config gauche-config))
690 (define-in-module gauche.config (gauche-config opt)
691 (cond [(equal? opt "--incdirs")
692 #"~|top-srcdir|/src~|sep|~|prefix|../../src/~|sep|~|top-srcdir|/gc/include"]
693 [(equal? opt "--archdirs")
694 #"~|prefix|../../src"]
695 [else (gauche-config-orig opt)]))
696 (load #"~|top-srcdir|/src/gauche-package.in")))))
697
698 (test-log "Running ~a" generate-command)
699
700 ($ run-process generate-command :wait #t :directory "test.o/")
701
702 (for-each file-check
703 (if scheme-only?
704 '("configure" "Makefile.in" "package.scm"
705 "test.scm" "test/tester.scm")
706 '("configure" "Makefile.in" "package.scm"
707 "test.c" "test.h" "test.scm" "testlib.scm"
708 "test/tester.scm")))
709
710 (test-log "Running ~a" compile-command)
711
712 (unless scheme-only?
713 (test* "gauche-package compile" #t
714 (let* ([p ($ run-process
715 compile-command
716 :redirects '((>& 2 1) (> 1 out))
717 :directory "test.o/Test")]
718 [o (port->string (process-output p 'out))])
719 (process-wait p)
720 ;; if compilation fails, returns the output for better diagnostics.
721 (or (zero? (process-exit-status p)) o))))
722
723 (test* "running test" #t
724 (let* ([p ($ run-process
725 test-command
726 :redirects '((>& 2 1) (> 1 out))
727 :directory "test.o/Test")]
728 [o (port->string (process-output p 'out))])
729 (process-wait p)
730 (or (zero? (process-exit-status p)) o)))
731 )
732
733 (wrap-with-test-directory (cut package-generate-tests #f) '("test.o"))
734 (wrap-with-test-directory (cut package-generate-tests #t) '("test.o"))
735
736 ;;=======================================================================
737 (test-section "precomp")
738
739 (define (do-precomp! files extra-options)
740 (do-process! `("../../src/gosh" "-ftest"
741 ,#"-I~|*top-srcdir*|/test/test-precomp"
742 ,(build-path *top-srcdir* "lib/tools/precomp")
743 "--strip-prefix"
744 ,(fix-path (build-path *top-srcdir* "test/test-precomp"))
745 ,@extra-options
746 ,@(map (^[file] (fix-path (build-path *top-srcdir* "test/test-precomp" file)))
747 files))
748 :directory "test.o"))
749
750 (define (do-compile! output files)
751 (do-process!
752 `("../../src/gosh" "-ftest"
753 ,(build-path *top-srcdir* "src/gauche-package.in")
754 "compile"
755 ,#"--cppflags=-I~(fix-path (build-path *top-srcdir* \"src\")) \
756 -I~(fix-path (build-path *top-srcdir* \"gc/include\")) \
757 -I~(fix-path (build-path *top-builddir* \"src\")) \
758 -I~(fix-path (build-path *top-builddir* \"gc/include\"))"
759 ,#"--ldflags=-L~(fix-path (build-path *top-srcdir* \"src\")) \
760 -L~(fix-path (build-path *top-builddir* \"src\"))"
761 ,output
762 ,@files)
763 :directory "test.o"))
764
765 ;; Removing a DSO file while it is opened tend to cause issues, so we invoke
766 ;; a child gosh to load and test it.
767 (define-syntax dynload-and-eval
768 (syntax-rules ()
769 [(_ libname expr)
770 (begin
771 (with-output-to-file "test.o/t.scm"
772 (^[]
773 (write '(add-load-path "."))
774 (write '(use gauche.threads)) ; some tests need this
775 (write '(use file.util))
776 (write `(define *top-srcdir*
777 ,(if (absolute-path? *top-srcdir*)
778 *top-srcdir*
779 (build-path ".." *top-srcdir*))))
780 (write '(load libname))
781 (write '(write expr))
782 (write '(exit 0))))
783 (let* ([p (run-process '("../../src/gosh" "-ftest" "./t.scm")
784 :output :pipe :directory "test.o")]
785 [result (read (process-output p))])
786 (process-wait p)
787 result))]))
788
789 (define (precomp-test-1)
790 (test* "running precomp 1" #t
791 (do-precomp! '("foo.scm" "foo/bar1.scm" "foo/bar2.scm" "foo/bar3.scm")
792 '()))
793 (test* "precomp generated files"
794 '("test.o/foo--bar1.c"
795 "test.o/foo--bar2.c"
796 "test.o/foo--bar3.c"
797 "test.o/foo.c"
798 "test.o/foo.sci"
799 "test.o/foo/bar1.sci"
800 "test.o/foo/bar2.sci"
801 "test.o/foo/bar3.sci")
802 (sort (map fix-path (directory-fold "test.o" cons '()))))
803 )
804
805 (add-load-path "test.o")
806 (use srfi.42)
807 (use scheme.vector :only (vector-every))
808
809 (define (precomp-test-2)
810 (define (literal=? x y)
811 (cond [(pair? x) (and (pair? y)
812 (literal=? (car x) (car y))
813 (literal=? (cdr x) (cdr y)))]
814 [(vector? x) (and (vector? y)
815 (= (vector-length x) (vector-length y))
816 (vector-every literal=? x y))]
817 [(uvector? x) (and (eqv? (class-of x) (class-of y))
818 (= (uvector-length x) (uvector-length y))
819 (every?-ec (:parallel (: ex x) (: ey y))
820 (literal=? ex ey)))]
821 [(number? x)
822 (and (number? y)
823 (cond [(nan? x) (nan? y)]
824 [(negative-zero? x) (negative-zero? y)]
825 [else (= x y)]))]
826 [else (equal? x y)]))
827
828 (test* "running precomp 2" #t
829 (do-precomp! '("foo.scm" "foo/bar1.scm" "foo/bar2.scm" "foo/bar3.scm")
830 '("--single-interface")))
831 (test* "precomp generated files with --single-interface"
832 '("test.o/foo--bar1.c"
833 "test.o/foo--bar2.c"
834 "test.o/foo--bar3.c"
835 "test.o/foo.c"
836 "test.o/foo.sci")
837 (sort (map fix-path (directory-fold "test.o" cons '()))))
838
839 (test*/diff "consolidated sci file"
840 '(";; generated automatically. DO NOT EDIT"
841 "#!no-fold-case"
842 "(define-module foo.bar2 (use util.match) (export bar2))"
843 "(select-module foo.bar2)"
844 "(dynamic-load \"foo\" :init-function \"Scm_Init_foo__bar2\")"
845 "(provide \"foo/bar2\")"
846 "(define-module foo.bar3 (use foo.bar2) (export bar3))"
847 "(select-module foo.bar3)"
848 "(dynamic-load \"foo\" :init-function \"Scm_Init_foo__bar3\")"
849 "(provide \"foo/bar3\")"
850 "(define-module foo.bar1 (use foo.bar2) (export bar1))"
851 "(select-module foo.bar1)"
852 "(dynamic-load \"foo\" :init-function \"Scm_Init_foo__bar1\")"
853 "(provide \"foo/bar1\")"
854 "(define-module foo (use gauche.uvector) (use foo.bar1) (use foo.bar3) (export foo-master foo-literals foo-shared-literals foo-begin1 foo-begin2) (export foo-include1 foo-include2))"
855 "(select-module foo)"
856 "(dynamic-load \"foo\" :init-function \"Scm_Init_foo\")")
857 (file->string-list "test.o/foo.sci"))
858
859 (test* "compile 2" #t
860 (do-compile! "foo"
861 '("foo.c" "foo--bar1.c" "foo--bar2.c" "foo--bar3.c")))
862 (test* "dynload and literals 1"
863 (list (include "test-precomp/literals.scm")
864 'begin1
865 'begin2
866 'include1
867 'include2)
868 (dynload-and-eval
869 "foo"
870 (list ((module-binding-ref 'foo 'foo-literals))
871 ((module-binding-ref 'foo 'foo-begin1))
872 ((module-binding-ref 'foo 'foo-begin2))
873 ((module-binding-ref 'foo 'foo-include1))
874 ((module-binding-ref 'foo 'foo-include2))))
875 literal=?)
876
877 (test* "literal sharing" '(#t #t #t #t #t #t)
878 (dynload-and-eval
879 "foo"
880 (let1 vs ((module-binding-ref 'foo 'foo-shared-literals))
881 (list
882 ;; See if partial lists are shared
883 (eq? (cdr (assq-ref vs 'list1))
884 (assq-ref vs 'list2))
885 (eq? (cdr (assq-ref vs 'list2))
886 (assq-ref vs 'list3))
887 ;; See if vectors are shared
888 (eq? (assq-ref vs 'vec1)
889 (assq-ref vs 'vec2))
890 (eq? (assq-ref vs 'uvec1)
891 (assq-ref vs 'uvec2))
892 ;; See if strings are shared
893 (eq? (assq-ref vs 'str1)
894 (assq-ref vs 'str2))
895 ;; Descriptive types
896 (eq? (assq-ref vs 'dtype1)
897 (assq-ref vs 'dtype2))
898 ))))
899 )
900
901 (define (precomp-test-3)
902 (test* "running precomp 3" #t (do-precomp! '("types-test.scm") '("-e")))
903 (test* "compile 3" #t (do-compile! "types-test" '("types-test.c")))
904
905 (test* "type reconstruction" '(#t #t #f #t)
906 (dynload-and-eval
907 "types-test"
908 (list
909 (eq? (module-binding-ref 'types-test '<A>)
910 (</> (<Tuple> (<?> <int8>) <string> <integer>)
911 (<List> <integer> 3 10)))
912 (eq? (module-binding-ref 'types-test '<A>)
913 (module-binding-ref 'types-test '<B>))
914 (eq? (module-binding-ref 'types-test '<A>)
915 (module-binding-ref 'types-test '<C>))
916 ;; NB: The following may not be eq?, since (<List> <integer>) is
917 ;; serialized as (<List> <integer> #f #f). The memoization is
918 ;; based on the argument list of the type constructor, so it
919 ;; doesn't match with (<List> <integer>) at runtime.
920 (equal? (module-binding-ref 'types-test '<C>)
921 (</> (<Tuple> (<?> <int8>) <string> <integer>)
922 (<List> <integer>)))
923 )))
924
925 (test* "assertion with reconstructed type" '((3 "ok" 8483958394)
926 (#f "ok" -8483958394)
927 (1 2 3 4 5))
928 (dynload-and-eval
929 "types-test"
930 (list
931 ((module-binding-ref 'types-test 'foo) '(3 "ok" 8483958394))
932 ((module-binding-ref 'types-test 'foo) '(#f "ok" -8483958394))
933 ((module-binding-ref 'types-test 'foo) '(1 2 3 4 5)))))
934
935 (test* "assertion with reconstructed type"
936 #t
937 (dynload-and-eval
938 "types-test"
939 (guard (e ((<error> e)
940 (boolean (#/supposed to be of type/ (~ e'message)))))
941 ((module-binding-ref 'types-test 'foo) "ng"))))
942 )
943
944 (define (precomp-test-4)
945 (test* "running precomp 4" #t (do-precomp! '("literal-mutex.scm") '("-e")))
946 (test* "compile 4" #t (do-compile! "literal-mutex" '("literal-mutex.c")))
947
948 (test* "run-once" '(1 1 1 1 1)
949 (dynload-and-eval
950 "literal-mutex"
951 (let* ([foo (module-binding-ref 'literal-mutex 'foo)]
952 [thrs (map (^_ (make-thread foo)) (iota 5))])
953 (for-each thread-start! thrs)
954 (map thread-join! thrs))))
955 )
956
957 (define (precomp-test-5)
958 (test* "running precomp 5" #t (do-precomp! '("macros.scm") '("-e")))
959 (test* "compile 5" #t (do-compile! "macros" '("macros.c")))
960
961 (test* "compiled macro"
962 '(("compiled macro (er)" ((apple) (banana) bonk))
963 ("compiled macro (id-macro)" (#f 1 2)))
964 (dynload-and-eval
965 "macros"
966 (begin
967 (load (build-path *top-srcdir* "test" "test-precomp" "macros-user"))
968 `(("compiled macro (er)"
969 ,(map er-aif-test '(apple banana dragonfruit)))
970 ("compiled macro (id-macro)"
971 ,(im-state-test))))))
972 )
973
974 (wrap-with-test-directory precomp-test-1 '("test.o"))
975 (wrap-with-test-directory precomp-test-2 '("test.o"))
976 (wrap-with-test-directory precomp-test-3 '("test.o"))
977 (wrap-with-test-directory precomp-test-4 '("test.o"))
978 (wrap-with-test-directory precomp-test-5 '("test.o"))
979
980 ;;=======================================================================
981 (test-section "build-standalone")
982
983 (test-script
984 (build-path (or (sys-getenv "top_srcdir") "..") "lib" "tools" "build-standalone"))
985
986 (define (run-build-static opts args)
987 (do-process `("../../src/gosh" "-ftest"
988 ,(build-path *top-srcdir* "lib/tools/build-standalone")
989 ,#"-I=~|*top-srcdir*|/test/test-static"
990 ,#"--header-dir=~|*top-srcdir*|/src"
991 ,#"--header-dir=~|*top-builddir*|/src"
992 ,#"--header-dir=~|*top-srcdir*|/gc/include"
993 ,#"--library-dir=~|*top-builddir*|/src"
994 ,@opts
995 ,@args)
996 :directory "test.o"))
997
998 (define (static-test-1)
999 (test* "static link test" #t
1000 (run-build-static `("-o" "staticmain"
1001 "-I" ,(build-path *top-srcdir* "test/test-static"))
1002 `(,(fix-path (build-path *top-srcdir*
1003 "test/test-static/main.scm"))
1004 "foo/bar.scm"
1005 "foo/bar-impl.scm"
1006 "foo/baz.scm")))
1007 (test* "static link executable" "ARGS: #(A b CdE)"
1008 (process-output->string '(test.o/staticmain A b CdE)))
1009 )
1010
1011 (wrap-with-test-directory static-test-1 '("test.o"))
1012
1013 ;;=======================================================================
1014 (test-section "test-script")
1015
1016 ;; Test compile-only option
1017
1018 (define (test-script-test-1)
1019 (with-output-to-file "test.o/script"
1020 (^[]
1021 (write `(use gauche.uvector))
1022 (write `(print "bar"))
1023 (write `(define (bar) (foo) (foo)))
1024 (write `(print "foo"))
1025 (write `(define (foo)
1026 (with-output-to-file "test.o/out.o"
1027 (cut print (u8vector 1 2 3 4 5)))))
1028 (write `(bar))))
1029 (test-script "./test.o/script" :compile-only #t)
1030 (test* "not executed" #f (file-exists? "test.o/out.o"))
1031
1032 (test-script "./test.o/script")
1033 (test* "executed" #t (file-exists? "test.o/out.o")))
1034
1035 (wrap-with-test-directory test-script-test-1 '("test.o") #t)
1036
1037 (test-end)