github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/iolib/lua/popen_unix.lua (about)

     1  -- tags: !windows
     2  
     3  local function testf(f)
     4      return function(...)
     5          (function(ok, ...) 
     6              if ok then
     7                  print("OK", ...)
     8              else
     9                  print("ERR", ...)
    10              end
    11          end)(pcall(f, ...))
    12      end
    13  end
    14  
    15  do
    16      print(pcall(io.popen))
    17      --> ~^false\t.*value needed
    18  
    19      print(pcall(io.popen, {}))
    20      --> ~^false\t.*must be a string
    21  
    22      print(pcall(io.popen, "aaa", false))
    23      --> ~^false\t.*must be a string
    24  
    25      local fh = io.popen("hello")
    26      print(fh)
    27      --> =file ("hello")
    28  
    29      local f = io.popen("cat files/iotest.txt")
    30  
    31      print(pcall(f.read))
    32      --> ~^false\t.*value needed
    33  
    34      print(pcall(f.read, 123))
    35      --> ~^false\t
    36  
    37      print(pcall(f.read, f, "?"))
    38      --> ~^false\t.*invalid format
    39  
    40      testf(io.type)()
    41      --> ~ERR	.*value needed
    42  
    43      print(io.type(123))
    44      --> =nil
    45  
    46      print(io.type(f))
    47      --> =file
    48  
    49      print(pcall(f.lines))
    50      --> ~^false\t.*value needed
    51  
    52      print(pcall(f.lines, 123))
    53      --> ~^false\t.*must be a file
    54  
    55      print(pcall(f.lines, f, "wat"))
    56      --> ~^false\t.*invalid format
    57  
    58  
    59      for line in f:lines() do
    60          print(line)
    61      end
    62      --> =hello
    63      --> =123
    64      --> =bye
    65  
    66      testf(f.close)()
    67      --> ~ERR	.*value needed
    68  
    69      testf(f.flush)()
    70      --> ~ERR	.*value needed
    71  
    72      testf(f.flush)(123)
    73      --> ~ERR	.*must be a file
    74  
    75      f:close()
    76      print(io.type(f))
    77      --> =closed file
    78  end
    79  
    80  do
    81      local function wp(x)
    82          print("[" .. tostring(x) .. "]")
    83      end
    84      local f = io.popen("cat > files/popenwrite.txt", "w")
    85  
    86      testf(f.write)()
    87      --> ~ERR	.*value needed
    88  
    89      print(pcall(f.write, 123))
    90      --> ~^false\t.*must be a file
    91  
    92      print(pcall(f.write, f, {}))
    93      --> ~^false\t.*must be a string or a number
    94  
    95      f:write("foobar", 1234, "\nabc\n")
    96      f:close()
    97      f = io.open("files/popenwrite.txt", "r")
    98  
    99      wp(f:read("a"))
   100      --> =[foobar1234
   101      --> =abc
   102      --> =]
   103  
   104      wp(f:read("l"))
   105      --> =[nil]
   106  end
   107  
   108  do
   109      testf(io.read)("z")
   110      --> ~ERR	.*invalid format
   111  
   112      testf(io.read)(false)
   113      --> ~ERR	.*invalid format
   114  
   115      local f = io.open("files/writetest2.txt", "w+")
   116      io.output(f)
   117      io.write([[Dear sir,
   118  Blah blah,
   119  
   120  Yours sincerely.
   121  ]])
   122      io.flush()
   123      io.input(f)
   124      f:seek("set", 0)
   125      print(io.read())
   126      --> =Dear sir,
   127  
   128      for line in io.lines() do
   129          print(line)
   130      end
   131      --> =Blah blah,
   132      --> =
   133      --> =Yours sincerely.
   134  end
   135  
   136  do
   137      local f = io.tmpfile()
   138      print(io.type(f))
   139      --> =file
   140  
   141      print(pcall(f.seek, f, "wat"))
   142      --> ~false\t.*: #1 must be "cur", "set" or "end"
   143  
   144      -- TODO: do something with the file
   145  end
   146