test-cd.scm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. (import
  2. (fslib)
  3. (prefix (commands cd) cmd:)
  4. (shell-state)
  5. ;; receive form
  6. (srfi srfi-8)
  7. (srfi srfi-64))
  8. ;; Example from the docs:
  9. ;; Initialize and give a name to a simple testsuite.
  10. (test-begin "cd-test")
  11. (test-group "cd-test"
  12. (define main-directory (getcwd))
  13. (define test-directory (fsing-join main-directory "test"))
  14. (define integration-directory
  15. (fsing-join test-directory "integration"))
  16. (define default-test-shell-state
  17. (update-shell-state default-shell-state
  18. 'current-working-directory
  19. integration-directory))
  20. (test-assert "cd one layer into"
  21. (begin
  22. ;; setup for integration tests
  23. (chdir integration-directory)
  24. (receive (_ state)
  25. (cmd:cd "one" #:shell-state default-test-shell-state)
  26. (let ([expected-dir (fsing-join integration-directory "one")])
  27. (and (string=? (get-shell-state state 'current-working-directory)
  28. expected-dir)
  29. (string=? (getcwd) expected-dir))))))
  30. (test-assert "cd one layer into - using previous result"
  31. (begin
  32. ;; Setup for integration tests.
  33. ;; Maybe I should not do this? -- Testing whether the Guile REPL
  34. ;; tracks the right directory, when using custom procedures.
  35. (chdir integration-directory)
  36. (receive (_ state)
  37. (cmd:cd #:shell-state default-test-shell-state
  38. #:previous-result '("one"))
  39. (let ([expected-dir (fsing-join integration-directory "one")])
  40. (and (string=? (get-shell-state state 'current-working-directory)
  41. expected-dir)
  42. ;; maybe should not test this?
  43. (string=? (getcwd) expected-dir))))))
  44. (test-assert "cd one layer out"
  45. (let* ([initial-dir (fsing-join integration-directory "one")]
  46. [initial-shell-state
  47. (update-shell-state default-test-shell-state
  48. 'current-working-directory
  49. initial-dir)])
  50. ;; setup for integration tests
  51. ;; TODO: Should I be doing this?
  52. (chdir initial-dir)
  53. (receive (_ state)
  54. (cmd:cd ".." #:shell-state initial-shell-state)
  55. (let ([expected-dir integration-directory])
  56. (and (string=? (get-shell-state state 'current-working-directory)
  57. expected-dir)
  58. (string=? (getcwd) expected-dir))))))
  59. (test-assert "cd multiple layers into"
  60. (begin
  61. ;; setup for integration tests
  62. (chdir integration-directory)
  63. (receive (_ state-after-one-cd)
  64. (cmd:cd "one" #:shell-state default-test-shell-state)
  65. (receive (_ state-after-two-cd)
  66. (cmd:cd "two" #:shell-state state-after-one-cd)
  67. (let ([expected-dir
  68. (fsing-join integration-directory
  69. "one"
  70. "two")])
  71. (and (string=? (get-shell-state state-after-two-cd
  72. 'current-working-directory)
  73. expected-dir)
  74. (string=? (getcwd) expected-dir)))))))
  75. (test-assert "cd multiple layers out"
  76. (let* ([initial-dir (fsing-join integration-directory "one" "two")]
  77. [initial-shell-state
  78. (update-shell-state default-test-shell-state
  79. 'current-working-directory
  80. initial-dir)])
  81. ;; setup for integration tests
  82. ;; TODO: Should I be doing this?
  83. (chdir initial-dir)
  84. (receive (_ state-after-1-cd)
  85. (cmd:cd ".." #:shell-state initial-shell-state)
  86. (receive (_ state-after-2-cd)
  87. (cmd:cd ".." #:shell-state state-after-1-cd)
  88. (let ([expected-dir integration-directory])
  89. (and (string=? (get-shell-state state-after-2-cd
  90. 'current-working-directory)
  91. expected-dir)
  92. (string=? (getcwd) expected-dir)))))))
  93. (test-assert "cd multiple layers out"
  94. (let* ([initial-dir (fsing-join integration-directory "one" "two")]
  95. [initial-shell-state
  96. (update-shell-state default-test-shell-state
  97. 'current-working-directory
  98. initial-dir)])
  99. ;; setup for integration tests
  100. ;; TODO: Should I be doing this?
  101. (chdir initial-dir)
  102. (receive (_ state-after-cd)
  103. (cmd:cd "../.." #:shell-state initial-shell-state)
  104. (let ([expected-dir integration-directory])
  105. (and (string=? (get-shell-state state-after-cd
  106. 'current-working-directory)
  107. expected-dir)
  108. (string=? (getcwd) expected-dir))))))
  109. (test-assert "cd here"
  110. (let* ([initial-dir (fsing-join integration-directory)]
  111. [initial-shell-state default-test-shell-state])
  112. ;; setup for integration tests
  113. ;; TODO: Should I be doing this?
  114. (chdir initial-dir)
  115. (receive (_ state-after-cd)
  116. (cmd:cd "." #:shell-state initial-shell-state)
  117. (let ([expected-dir initial-dir])
  118. (and (string=? (get-shell-state state-after-cd
  119. 'current-working-directory)
  120. expected-dir)
  121. (string=? (getcwd) expected-dir)))))))
  122. ;; Finish the testsuite, and report results.
  123. (test-end "cd-test")