logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

mktemp.sh (2341B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. target="$(dirname "$0")/../cmd/mktemp"
  5. plans=14
  6. . "$(dirname "$0")/tap.sh"
  7. t_mktemp()
  8. {
  9. count=$((count+1))
  10. name="$1" ; shift
  11. # Append a final slash so sh(1) doesn't trims final newlines
  12. out=$("${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)
  13. ret=$?
  14. out=${out%/}
  15. test -f "${out%
  16. }"
  17. test_ret="$?"
  18. rm -f "${out%
  19. }"
  20. if [ "$ret" = 0 ] && [ "$test_ret" = 0 ]; then
  21. printf 'ok %d - %s\n' "$count" "$name"
  22. else
  23. printf 'not ok %d - %s\n' "$count" "$name"
  24. printf '# exit code: %d\n' "$ret"
  25. printf '# test -f => %d\n' "$test_ret"
  26. err=1
  27. fi
  28. printf "$out" | sed -e 's;^;# ;'
  29. }
  30. t_mkdtemp()
  31. {
  32. count=$((count+1))
  33. name="$1" ; shift
  34. # Append a final slash so sh(1) doesn't trims final newlines
  35. out=$("${target?}" "$@" 2>&1;r=$?;printf %s /;exit $r)
  36. ret=$?
  37. out=${out%/}
  38. test -d "${out%
  39. }"
  40. test_ret="$?"
  41. rm -fr "${out%
  42. }"
  43. if [ "$ret" = 0 ] && [ "$test_ret" = 0 ]; then
  44. printf 'ok %d - %s\n' "$count" "$name"
  45. else
  46. printf 'not ok %d - %s\n' "$count" "$name"
  47. printf '# exit code: %d\n' "$ret"
  48. printf '# test -d => %d\n' "$test_ret"
  49. err=1
  50. fi
  51. printf "$out" | sed -e 's;^;# ;'
  52. }
  53. # TODO: Figure a way to do harsher tests, for example testing patterns, directory contents, …
  54. t_mktemp noargs
  55. t_mktemp template template.XXXXXX
  56. t_mktemp tmpdir -t
  57. t_mktemp tmpdir -t template.XXXXXX
  58. t_mkdtemp dir -d
  59. t_mkdtemp dir_template -d template.XXXXXX
  60. t_mkdtemp dir_tmpdir -dt
  61. t --exit=1 templ2 'foo bar' 'mktemp: error: Only one template argument is supported, got 2
  62. '
  63. cmd_mktemp_u() {
  64. if tmpfile=$("$target" "$@"); then
  65. if test -e "$tmpfile"; then
  66. printf '# Unexpectedly created a file:\n' "$tmpfile"
  67. ls -l "$tmpfile" 2>&1 | sed 's;^;# ;'
  68. rm -fr "$tmpfile" 2>&1 | sed 's;^;# ;'
  69. return 1
  70. fi
  71. return 0
  72. else
  73. return 1
  74. fi
  75. }
  76. t_cmd unsafe:file '' cmd_mktemp_u -u
  77. t_cmd unsafe:file_template '' cmd_mktemp_u -u template.XXXXXX
  78. t_args --exit=1 unsafe:file_template_line 'mktemp: error: Invalid character (newline) in template
  79. ' -u 'template
  80. XXXXXX'
  81. t_cmd unsafe:dir '' cmd_mktemp_u -u -d
  82. t_cmd unsafe:dir_template '' cmd_mktemp_u -u -d template.XXXXXX
  83. t_args --exit=1 unsafe:dir_template_line 'mktemp: error: Invalid character (newline) in template
  84. ' -u -d 'template
  85. XXXXXX'