load.test 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ;;;; load.test --- test LOAD and path searching functions -*- scheme -*-
  2. ;;;; Jim Blandy <jimb@red-bean.com> --- September 1999
  3. ;;;;
  4. ;;;; Copyright (C) 1999, 2001, 2006, 2010, 2012 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (test-suite test-load)
  20. #:use-module (test-suite lib)
  21. #:use-module (test-suite guile-test)
  22. #:use-module (system base compile))
  23. (define temp-dir (data-file-name "load-test.dir"))
  24. (define (create-tree parent tree)
  25. (let loop ((parent parent)
  26. (tree tree))
  27. (if (pair? tree)
  28. (let ((elt (car tree)))
  29. (cond
  30. ;; A string means to create an empty file with that name.
  31. ((string? elt)
  32. (close-port (open-file (string-append parent "/" elt) "w")))
  33. ;; A list means to create a directory, and then create files
  34. ;; within it.
  35. ((pair? elt)
  36. (let ((dirname (string-append parent "/" (car elt))))
  37. (mkdir dirname)
  38. (loop dirname (cdr elt))))
  39. (else
  40. (error "create-tree: bad tree structure")))
  41. (loop parent (cdr tree))))))
  42. (define (delete-tree tree)
  43. (cond
  44. ((file-is-directory? tree)
  45. (let ((dir (opendir tree)))
  46. (let loop ()
  47. (let ((entry (readdir dir)))
  48. (cond
  49. ((member entry '("." ".."))
  50. (loop))
  51. ((not (eof-object? entry))
  52. (let ((name (string-append tree "/" entry)))
  53. (delete-tree name)
  54. (loop))))))
  55. (closedir dir)
  56. (rmdir tree)))
  57. ((file-exists? tree)
  58. (delete-file tree))
  59. (else
  60. (error "delete-tree: can't delete " tree))))
  61. (define (try-search-with-extensions path input extensions expected)
  62. (let ((test-name (call-with-output-string
  63. (lambda (port)
  64. (display "search-path for " port)
  65. (write input port)
  66. (if (pair? extensions)
  67. (begin
  68. (display " with extensions " port)
  69. (write extensions port)))
  70. (display " yields " port)
  71. (write expected port)))))
  72. (let ((result (search-path path input extensions)))
  73. (pass-if test-name
  74. (equal? (if (string? expected)
  75. (string-append temp-dir "/" expected)
  76. expected)
  77. result)))))
  78. (define (try-search path input expected)
  79. (try-search-with-extensions path input '() expected))
  80. ;; Create a bunch of files for use in testing.
  81. (mkdir temp-dir)
  82. (create-tree temp-dir
  83. '(("dir1" "foo.scm" "bar.scm" "ugly.scm.scm"
  84. ("subdir1"))
  85. ("dir2" "foo.scm" "baz.scm" "baz.ss" "ugly.scm.ss")
  86. ("dir3" "ugly.scm" "ugly.ss.scm")))
  87. ;; Try some searches without extensions.
  88. (define path (list
  89. (string-append temp-dir "/dir1")
  90. (string-append temp-dir "/dir2")
  91. (string-append temp-dir "/dir3")))
  92. (try-search path "foo.scm" "dir1/foo.scm")
  93. (try-search path "bar.scm" "dir1/bar.scm")
  94. (try-search path "baz.scm" "dir2/baz.scm")
  95. (try-search path "baz.ss" "dir2/baz.ss")
  96. (try-search path "ugly.scm" "dir3/ugly.scm")
  97. (try-search path "subdir1" #f)
  98. (define extensions '(".ss" ".scm" ""))
  99. (try-search-with-extensions path "foo" extensions "dir1/foo.scm")
  100. (try-search-with-extensions path "bar" extensions "dir1/bar.scm")
  101. (try-search-with-extensions path "baz" extensions "dir2/baz.ss")
  102. (try-search-with-extensions path "ugly.scm" extensions "dir3/ugly.scm")
  103. (try-search-with-extensions path "ugly.ss" extensions #f)
  104. ;; Check that search-path accepts Elisp nil-terminated lists for
  105. ;; PATH and EXTENSIONS.
  106. (with-test-prefix "elisp-nil"
  107. (set-cdr! (last-pair path)
  108. #nil)
  109. (set-cdr! (last-pair extensions) #nil)
  110. (try-search-with-extensions path "ugly.scm" extensions "dir3/ugly.scm")
  111. (try-search-with-extensions path "ugly.ss" extensions #f))
  112. (with-test-prefix "return value of `load'"
  113. (let ((temp-file (in-vicinity temp-dir "foo.scm")))
  114. (call-with-output-file temp-file
  115. (lambda (port)
  116. (write '(+ 2 3) port)
  117. (newline port)))
  118. (pass-if "primitive-load"
  119. (equal? 5 (primitive-load temp-file)))
  120. (let ((temp-compiled-file (in-vicinity temp-dir "foo.go")))
  121. (compile-file temp-file #:output-file temp-compiled-file)
  122. (pass-if "load-compiled"
  123. (equal? 5 (load-compiled temp-compiled-file))))))
  124. (delete-tree temp-dir)