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

Commit 5dc41d1

Browse files
committed
Add function with_wd
1 parent ad5482c commit 5dc41d1

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ Returns:
213213

214214
- The result(s) of the call to `callback`
215215

216-
217216
### with\_tmpdir
218217

219218
`with_tmpdir ([parent_dir,] templ, callback)`
@@ -239,6 +238,28 @@ Returns:
239238

240239
- The result of the call to `callback`.
241240

241+
### with\_wd
242+
243+
`with_wd (directory, callback)`
244+
245+
Run an action within a different directory. This function will
246+
change the working directory to `directory`, execute `callback`,
247+
then switch back to the original working directory, even if an
248+
error occurs while running the callback action.
249+
250+
Parameters:
251+
252+
`directory`
253+
: Directory in which the given `callback` should be executed
254+
(string)
255+
256+
`callback`
257+
: Action to execute in the given directory (function)
258+
259+
Returns:
260+
261+
- The result(s) of the call to `callback`
262+
242263

243264
License
244265
-------

src/Foreign/Lua/Module/System.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Foreign.Lua.Module.System (
3232
, tmpdirname
3333
, with_env
3434
, with_tmpdir
35+
, with_wd
3536
)
3637
where
3738

@@ -73,6 +74,7 @@ pushModule = do
7374
addFunction "tmpdirname" tmpdirname
7475
addFunction "with_env" with_env
7576
addFunction "with_tmpdir" with_tmpdir
77+
addFunction "with_wd" with_wd
7678
return 1
7779

7880
-- | Add the @system@ module under the given name to the table of
@@ -162,6 +164,17 @@ setenv name value = ioToLua (Env.setEnv name value)
162164
tmpdirname :: Lua FilePath
163165
tmpdirname = ioToLua Directory.getTemporaryDirectory
164166

167+
-- | Run an action in a different directory, then restore the old
168+
-- working directory.
169+
with_wd :: FilePath -> Callback -> Lua NumResults
170+
with_wd fp callback =
171+
bracket (Lua.liftIO Directory.getCurrentDirectory)
172+
(Lua.liftIO . Directory.setCurrentDirectory)
173+
$ \_ -> do
174+
Lua.liftIO (Directory.setCurrentDirectory fp)
175+
callback `invokeWithFilePath` fp
176+
177+
165178
-- | Run an action, then restore the old environment variable values.
166179
with_env :: Map.Map String String -> Callback -> Lua NumResults
167180
with_env environment callback =

test/system-module-tests.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,15 @@ assert(in_tmpdir(create_then_count_files) == 1, 'Number of files should be 1')
110110
system.setenv('TESTING', token)
111111
assert(system.getenv 'TESTING' == token,
112112
'setting and getting env var is inconsistent')
113+
114+
-- with_wd
115+
local cwd = system.getwd()
116+
function check_wd (path)
117+
assert(path == system.getwd(), "current path is given as arg")
118+
assert(path ~= cwd, "current path has changed from original")
119+
end
120+
121+
system.with_tmpdir(
122+
'wd-test',
123+
function (path) return system.with_wd(path, check_wd) end
124+
)

0 commit comments

Comments
 (0)