logo

utils-std

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

chmod.sh (2706B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. plans=17
  5. WD="$(dirname "$0")/../"
  6. target="${WD}/cmd/chmod"
  7. . "${WD}/test-cmd/tap.sh"
  8. tmpfile="${TMPDIR-/tmp}/test_chmod_$(date +%s)"
  9. ref_file="${TMPDIR-/tmp}/test_chmod_$(date +%s).ref"
  10. dash_warn='chmod: warning: (portability) Pass -- before a mode with a leading dash(-) to separate it from options
  11. '
  12. touch "$tmpfile" || exit 1
  13. touch "$ref_file" || exit 1
  14. t '0' "-v 0 $tmpfile" "chmod: Permissions changed from 00644/-rw-r--r-- to 00000/---------- for '${tmpfile}'
  15. "
  16. t '00' "-v 00 $tmpfile" "chmod: Permissions already set to 00000/---------- for '${tmpfile}'
  17. "
  18. t '000' "-v 000 $tmpfile" "chmod: Permissions already set to 00000/---------- for '${tmpfile}'
  19. "
  20. t '0000' "-v 0000 $tmpfile" "chmod: Permissions already set to 00000/---------- for '${tmpfile}'
  21. "
  22. t '0444' "-v 0444 $tmpfile" "chmod: Permissions changed from 00000/---------- to 00444/-r--r--r-- for '${tmpfile}'
  23. "
  24. t '0777' "-v 0777 $tmpfile" "chmod: Permissions changed from 00444/-r--r--r-- to 00777/-rwxrwxrwx for '${tmpfile}'
  25. "
  26. t --exit=1 'invalid 0888' "-v 0888 $tmpfile" "chmod: error: Failed parsing mode '0888': contains digit outside of [0-7]
  27. "
  28. t 'mode:-w,+x' "-v -w,+x $tmpfile" "${dash_warn}chmod: Permissions changed from 00777/-rwxrwxrwx to 00577/-r-xrwxrwx for '${tmpfile}'
  29. "
  30. t 'mode:-r' "-v -r $tmpfile" "${dash_warn}chmod: Permissions changed from 00577/-r-xrwxrwx to 00133/---x-wx-wx for '${tmpfile}'
  31. "
  32. t 'mode:+t' "-v +t $tmpfile" "chmod: Permissions changed from 00133/---x-wx-wx to 01133/---x-wx-wt for '${tmpfile}'
  33. "
  34. t 'mode:-t' "-v -t $tmpfile" "${dash_warn}chmod: Permissions changed from 01133/---x-wx-wt to 00133/---x-wx-wx for '${tmpfile}'
  35. "
  36. t 'ugo+s' "-v ugo+s $tmpfile" "chmod: Permissions changed from 00133/---x-wx-wx to 06133/---s-ws-wx for '${tmpfile}'
  37. "
  38. t 'ugo-s' "-v ugo-s $tmpfile" "chmod: Permissions changed from 06133/---s-ws-wx to 00133/---x-wx-wx for '${tmpfile}'
  39. "
  40. t '__mode:-x,+w' "-v -- -x,+w $tmpfile" "chmod: Permissions changed from 00133/---x-wx-wx to 00222/--w--w--w- for '${tmpfile}'
  41. "
  42. t '__mode:-w' "-v -- -w $tmpfile" "chmod: Permissions changed from 00222/--w--w--w- to 00022/-----w--w- for '${tmpfile}'
  43. "
  44. if grep -q HAS_GETOPT_LONG "${WD}/config.h"; then
  45. t ref_file "-v --reference $ref_file $tmpfile" "chmod: Permissions changed from 00022/-----w--w- to 00644/-rw-r--r-- for '${tmpfile}'
  46. "
  47. t ref_file:repeat "-v --reference $ref_file $tmpfile" "chmod: Permissions already set to 00644/-rw-r--r-- for '${tmpfile}'
  48. "
  49. else
  50. skip ref_file 'Lacks getopt_long'
  51. skip ref_file:repeat 'Lacks getopt_long'
  52. fi
  53. rm -f "$tmpfile" "$ref_file" || exit 1