hslua-module-system-1.1.0.1/0000755000000000000000000000000007346545000013762 5ustar0000000000000000hslua-module-system-1.1.0.1/CHANGELOG.md0000644000000000000000000000430607346545000015576 0ustar0000000000000000# Changelog `hslua-module-system` uses [PVP Versioning][]. ## hslua-module-system-1.1.0.1 Released 2023-03-26. - Improve doc strings. ## hslua-module-system-1.1.0 - Update to hslua-2.3; this includes the addition of type initializers to the module and type specifiers to the fields. ## hslua-module-system-1.0.3 Released 2023-02-14. - Added new function `cputime` and field `cputime_precision`, e.g. for benchmarking. ## hslua-module-system-1.0.2 Released 2022-02-19. - Adjusted package bounds, for hslua-core and hslua-packaging. ## hslua-module-system-1.0.1 Released 2022-01-29. - Relaxed upper bound of hslua-core, hslua-marshalling, and hslua-packaging, allowing their respective version 2.1. ## hslua-module-system-1.0.0 Released 2021-10-21. - Use hslua 2.0. ## hslua-module-system-0.2.2.1 Released 2020-10-16. - Relaxed upper bound for hslua, allow `hslua-1.3.*`. ## hslua-module-system-0.2.2 Released 2020-08-15. - Relaxed upper bound for hslua, allow `hslua-1.2.*`. - Improved documentation of internal types. - Use tasty-lua for unit tests. - Update CI to test with all GHC versions. ## hslua-module-system-0.2.1 Released 2019-05-04. - Use module helpers made available with HsLua 1.0.3. This avoids code duplication when used with other hslua modules. ## hslua-module-system-0.2.0 Released 2019-05-01. All fields and functions are now exported from the Haskell module under the same name as that used in Lua. ### New fields - `arch`: processor architecture. - `compiler_name`: Haskell compiler that was used to compile the module. - `compiler_version`: version of the compiler. - `os`: operating system. ### New functions - `mkdir`: create a new directory. - `rmdir`: remove a directory. - `with_env`: perform action with custom environment. - `with_wd`: perform action in another directory. ### Removed or renamed functions - `currentdir` was renamed to `getwd`. - `chdir` was renamed to `setwd`. - `pwd` was removed. ### Misc - Fix typos and copy-paste errors in docs, tests. ## hslua-module-system-0.1.0 Released 2019-04-26. - First version. Released on an unsuspecting world. [PVP Versioning]: https://pvp.haskell.org hslua-module-system-1.1.0.1/LICENSE0000644000000000000000000000205107346545000014765 0ustar0000000000000000Copyright (c) 2019-2023 Albert Krewinkel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. hslua-module-system-1.1.0.1/hslua-module-system.cabal0000644000000000000000000000570007346545000020671 0ustar0000000000000000cabal-version: 2.2 name: hslua-module-system version: 1.1.0.1 synopsis: Lua module wrapper around Haskell's System module. description: Provides access to system information and functionality to Lua scripts via Haskell's `System` module. . This package is part of HsLua, a Haskell framework built around the embeddable scripting language . homepage: https://github.com/hslua/hslua license: MIT license-file: LICENSE author: Albert Krewinkel maintainer: tarleb@hslua.org copyright: © 2019-2023 Albert Krewinkel category: Foreign extra-source-files: CHANGELOG.md , test/test-system.lua tested-with: GHC == 8.4.4 , GHC == 8.6.5 , GHC == 8.8.3 , GHC == 8.10.7 , GHC == 9.0.2 , GHC == 9.2.5 , GHC == 9.4.4 source-repository head type: git location: https://github.com/hslua/hslua.git subdir: hslua-module-system common common-options default-language: Haskell2010 build-depends: base >= 4.11 && < 5 , hslua-core >= 2.1 && < 2.4 , hslua-packaging >= 2.3 && < 2.4 , text >= 1.2 && < 2.1 default-extensions: LambdaCase , OverloadedStrings ghc-options: -Wall -Wincomplete-record-updates -Wnoncanonical-monad-instances -Wredundant-constraints if impl(ghc >= 8.2) ghc-options: -Wcpp-undef -Werror=missing-home-modules if impl(ghc >= 8.4) ghc-options: -Widentities -Wincomplete-uni-patterns -Wpartial-fields -fhide-source-paths library import: common-options build-depends: directory >= 1.3 && < 1.4 , exceptions >= 0.8 && < 0.11 , hslua-marshalling >= 2.1 && < 2.4 , temporary >= 1.2 && < 1.4 exposed-modules: HsLua.Module.System other-modules: HsLua.Module.SystemUtils hs-source-dirs: src test-suite test-hslua-module-system import: common-options type: exitcode-stdio-1.0 main-is: test-hslua-module-system.hs hs-source-dirs: test build-depends: hslua-module-system , tasty >= 0.11 , tasty-hunit >= 0.9 , tasty-lua >= 1.0 && < 1.2 , text hslua-module-system-1.1.0.1/src/HsLua/Module/0000755000000000000000000000000007346545000017012 5ustar0000000000000000hslua-module-system-1.1.0.1/src/HsLua/Module/System.hs0000644000000000000000000003161407346545000020637 0ustar0000000000000000{-| Module : HsLua.Module.System Copyright : © 2019-2023 Albert Krewinkel License : MIT Maintainer : Albert Krewinkel Stability : alpha Portability : Requires GHC 8 or later. Provide a Lua module containing a selection of @'System'@ functions. -} module HsLua.Module.System ( -- * Module documentedModule -- ** Fields , arch , compiler_name , compiler_version , os -- ** Functions , cputime , env , getenv , getwd , ls , mkdir , rmdir , setenv , setwd , tmpdirname , with_env , with_tmpdir , with_wd ) where import Control.Monad (forM_) import Control.Monad.Catch (bracket) import Data.Maybe (fromMaybe) import Data.Text (Text) import Data.Version (versionBranch) import HsLua.Core import HsLua.Marshalling import HsLua.Packaging import HsLua.Module.SystemUtils import qualified Data.Text as T import qualified HsLua.Core as Lua import qualified System.CPUTime as CPUTime import qualified System.Directory as Directory import qualified System.Environment as Env import qualified System.Info as Info import qualified System.IO.Temp as Temp -- | The "system" module. documentedModule :: LuaError e => Module e documentedModule = Module { moduleName = "system" , moduleFields = [ arch , compiler_name , compiler_version , cputime_precision , os ] , moduleFunctions = [ cputime , env , getenv , getwd , ls , mkdir , rmdir , setenv , setwd , tmpdirname , with_env , with_tmpdir , with_wd ] , moduleOperations = [] , moduleTypeInitializers = [] , moduleDescription = "Access to the system's information and file functionality." } -- -- Fields -- -- | Module field containing the machine architecture on which the -- program is running. Wraps @'Info.arch'@ arch :: Field e arch = Field { fieldName = "arch" , fieldType = "string" , fieldDescription = "The machine architecture on which the program is running." , fieldPushValue = pushString Info.arch } -- | Module field containing the Haskell implementation with which the -- host program was compiled. Wraps @'Info.compilerName'@. compiler_name :: Field e compiler_name = Field { fieldName = "compiler_name" , fieldType = "string" , fieldDescription = "The Haskell implementation with which the host " `T.append` "program was compiled." , fieldPushValue = pushString Info.compilerName } -- | Module field containing the version of `compiler_name` with which -- the host program was compiled. compiler_version :: LuaError e => Field e compiler_version = Field { fieldName = "compiler_version" , fieldType = "string" , fieldDescription = T.unwords [ "The Haskell implementation with which the host " , "program was compiled." ] , fieldPushValue = pushList pushIntegral $ versionBranch Info.compilerVersion } -- | Field containing the smallest measurable difference in CPU time. cputime_precision :: Field e cputime_precision = Field { fieldName = "cputime_precision" , fieldType = "integer" , fieldDescription = T.unlines [ "The smallest measurable difference in CPU time that the" , "implementation can record, and is given as an integral number of" , "picoseconds." ] , fieldPushValue = pushIntegral CPUTime.cpuTimePrecision } -- | Field containing the operating system on which the program is -- running. os :: Field e os = Field { fieldName = "os" , fieldType = "string" , fieldDescription = "The operating system on which the program is running." , fieldPushValue = pushString Info.os } -- -- Functions -- -- | Access the CPU time, e.g. for benchmarking. cputime :: LuaError e => DocumentedFunction e cputime = defun "cputime" ### ioToLua CPUTime.getCPUTime =#> functionResult pushIntegral "integer" "CPU time in picoseconds" #? T.unlines [ "Returns the number of picoseconds CPU time used by the current" , "program. The precision of this result may vary in different" , "versions and on different platforms." ] -- | Retrieve the entire environment env :: LuaError e => DocumentedFunction e env = defun "env" ### ioToLua Env.getEnvironment =#> functionResult (pushKeyValuePairs pushString pushString) "table" "A table mapping environment variable names to their value." #? "Retrieves the entire environment as a string-indexed table." -- | Return the current working directory as an absolute path. getwd :: LuaError e => DocumentedFunction e getwd = defun "getwd" ### ioToLua Directory.getCurrentDirectory =#> filepathResult "The current working directory." #? "Obtain the current working directory as an absolute path." -- | Returns the value of an environment variable getenv :: LuaError e => DocumentedFunction e getenv = defun "getenv" ### ioToLua . Env.lookupEnv <#> parameter peekString "string" "var" "name of the environment" =#> functionResult (maybe pushnil pushString) "string or nil" "value of the variable, or nil if the variable is not defined." #? T.unwords [ "Return the value of the environment variable `var`, or `nil` " , "if there is no such value." ] -- | List the contents of a directory. ls :: LuaError e => DocumentedFunction e ls = defun "ls" ### ioToLua . Directory.listDirectory . fromMaybe "." <#> opt (stringParam "directory" ("Path of the directory whose contents should be listed. " `T.append` "Defaults to `.`.")) =#> functionResult (pushList pushString) "table" ("A table of all entries in `directory`, except for the " `T.append` "special entries (`.` and `..`).") #? "List the contents of a directory." -- | Create a new directory which is initially empty, or as near to -- empty as the operating system allows. -- -- If the optional second parameter is `false`, then create the new -- directory only if it doesn't exist yet. If the parameter is `true`, -- then parent directories are created as necessary. mkdir :: LuaError e => DocumentedFunction e mkdir = defun "mkdir" ### (\fp createParent -> if createParent == Just True then ioToLua (Directory.createDirectoryIfMissing True fp) else ioToLua (Directory.createDirectory fp)) <#> filepathParam "dirname" "name of the new directory" <#> opt (boolParam "create_parent" "create parent directory if necessary") =#> [] #? T.concat [ "Create a new directory which is initially empty, or as near " , "to empty as the operating system allows. The function throws " , "an error if the directory cannot be created, e.g., if the " , "parent directory does not exist or if a directory of the " , "same name is already present.\n" , "\n" , "If the optional second parameter is provided and truthy, " , "then all directories, including parent directories, are " , "created as necessary.\n" ] -- | Remove an existing directory. rmdir :: LuaError e => DocumentedFunction e rmdir = defun "rmdir" ### (\fp recursive -> if recursive == Just True then ioToLua (Directory.removeDirectoryRecursive fp) else ioToLua (Directory.removeDirectory fp)) <#> filepathParam "dirname" "name of the directory to delete" <#> opt (boolParam "recursive" "delete content recursively") =#> [] #?("Remove an existing, empty directory. If `recursive` is given, " `T.append` "then delete the directory and its contents recursively.") -- | Set the specified environment variable to a new value. setenv :: LuaError e => DocumentedFunction e setenv = defun "setenv" ### (\name value -> ioToLua (Env.setEnv name value)) <#> parameter peekString "string" "name" "name of the environment variable" <#> parameter peekString "string" "value" "new value" =#> [] #? "Set the specified environment variable to a new value." -- | Change current working directory. setwd :: LuaError e => DocumentedFunction e setwd = defun "setwd" ### ioToLua . Directory.setCurrentDirectory <#> filepathParam "directory" "Path of the new working directory" =#> [] #? "Change the working directory to the given path." -- | Get the current directory for temporary files. tmpdirname :: LuaError e => DocumentedFunction e tmpdirname = defun "tmpdirname" ### ioToLua Directory.getTemporaryDirectory =#> functionResult pushString "string" "The current directory for temporary files." #? mconcat [ "Returns the current directory for temporary files.\n" , "\n" , "On Unix, `tmpdirname()` returns the value of the `TMPDIR` " , "environment variable or \"/tmp\" if the variable isn't defined. " , "On Windows, the function checks for the existence of environment " , "variables in the following order and uses the first path found:\n" , "\n" , "- TMP environment variable.\n" , "- TEMP environment variable.\n" , "- USERPROFILE environment variable.\n" , "- The Windows directory\n" , "\n" , "The operation may fail if the operating system has no notion of " , "temporary directory.\n" , "\n" , "The function doesn't verify whether the path exists.\n" ] -- | Run an action in a different directory, then restore the old -- working directory. with_wd :: LuaError e => DocumentedFunction e with_wd = defun "with_wd" ### (\fp callback -> bracket (Lua.liftIO Directory.getCurrentDirectory) (Lua.liftIO . Directory.setCurrentDirectory) (\_ -> do Lua.liftIO (Directory.setCurrentDirectory fp) callback `invokeWithFilePath` fp)) <#> filepathParam "directory" "Directory in which the given `callback` should be executed" <#> parameter peekCallback "function" "callback" "Action to execute in the given directory" =?> "The results of the call to `callback`." #? T.unwords [ "Run an action within a different directory. This function will" , "change the working directory to `directory`, execute `callback`," , "then switch back to the original working directory, even if an" , "error occurs while running the callback action." ] -- | Run an action, then restore the old environment variable values. with_env :: LuaError e => DocumentedFunction e with_env = defun "with_env" ### (\environment callback -> bracket (Lua.liftIO Env.getEnvironment) setEnvironment (\_ -> setEnvironment environment *> invoke callback)) <#> parameter (peekKeyValuePairs peekString peekString) "table" "environment" ("Environment variables and their values to be set before " `T.append` "running `callback`") <#> parameter peekCallback "function" "callback" "Action to execute in the custom environment" =?> "The results of the call to `callback`." #? T.unwords [ "Run an action within a custom environment. Only the environment" , "variables given by `environment` will be set, when `callback` is" , "called. The original environment is restored after this function" , "finishes, even if an error occurs while running the callback" , "action." ] where setEnvironment newEnv = Lua.liftIO $ do -- Crude, but fast enough: delete all entries in new environment, -- then restore old environment one-by-one. curEnv <- Env.getEnvironment forM_ curEnv (Env.unsetEnv . fst) forM_ newEnv (uncurry Env.setEnv) with_tmpdir :: LuaError e => DocumentedFunction e with_tmpdir = defun "with_tmpdir" ### (\mParentDir tmpl callback -> case mParentDir of Nothing -> do Temp.withSystemTempDirectory tmpl $ invokeWithFilePath callback Just parentDir -> do Temp.withTempDirectory parentDir tmpl $ invokeWithFilePath callback) <#> parameter peekParentDir "string" "parent_dir" (mconcat [ "Parent directory to create the directory in. If this " , "parameter is omitted, the system's canonical temporary " , "directory is used." ]) <#> stringParam "templ" "Directory name template." <#> parameter peekCallback "function" "callback" ("Function which takes the name of the temporary directory as " `T.append` "its first argument.") =?> "The results of the call to `callback`." #? T.unlines [ "Create and use a temporary directory inside the given directory." , "The directory is deleted after the callback returns." ] where peekParentDir idx = do args <- liftLua gettop if args < 3 then liftLua $ do pushnil insert idx return Nothing else Just <$> peekString idx -- -- Parameters -- -- | Filepath function parameter. filepathParam :: Text -- ^ name -> Text -- ^ description -> Parameter e FilePath filepathParam = stringParam -- | Result of a function returning a file path. filepathResult :: Text -- ^ Description -> [FunctionResult e FilePath] filepathResult = functionResult pushString "string" hslua-module-system-1.1.0.1/src/HsLua/Module/SystemUtils.hs0000644000000000000000000000342107346545000021653 0ustar0000000000000000{-| Module : HsLua.Module.SystemUtils Copyright : © 2019-2023 Albert Krewinkel License : MIT Maintainer : Albert Krewinkel Utility functions and types for HsLua's system module. -} module HsLua.Module.SystemUtils ( Callback (..) , peekCallback , invoke , invokeWithFilePath , ioToLua ) where import Control.Exception (IOException, try) import HsLua.Core hiding (try) import HsLua.Marshalling -- | Lua callback function. This type is similar to @'AnyValue'@, and -- the same caveats apply. newtype Callback = Callback StackIndex peekCallback :: Peeker e Callback peekCallback = reportValueOnFailure "function" $ \idx -> do idx' <- absindex idx isFn <- isfunction idx' return $ if isFn then Just $ Callback idx' else Nothing pushCallback :: Pusher e Callback pushCallback (Callback idx) = pushvalue idx -- | Call Lua callback function and return all of its results. invoke :: LuaError e => Callback -> LuaE e NumResults invoke callback = do oldTop <- gettop pushCallback callback call 0 multret newTop <- gettop return . NumResults . fromStackIndex $ newTop - oldTop -- | Call Lua callback function with the given filename as its argument. invokeWithFilePath :: LuaError e => Callback -> FilePath -> LuaE e NumResults invokeWithFilePath callback filename = do oldTop <- gettop pushCallback callback pushString filename call (NumArgs 1) multret newTop <- gettop return . NumResults . fromStackIndex $ newTop - oldTop -- | Convert a System IO operation to a Lua operation. ioToLua :: LuaError e => IO a -> LuaE e a ioToLua action = do result <- liftIO (try action) case result of Right result' -> return result' Left err -> failLua (show (err :: IOException)) hslua-module-system-1.1.0.1/test/0000755000000000000000000000000007346545000014741 5ustar0000000000000000hslua-module-system-1.1.0.1/test/test-hslua-module-system.hs0000644000000000000000000000345507346545000022202 0ustar0000000000000000{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} {-| Module : Main Copyright : © 2019-2023 Albert Krewinkel License : MIT Maintainer : Albert Krewinkel Stability : alpha Portability : Requires language extensions ForeignFunctionInterface, OverloadedStrings. Tests for the `system` Lua module. -} module Main (main) where import Control.Monad (void) import HsLua.Core as Lua import HsLua.Module.System (documentedModule) import HsLua.Packaging.Module (preloadModule, preloadModuleWithName, pushModule, registerModule) import Test.Tasty (TestTree, defaultMain, testGroup) import Test.Tasty.HUnit (assertEqual, testCase) import Test.Tasty.Lua (translateResultsFromFile) main :: IO () main = do luaTestResults <- run @Lua.Exception $ do openlibs registerModule documentedModule pop 1 translateResultsFromFile "test/test-system.lua" defaultMain $ testGroup "hslua-module-system" [tests, luaTestResults] -- | HSpec tests for the Lua 'system' module tests :: TestTree tests = testGroup "HsLua System module" [ testCase "system module can be pushed to the stack" $ run (void (pushModule documentedModule) :: Lua ()) , testCase "system module can be added to the preloader" . run $ do openlibs preloadModule documentedModule assertEqual' "function not added to preloader" TypeFunction =<< do _ <- dostring "return package.preload.system" ltype top , testCase "system module can be loaded as hssystem" . run $ do openlibs preloadModuleWithName documentedModule "hssystem" assertEqual' "loading the module fails " OK =<< dostring "require 'hssystem'" ] assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua () assertEqual' msg expected = liftIO . assertEqual msg expected hslua-module-system-1.1.0.1/test/test-system.lua0000644000000000000000000001504707346545000017754 0ustar0000000000000000-- -- Tests for the system module -- local system = require 'system' local tasty = require 'tasty' local group = tasty.test_group local test = tasty.test_case local assert = tasty.assert --- helper function, combining with_wd and with_tmpdir function in_tmpdir (callback) return function () system.with_tmpdir('test-sytem-tmpdir', function (tmpdir) system.with_wd(tmpdir, callback) end) end end --- dummy string for testing local token = 'Banana' --- check if token can be written into a file in given directory; returns the --- content of this file. function write_read_token (dir, filename) local filename = string.format('%s/%s', dir, 'foo.txt') local fh = io.open(filename, 'w') fh:write(token .. '\n') fh:close() return io.open(filename):read '*l' end -- Check existence static fields return { group 'static fields' { test('arch', function () assert.are_equal(type(system.arch), 'string') end), test('compiler_name', function () assert.are_equal(type(system.compiler_name), 'string') end), test('compiler_version', function () assert.are_equal(type(system.compiler_version), 'table') end), test('cputime_precision', function () assert.are_equal(type(system.cputime_precision), 'number') end), test('os', function () assert.are_equal(type(system.os), 'string') end), }, group 'cputime' { test('returns a number', function () assert.are_equal(type(system.cputime()), 'number') end), }, group 'environment' { test('getenv returns same result as os.getenv', function () assert.are_equal(system.getenv 'PATH', os.getenv 'PATH') end), test('setenv sets environment values', function () system.setenv('HSLUA_SYSTEM_MODULE', 'test') -- apparently this works differently on Windows. local getenv = system.os == 'mingw32' and system.getenv or os.getenv assert.are_equal(getenv 'HSLUA_SYSTEM_MODULE', 'test') end), }, group 'getwd' { test('returns a string', function () assert.are_equal(type(system.getwd()), 'string') end) }, group 'env' { test('returns a table', function () assert.are_equal(type(system.env()), 'table') end) }, group 'ls' { test('returns a table', function () assert.are_equal(type(system.ls('.')), 'table') end), test('lists files in directory', in_tmpdir(function () io.open('README.org', 'w'):close() assert.are_same(system.ls '.', {'README.org'}) end)), test('argument defaults to `.`', function () assert.are_equal(#system.ls('.'), #system.ls()) end), test('fails when arg is not a directory', function () assert.error_matches( function () system.ls('thisdoesnotexist') end, 'thisdoesnotexist' ) assert.error_matches( function () system.ls('README.md') end, 'README%.md' ) end) }, group 'mkdir' { test('create directory', in_tmpdir(function () system.mkdir 'foo' assert.are_equal((system.ls())[1], 'foo') end)), test('create nested directories', in_tmpdir(function () system.mkdir('foo/bar', true) assert.are_equal((system.ls())[1], 'foo') assert.are_equal((system.ls 'foo')[1], 'bar') end)), test('cannot create existing directory', in_tmpdir(function () assert.error_matches(function () system.mkdir '.' end, '%.') end)), test('optionally ignores existing directories', in_tmpdir(function () system.mkdir 'foo' system.mkdir('foo', true) end)), test('normal operation', in_tmpdir(function () system.mkdir 'foo' end)), }, group 'rmdir' { test('remove empty directory', in_tmpdir(function () system.mkdir 'remove-me' system.rmdir 'remove-me' assert.are_same(system.ls(), {}) end)), test('fail if directory is not empty', in_tmpdir(function () system.mkdir('outer/inner', true) assert.error_matches(function () system.rmdir('outer') end, '.') end)), test('optionally delete recursively', in_tmpdir(function () system.mkdir('outer/inner', true) system.rmdir('outer', true) assert.are_same(system.ls(), {}) end)) }, group 'tmpdirname' { test('returns a string', function () assert.are_equal(type(system.tmpdirname()), 'string') end) }, group 'with_env' { test('resets environment', function () -- TODO: this test fails on Windows for unknown reasons and is -- disabled on there for that reason. This needs fixing. if system.os == 'mingw32' then return nil end local outer_value = 'outer test value' local inner_value = 'inner test value' local inner_only = 'test #2' function check_env () assert.are_equal(os.getenv 'HSLUA_SYSTEM_TEST', inner_value) assert.are_equal( os.getenv 'HSLUA_SYSTEM_TEST_INNER_ONLY', inner_only ) assert.is_nil(os.getenv 'HSLUA_SYSTEM_TEST_OUTER_ONLY') end local test_env = { HSLUA_SYSTEM_TEST = inner_value, HSLUA_SYSTEM_TEST_INNER_ONLY = inner_only } system.setenv('HSLUA_SYSTEM_TEST_OUTER_ONLY', outer_value) system.setenv('HSLUA_SYSTEM_TEST', outer_value) system.with_env(test_env, check_env) assert.are_equal(system.getenv 'HSLUA_SYSTEM_TEST', outer_value) assert.is_nil(system.getenv 'HSLUA_SYSTEM_TEST_INNER_ONLY') assert.are_equal( system.getenv 'HSLUA_SYSTEM_TEST_OUTER_ONLY', outer_value ) end) }, group 'with_tmpdir' { test('no base directory given', function () assert.are_equal(system.with_tmpdir('foo', write_read_token), token) end), test('cwd as base directory', function () assert.are_equal(system.with_tmpdir('.', 'foo', write_read_token), token) end), }, group 'with_wd' { test('can change to test directory', function () system.with_wd('test', function () local cwd = system.getwd() assert.is_truthy(cwd:match 'test$') end) end), test('returns to old directory once done', function () local cwd = system.getwd() system.with_wd('test', function () end) assert.are_equal(system.getwd(), cwd) end), test('working directory is passed to callback', function () system.with_wd('test', function (path) assert.is_truthy(system.getwd():match (path .. '$')) end) end), test('all callback results are returned', function () local a, b, c = system.with_wd('test', function (path) return 'a', 'b', 'c' end) assert.are_same({a, b, c}, {'a', 'b', 'c'}) end), }, }