load.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 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. (define temp-dir (data-file-name "load-test.dir"))
  23. (define (create-tree parent tree)
  24. (let loop ((parent parent)
  25. (tree tree))
  26. (if (pair? tree)
  27. (let ((elt (car tree)))
  28. (cond
  29. ;; A string means to create an empty file with that name.
  30. ((string? elt)
  31. (close-port (open-file (string-append parent "/" elt) "w")))
  32. ;; A list means to create a directory, and then create files
  33. ;; within it.
  34. ((pair? elt)
  35. (let ((dirname (string-append parent "/" (car elt))))
  36. (mkdir dirname)
  37. (loop dirname (cdr elt))))
  38. (else
  39. (error "create-tree: bad tree structure")))
  40. (loop parent (cdr tree))))))
  41. (define (delete-tree tree)
  42. (cond
  43. ((file-is-directory? tree)
  44. (let ((dir (opendir tree)))
  45. (let loop ()
  46. (let ((entry (readdir dir)))
  47. (cond
  48. ((member entry '("." ".."))
  49. (loop))
  50. ((not (eof-object? entry))
  51. (let ((name (string-append tree "/" entry)))
  52. (delete-tree name)
  53. (loop))))))
  54. (closedir dir)
  55. (rmdir tree)))
  56. ((file-exists? tree)
  57. (delete-file tree))
  58. (else
  59. (error "delete-tree: can't delete " tree))))
  60. (define (try-search-with-extensions path input extensions expected)
  61. (let ((test-name (call-with-output-string
  62. (lambda (port)
  63. (display "search-path for " port)
  64. (write input port)
  65. (if (pair? extensions)
  66. (begin
  67. (display " with extensions " port)
  68. (write extensions port)))
  69. (display " yields " port)
  70. (write expected port)))))
  71. (let ((result (search-path path input extensions)))
  72. (pass-if test-name
  73. (equal? (if (string? expected)
  74. (string-append temp-dir "/" expected)
  75. expected)
  76. result)))))
  77. (define (try-search path input expected)
  78. (try-search-with-extensions path input '() expected))
  79. ;; Create a bunch of files for use in testing.
  80. (mkdir temp-dir)
  81. (create-tree temp-dir
  82. '(("dir1" "foo.scm" "bar.scm" "ugly.scm.scm"
  83. ("subdir1"))
  84. ("dir2" "foo.scm" "baz.scm" "baz.ss" "ugly.scm.ss")
  85. ("dir3" "ugly.scm" "ugly.ss.scm")))
  86. ;; Try some searches without extensions.
  87. (define path (list
  88. (string-append temp-dir "/dir1")
  89. (string-append temp-dir "/dir2")
  90. (string-append temp-dir "/dir3")))
  91. (try-search path "foo.scm" "dir1/foo.scm")
  92. (try-search path "bar.scm" "dir1/bar.scm")
  93. (try-search path "baz.scm" "dir2/baz.scm")
  94. (try-search path "baz.ss" "dir2/baz.ss")
  95. (try-search path "ugly.scm" "dir3/ugly.scm")
  96. (try-search path "subdir1" #f)
  97. (define extensions '(".ss" ".scm" ""))
  98. (try-search-with-extensions path "foo" extensions "dir1/foo.scm")
  99. (try-search-with-extensions path "bar" extensions "dir1/bar.scm")
  100. (try-search-with-extensions path "baz" extensions "dir2/baz.ss")
  101. (try-search-with-extensions path "ugly.scm" extensions "dir3/ugly.scm")
  102. (try-search-with-extensions path "ugly.ss" extensions #f)
  103. ;; Check that search-path accepts Elisp nil-terminated lists for
  104. ;; PATH and EXTENSIONS.
  105. (with-test-prefix "elisp-nil"
  106. (set-cdr! (last-pair path)
  107. #nil)
  108. (set-cdr! (last-pair extensions) #nil)
  109. (try-search-with-extensions path "ugly.scm" extensions "dir3/ugly.scm")
  110. (try-search-with-extensions path "ugly.ss" extensions #f))
  111. (delete-tree temp-dir)