summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-08-19 23:54:32 +0800
committerruki <[email protected]>2019-08-19 17:27:13 +0800
commit905b289ecf007df7d30bcdcee927e46931639ea8 (patch)
treeaa0296aa54a9b349570a2d0f112c35806f3dc69e
parent612dd5918297120bc7f0c2011fbbce90863fafaa (diff)
fix io.isatty
-rw-r--r--tests/modules/io/test.lua33
-rw-r--r--xmake/core/base/io.lua19
-rw-r--r--xmake/core/sandbox/modules/io.lua12
3 files changed, 22 insertions, 42 deletions
diff --git a/tests/modules/io/test.lua b/tests/modules/io/test.lua
index 4666ac468..cb893b490 100644
--- a/tests/modules/io/test.lua
+++ b/tests/modules/io/test.lua
@@ -26,7 +26,7 @@ end
function test_readlines(t)
- function get_all(file, opt)
+ function get_all_keep_crlf(file, opt)
local fp = io.open(file, "r", opt)
local r = {}
while true do
@@ -35,33 +35,32 @@ function test_readlines(t)
table.insert(r, l)
end
t:require(fp:close())
-
return r
end
- function get_all2(file, opt)
- local fp = io.open(file, "r", opt)
+ function get_all_without_crlf(file, opt)
local r = {}
- for l in fp:lines(opt) do
+ local fp = io.open(file, "r", opt)
+ for l in fp:lines() do
table.insert(r, l)
end
t:require(fp:close())
return r
end
- t:are_equal(get_all("files/utf8bom-lf-eleof"), {"123\\\n", "456\n", "789\n"})
- t:are_equal(get_all("files/utf8-crlf-neleof"), {"123\\\n", "456\n", "789"})
- t:are_equal(get_all("files/utf8-crlf-neleof", {encoding = "binary"}), {"123\\\r\n", "456\r\n", "789"})
- t:are_equal(get_all("files/utf8-crlf-neleof", {continuation = "\\"}), {"123456\n", "789"})
- t:are_equal(get_all("files/utf16be-lf-eleof"), {"123\\\n", "456\n", "789\n"})
- t:are_equal(get_all("files/utf16le-crlf-neleof"), {"123\\\n", "456\n", "789"})
+ t:are_equal(get_all_keep_crlf("files/utf8bom-lf-eleof"), {"123\\\n", "456\n", "789\n"})
+ t:are_equal(get_all_keep_crlf("files/utf8-crlf-neleof"), {"123\\\n", "456\n", "789"})
+ t:are_equal(get_all_keep_crlf("files/utf8-crlf-neleof", {encoding = "binary"}), {"123\\\r\n", "456\r\n", "789"})
+ t:are_equal(get_all_keep_crlf("files/utf8-crlf-neleof", {continuation = "\\"}), {"123456\n", "789"})
+ t:are_equal(get_all_keep_crlf("files/utf16be-lf-eleof"), {"123\\\n", "456\n", "789\n"})
+ t:are_equal(get_all_keep_crlf("files/utf16le-crlf-neleof"), {"123\\\n", "456\n", "789"})
- t:are_equal(get_all2("files/utf8bom-lf-eleof"), {"123\\\n", "456\n", "789\n"})
- t:are_equal(get_all2("files/utf8-crlf-neleof"), {"123\\\n", "456\n", "789"})
- t:are_equal(get_all2("files/utf8-crlf-neleof", {encoding = "binary"}), {"123\\\r\n", "456\r\n", "789"})
- t:are_equal(get_all2("files/utf8-crlf-neleof", {continuation = "\\"}), {"123456\n", "789"})
- t:are_equal(get_all2("files/utf16be-lf-eleof"), {"123\\\n", "456\n", "789\n"})
- t:are_equal(get_all2("files/utf16le-crlf-neleof"), {"123\\\n", "456\n", "789"})
+ t:are_equal(get_all_without_crlf("files/utf8bom-lf-eleof"), {"123\\", "456", "789"})
+ t:are_equal(get_all_without_crlf("files/utf8-crlf-neleof"), {"123\\", "456", "789"})
+ t:are_equal(get_all_without_crlf("files/utf8-crlf-neleof", {encoding = "binary"}), {"123\\\r\n", "456\r\n", "789"})
+ t:are_equal(get_all_without_crlf("files/utf8-crlf-neleof", {continuation = "\\"}), {"123\\", "456", "789"})
+ t:are_equal(get_all_without_crlf("files/utf16be-lf-eleof"), {"123\\", "456", "789"})
+ t:are_equal(get_all_without_crlf("files/utf16le-crlf-neleof"), {"123\\", "456", "789"})
end
function test_prop(t)
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index aa00507f3..ac405dbac 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -37,7 +37,7 @@ io._stdfile = io._stdfile or io.stdfile
function _file.new(filepath, fileref)
local file = table.inherit(_file)
file._NAME = path.filename(filepath)
- file._PATH = filepath
+ file._PATH = path.absolute(filepath)
file._FILE = fileref
setmetatable(file, _file)
return file
@@ -149,7 +149,7 @@ function _file:isatty()
return false, string.format("file(%s) has been closed!", self:name())
end
local ok, errors = io.file_isatty(self._FILE)
- if not ok and errors then
+ if ok == nil and errors then
errors = string.format("file(%s): %s", self:name(), errors)
end
return ok, errors
@@ -204,7 +204,7 @@ end
function _filelock.new(lockpath, lock)
local filelock = table.inherit(_filelock)
filelock._NAME = path.filename(lockpath)
- filelock._PATH = lockpath
+ filelock._PATH = path.absolute(lockpath)
filelock._LOCK = lock
filelock._LOCKED_NUM = 0
setmetatable(filelock, _filelock)
@@ -308,8 +308,8 @@ end
-- read all lines from file
function io.lines(filepath, opt)
+ -- close on finished
opt = opt or {}
-
if opt.close_on_finished == nil then
opt.close_on_finished = true
end
@@ -317,7 +317,6 @@ function io.lines(filepath, opt)
-- open file
local file = io.open(filepath, "r", opt)
if not file then
- -- error
return function() return nil end
end
@@ -366,16 +365,6 @@ function io.flush()
return io.stdout:flush()
end
-function io.lines(filepath, opt)
- opt = opt or {}
- opt.close_on_finished = true
- local file, errors = io.open(filepath, 'r', opt)
- if file then
- return file:lines()
- end
- return file, errors
-end
-
-- write data to file
function io.writefile(filepath, data, opt)
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 7ff2d1d5e..3d6a12011 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -31,6 +31,7 @@ local sandbox_io_file = sandbox_io_file or {}
local sandbox_io_filelock = sandbox_io_filelock or {}
sandbox_io._file = sandbox_io._file or io._file
sandbox_io._filelock = sandbox_io._filelock or io._filelock
+sandbox_io.lines = io.lines
-- get file size
function sandbox_io_file.size(file)
@@ -62,7 +63,7 @@ end
-- this file is a tty?
function sandbox_io_file.isatty(file)
local ok, errors = file:_isatty()
- if not ok then
+ if ok == nil then
raise(errors)
end
return ok
@@ -331,15 +332,6 @@ function sandbox_io.flush(file)
return (file or sandbox_io.stdout):flush()
end
--- read lines from the given file
-function sandbox_io.lines(filepath, opt)
- local iter, data_or_errors = io.lines(filepath, opt)
- if not iter and data_or_errors then
- raise(data_or_errors)
- end
- return iter, data_or_errors
-end
-
-- isatty
function sandbox_io.isatty(file)
file = file or sandbox_io.stdout