Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tests: test-fs-chmod and fixtures ported from node.js
  • Loading branch information
hnakamur committed Aug 12, 2012
1 parent c4570f3 commit 6deb388
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/fixtures/a.lua
@@ -0,0 +1,46 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local c = require('./b/c')
p('load fixtures/a.lua')
local string = 'A'
local exports = {}
exports.SomeClass = c.SomeClass
exports.A = function()
return string
end
exports.C = function()
return c.C()
end
exports.D = function()
return c.D()
end
exports.number = 42
process:on('exit', function()
string = 'A done'
end)
return exports
46 changes: 46 additions & 0 deletions tests/fixtures/a1.lua
@@ -0,0 +1,46 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local c = require('./b/c')
p('load fixtures/a.lua')
local string = 'A'
local exports = {}
exports.SomeClass = c.SomeClass
exports.A = function()
return string
end
exports.C = function()
return c.C()
end
exports.D = function()
return c.D()
end
exports.number = 42
process:on('exit', function()
string = 'A done'
end)
return exports
49 changes: 49 additions & 0 deletions tests/fixtures/b/c.lua
@@ -0,0 +1,49 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local d = require('./d')
require('helper')
local package = require('./package')
assert('world' == package.hello)
p('load fixtures/b/c.lua')
local string = 'C'
local exports = {}
exports.SomeClass = function()
end
exports.C = function()
return string
end
exports.D = function()
return d.D()
end
process:on('exit', function()
string = 'C done'
p('b/c.lua exit')
end)
return exports
32 changes: 32 additions & 0 deletions tests/fixtures/b/d.lua
@@ -0,0 +1,32 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
p('load fixtures/b/d.lua')
local string = 'D'
local exports = {}
exports.D = function()
return string
end
process:on('exit', function()
string = 'D done'
end)
return exports
24 changes: 24 additions & 0 deletions tests/fixtures/b/package/init.lua
@@ -0,0 +1,24 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local exports = {}
exports.hello = 'world'
p('load package/index.lua')
return exports
169 changes: 169 additions & 0 deletions tests/test-fs-chmod.lua
@@ -0,0 +1,169 @@
--[[
Copyright 2012 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License")
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
require("helper")
local FS = require('fs')
local Path = require('path')
local string = require('string')
local bit = require('bit')
local got_error = false
local success_count = 0
local mode_async
local mode_sync
local is_windows = process.platform == 'win32'
local openCount = 0
function open(...)
openCount = openCount + 1
return FS._open(...)
end
function openSync(...)
openCount = openCount + 1
return FS._openSync(...)
end
function close(...)
openCount = openCount - 1
return FS._close(...)
end
function closeSync(...)
openCount = openCount - 1
return FS._closeSync(...)
end
-- Need to hijack FS.open/close to make sure that things
-- get closed once they're opened.
FS._open = FS.open
FS._openSync = FS.openSync
FS.open = open
FS.openSync = openSync
FS._close = FS.close
FS._closeSync = FS.closeSync
FS.close = close
FS.closeSync = closeSync
-- On Windows chmod is only able to manipulate read-only bit
-- TODO: test on windows
if is_windows then
mode_async = 256 --[[tonumber('0400', 8)]] -- read-only
mode_sync = 384 --[[tonumber('0600', 8)]] -- read-write
else
mode_async = 511 --[[tonumber('0777', 8)]]
mode_sync = 420 --[[tonumber('0644', 8)]]
end
local file1 = Path.join(__dirname, 'fixtures', 'a.lua')
local file2 = Path.join(__dirname, 'fixtures', 'a1.lua')
function maskMode(mode, mask)
return bit.band(mode, mask or 511 --[[tonumber('0777',8)]])
end
FS.chmod(file1, string.format('%o', mode_async), function(err)
if err then
got_error = true
else
p(FS.statSync(file1).mode)
if is_windows then
assert(maskMode(maskMode(FS.statSync(file1).mode), mode_async))
else
assert(mode_async == maskMode(FS.statSync(file1).mode))
end
-- TODO: accept mode in number
FS.chmodSync(file1, string.format('%o', mode_sync))
if is_windows then
assert(maskMode(maskMode(FS.statSync(file1).mode), mode_sync))
else
assert(mode_sync == maskMode(FS.statSync(file1).mode))
end
success_count = success_count + 1
end
end)
FS.open(file2, 'a', '0666', function(err, fd)
if err then
got_error = true
p(err.stack)
return
end
FS.fchmod(fd, string.format('%o', mode_async), function(err)
if err then
got_error = true
else
p(FS.fstatSync(fd).mode)
if is_windows then
assert(maskMode(maskMode(FS.fstatSync(fd).mode), mode_async))
else
assert(mode_async == maskMode(FS.fstatSync(fd).mode))
end
-- TODO: accept mode in number
FS.fchmodSync(fd, string.format('%o', mode_sync))
if is_windows then
assert(maskMode(maskMode(FS.fstatSync(fd).mode), mode_sync))
else
assert(mode_sync == maskMode(FS.fstatSync(fd).mode))
end
success_count = success_count + 1
FS.close(fd)
end
end)
end)
-- lchmod
if FS.lchmod then
local link = Path.join(__dirname, 'tmp', 'symbolic-link')
pcall(function()
FS.unlinkSync(link)
end)
FS.symlinkSync(file2, link)
FS.lchmod(link, mode_async, function(err)
if err then
got_error = true
else
p(FS.lstatSync(link).mode)
assert(mode_async == maskMode(FS.lstatSync(link).mode))
-- TODO: accept mode in number
FS.lchmodSync(link, string.format('%o', mode_sync))
assert(mode_sync == maskMode(FS.lstatSync(link).mode))
success_count = success_count + 1
end
end)
else
success_count = success_count + 1
end
process:on('exit', function()
assert(3 == success_count)
assert(0 == openCount)
assert(false == got_error)
end)

0 comments on commit 6deb388

Please sign in to comment.