Blame specs/posix_compat_spec.yaml

Packit 437b5e
specify posix.compat:
Packit 437b5e
- describe chmod:
Packit 437b5e
  - before:
Packit 437b5e
      chmod, stat = posix.chmod, posix.stat
Packit 437b5e
      touch "xxx"
Packit 437b5e
      chmod ("xxx", "rwxr-x---")
Packit 437b5e
  - after:
Packit 437b5e
      os.remove "xxx"
Packit 437b5e
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      examples {
Packit 437b5e
        ["it diagnoses argument #2 invalid mode"] = function ()
Packit 437b5e
          expect (chmod (".", "g+vv")).to_raise.any_of {
Packit 437b5e
            "bad argument #2 to '?' (bad mode)",
Packit 437b5e
            "bad argument #2 to 'chmod' (bad mode)",
Packit 437b5e
          }
Packit 437b5e
        end
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
      badargs.diagnose (chmod, "chmod (string, string)")
Packit 437b5e
Packit 437b5e
  - it sets file mode with longhand mode string:
Packit 437b5e
      mode = "rw---xr--"
Packit 437b5e
      expect (Emsg (chmod ("xxx", mode))).to_be ""
Packit 437b5e
      expect (stat ("xxx", "mode")).to_be (mode)
Packit 437b5e
  - "it sets attributes with '='":
Packit 437b5e
      expect (Emsg (chmod ("xxx", "o=w"))).to_be ""
Packit 437b5e
      expect (stat ("xxx", "mode")).to_be "rwxr-x-w-"
Packit 437b5e
  - "it adds attributes with '+'":
Packit 437b5e
      expect (Emsg (chmod ("xxx", "g+w"))).to_be ""
Packit 437b5e
      expect (stat ("xxx", "mode")).to_be "rwxrwx---"
Packit 437b5e
  - "it removes attributes with '-'":
Packit 437b5e
      expect (Emsg (chmod ("xxx",  "u-r"))).to_be ""
Packit 437b5e
      expect (stat ("xxx", "mode")).to_be "-wxr-x---"
Packit 437b5e
  - it accepts comma separated attribute specifications:
Packit 437b5e
      expect (Emsg (chmod ("xxx", "a+x,g+w,u-w"))).to_be ""
Packit 437b5e
      expect (stat ("xxx", "mode")).to_be "r-xrwx--x"
Packit 437b5e
  - it diagnoses missing files:
Packit 437b5e
      os.remove "xxx"
Packit 437b5e
      expect (Emsg (chmod ("xxx", "a=rwx"))).to_contain "No such file or directory"
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe clock_getres:
Packit 437b5e
  - before:
Packit 437b5e
      clock_getres = posix.clock_getres
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      if clock_getres then
Packit 437b5e
        badargs.diagnose (clock_getres, "clock_getres (?string)")
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe clock_gettime:
Packit 437b5e
  - before:
Packit 437b5e
      clock_gettime = posix.clock_gettime
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      if clock_gettime then
Packit 437b5e
        badargs.diagnose (clock_gettime, "clock_gettime (?string)")
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe creat:
Packit 437b5e
  - before:
Packit 437b5e
      creat = posix.creat
Packit 437b5e
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      badargs.diagnose (creat, "creat (string, string)")
Packit 437b5e
Packit 437b5e
      examples {
Packit 437b5e
        ["it diagnoses argument #2 invalid mode"] = function ()
Packit 437b5e
          expect (creat ("not/existing", "g+vv")).to_raise.any_of {
Packit 437b5e
            "bad argument #2 to '?' (bad mode)",
Packit 437b5e
            "bad argument #2 to 'creat' (bad mode)",
Packit 437b5e
          }
Packit 437b5e
        end
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe fadvise:
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      if posix.fadvise then
Packit 437b5e
        badargs.diagnose (posix.fadvise, "fadvise (file, int, int, int)")
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe fnmatch:
Packit 437b5e
  - before:
Packit 437b5e
      fnmatch, FNM_PATHNAME, FNM_PERIOD =
Packit 437b5e
        posix.fnmatch, posix.FNM_PATHNAME, posix.FNM_PERIOD
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (fnmatch, "fnmatch (string, string, ?int)")
Packit 437b5e
Packit 437b5e
  - it matches a file path against itself:
Packit 437b5e
      expect (fnmatch ("test", "test")).to_be (true)
Packit 437b5e
  - "it matches * against any filename characters":
Packit 437b5e
      expect (fnmatch ("tes*", "test")).to_be (true)
Packit 437b5e
      expect (fnmatch ("tes*", "test2")).to_be (true)
Packit 437b5e
      expect (fnmatch ("*t*", "test")).to_be (true)
Packit 437b5e
  - "it matches ? against a single filename character":
Packit 437b5e
      expect (fnmatch ("tes?", "test")).to_be (true)
Packit 437b5e
      expect (fnmatch ("t???", "test")).to_be (true)
Packit 437b5e
      expect (fnmatch ("tes?", "tes")).to_be (false)
Packit 437b5e
      expect (fnmatch ("tes?", "test2")).to_be (false)
Packit 437b5e
  - "it doesn't match path separators with FNM_PATHNAME":
Packit 437b5e
      expect (fnmatch ("*test", "/test")).to_be (true)
Packit 437b5e
      expect (fnmatch ("*test", "/test", FNM_PATHNAME)).to_be (false)
Packit 437b5e
  - "it doesn't match periods with FNM_PERIOD":
Packit 437b5e
      expect (fnmatch ("*test", ".test")).to_be (true)
Packit 437b5e
      expect (fnmatch ("*test", ".test", FNM_PERIOD)).to_be (false)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe getgroup:
Packit 437b5e
  - before:
Packit 437b5e
      getgrgid, getgroup, getegid =
Packit 437b5e
        posix.getgrgid, posix.getgroup, posix.getegid
Packit 437b5e
      groot = getgrgid (0).gr_name
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (posix.getgroup, "getgroup (?string|int)")
Packit 437b5e
Packit 437b5e
  - it returns a table for an existing group:
Packit 437b5e
      expect (type (getgroup (groot))).to_be "table"
Packit 437b5e
  - it fetches current group by default:
Packit 437b5e
      expect (getgroup ()).to_equal (getgroup (getegid ()))
Packit 437b5e
  - it fetches a group by gid:
Packit 437b5e
      expect (getgroup (0).name).to_be (groot)
Packit 437b5e
      expect (getgroup (0).gid).to_be (0)
Packit 437b5e
      expect (type (getgroup (0).mem)).to_be "table"
Packit 437b5e
  - it fetches a group by name:
Packit 437b5e
      expect (getgroup (groot).name).to_be (groot)
Packit 437b5e
      expect (getgroup (groot).gid).to_be (0)
Packit 437b5e
      expect (type (getgroup (groot).mem)).to_be "table"
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe getpasswd:
Packit 437b5e
  - before:
Packit 437b5e
      getenv, getgid, getpasswd, getuid =
Packit 437b5e
        posix.getenv, posix.getgid, posix.getpasswd, posix.getuid
Packit 437b5e
      user = getpasswd ((getenv "USER"), "name")
Packit 437b5e
      root = getpasswd (0, "name")
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
    - before:
Packit 437b5e
        getpasswd, typeerrors = init (posix, "getpasswd")
Packit 437b5e
Packit 437b5e
    - 'it diagnoses argument #1 type not string, int or nil':
Packit 437b5e
        expect (getpasswd (false)).
Packit 437b5e
          to_raise.any_of (typeerrors (1, "?string|int", "boolean"))
Packit 437b5e
Packit 437b5e
  - it fetches the user uid:
Packit 437b5e
      expect (getpasswd (user).uid).to_be (getuid ())
Packit 437b5e
      expect (getpasswd (root, "uid")).to_be (0)
Packit 437b5e
  - it fetches the user name:
Packit 437b5e
      expect (getpasswd (user).name).to_be (user)
Packit 437b5e
      expect (getpasswd (0, "name")).to_be (root)
Packit 437b5e
      expect (getpasswd (root, "name")).to_be (root)
Packit 437b5e
  - it fetches the user gid:
Packit 437b5e
      expect (getpasswd (user).gid).to_be (getgid ())
Packit 437b5e
      expect (getpasswd (0, "gid")).to_be (0)
Packit 437b5e
      expect (getpasswd (root, "gid")).to_be (0)
Packit 437b5e
  - it fetches the user password:
Packit 437b5e
      expect (getpasswd (user).passwd).to_match.any_of {"x", "%*+"}
Packit 437b5e
      expect (getpasswd (0, "passwd")).to_match.any_of {"x", "%*+"}
Packit 437b5e
      expect (getpasswd (root, "passwd")).to_match.any_of {"x", "%*+"}
Packit 437b5e
  - it fetches the user home directory:
Packit 437b5e
      expect (getpasswd (user, "dir")).to_be (getenv "HOME")
Packit 437b5e
  - it fetches the user shell:
Packit 437b5e
      expect (getpasswd (user, "shell")).to_be (getenv "SHELL")
Packit 437b5e
  - it fetches a subtable of named fields:
Packit 437b5e
      expect ({getpasswd (user, "name", "shell", "dir")}).
Packit 437b5e
        to_equal {user, getenv "SHELL", getenv "HOME"}
Packit 437b5e
  - it fetches everything without an argument:
Packit 437b5e
      t = getpasswd (user)
Packit 437b5e
      for k, v in pairs (t) do
Packit 437b5e
        expect (t[k]).to_be (getpasswd (user, k))
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe getpid:
Packit 437b5e
  - before:
Packit 437b5e
      getpid, typeerrors = init (posix, "getpid")
Packit 437b5e
Packit 437b5e
  # posix.getpid takes an optional string or table as its first
Packit 437b5e
  # argument, followed by zero or more strings only if the first
Packit 437b5e
  # argument was a string; since we can't express that with
Packit 437b5e
  # `badargs.diagnose` do it all manually again...
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
    - 'it diagnoses argument #1 type not table, string or nil':
Packit 437b5e
        expect (getpid (false)).to_raise.
Packit 437b5e
          any_of (typeerrors (1, "?table|string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #1 string invalid': |
Packit 437b5e
        expect (getpid ("fubar")).to_raise.any_of {
Packit 437b5e
          "bad argument #1 to '?' (invalid option 'fubar')",
Packit 437b5e
          "bad argument #1 to 'getpid' (invalid option 'fubar')",
Packit 437b5e
        }
Packit 437b5e
    - 'it diagnoses argument #2 type not string':
Packit 437b5e
        expect (getpid ("ppid", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (2, "string", "boolean"))
Packit 437b5e
    - it diagnoses too many arguments:
Packit 437b5e
        expect (getpid ({}, false)).to_raise.any_of (typeerrors (2))
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe getrlimit:
Packit 437b5e
  - before:
Packit 437b5e
      getrlimit = posix.getrlimit
Packit 437b5e
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      examples {
Packit 437b5e
        ["it diagnoses argument #1 invalid option"] = function ()
Packit 437b5e
          expect (getrlimit ("fubar")).to_raise.any_of {
Packit 437b5e
            "bad argument #1 to '?' (invalid option 'fubar')",
Packit 437b5e
            "bad argument #1 to 'getrlimit' (invalid option 'fubar')",
Packit 437b5e
          }
Packit 437b5e
        end
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
      badargs.diagnose (getrlimit, "getrlimit (string)")
Packit 437b5e
Packit 437b5e
  - it fetches resource limits for a process:
Packit 437b5e
      for _, rc in pairs {"core", "cpu", "data", "fsize", "nofile", "stack", "as"}
Packit 437b5e
      do
Packit 437b5e
        cur, max = getrlimit (rc)
Packit 437b5e
        expect (type (cur)).to_be "number"
Packit 437b5e
        expect (type (max)).to_be "number"
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe gettimeofday:
Packit 437b5e
  - before:
Packit 437b5e
      gettimeofday = posix.gettimeofday
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (gettimeofday, "gettimeofday ()")
Packit 437b5e
Packit 437b5e
  - it fetches the current epoch time:
Packit 437b5e
      t, epoch = gettimeofday (), posix.time ()
Packit 437b5e
      expect (t.sec).to_be (epoch)
Packit 437b5e
      expect (type (t.usec)).to_be "number"
Packit 437b5e
      expect (t.usec >= 0).to_be (true)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe gmtime:
Packit 437b5e
  - before:
Packit 437b5e
      gmtime = posix.gmtime
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (gmtime, "gmtime (?int)")
Packit 437b5e
Packit 437b5e
  - it returns a table:
Packit 437b5e
      expect (prototype (gmtime (now))).to_be "table"
Packit 437b5e
  - it fetches broken-down time values:
Packit 437b5e
      t = gmtime (now)
Packit 437b5e
      fields = {"sec", "min", "hour", "day", "monthday", "month", "year",
Packit 437b5e
                "weekday", "yearday", "is_dst"}
Packit 437b5e
      expect (t).to_contain.a_permutation_of (fields)
Packit 437b5e
      for _, field in pairs (fields) do
Packit 437b5e
        if field == "is_dst" then
Packit 437b5e
          expect (type (t.is_dst)).to_be "boolean"
Packit 437b5e
        else
Packit 437b5e
          expect (type (t[field])).to_be "number"
Packit 437b5e
          expect (t[field] >= 0).to_be (true)
Packit 437b5e
        end
Packit 437b5e
      end
Packit 437b5e
  - it returns a month in the range 1-12:
Packit 437b5e
      # A recent December afternoon in epoch seconds...
Packit 437b5e
      expect (gmtime (1418734089).month).to_be (12)
Packit 437b5e
      t = gmtime (now)
Packit 437b5e
      expect (t.month >= 1 and t.month <= 12).to_be (true)
Packit 437b5e
  - it returns full year:
Packit 437b5e
      expect (gmtime (now).year > 2000).to_be (true)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe hostid:
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (posix.hostid, "()")
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe isgraph:
Packit 437b5e
  - before:
Packit 437b5e
      isgraph = posix.isgraph
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (isgraph, "isgraph (string)")
Packit 437b5e
Packit 437b5e
  - it returns true for successful tests:
Packit 437b5e
      expect (isgraph 'a').to_be (true)
Packit 437b5e
  - it returns false for failed tests:
Packit 437b5e
      expect (isgraph ' ').to_be (false)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe isprint:
Packit 437b5e
  - before:
Packit 437b5e
      isprint = posix.isprint
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (isprint, "isprint (string)")
Packit 437b5e
Packit 437b5e
  - it returns true for successful tests:
Packit 437b5e
      expect (isprint 'a').to_be (true)
Packit 437b5e
  - it returns false for failed tests:
Packit 437b5e
      expect (isprint (string.char (0))).to_be (false)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe localtime:
Packit 437b5e
  - before:
Packit 437b5e
      localtime = posix.localtime
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (localtime, "localtime (?int)")
Packit 437b5e
Packit 437b5e
  - it returns a table:
Packit 437b5e
      expect (prototype (localtime (now))).to_be "table"
Packit 437b5e
  - it fetches broken-down time values:
Packit 437b5e
      t = localtime (now)
Packit 437b5e
      fields = {"sec", "min", "hour", "day", "monthday", "month", "year",
Packit 437b5e
                "weekday", "yearday", "is_dst"}
Packit 437b5e
      expect (t).to_contain.a_permutation_of (fields)
Packit 437b5e
      for _, field in pairs (fields) do
Packit 437b5e
        if field == "is_dst" then
Packit 437b5e
          expect (type (t.is_dst)).to_be "boolean"
Packit 437b5e
        else
Packit 437b5e
          expect (type (t[field])).to_be "number"
Packit 437b5e
          expect (t[field] >= 0).to_be (true)
Packit 437b5e
        end
Packit 437b5e
      end
Packit 437b5e
  - it returns a month in the range 1-12:
Packit 437b5e
      # A recent December afternoon in epoch seconds...
Packit 437b5e
      expect (localtime (1418734089).month).to_be (12)
Packit 437b5e
      t = localtime (now)
Packit 437b5e
      expect (t.month >= 1 and t.month <= 12).to_be (true)
Packit 437b5e
  - it returns years since 1900:
Packit 437b5e
      expect (localtime (now).year > 2000).to_be (true)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe mkdir:
Packit 437b5e
  - before:
Packit 437b5e
      dir = posix.mkdtemp (template)
Packit 437b5e
      chdir, mkdir = posix.chdir, posix.mkdir
Packit 437b5e
      cwd = posix.getcwd ()
Packit 437b5e
Packit 437b5e
  - after:
Packit 437b5e
      chdir (cwd)
Packit 437b5e
      rmtmp (dir)
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (mkdir, "mkdir (string)")
Packit 437b5e
Packit 437b5e
  - it creates the named directory:
Packit 437b5e
      expect (Emsg (mkdir (dir .. "/subdir"))).not_to_contain "exists"
Packit 437b5e
      expect (Emsg (chdir (dir .. "/subdir"))).not_to_contain "No such flle or directory"
Packit 437b5e
  - it diagnoses already existing directory:
Packit 437b5e
      expect (Emsg (mkdir (dir))).to_contain "exists"
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe mkfifo:
Packit 437b5e
  - before:
Packit 437b5e
      dir    = posix.mkdtemp (template)
Packit 437b5e
      mkfifo = posix.mkfifo
Packit 437b5e
Packit 437b5e
  - after:
Packit 437b5e
      rmtmp (dir)
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (mkfifo, "mkfifo (string)")
Packit 437b5e
Packit 437b5e
  - it creates the named fifo:
Packit 437b5e
      expect (Emsg (mkfifo (dir .. "/fifo"))).not_to_contain "exists"
Packit 437b5e
      expect (posix.stat (dir .. "/fifo").type).to_be "fifo"
Packit 437b5e
  - it diagnoses already existing fifo:
Packit 437b5e
      expect (Emsg (mkfifo (dir))).to_contain "exists"
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe mktime:
Packit 437b5e
  - before:
Packit 437b5e
      localtime, mktime, time = posix.localtime, posix.mktime, posix.time
Packit 437b5e
      epoch = time ()
Packit 437b5e
      t = localtime (epoch)
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (mktime, "mktime (?table)")
Packit 437b5e
Packit 437b5e
  - it returns an epoch time:
Packit 437b5e
      expect (prototype (mktime (t))).to_be "number"
Packit 437b5e
  - it is the inverse of localtime:
Packit 437b5e
      expect (mktime (t)).to_be (epoch)
Packit 437b5e
  - it defaults to current time: |
Packit 437b5e
      -- avoid a race by discarding the unit seconds
Packit 437b5e
      expect (mktime () / 10).to_be (epoch / 10)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe msgget:
Packit 437b5e
  - before:
Packit 437b5e
      msgget = posix.msgget
Packit 437b5e
      EEXIST, IPC_CREAT, IPC_EXCL = posix.EEXIST, posix.IPC_CREAT, posix.IPC_EXCL
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (msgget, "msgget (int, ?int, ?string)")
Packit 437b5e
Packit 437b5e
  - it creates a queue:
Packit 437b5e
      if msgget then
Packit 437b5e
        modestr = "rwxrwxrwx"
Packit 437b5e
        mq, err, errnum = msgget (100, bor (IPC_CREAT, IPC_EXCL), modestr)
Packit 437b5e
        if errnum == EEXIST then
Packit 437b5e
          mq, err = msgget (100, 0, modestr)
Packit 437b5e
        end
Packit 437b5e
        expect (mq).not_to_be (nil)
Packit 437b5e
        expect (err).to_be (nil)
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
  - describe nanosleep:
Packit 437b5e
    - context with bad arguments:
Packit 437b5e
        badargs.diagnose (posix.nanosleep, "nanosleep (int, int)")
Packit 437b5e
Packit 437b5e
    - it returns an integer:
Packit 437b5e
        expect (posix.nanosleep (0, 10)).to_be (0)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe open:
Packit 437b5e
  # posix.open ignores the mode argument if flags does not include
Packit 437b5e
  # O_CREAT, which `badargs.diagnose` can't express; ergo manual
Packit 437b5e
  # checks here...
Packit 437b5e
  - before:
Packit 437b5e
      O_CREAT = posix.O_CREAT
Packit 437b5e
      open, typeerrors = init (posix, "open")
Packit 437b5e
Packit 437b5e
  - 'it diagnoses missing argument #1':
Packit 437b5e
      expect (open ()).to_raise.any_of (typeerrors (1, "string"))
Packit 437b5e
  - 'it diagnoses argument #1 type not string':
Packit 437b5e
      expect (open (false)).to_raise.any_of (typeerrors (1, "string", "boolean"))
Packit 437b5e
  - 'it diagnoses missing argument #2':
Packit 437b5e
      expect (open ("not/existing")).to_raise.any_of (typeerrors (2, "int"))
Packit 437b5e
  - 'it diagnoses argument #2 type not int':
Packit 437b5e
      expect (open ("not/existing", false)).
Packit 437b5e
        to_raise.any_of (typeerrors (2, "int", "boolean"))
Packit 437b5e
  - 'it diagnoses missing argument #3':
Packit 437b5e
      expect (open ("not/existing", O_CREAT)).
Packit 437b5e
        to_raise.any_of (typeerrors (3, "string"))
Packit 437b5e
  - 'it diagnoses argument #3 type not string':
Packit 437b5e
      expect (open ("not/existing", O_CREAT, false)).
Packit 437b5e
        to_raise.any_of (typeerrors (3, "string", "boolean"))
Packit 437b5e
  - 'it diagnoses argument #3 invalid mode': |
Packit 437b5e
      expect (open ("not/existing", O_CREAT, "g+vv")).to_raise.any_of {
Packit 437b5e
         "bad argument #3 to '?' (bad mode)",
Packit 437b5e
         "bad argument #3 to 'open' (bad mode)",
Packit 437b5e
      }
Packit 437b5e
  - 'it diagnoses too many arguments':
Packit 437b5e
      expect (open ("not/existing", -1, "o-s", false)).to_raise.any_of (typeerrors (4))
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe pathconf:
Packit 437b5e
  - before:
Packit 437b5e
      pathconf, typeerrors = init (posix, "pathconf")
Packit 437b5e
Packit 437b5e
  # posix.pathconf takes an optional string or table as its second
Packit 437b5e
  # argument, followed by zero or more strings only if the second
Packit 437b5e
  # argument was a string; since we can't express that with
Packit 437b5e
  # `badargs.diagnose` do it all manually again...
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
    - 'it diagnoses argument #1 type not string':
Packit 437b5e
        expect (pathconf (false)).
Packit 437b5e
          to_raise.any_of (typeerrors (1, "?string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #2 type not table, string or nil':
Packit 437b5e
        expect (pathconf (".", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (2, "?table|string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #2 string invalid': |
Packit 437b5e
        expect (pathconf (".", "fubar")).to_raise.any_of {
Packit 437b5e
          "bad argument #2 to '?' (invalid option 'fubar')",
Packit 437b5e
          "bad argument #2 to 'pathconf' (invalid option 'fubar')",
Packit 437b5e
        }
Packit 437b5e
    - 'it diagnoses argument #3 type not string':
Packit 437b5e
        expect (pathconf (".", "NAME_MAX", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (3, "string", "boolean"))
Packit 437b5e
    - it diagnoses too many arguments:
Packit 437b5e
        expect (pathconf (".", {}, false)).to_raise.any_of (typeerrors (3))
Packit 437b5e
Packit 437b5e
  - it returns whether chown can be used on the given file:
Packit 437b5e
      expect (type (pathconf ().CHOWN_RESTRICTED)).to_be "number"
Packit 437b5e
      expect (pathconf (".", "CHOWN_RESTRICTED") >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum number of links to the given file:
Packit 437b5e
      expect (type (pathconf ().LINK_MAX)).to_be "number"
Packit 437b5e
      expect (pathconf (".", "LINK_MAX") >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum formatted line input length for a tty: |
Packit 437b5e
      -- not passing a tty, so should return -1
Packit 437b5e
      expect (type (pathconf ().MAX_CANON)).to_be "number"
Packit 437b5e
      pending "issue #102"
Packit 437b5e
      expect (pathconf (".", "MAX_CANON")).to_be (-1)
Packit 437b5e
  - it fetches the maximum raw line input length for a tty: |
Packit 437b5e
      -- not passing a tty, so should return -1
Packit 437b5e
      expect (type (pathconf ().MAX_INPUT)).to_be "number"
Packit 437b5e
      pending "issue #102"
Packit 437b5e
      expect (pathconf (".", "MAX_INPUT")).to_be (-1)
Packit 437b5e
  - it fetches the maximum filename length in this directory:
Packit 437b5e
      expect (type (pathconf ().NAME_MAX)).to_be "number"
Packit 437b5e
      expect (pathconf (".", "NAME_MAX") >= 0).to_be (true)
Packit 437b5e
  - it fetches whether accessing overlong filenames is an error:
Packit 437b5e
      expect (type (pathconf ().NO_TRUNC)).to_be "number"
Packit 437b5e
      expect (pathconf (".", "NO_TRUNC") >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum relative path length from this directory:
Packit 437b5e
      expect (type (pathconf ().PATH_MAX)).to_be "number"
Packit 437b5e
      expect (pathconf (".", "PATH_MAX") >= 0).to_be (true)
Packit 437b5e
  - it fetches the size of the pipe buffer:
Packit 437b5e
      expect (type (pathconf ().PIPE_BUF)).to_be "number"
Packit 437b5e
      expect (pathconf (".", "PIPE_BUF") >= 0).to_be (true)
Packit 437b5e
  - it fetches whether special character processing can be disabled: |
Packit 437b5e
      -- not passing a tty, so should return -1
Packit 437b5e
      expect (type (pathconf ().VDISABLE)).to_be "number"
Packit 437b5e
      pending "issue #102"
Packit 437b5e
      expect (pathconf (".", "VDISABLE")).to_be (-1)
Packit 437b5e
  - it fetches a subtable of named fields:
Packit 437b5e
      expect ({pathconf (".", "VDISABLE", "NAME_MAX")}).
Packit 437b5e
        to_equal {pathconf (".", "VDISABLE"), pathconf (".", "NAME_MAX")}
Packit 437b5e
  - it fetches everything without an argument:
Packit 437b5e
      t = pathconf ()
Packit 437b5e
      for k, v in pairs (t) do
Packit 437b5e
        expect (t[k]).to_be (pathconf (".", k))
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe setrlimit:
Packit 437b5e
  - before:
Packit 437b5e
      setrlimit = posix.setrlimit
Packit 437b5e
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      examples {
Packit 437b5e
        ["it diagnoses argument #1 invalid option"] = function ()
Packit 437b5e
          expect (setrlimit ("fubar")).to_raise.any_of {
Packit 437b5e
            "bad argument #1 to '?' (invalid option 'fubar')",
Packit 437b5e
            "bad argument #1 to 'setrlimit' (invalid option 'fubar')",
Packit 437b5e
          }
Packit 437b5e
        end
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
      badargs.diagnose (setrlimit, "setrlimit (string, ?int, ?int)")
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe stat:
Packit 437b5e
  - before:
Packit 437b5e
      # choose a format without seconds, that won't cause a race condition
Packit 437b5e
      fmt = "%b %d %H:%M"
Packit 437b5e
      getegid, geteuid = posix.getegid, posix.geteuid
Packit 437b5e
      stat, typeerrors = init (posix, "stat")
Packit 437b5e
Packit 437b5e
      dir = posix.mkdtemp (template)
Packit 437b5e
      posix.mkdir (dir .. "/subdir")
Packit 437b5e
      posix.link ("subdir", dir .. "/soft", true)
Packit 437b5e
      touch (dir .. "/file")
Packit 437b5e
      posix.link (dir .. "/file", dir .. "/hard")
Packit 437b5e
      posix.link ("no such destination", dir .. "/dangling", true)
Packit 437b5e
      posix.mkfifo (dir .. "/fifo")
Packit 437b5e
Packit 437b5e
  - after:
Packit 437b5e
      rmtmp (dir)
Packit 437b5e
Packit 437b5e
  # posix.stat takes an optional string or table as its second
Packit 437b5e
  # argument, followed by zero or more strings only if the second
Packit 437b5e
  # argument was a string; since we can't express that with
Packit 437b5e
  # `badargs.diagnose` do it all manually again...
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
    - 'it diagnoses missing argument #1':
Packit 437b5e
        expect (stat ()).to_raise.any_of (typeerrors (1, "string"))
Packit 437b5e
    - 'it diagnoses argument #1 type not string':
Packit 437b5e
        expect (stat (false)).
Packit 437b5e
          to_raise.any_of (typeerrors (1, "string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #2 type not table, string or nil':
Packit 437b5e
        expect (stat (".", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (2, "?table|string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #2 string invalid': |
Packit 437b5e
        expect (stat (".", "fubar")).to_raise.any_of {
Packit 437b5e
          "bad argument #2 to '?' (invalid option 'fubar')",
Packit 437b5e
          "bad argument #2 to 'stat' (invalid option 'fubar')",
Packit 437b5e
        }
Packit 437b5e
    - 'it diagnoses argument #3 type not string':
Packit 437b5e
        expect (stat (".", "type", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (3, "string", "boolean"))
Packit 437b5e
    - it diagnoses too many arguments:
Packit 437b5e
        expect (stat (".", {}, false)).
Packit 437b5e
          to_raise.any_of (typeerrors (3))
Packit 437b5e
Packit 437b5e
  - it fetches the file inode:
Packit 437b5e
      expect (stat (dir .. "/hard").ino).to_be (stat (dir .. "/file").ino)
Packit 437b5e
  - it fetches the file type:
Packit 437b5e
      expect (stat (dir).type).to_be "directory"
Packit 437b5e
      expect (stat (dir .. "/file", "type")).to_be "regular"
Packit 437b5e
      expect (stat (dir .. "/soft", "type")).to_be "link"
Packit 437b5e
      expect (stat (dir .. "/hard", "type")).to_be "regular"
Packit 437b5e
  - it fetches the file size:
Packit 437b5e
      # skip directory size, which is system dependent
Packit 437b5e
      expect (stat (dir .. "/file").size).to_be (0)
Packit 437b5e
      expect (stat (dir .. "/soft", "size")).to_be (string.len ("subdir"))
Packit 437b5e
      expect (stat (dir .. "/hard", "size")).
Packit 437b5e
        to_be (stat (dir .. "/file", "size"))
Packit 437b5e
  - it fetches the file access time:
Packit 437b5e
      expect (os.date (fmt, stat (dir .. "/file", "atime"))).
Packit 437b5e
        to_be (os.date (fmt))
Packit 437b5e
  - it fetches the file modification time:
Packit 437b5e
      expect (os.date (fmt, stat (dir .. "/file", "mtime"))).
Packit 437b5e
        to_be (os.date (fmt))
Packit 437b5e
  - it fetches the file creation time:
Packit 437b5e
      expect (os.date (fmt, stat (dir .. "/file", "ctime"))).
Packit 437b5e
        to_be (os.date (fmt))
Packit 437b5e
  - it fetches the file access mode:
Packit 437b5e
      expect (stat (dir .. "/file").mode).to_match ("^[-rwx]+$")
Packit 437b5e
      expect (stat (dir .. "/subdir", "mode")).to_match ("^[-rwx]+$")
Packit 437b5e
  - it fetches the number of links:
Packit 437b5e
      expect (stat (dir .. "/file").nlink).to_be (2)
Packit 437b5e
      expect (stat (dir .. "/soft", "nlink")).to_be (1)
Packit 437b5e
      expect (stat (dir .. "/hard", "nlink")).
Packit 437b5e
        to_be (stat (dir .. "/file", "nlink"))
Packit 437b5e
      expect (stat (dir .. "/subdir", "nlink")).to_be (2)
Packit 437b5e
  - it fetches the owner id:
Packit 437b5e
      expect (stat (dir .. "/file").uid).to_be (geteuid ())
Packit 437b5e
      expect (stat (dir .. "/subdir", "uid")).to_be (geteuid ())
Packit 437b5e
  - it fetches the owner group id:
Packit 437b5e
      expect (stat (dir .. "/file").gid).to_be (getegid ())
Packit 437b5e
      expect (stat (dir .. "/subdir", "gid")).to_be (getegid ())
Packit 437b5e
  - it fetches a subtable of named fields:
Packit 437b5e
      expect ({stat (dir .. "/file", "type", "size", "nlink")}).
Packit 437b5e
        to_equal {"regular", 0, 2}
Packit 437b5e
  - it fetches everything without an argument:
Packit 437b5e
      t = stat (dir .. "/file")
Packit 437b5e
      for k, v in pairs (t) do
Packit 437b5e
        expect (t[k]).to_be (stat (dir .. "/file", k))
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe statvfs:
Packit 437b5e
  - before:
Packit 437b5e
      statvfs, typeerrors = init (posix, "statvfs")
Packit 437b5e
Packit 437b5e
  # posix.statvfs takes an optional string or table as its second
Packit 437b5e
  # argument, followed by zero or more strings only if the second
Packit 437b5e
  # argument was a string; since we can't express that with
Packit 437b5e
  # `badargs.diagnose` do it all manually again...
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      if statvfs then
Packit 437b5e
        examples {
Packit 437b5e
          ["it diagnoses missing argument #1"] = function ()
Packit 437b5e
            expect (statvfs ()).to_raise.any_of (typeerrors (1, "string"))
Packit 437b5e
          end
Packit 437b5e
        }
Packit 437b5e
        examples {
Packit 437b5e
          ["it diagnoses argument #1 type not string"] = function ()
Packit 437b5e
            expect (statvfs (false)).
Packit 437b5e
              to_raise.any_of (typeerrors (1, "string", "boolean"))
Packit 437b5e
          end
Packit 437b5e
        }
Packit 437b5e
        examples {
Packit 437b5e
          ["it diagnoses argument #2 type not table, string or nil"] = function ()
Packit 437b5e
            expect (statvfs (".", false)).
Packit 437b5e
              to_raise.any_of (typeerrors (2, "?table|string", "boolean"))
Packit 437b5e
          end
Packit 437b5e
        }
Packit 437b5e
        examples {
Packit 437b5e
          ["it diagnoses argument #2 string invalid"] = function ()
Packit 437b5e
            expect (statvfs (".", "fubar")).to_raise.any_of {
Packit 437b5e
              "bad argument #2 to '?' (invalid option 'fubar')",
Packit 437b5e
              "bad argument #2 to 'statvfs' (invalid option 'fubar')",
Packit 437b5e
            }
Packit 437b5e
          end
Packit 437b5e
        }
Packit 437b5e
        examples {
Packit 437b5e
          ["it diagnoses argument #3 type not string"] = function ()
Packit 437b5e
            expect (statvfs (".", "files", false)).
Packit 437b5e
              to_raise.any_of (typeerrors (3, "string", "boolean"))
Packit 437b5e
          end
Packit 437b5e
        }
Packit 437b5e
        examples {
Packit 437b5e
          ["it diagnoses too many arguments"] = function ()
Packit 437b5e
            expect (statvfs (".", {}, false)).to_raise.any_of (typeerrors (3))
Packit 437b5e
          end
Packit 437b5e
        }
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
  - it fetches statistics for a mounted file system:
Packit 437b5e
      if statvfs then
Packit 437b5e
        sv = statvfs "/"
Packit 437b5e
        expect (type (sv)).to_be "table"
Packit 437b5e
        expect (sv.bsize).to_be (statvfs ("/", "bsize"))
Packit 437b5e
        for _, field in pairs {"bsize", "frsize", "blocks", "bfree", "bavail",
Packit 437b5e
                               "files", "ffree", "favail", "flag", "namemax"}
Packit 437b5e
        do
Packit 437b5e
          expect (type (sv[field])).to_be "number"
Packit 437b5e
          expect (sv[field] >= 0).to_be (true)
Packit 437b5e
        end
Packit 437b5e
      end
Packit 437b5e
  - it returns a non-negative value from fsid: |
Packit 437b5e
      -- Merge this back into the previous example when #102 is fixed
Packit 437b5e
      if statvfs then
Packit 437b5e
        sv = statvfs "/"
Packit 437b5e
        pending "issue #102"
Packit 437b5e
        expect (sv[field] >= 0).to_be (true)
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe strftime:
Packit 437b5e
  - before:
Packit 437b5e
      strftime = posix.strftime
Packit 437b5e
Packit 437b5e
      t = {
Packit 437b5e
        is_dst = true, weekday = 0, sec = 2, min = 3, hour = 4,
Packit 437b5e
        monthday = 5, month = 6, year = 7, yearday = 8
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (strftime, "strftime (string, ?table)")
Packit 437b5e
Packit 437b5e
  - context with place-holders:
Packit 437b5e
    - it plugs weekday:
Packit 437b5e
        expect (strftime ("%w", t)).to_be "0"
Packit 437b5e
    - it plugs sec:
Packit 437b5e
        expect (strftime ("%S", t)).to_be "02"
Packit 437b5e
    - it plugs min:
Packit 437b5e
        expect (strftime ("%M", t)).to_be "03"
Packit 437b5e
    - it plugs hour:
Packit 437b5e
        expect (strftime ("%H", t)).to_be "04"
Packit 437b5e
    - it plugs monthday:
Packit 437b5e
        expect (strftime ("%d", t)).to_be "05"
Packit 437b5e
    - it plugs month:
Packit 437b5e
        expect (strftime ("%m", t)).to_be "06"
Packit 437b5e
    - it plugs year:
Packit 437b5e
        expect (strftime ("%y", t)).to_be "07"
Packit 437b5e
    - it plugs yearday:
Packit 437b5e
        expect (strftime ("%j", t)).to_be "009"
Packit 437b5e
  - it defaults to current time:
Packit 437b5e
      expect (strftime "%Y-%m-%d\n").
Packit 437b5e
        to_be (io.popen ("date +'%Y-%m-%d'", "r"):read "*a")
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe strptime:
Packit 437b5e
  - before:
Packit 437b5e
      strptime = posix.strptime
Packit 437b5e
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      badargs.diagnose (strptime, "strptime (string, string)")
Packit 437b5e
Packit 437b5e
  - context with place-holders:
Packit 437b5e
    - before:
Packit 437b5e
        t, i = strptime ("Mon Jun  4 03:02:01 BST 1906 garbage",
Packit 437b5e
                         "%a %b %d %H:%M:%S BST %Y")
Packit 437b5e
    - it returns the first unconsumed character:
Packit 437b5e
        expect (i).to_be (29)
Packit 437b5e
    # tm_yday and tm_isdst are not set by strptime
Packit 437b5e
    - it scans into weekday:
Packit 437b5e
        expect (t.weekday).to_be (1)
Packit 437b5e
    - it scans into sec:
Packit 437b5e
        expect (t.sec).to_be (1)
Packit 437b5e
    - it scans into min:
Packit 437b5e
        expect (t.min).to_be (2)
Packit 437b5e
    - it scans into hour:
Packit 437b5e
        expect (t.hour).to_be (3)
Packit 437b5e
    - it scans into monthday:
Packit 437b5e
        expect (t.monthday).to_be (4)
Packit 437b5e
    - it scans into month:
Packit 437b5e
        expect (t.month).to_be (6)
Packit 437b5e
    - it scans into year:
Packit 437b5e
        expect (t.year).to_be (1906)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe setlogmask:
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
      if posix.setlogmask then
Packit 437b5e
        badargs.diagnose (posix.setlogmask, "setlogmask (?int*)")
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe sysconf:
Packit 437b5e
  - before:
Packit 437b5e
      sysconf, typeerrors = init (posix, "sysconf")
Packit 437b5e
Packit 437b5e
  # posix.sysconf takes an optional string or table as its first
Packit 437b5e
  # argument, followed by zero or more strings only if the first
Packit 437b5e
  # argument was a string; since we can't express that with
Packit 437b5e
  # `badargs.diagnose` do it all manually again...
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
    - 'it diagnoses argument #1 type not table, string or nil':
Packit 437b5e
        expect (sysconf (false)).
Packit 437b5e
          to_raise.any_of (typeerrors (1, "?table|string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #1 string invalid': |
Packit 437b5e
        expect (sysconf ("fubar")).to_raise.any_of {
Packit 437b5e
          "bad argument #1 to '?' (invalid option 'fubar')",
Packit 437b5e
          "bad argument #1 to 'sysconf' (invalid option 'fubar')",
Packit 437b5e
        }
Packit 437b5e
    - 'it diagnoses argument #2 type not string':
Packit 437b5e
        expect (sysconf ("ARG_MAX", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (2, "string", "boolean"))
Packit 437b5e
    - it diagnoses too many arguments:
Packit 437b5e
        expect (sysconf ({}, false)).to_raise.any_of (typeerrors (2))
Packit 437b5e
Packit 437b5e
  - it fetches the maximum number of exec arguments:
Packit 437b5e
      expect (type (sysconf ().ARG_MAX)).to_be "number"
Packit 437b5e
      expect (sysconf "ARG_MAX" >= 0).to_be (true)
Packit 437b5e
  - it fetches the number processes per user:
Packit 437b5e
      expect (type (sysconf ().CHILD_MAX)).to_be "number"
Packit 437b5e
      expect (sysconf "CHILD_MAX" >= 0).to_be (true)
Packit 437b5e
  - it fetches the number of clock ticks per second:
Packit 437b5e
      expect (type (sysconf ().CLK_TCK)).to_be "number"
Packit 437b5e
      expect (sysconf "CLK_TCK" >= 0).to_be (true)
Packit 437b5e
  - it fetches the job control version:
Packit 437b5e
      expect (type (sysconf ().JOB_CONTROL)).to_be "number"
Packit 437b5e
      expect (sysconf "JOB_CONTROL" >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum number of groups:
Packit 437b5e
      expect (type (sysconf ().NGROUPS_MAX)).to_be "number"
Packit 437b5e
      expect (sysconf "NGROUPS_MAX" >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum number of open descriptors:
Packit 437b5e
      expect (type (sysconf ().OPEN_MAX)).to_be "number"
Packit 437b5e
      expect (sysconf "OPEN_MAX" >= 0).to_be (true)
Packit 437b5e
  - it fetches the number of saved ids:
Packit 437b5e
      expect (type (sysconf ().SAVED_IDS)).to_be "number"
Packit 437b5e
      expect (sysconf "SAVED_IDS" >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum number of open streams:
Packit 437b5e
      expect (type (sysconf ().STREAM_MAX)).to_be "number"
Packit 437b5e
      expect (sysconf "STREAM_MAX" >= 0).to_be (true)
Packit 437b5e
  - it fetches the maximum length of a timezone name:
Packit 437b5e
      expect (type (sysconf ().TZNAME_MAX)).to_be "number"
Packit 437b5e
      expect (sysconf "TZNAME_MAX" >= 0).to_be (true)
Packit 437b5e
  - "it fetches the POSIX.1 version":
Packit 437b5e
      expect (type (sysconf ().VERSION)).to_be "number"
Packit 437b5e
      expect (sysconf "VERSION" >= 0).to_be (true)
Packit 437b5e
  - it fetches a subtable of named fields:
Packit 437b5e
      expect ({sysconf ("VERSION", "ARG_MAX", "OPEN_MAX")}).
Packit 437b5e
        to_equal {sysconf "VERSION", sysconf "ARG_MAX", sysconf "OPEN_MAX"}
Packit 437b5e
  - it fetches everything without an argument:
Packit 437b5e
      t = sysconf ()
Packit 437b5e
      for k, v in pairs (t) do
Packit 437b5e
        expect (t[k]).to_be (sysconf (k))
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe times:
Packit 437b5e
  - before:
Packit 437b5e
      table.unpack = table.unpack or unpack
Packit 437b5e
      times, typeerrors = init (posix, "times")
Packit 437b5e
Packit 437b5e
  # posix.times takes an optional string or table as its first
Packit 437b5e
  # argument, followed by zero or more strings only if the first
Packit 437b5e
  # argument was a string; since we can't express that with
Packit 437b5e
  # `badargs.diagnose` do it all manually again...
Packit 437b5e
  - context with bad arguments:
Packit 437b5e
    - 'it diagnoses argument #1 type not table, string or nil':
Packit 437b5e
        expect (times (false)).
Packit 437b5e
          to_raise.any_of (typeerrors (1, "?table|string", "boolean"))
Packit 437b5e
    - 'it diagnoses argument #1 string invalid': |
Packit 437b5e
        expect (times ("fubar")).to_raise.any_of {
Packit 437b5e
          "bad argument #1 to '?' (invalid option 'fubar')",
Packit 437b5e
          "bad argument #1 to 'times' (invalid option 'fubar')",
Packit 437b5e
        }
Packit 437b5e
    - 'it diagnoses argument #2 type not string':
Packit 437b5e
        expect (times ("utime", false)).
Packit 437b5e
          to_raise.any_of (typeerrors (2, "string", "boolean"))
Packit 437b5e
    - it diagnoses too many arguments:
Packit 437b5e
        expect (times ({}, false)).to_raise.any_of (typeerrors (2))
Packit 437b5e
Packit 437b5e
  - it fetches the user time:
Packit 437b5e
      expect (type (times ().utime)).to_be "number"
Packit 437b5e
      expect (times ("utime") >= 0).to_be (true)
Packit 437b5e
  - it fetches the system time:
Packit 437b5e
      expect (type (times ().stime)).to_be "number"
Packit 437b5e
      expect (times ("stime") >= 0).to_be (true)
Packit 437b5e
  - it fetches the children user time:
Packit 437b5e
      expect (type (times ().cutime)).to_be "number"
Packit 437b5e
      expect (times ("cutime") >= 0).to_be (true)
Packit 437b5e
  - it fetches the children system time:
Packit 437b5e
      expect (type (times ().cstime)).to_be "number"
Packit 437b5e
      expect (times ("cstime") >= 0).to_be (true)
Packit 437b5e
  - it fetches the elapsed time:
Packit 437b5e
      expect (type (times ().elapsed)).to_be "number"
Packit 437b5e
      expect (times ("elapsed") >= 0).to_be (true)
Packit 437b5e
  - it fetches a subtable of named fields: |
Packit 437b5e
      keys = {"utime", "cutime"}
Packit 437b5e
      t = {times (table.unpack (keys))}
Packit 437b5e
      for _, v in ipairs (t) do
Packit 437b5e
        expect (type (v)).to_be "number"
Packit 437b5e
      end
Packit 437b5e
  - it fetches everything without an argument:
Packit 437b5e
      keys = {"utime", "stime", "cutime", "cstime", "elapsed"}
Packit 437b5e
      t = times ()
Packit 437b5e
      expect (t).to_contain.all_of (keys)
Packit 437b5e
      for _, v in ipairs (keys) do
Packit 437b5e
        expect (type (t[v])).to_be "number"
Packit 437b5e
      end
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe umask:
Packit 437b5e
  - before:
Packit 437b5e
      stat, umask = posix.stat, posix.umask
Packit 437b5e
      saved = umask ()
Packit 437b5e
      umask "rwxr-x---"
Packit 437b5e
Packit 437b5e
      dir = posix.mkdtemp (template)
Packit 437b5e
Packit 437b5e
  - after:
Packit 437b5e
      rmtmp (dir)
Packit 437b5e
      umask (saved)
Packit 437b5e
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      examples {
Packit 437b5e
        ["it diagnoses argument #1 invalid mode"] = function ()
Packit 437b5e
          expect (umask ("g+vv")).to_raise.any_of {
Packit 437b5e
            "bad argument #1 to '?' (bad mode)",
Packit 437b5e
            "bad argument #1 to 'umask' (bad mode)",
Packit 437b5e
          }
Packit 437b5e
        end
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
      badargs.diagnose (umask, "umask (?string)")
Packit 437b5e
Packit 437b5e
  - it returns current umask:
Packit 437b5e
      expect (umask ()).to_be "rwxr-x---"
Packit 437b5e
  - "it sets attributes with '='":
Packit 437b5e
      expect (umask "a=r").to_be "r--r--r--"
Packit 437b5e
  - "it adds attributes with '+'":
Packit 437b5e
      expect (umask "g+w").to_be "rwxrwx---"
Packit 437b5e
  - "it removes attributes with '-'":
Packit 437b5e
      expect (umask "u-r").to_be "-wxr-x---"
Packit 437b5e
  - it accepts comma separated attribute specifications:
Packit 437b5e
      expect (umask "a+r,g+w,u-x").to_be "rw-rwxr--"
Packit 437b5e
  - it controls the mode of newly created files:
Packit 437b5e
      xxx, mode = dir .. "/xxx", "rw--w-r--"
Packit 437b5e
      umask (mode)
Packit 437b5e
      touch (xxx)
Packit 437b5e
      expect (stat (xxx, "mode")).to_be (mode)
Packit 437b5e
      os.remove (xxx)
Packit 437b5e
Packit 437b5e
Packit 437b5e
- describe uname:
Packit 437b5e
  - before:
Packit 437b5e
      uname = posix.uname
Packit 437b5e
Packit 437b5e
  - context with bad arguments: |
Packit 437b5e
      badargs.diagnose (uname, "uname (?string)")
Packit 437b5e
Packit 437b5e
      examples {
Packit 437b5e
        ['it diagnoses bad specifier format options'] = function ()
Packit 437b5e
          expect (uname ("foo %_")).
Packit 437b5e
            to_error "bad argument #1 to 'uname' (invalid format option '_')"
Packit 437b5e
        end
Packit 437b5e
      }
Packit 437b5e
Packit 437b5e
  - it substitutes %n:
Packit 437b5e
      expect (uname "%n").to_be (cmd_output "uname -n")
Packit 437b5e
  - it substitutes %m:
Packit 437b5e
      expect (uname "%m").to_be (cmd_output "uname -m")
Packit 437b5e
  - it substitutes %r:
Packit 437b5e
      expect (uname "%r").to_be (cmd_output "uname -r")
Packit 437b5e
  - it outputs everything with no arguments:
Packit 437b5e
      expect (uname ()).to_be (cmd_output "uname -s -n -r -v -m")