heredocs.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. source $(dirname "$0")/test-commons.inc
  3. # go to a writable directory
  4. cd /tmp
  5. # Simple usage, single doc
  6. echo <<-test > sh.doc.test
  7. this is a test
  8. test
  9. if test "$(cat sh.doc.test)" != "this is a test" {
  10. fail "Could not use normal interpolated heredoc"
  11. }
  12. echo <<-'test' > sh.doc.test
  13. this is a test
  14. test
  15. if test "$(cat sh.doc.test)" != "this is a test" {
  16. fail "Could not use normal non-interpolated heredoc"
  17. }
  18. echo <<~test > sh.doc.test
  19. this is a test
  20. test
  21. if test "$(cat sh.doc.test)" != "this is a test" {
  22. fail "Could not use normal dedented heredoc"
  23. }
  24. echo <<~'test' > sh.doc.test
  25. this is a test
  26. test
  27. if test "$(cat sh.doc.test)" != "this is a test" {
  28. fail "Could not use normal non-interpolated dedented heredoc"
  29. }
  30. var=test
  31. echo <<-test > sh.doc.test
  32. this is a $var
  33. test
  34. if test "$(cat sh.doc.test)" != "this is a test" {
  35. fail "Could not use interpolated heredoc with interpolation"
  36. }
  37. echo <<~test > sh.doc.test
  38. this is a $var
  39. test
  40. if test "$(cat sh.doc.test)" != "this is a test" {
  41. fail "Could not use dedented interpolated heredoc with interpolation"
  42. }
  43. # Multiple heredocs
  44. echo <<-test <<-test2 > sh.doc.test
  45. contents for test
  46. test
  47. contents for test2
  48. test2
  49. if test "$(cat sh.doc.test)" != "contents for test contents for test2" {
  50. fail "Could not use two heredocs"
  51. }
  52. # Why would you do this you crazy person?
  53. if test "$(echo <<~text)" != "test" {
  54. test
  55. text
  56. fail "Could not use heredocs in a weird place"
  57. }
  58. # Now let's try something _really_ weird!
  59. if test "$(echo <<~test1)" != "$(echo <<~test2)" { fail "The parser forgot about heredocs after a block, oops" }
  60. test
  61. test1
  62. test
  63. test2
  64. rm -f sh.doc.test
  65. # return to original directory
  66. cd -
  67. pass