Skip to content
This repository was archived by the owner on Mar 22, 2021. It is now read-only.

Commit 3687681

Browse files
committed
Add functions mkdir and rmdir
1 parent 8c56271 commit 3687681

3 files changed

Lines changed: 99 additions & 19 deletions

File tree

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,43 @@ Returns:
113113
- A table of all entries in `directory` without the special entries (`.`
114114
and `..`).
115115

116+
### mkdir
117+
118+
`mkdir (dirname [, create_parent])`
119+
120+
Create a new directory which is initially empty, or as near to
121+
empty as the operating system allows. The function throws an
122+
error if the directory cannot be created, e.g., if the parent
123+
directory does not exist or if a directory of the same name is
124+
already present.
125+
126+
If the optional second parameter is provided and truthy, then all
127+
directories, including parent directories, are created as
128+
necessary.
129+
130+
Parameters:
131+
132+
`dirname`:
133+
: name of the new directory
134+
135+
`create_parent`:
136+
: create parent directories if necessary
137+
138+
### rmdir
139+
140+
`rmdir (dirname [, recursive])`
141+
142+
Remove an existing, empty directory. If `recursive` is given,
143+
then delete the directory and its contents recursively.
144+
145+
Parameters:
146+
147+
`dirname`:
148+
: name of the directory to delete
149+
150+
`recursive`:
151+
: delete content recursively
152+
116153
### setenv {#system-setenv}
117154

118155
`setenv (var, value)`

src/Foreign/Lua/Module/System.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pushModule = do
4242
addFunction "env" env
4343
addFunction "getenv" getenv
4444
addFunction "ls" ls
45+
addFunction "mkdir" mkdir
46+
addFunction "rmdir" rmdir
4547
addFunction "setenv" setenv
4648
addFunction "tmpdirname" tmpdirname
4749
addFunction "with_tmpdir" with_tmpdir
@@ -154,6 +156,25 @@ env = do
154156
getenv :: String -> Lua (Optional String)
155157
getenv name = ioToLua (Optional <$> Env.lookupEnv name)
156158

159+
-- | Create a new directory which is initially empty, or as near to
160+
-- empty as the operating system allows.
161+
--
162+
-- If the optional second parameter is `false`, then create the new
163+
-- directory only if it doesn't exist yet. If the parameter is `true`,
164+
-- then parent directories are created as necessary.
165+
mkdir :: FilePath -> Bool -> Lua ()
166+
mkdir fp createParent =
167+
if createParent
168+
then ioToLua (Directory.createDirectoryIfMissing True fp)
169+
else ioToLua (Directory.createDirectory fp)
170+
171+
-- | Remove an existing directory.
172+
rmdir :: FilePath -> Bool -> Lua ()
173+
rmdir fp recursive =
174+
if recursive
175+
then ioToLua (Directory.removeDirectoryRecursive fp)
176+
else ioToLua (Directory.removeDirectory fp)
177+
157178
-- | Set the specified environment variable to a new value.
158179
setenv :: String -> String -> Lua ()
159180
setenv name value = ioToLua (Env.setEnv name value)

test/system-module-tests.lua

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,8 @@ assert(type(system.compiler_name) == 'string')
99
assert(type(system.compiler_version) == 'table')
1010
assert(type(system.os) == 'string')
1111

12-
local token = 'Banana'
13-
function write_read_token (tmpdir)
14-
local filename = tmpdir .. '/foo.txt'
15-
local fh = io.open(filename, 'w')
16-
fh:write(token .. '\n')
17-
fh:close()
18-
return io.open(filename):read '*l'
19-
end
20-
21-
-- with_tmpdir
22-
assert(system.with_tmpdir('.', 'foo', write_read_token) == token)
23-
assert(system.with_tmpdir('foo', write_read_token) == token)
24-
25-
-- tmpdirname
26-
assert(type(system.tmpdirname()) == 'string', "tmpdirname should return a string")
12+
-- currentdir
13+
assert(type(system.currentdir()) == 'string')
2714

2815
-- env
2916
assert(type(system.env()) == 'table')
@@ -35,10 +22,7 @@ assert(#system.ls('.') == #system.ls())
3522
assert(pcall(system.ls, 'thisdoesnotexist') == false)
3623
assert(pcall(system.ls, 'README.md') == false)
3724

38-
-- currentdir
39-
assert(type(system.currentdir()) == 'string')
40-
41-
-- Complex scripts
25+
-- mkdir and rmdir
4226
function in_tmpdir (callback)
4327
local orig_dir = system.currentdir()
4428
return system.with_tmpdir(
@@ -52,6 +36,44 @@ function in_tmpdir (callback)
5236
)
5337
end
5438

39+
function test_mkdir_rmdir ()
40+
-- mkdir
41+
assert(not pcall(system.mkdir, '.'), "should not be possible to create `.`")
42+
assert(pcall(system.mkdir, 'foo'), "normal dir creation")
43+
assert(pcall(system.mkdir, 'foo', true), "dir creation if exists")
44+
assert((system.ls())[1] == 'foo')
45+
assert(not pcall(system.mkdir, 'bar/baz'),
46+
"creation of nested dir")
47+
assert(pcall(system.mkdir, 'bar/baz', true),
48+
"nested dir creation, including parent directories")
49+
assert((system.ls 'bar')[1] == 'baz')
50+
51+
-- rmdir
52+
assert(pcall(system.rmdir, 'foo'), "delete empty directory")
53+
assert(not pcall(system.rmdir, 'bar'), "cannot delete non-empty dir")
54+
assert(pcall(system.rmdir, 'bar', true), "delete dir recursively")
55+
assert(#system.ls() == 0, "dir should be empty")
56+
end
57+
in_tmpdir(test_mkdir_rmdir)
58+
59+
-- tmpdirname
60+
assert(type(system.tmpdirname()) == 'string', "tmpdirname should return a string")
61+
62+
-- with_tmpdir
63+
local token = 'Banana'
64+
function write_read_token (tmpdir)
65+
local filename = tmpdir .. '/foo.txt'
66+
local fh = io.open(filename, 'w')
67+
fh:write(token .. '\n')
68+
fh:close()
69+
return io.open(filename):read '*l'
70+
end
71+
72+
assert(system.with_tmpdir('.', 'foo', write_read_token) == token)
73+
assert(system.with_tmpdir('foo', write_read_token) == token)
74+
75+
76+
-- Complex scripts
5577
function create_then_count_files ()
5678
io.open('README.org', 'w'):close()
5779
return #system.ls '.'

0 commit comments

Comments
 (0)