A JavaScript development environment for Emacs https://indium.readthedocs.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.6 KiB

  1. ;;; indium-workspace-test.el --- Tests for indium-workspace.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Nicolas Petton
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (require 'map)
  17. (require 'buttercup)
  18. (require 'assess)
  19. (require 'indium-workspace)
  20. (defvar indium-workspace--test-fs
  21. '(".indium"
  22. ("js" ("app.js")))
  23. "Fake filesystem used in workspace tests.")
  24. (describe "Reading and saving workspace directory list"
  25. (it "can add a workspace directory"
  26. (let ((indium-workspaces nil))
  27. (indium-workspace--add-directory "foobar")
  28. (expect indium-workspaces :to-equal '("foobar"))))
  29. (it "can save and read the workspace file"
  30. (with-temp-workspace-file
  31. (let ((indium-workspaces nil))
  32. (indium-workspace--add-directory "foobar")
  33. (indium-workspace--save-workspaces-file)
  34. (setq indium-workspaces nil)
  35. (indium-workspace--read-workspaces-file)
  36. (expect indium-workspaces :to-equal '("foobar")))))
  37. (it "should read the workspace file when prompting for a workspace"
  38. (let ((indium-workspace-use-workspace-file t))
  39. (spy-on 'indium-workspace--read-workspaces-file)
  40. (spy-on 'y-or-n-p)
  41. (spy-on 'completing-read)
  42. (indium-workspace-read)
  43. (expect #'indium-workspace--read-workspaces-file
  44. :to-have-been-called)))
  45. (it "should write the workspace file when prompting for a workspace"
  46. (let ((indium-workspace-use-workspace-file t))
  47. (spy-on 'indium-workspace--read-workspaces-file)
  48. (spy-on 'indium-workspace--save-workspaces-file)
  49. (spy-on 'indium-workspace-root :and-return-value 'foo)
  50. (spy-on 'y-or-n-p)
  51. (spy-on 'completing-read)
  52. (indium-workspace-read)
  53. (expect #'indium-workspace--save-workspaces-file
  54. :to-have-been-called)))
  55. (it "should not read or write the workspace file if `indium-workspace-use-workspace-file' is nil"
  56. (let ((indium-workspace-use-workspace-file nil))
  57. (spy-on 'indium-workspace--read-workspaces-file)
  58. (spy-on 'indium-workspace--save-workspaces-file)
  59. (spy-on 'indium-workspace-root :and-return-value 'foo)
  60. (spy-on 'y-or-n-p)
  61. (spy-on 'completing-read)
  62. (indium-workspace-read)
  63. (expect #'indium-workspace--save-workspaces-file
  64. :not :to-have-been-called)
  65. (expect #'indium-workspace--read-workspaces-file
  66. :not :to-have-been-called)))
  67. (it "should not signal an error if the workspace file does not exist"
  68. (let ((indium-workspace-use-workspace-file t)
  69. (indium-workspace-file "foobarbaz"))
  70. (expect #'indium-workspace--read-workspaces-file
  71. :not :to-throw))))
  72. (describe "Looking up files"
  73. (it "cannot lookup file when no workspace it set"
  74. (spy-on 'indium-workspace-root :and-return-value nil)
  75. (spy-on 'indium-repl-get-buffer :and-return-value (current-buffer))
  76. (expect (indium-workspace-lookup-file "http://localhost:9229/foo/bar")
  77. :to-be nil))
  78. (it "can lookup file with .indium marker file"
  79. (assess-with-filesystem indium-workspace--test-fs
  80. (spy-on 'indium-repl-get-buffer :and-return-value (current-buffer))
  81. (expect (indium-workspace-lookup-file "http://localhost:9229/js/app.js")
  82. :to-equal (expand-file-name "js/app.js"))))
  83. (it "should ignore query strings from urls when looking up files"
  84. (assess-with-filesystem indium-workspace--test-fs
  85. (spy-on 'indium-repl-get-buffer :and-return-value (current-buffer))
  86. (expect (indium-workspace-lookup-file "http://localhost:9229/js/app.js?foo=bar")
  87. :to-equal (expand-file-name "js/app.js"))))
  88. (it "cannot find a file that does not exist"
  89. (assess-with-filesystem indium-workspace--test-fs
  90. (spy-on 'indium-repl-get-buffer :and-return-value (current-buffer))
  91. (expect (indium-workspace-lookup-file "http://localhost:9229/non-existant-file-name.js")
  92. :to-be nil))))
  93. (describe "Looking up files safely"
  94. (it "should fallback to the url when no file can be found"
  95. (assess-with-filesystem indium-workspace--test-fs
  96. (spy-on 'indium-repl-get-buffer :and-return-value (current-buffer))
  97. (let ((url "http://localhost:9229/non-existant-file-name.js"))
  98. (expect (indium-workspace-lookup-file-safe url)
  99. :to-equal url))))
  100. (it "can lookup files that exist"
  101. (assess-with-filesystem indium-workspace--test-fs
  102. (spy-on 'indium-repl-get-buffer :and-return-value (current-buffer))
  103. (let ((url "http://localhost:9229/js/app.js")
  104. (file (expand-file-name "js/app.js")))
  105. (expect (indium-workspace-lookup-file-safe url)
  106. :to-equal file)))))
  107. (describe "Making workspace urls from file names"
  108. (it "cannot make a url when no workspace is set"
  109. (with-indium-connection (make-indium-connection :url "http://localhost:9229")
  110. (expect (indium-workspace-make-url "js/app.js")
  111. :to-be nil)))
  112. (it "can make workspace urls"
  113. (with-indium-connection (make-indium-connection :url "http://localhost:9229")
  114. (assess-with-filesystem indium-workspace--test-fs
  115. (expect (indium-workspace-make-url "js/app.js")
  116. :to-equal "http://localhost:9229/js/app.js"))))
  117. (it "should strip query strings from computing urls"
  118. (with-indium-connection (make-indium-connection :url "http://localhost:9229?foo=bar")
  119. (assess-with-filesystem indium-workspace--test-fs
  120. (expect (indium-workspace-make-url "js/app.js")
  121. :to-equal "http://localhost:9229/js/app.js"))))
  122. (it "should strip paths based on the .indium marker when computing urls"
  123. (with-indium-connection (make-indium-connection :url "http://localhost:9229/foo/bar")
  124. (assess-with-filesystem indium-workspace--test-fs
  125. (expect (indium-workspace-make-url "js/app.js")
  126. :to-equal "http://localhost:9229/js/app.js"))))
  127. (it "should use the file path if the connection uses nodejs when computing urls"
  128. (with-indium-connection (make-indium-connection)
  129. (map-put (indium-current-connection-props)
  130. 'nodejs t)
  131. (assess-with-filesystem indium-workspace--test-fs
  132. (let ((file (expand-file-name "js/app.js")))
  133. (expect (indium-workspace-make-url file)
  134. :to-equal (expand-file-name "js/app.js")))))))
  135. (describe "File protocol"
  136. (it "can lookup files using the file:// protocol"
  137. (assess-with-filesystem indium-workspace--test-fs
  138. (with-indium-connection (make-indium-connection :url "file:///foo/bar/index.html")
  139. (let* ((file (expand-file-name "js/app.js"))
  140. (url (format "file://%s" file)))
  141. (expect (indium-workspace-lookup-file url)
  142. :to-equal file)))))
  143. (it "can make a url when using the file protocol"
  144. (assess-with-filesystem indium-workspace--test-fs
  145. (with-indium-connection (make-indium-connection :url "file:///foo/bar/index.html")
  146. (let* ((file (expand-file-name "js/app.js")))
  147. (expect (indium-workspace-make-url file)
  148. :to-equal (format "file://%s" file)))))))
  149. (provide 'indium-workspace-test)
  150. ;;; indium-workspace-test.el ends here