Skip to content

Commit c9ffd97

Browse files
committed
Hoist the FileHandle check and add more tests
1 parent 6d8753e commit c9ffd97

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

apps/openmw/mwlua/vfsbindings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ namespace MWLua
190190
return seek(lua, self, std::ios_base::cur, off);
191191
});
192192
handle["lines"] = [](sol::this_state lua, sol::object self) {
193+
if (!self.is<FileHandle*>())
194+
throw std::runtime_error("self should be a file handle");
193195
return sol::as_function([lua, self]() -> sol::object {
194-
if (!self.is<FileHandle*>())
195-
return sol::nil;
196196
FileHandle* handle = self.as<FileHandle*>();
197197
validateFile(*handle);
198198
return readLineFromFile(lua, *handle);

scripts/data/integration_tests/test_lua_api/test.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,18 @@ end
227227

228228
local function testVFS()
229229
local file = 'test_vfs_dir/lines.txt'
230+
local nosuchfile = 'test_vfs_dir/nosuchfile'
230231
testing.expectEqual(vfs.fileExists(file), true, 'lines.txt should exist')
231-
testing.expectEqual(vfs.fileExists('test_vfs_dir/nosuchfile'), false, 'nosuchfile should not exist')
232+
testing.expectEqual(vfs.fileExists(nosuchfile), false, 'nosuchfile should not exist')
232233

234+
local expectedLines = { '1', '2', '', '4' }
233235
local getLine = vfs.lines(file)
234-
for _,v in pairs({ '1', '2', '', '4' }) do
236+
for _,v in pairs(expectedLines) do
235237
testing.expectEqual(getLine(), v)
236238
end
237239
testing.expectEqual(getLine(), nil, 'All lines should have been read')
238240
local ok = pcall(function()
239-
vfs.lines('test_vfs_dir/nosuchfile')
241+
vfs.lines(nosuchfile)
240242
end)
241243
testing.expectEqual(ok, false, 'Should not be able to read lines from nonexistent file')
242244

@@ -259,6 +261,14 @@ local function testVFS()
259261

260262
testing.expectEqual(handle:close(), true, 'File should be closeable')
261263
testing.expectEqual(vfs.type(handle), 'closed file', 'File should be closed')
264+
265+
handle = vfs.open(nosuchfile)
266+
testing.expectEqual(handle, nil, 'vfs.open should return nil on nonexistent files')
267+
268+
getLine = vfs.open(file):lines()
269+
for _,v in pairs(expectedLines) do
270+
testing.expectEqual(getLine(), v)
271+
end
262272
end
263273

264274
local function testCommitCrime()

0 commit comments

Comments
 (0)