test_get_absolute_path 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. set -e
  3. set -u
  4. set -o pipefail
  5. source "${BASH_SOURCE%/*}/assertions.sh"
  6. test_get_absolute_path() {
  7. local current_dir=${1:?}
  8. local relative_dir=${2:?}
  9. source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
  10. get_absolute_path "$current_dir" "$relative_dir"
  11. }
  12. test_get_absolute_path_old() {
  13. assert_equal \
  14. "test_get_absolute_path" \
  15. "/glux" \
  16. "$(test_get_absolute_path "/" "glux")"
  17. assert_equal \
  18. "test_get_absolute_path" \
  19. "/foo/bar/." \
  20. "$(test_get_absolute_path "/foo/bar" ".")"
  21. assert_equal \
  22. "test_get_absolute_path" \
  23. "/foo/bar/glux" \
  24. "$(test_get_absolute_path "/foo/bar" "glux")"
  25. }
  26. test_get_absolute_path_new() {
  27. source "${BASH_SOURCE%/*}/../src/utils/functions.sh"
  28. local beg_dir=${1:?}
  29. test "$(get_absolute_path_better "/")" == "/" || \
  30. echo "root path failed"
  31. test "$(get_absolute_path_better ".")" == "$(pwd)" || \
  32. echo "current directory failed"
  33. test "$(get_absolute_path_better "..")" == "$(dirname "$(pwd)")" || \
  34. echo "parent directory failed"
  35. test "$(get_absolute_path_better "./test_get_absolute_path")" == "$(pwd)/test_get_absolute_path" || \
  36. echo "path to this file failed"
  37. test "$(get_absolute_path_better "../README.org")" == "$(dirname "$(pwd)")/README.org" || \
  38. echo "path to sibling file failed"
  39. echo "INFO: Expect output to stderr:"
  40. test "$(get_absolute_path_better)" == "" || echo "no input failed"
  41. test "$(get_absolute_path_better "")" == "" || echo "empty string failed"
  42. test "$(get_absolute_path_better "/probably_no_such_path")" == "" || echo "bad path failed"
  43. if [[ "$beg_dir" != "$(pwd)" ]]; then
  44. echo "WARNING: We changed directories"
  45. fi
  46. }
  47. test_get_absolute_path_main() {
  48. echo "${FUNCNAME[0]}"
  49. test_get_absolute_path_old
  50. test_get_absolute_path_new "$(pwd)"
  51. }
  52. test_get_absolute_path_main