Testing.icl 2.59 KB
Newer Older
1
implementation module iTasks.Extensions.Development.Testing
2
import iTasks
3
import System.Time
4 5 6

import Testing.TestEvents 
import iTasks.Util.Testing 
7
import iTasks.Extensions.Files
8
import iTasks.Extensions.Development.Tools
9 10
import iTasks.Extensions.Development.Codebase
import Text, Data.Tuple, Data.Error, Data.Func, System.FilePath, System.OS
11

12
derive class iTask EndEventType
13 14 15 16 17 18 19 20 21 22 23 24 25
derive gEditor EndEvent, FailReason, FailedAssertion, CounterExample, Relation
derive gText EndEvent, FailReason, FailedAssertion, CounterExample, Relation
derive gDefault EndEvent, FailReason, FailedAssertion, CounterExample, Relation
derive gEq EndEvent, FailReason, FailedAssertion, CounterExample, Relation

compileTestModule :: CleanModuleName -> Task EndEvent
compileTestModule (path,name)
	=           copyFile prjDefaultPath prjPath
	>>- \_   -> get cpmExecutable
	>>- \cpm -> runWithOutput cpm [prjPath] Nothing //Build the test
	@   \(c,o) -> if (passed c o) 
			{name = testName, event = Passed, message = join "" o}
			{name = testName, event = (Failed Nothing), message = join "" o}
26
where
27 28 29 30 31
	testName = "Compile: " +++ name
	iclPath = cleanFilePath (path,name,Icl)
	prjDefaultPath = path </> name +++ ".prj.default"
	prjPath = path </> name +++ ".prj"

32 33
    //Cpm still returns exitcode 0 on failure, so we have to check the output
	passed 0 o = let lines = split OS_NEWLINE (join "" o) in not (any isErrorLine lines) 
34 35
	passed _ _ = False

36 37
 	isErrorLine l = startsWith "Error" l || startsWith "Type error" l || startsWith "Parse error" l

38
//Copy-paste.. should be in library
39 40 41 42 43 44
runTestModule :: CleanModuleName -> Task [EndEvent]
runTestModule (path,name)
	=   compileTestModule (path,name)
	>>- \res=:{EndEvent|event} -> case event of
		Passed = runWithOutput exe [] Nothing @ (parseTestResults o appSnd (join "")) //Run the test
	    _      = return [res]
45
where
46
	exe = IF_WINDOWS (base </> addExtension name "exe") (path </> name)
47 48
	baseDir = takeDirectory path
	base = dropExtension path
49

50 51
	parseTestResults (ecode,output)
		# lines = split OS_NEWLINE output
52
		| length lines < 2 = fallback ecode output
53
		= [res \\ Just res <- map (fromJSON o fromString) lines]		
54 55
	where
		//If we can't parse the output, We'll treat it as a single simple test executable
56 57
		fallback 0 _ = [{name=name,event=Passed,message="Execution returned 0"}]
		fallback _ output = [{name=name,event=Failed Nothing,message=output}]
58

59 60 61 62
runWithOutput :: FilePath [String] (Maybe FilePath) -> Task (Int,[String])
runWithOutput prog args dir = withShared ([], []) \out->withShared [] \stdin->
	externalProcess {tv_sec=0,tv_nsec=100000000} prog args dir Nothing stdin out
	>>- \c->get out @ tuple c o fst