Skip to content

Commit

Permalink
Merge pull request #290 from luvit/add-readdir-test
Browse files Browse the repository at this point in the history
tests: test-readdir: initial commit
  • Loading branch information
philips committed Aug 14, 2012
2 parents 4a8df64 + e1e8377 commit 6380368
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
40 changes: 32 additions & 8 deletions tests/runner.lua
Expand Up @@ -100,18 +100,42 @@ local function run(callback)
end

local tmp_dir = path.join(__dirname, 'tmp')
local function remove_tmp(callback)
fs.readdir(tmp_dir, function(err, files)
if (files ~= nil) then
for i, v in ipairs(files) do
fs.unlinkSync(path.join(tmp_dir, v))
end

local remove_recursive
remove_recursive = function(dir, done)
fs.readdir(dir, function(err, files)
if files == nil then
done()
return
end

if err then
done(err)
return
end

for i, v in ipairs(files) do
local file = path.join(dir, v)
fs.stat(file, function(err, stat)
if err then
done(err)
return
end

if stat.is_directory then
remove_recursive(file, function(err)
fs.rmdir(file, function() end)
end)
elseif stat.is_file then
fs.unlink(file, function() end)
end
end)
end
fs.rmdir(tmp_dir, callback)
done()
end)
end

remove_tmp(function ()
remove_recursive(tmp_dir, function ()
fs.mkdir(tmp_dir, "0755", function()
run(remove_tmp)
end)
Expand Down
38 changes: 38 additions & 0 deletions tests/test-readdir.lua
@@ -0,0 +1,38 @@
--[[
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 path = require('path')
local fs = require('fs')
local dir = path.join(__dirname, 'tmp', 'readdir')
if not fs.existsSync(dir) then
fs.mkdirSync(dir, "0755")
end
fs.writeFileSync(path.join(dir, "1"), "")
fs.writeFileSync(path.join(dir, "2"), "")
fs.writeFileSync(path.join(dir, "3"), "")
local files = fs.readdirSync(dir)
assert(#files == 3)
fs.readdir(dir, function(err, files)
assert(err == nil)
assert(#files == 3)
end)

0 comments on commit 6380368

Please sign in to comment.