logo

utils-std

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

chmod.sh (2216B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. plans=13
  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:-x,+w' "-v -- -x,+w $tmpfile" "chmod: Permissions changed from 00133/---x-wx-wx to 00222/--w--w--w- for '${tmpfile}'
  33. "
  34. t '__mode:-w' "-v -- -w $tmpfile" "chmod: Permissions changed from 00222/--w--w--w- to 00022/-----w--w- for '${tmpfile}'
  35. "
  36. if grep -q HAS_GETOPT_LONG "${WD}/config.h"; then
  37. t ref_file "-v --reference $ref_file $tmpfile" "chmod: Permissions changed from 00022/-----w--w- to 00644/-rw-r--r-- for '${tmpfile}'
  38. "
  39. t ref_file:repeat "-v --reference $ref_file $tmpfile" "chmod: Permissions already set to 00644/-rw-r--r-- for '${tmpfile}'
  40. "
  41. else
  42. skip ref_file 'Lacks getopt_long'
  43. skip ref_file:repeat 'Lacks getopt_long'
  44. fi
  45. rm -f "$tmpfile" "$ref_file" || exit 1