logo

utils-std

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

mktemp.sh (2760B)


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