load.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This program is free software; you can redistribute it and/or modify
  7. ;;;; it under the terms of the GNU General Public License as published by
  8. ;;;; the Free Software Foundation; either version 2, or (at your option)
  9. ;;;; any later version.
  10. ;;;;
  11. ;;;; This program 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
  14. ;;;; GNU General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU General Public License
  17. ;;;; along with this software; see the file COPYING. If not, write to
  18. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;;;; Boston, MA 02110-1301 USA
  20. (define-module (test-suite test-load)
  21. :use-module (test-suite lib)
  22. :use-module (test-suite guile-test))
  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. (if (defined? '%nil)
  105. ;; Check that search-path accepts Elisp nil-terminated lists for
  106. ;; PATH and EXTENSIONS.
  107. (with-test-prefix "elisp-nil"
  108. (set-cdr! (last-pair path) %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. (delete-tree temp-dir)