2011-02-10 17:24:51 +00:00
--
-- tests/base/test_os.lua
-- Automated test suite for the new OS functions.
2014-04-24 14:49:06 +00:00
-- Copyright (c) 2008-2014 Jason Perkins and the Premake project
2011-02-10 17:24:51 +00:00
--
2012-12-29 18:26:41 +00:00
local suite = test.declare ( " base_os " )
2011-02-10 17:24:51 +00:00
2014-09-17 23:19:47 +00:00
local cwd
function suite . setup ( )
cwd = os.getcwd ( )
os.chdir ( _TESTS_DIR )
end
function suite . teardown ( )
os.chdir ( cwd )
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
--
-- os.findlib() tests
--
function suite . findlib_FindSystemLib ( )
if os.is ( " windows " ) then
test.istrue ( os.findlib ( " user32 " ) )
elseif os.is ( " haiku " ) then
2013-01-12 16:52:59 +00:00
test.istrue ( os.findlib ( " root " ) )
2011-02-10 17:24:51 +00:00
else
2013-01-12 16:52:59 +00:00
test.istrue ( os.findlib ( " m " ) )
2011-02-10 17:24:51 +00:00
end
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . findlib_FailsOnBadLibName ( )
test.isfalse ( os.findlib ( " NoSuchLibraryAsThisOneHere " ) )
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
--
-- os.isfile() tests
--
function suite . isfile_ReturnsTrue_OnExistingFile ( )
2014-09-26 13:25:14 +00:00
test.istrue ( os.isfile ( " _tests.lua " ) )
2011-02-10 17:24:51 +00:00
end
function suite . isfile_ReturnsFalse_OnNonexistantFile ( )
test.isfalse ( os.isfile ( " no_such_file.lua " ) )
end
2014-04-29 19:45:15 +00:00
--
-- os.matchdirs() tests
--
function suite . matchdirs_skipsDottedDirs ( )
local result = os.matchdirs ( " * " )
test.isfalse ( table.contains ( result , " .. " ) )
end
2011-02-10 17:24:51 +00:00
--
-- os.matchfiles() tests
--
function suite . matchfiles_OnNonRecursive ( )
local result = os.matchfiles ( " *.lua " )
2016-05-16 20:27:14 +00:00
test.istrue ( table.contains ( result , " _tests.lua " ) )
2013-01-12 16:52:59 +00:00
test.isfalse ( table.contains ( result , " folder/ok.lua " ) )
2011-02-10 17:24:51 +00:00
end
function suite . matchfiles_Recursive ( )
local result = os.matchfiles ( " **.lua " )
test.istrue ( table.contains ( result , " folder/ok.lua " ) )
end
function suite . matchfiles_SkipsDotDirs_OnRecursive ( )
2015-04-01 17:35:12 +00:00
local result = os.matchfiles ( " **.lua " )
test.isfalse ( table.contains ( result , " .svn/text-base/testfx.lua.svn-base " ) )
2011-02-10 17:24:51 +00:00
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . matchfiles_OnSubfolderMatch ( )
2013-01-12 16:52:59 +00:00
local result = os.matchfiles ( " **/vc2010/* " )
test.istrue ( table.contains ( result , " actions/vstudio/vc2010/test_globals.lua " ) )
2011-02-10 17:24:51 +00:00
test.isfalse ( table.contains ( result , " premake4.lua " ) )
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . matchfiles_OnDotSlashPrefix ( )
local result = os.matchfiles ( " ./**.lua " )
test.istrue ( table.contains ( result , " folder/ok.lua " ) )
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . matchfiles_OnImplicitEndOfString ( )
local result = os.matchfiles ( " folder/*.lua " )
test.istrue ( table.contains ( result , " folder/ok.lua " ) )
test.isfalse ( table.contains ( result , " folder/ok.lua.2 " ) )
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . matchfiles_OnLeadingDotSlashWithPath ( )
local result = os.matchfiles ( " ./folder/*.lua " )
test.istrue ( table.contains ( result , " folder/ok.lua " ) )
end
2014-04-24 14:49:06 +00:00
function suite . matchfiles_OnDottedFile ( )
2016-11-03 11:22:42 +00:00
local result = os.matchfiles ( " base/.* " )
test.istrue ( table.contains ( result , " base/.testDotFile " ) )
2014-04-24 14:49:06 +00:00
end
2015-08-23 18:53:56 +00:00
function suite . matchfiles_onComboSearch ( )
local result = os.matchfiles ( " folder/**/*.txt " )
test.istrue ( table.contains ( result , " folder/subfolder/hello.txt " ) )
end
2011-02-10 17:24:51 +00:00
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
--
-- os.pathsearch() tests
--
function suite . pathsearch_ReturnsNil_OnNotFound ( )
2014-09-18 20:19:08 +00:00
test.istrue ( os.pathsearch ( " nosuchfile " , " aaa;bbb;ccc " ) == nil )
2011-02-10 17:24:51 +00:00
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . pathsearch_ReturnsPath_OnFound ( )
2014-09-26 13:25:14 +00:00
test.isequal ( _TESTS_DIR , os.pathsearch ( " _tests.lua " , _TESTS_DIR ) )
2011-02-10 17:24:51 +00:00
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . pathsearch_FindsFile_OnComplexPath ( )
2014-09-26 13:25:14 +00:00
test.isequal ( _TESTS_DIR , os.pathsearch ( " _tests.lua " , " aaa; " .. _TESTS_DIR .. " ;bbb " ) )
2011-02-10 17:24:51 +00:00
end
2013-01-12 16:52:59 +00:00
2011-02-10 17:24:51 +00:00
function suite . pathsearch_NilPathsAllowed ( )
2014-09-26 13:25:14 +00:00
test.isequal ( _TESTS_DIR , os.pathsearch ( " _tests.lua " , nil , _TESTS_DIR , nil ) )
2011-02-10 17:24:51 +00:00
end
2014-11-27 17:56:48 +00:00
2014-01-25 19:23:13 +00:00
--
2014-01-25 19:31:54 +00:00
-- os.outputof() tests
2014-01-25 19:23:13 +00:00
--
2015-02-03 20:20:16 +00:00
2015-03-10 21:26:03 +00:00
-- Check if outputof returns the command exit code
2014-01-25 19:37:31 +00:00
-- in addition of the command output
2014-01-25 19:23:13 +00:00
function suite . outputof_commandExitCode ( )
if os.is ( " macosx " )
or os.is ( " linux " )
or os.is ( " solaris " )
or os.is ( " bsd " )
then
2014-01-25 19:37:31 +00:00
-- Assumes 'true' and 'false' commands exist
2015-03-10 21:26:03 +00:00
-- which should be the case on all *nix platforms
2014-01-25 19:23:13 +00:00
for cmd , exitcode in pairs ( {
[ " true " ] = 0 ,
[ " false " ] = 1
} )
do
local o , e = os.outputof ( cmd )
test.isequal ( e , exitcode )
end
end
2015-03-10 21:26:03 +00:00
end
2014-11-27 17:56:48 +00:00
--
-- os.translateCommand() tests
--
function suite . translateCommand_onNoToken ( )
2014-11-29 19:51:49 +00:00
test.isequal ( " cp a b " , os.translateCommands ( " cp a b " ) )
2014-11-27 17:56:48 +00:00
end
function suite . translateCommand_callsProcessor ( )
2014-11-29 19:51:49 +00:00
os.commandTokens . test = {
copy = function ( value ) return " test " .. value end
}
test.isequal ( " test a b " , os.translateCommands ( " {COPY} a b " , " test " ) )
2015-02-03 20:20:16 +00:00
end
2015-08-26 13:47:43 +00:00
2017-04-19 16:05:27 +00:00
function suite . translateCommand_callsProcessor_multipleTokens ( )
os.commandTokens . test = {
copy = function ( value ) return " test " .. value end
}
test.isequal ( " test a b; test c d; test e f; " , os.translateCommands ( " {COPY} a b; {COPY} c d; {COPY} e f; " , " test " ) )
end
2015-08-26 13:47:43 +00:00
--
-- os.translateCommand() windows COPY tests
--
function suite . translateCommand_windowsCopyNoDst ( )
test.isequal ( ' IF EXIST a \\ (xcopy /Q /E /Y /I a > nul) ELSE (xcopy /Q /Y /I a > nul) ' , os.translateCommands ( ' {COPY} a ' , " windows " ) )
end
function suite . translateCommand_windowsCopyNoDst_ExtraSpace ( )
2015-10-23 18:45:09 +00:00
test.isequal ( ' IF EXIST a \\ (xcopy /Q /E /Y /I a > nul) ELSE (xcopy /Q /Y /I a > nul) ' , os.translateCommands ( ' {COPY} a ' , " windows " ) )
2015-08-26 13:47:43 +00:00
end
function suite . translateCommand_windowsCopyNoQuotes ( )
test.isequal ( ' IF EXIST a \\ (xcopy /Q /E /Y /I a b > nul) ELSE (xcopy /Q /Y /I a b > nul) ' , os.translateCommands ( ' {COPY} a b ' , " windows " ) )
end
function suite . translateCommand_windowsCopyNoQuotes_ExtraSpace ( )
2015-10-23 18:45:09 +00:00
test.isequal ( ' IF EXIST a \\ (xcopy /Q /E /Y /I a b > nul) ELSE (xcopy /Q /Y /I a b > nul) ' , os.translateCommands ( ' {COPY} a b ' , " windows " ) )
2015-08-26 13:47:43 +00:00
end
function suite . translateCommand_windowsCopyQuotes ( )
test.isequal ( ' IF EXIST "a a" \\ (xcopy /Q /E /Y /I "a a" "b" > nul) ELSE (xcopy /Q /Y /I "a a" "b" > nul) ' , os.translateCommands ( ' {COPY} "a a" "b" ' , " windows " ) )
end
function suite . translateCommand_windowsCopyQuotes_ExtraSpace ( )
2015-10-23 18:45:09 +00:00
test.isequal ( ' IF EXIST "a a" \\ (xcopy /Q /E /Y /I "a a" "b" > nul) ELSE (xcopy /Q /Y /I "a a" "b" > nul) ' , os.translateCommands ( ' {COPY} "a a" "b" ' , " windows " ) )
2015-08-26 13:47:43 +00:00
end
function suite . translateCommand_windowsCopyNoQuotesDst ( )
test.isequal ( ' IF EXIST "a a" \\ (xcopy /Q /E /Y /I "a a" b > nul) ELSE (xcopy /Q /Y /I "a a" b > nul) ' , os.translateCommands ( ' {COPY} "a a" b ' , " windows " ) )
end
function suite . translateCommand_windowsCopyNoQuotesDst_ExtraSpace ( )
2015-10-23 18:45:09 +00:00
test.isequal ( ' IF EXIST "a a" \\ (xcopy /Q /E /Y /I "a a" b > nul) ELSE (xcopy /Q /Y /I "a a" b > nul) ' , os.translateCommands ( ' {COPY} "a a" b ' , " windows " ) )
2015-08-26 13:47:43 +00:00
end
function suite . translateCommand_windowsCopyNoQuotesSrc ( )
test.isequal ( ' IF EXIST a \\ (xcopy /Q /E /Y /I a "b" > nul) ELSE (xcopy /Q /Y /I a "b" > nul) ' , os.translateCommands ( ' {COPY} a "b" ' , " windows " ) )
end
function suite . translateCommand_windowsCopyNoQuotesSrc_ExtraSpace ( )
2015-10-23 18:45:09 +00:00
test.isequal ( ' IF EXIST a \\ (xcopy /Q /E /Y /I a "b" > nul) ELSE (xcopy /Q /Y /I a "b" > nul) ' , os.translateCommands ( ' {COPY} a "b" ' , " windows " ) )
2015-08-26 13:47:43 +00:00
end
2016-12-28 15:43:54 +00:00
--
2017-01-30 17:24:14 +00:00
-- os.getWindowsRegistry windows tests
2016-12-28 15:43:54 +00:00
--
function suite . getreg_nonExistentValue ( )
if os.is ( " windows " ) then
2017-01-30 17:24:14 +00:00
local x = os.getWindowsRegistry ( " HKCU:Should \\ Not \\ Exist \\ At \\ All " )
2016-12-28 15:43:54 +00:00
test.isequal ( nil , x )
end
end
function suite . getreg_nonExistentDefaultValue ( )
if os.is ( " windows " ) then
2017-01-30 17:24:14 +00:00
local x = os.getWindowsRegistry ( " HKCU:Should \\ Not \\ Exist \\ At \\ All \\ " )
2016-12-28 15:43:54 +00:00
test.isequal ( nil , x )
end
end
function suite . getreg_noSeparators ( )
if os.is ( " windows " ) then
2017-01-30 17:24:14 +00:00
os.getWindowsRegistry ( " HKCU:ShouldNotExistAtAll " )
2016-12-28 15:43:54 +00:00
end
end
function suite . getreg_namedValue ( )
if os.is ( " windows " ) then
2017-02-21 17:16:22 +00:00
local x = os.getWindowsRegistry ( " HKCU:Environment \\ TEMP " )
2016-12-28 15:43:54 +00:00
test.istrue ( x ~= nil )
end
end
function suite . getreg_namedValueOptSeparator ( )
if os.is ( " windows " ) then
2017-02-21 17:16:22 +00:00
local x = os.getWindowsRegistry ( " HKCU: \\ Environment \\ TEMP " )
2016-12-28 15:43:54 +00:00
test.istrue ( x ~= nil )
end
end
function suite . getreg_defaultValue ( )
if os.is ( " windows " ) then
2017-02-21 17:16:22 +00:00
local x = os.getWindowsRegistry ( " HKLM:SYSTEM \\ CurrentControlSet \\ Control \\ SafeBoot \\ Minimal \\ AppInfo \\ " )
test.isequal ( " Service " , x )
2016-12-28 15:43:54 +00:00
end
end
2017-02-01 17:19:13 +00:00
--
-- os.getversion tests.
--
function suite . getversion ( )
local version = os.getversion ( ) ;
test.istrue ( version ~= nil )
end