Permalink
Browse files

convert test runner to coco

  • Loading branch information...
andrewrk committed Jul 12, 2012
1 parent a228bf0 commit 7addbe329189835098bb75943517f1dbc8dbb0b4
Showing with 100 additions and 96 deletions.
  1. +100 −0 src/test.co
  2. +0 −96 src/test.coffee
View
@@ -0,0 +1,100 @@
+const {exec} = require('child_process')
+const fs = require('fs')
+const path = require('path')
+const async = require('async')
+const temp = require('temp')
+
+const tests_dir = "./tests/"
+const tmp_js_file = "./.test_out.tmp.js"
+
+msg = ""
+pass_count = 0
+fail_count = 0
+
+fs.readdir tests_dir, !(err, files) ->
+ const doTest = !(test_dir, testDone) ->
+ const main_file = path.join(tests_dir, test_dir, "test")
+ const expect_file = path.join(tests_dir, test_dir, "expected.txt")
+ const switches_file = path.join(tests_dir, test_dir, "switches.txt")
+
+ let
+ exec_result = null
+ expected_output = null
+ const execTest = !(cb) ->
+ fs.readFile switches_file, 'utf8', !(err, switches) ->
+ switches = (switches || "").replace(/\n/g, " ")
+ temp.open "", !(err, tmp_js_file) ->
+ const cmdline = "bin/jspackage #switches #main_file #{tmp_js_file.path}"
+ exec cmdline, !(err, stdout, stderr) ->
+ if stderr.length > 0
+ exec_result :=
+ compile: false
+ msg: stderr
+ cb!
+
+ exec "node #{tmp_js_file.path}", !(err, stdout, stderr) ->
+ fs.close tmp_js_file.fd, ->
+ fs.unlink tmp_js_file.path
+
+ if stderr.length > 0
+ exec_result :=
+ compile: true
+ run: false
+ msg: stderr
+ else
+ exec_result :=
+ compile: true
+ run: true
+ output: stdout
+ cb!
+
+ readExpected = !(cb) -> fs.readFile expect_file, \utf8, !(err, out) ->
+ expected_output := out
+ cb!
+
+ async.parallel [execTest, readExpected], !->
+ if exec_result.compile
+ if exec_result.run
+ if exec_result.output is expected_output
+ process.stdout.write(\.)
+ pass_count += 1
+ else
+ process.stdout.write(\F)
+ fail_count += 1
+ msg += """\n
+ ======== #test_dir failed =========
+ -------- Expected Output: ---------
+ #expected_output
+ -------------------------------------
+ -------- Actual Output: ---------
+ #{exec_result.output}
+ -------------------------------------
+ """
+ else
+ process.stdout.write(\E)
+ fail_count += 1
+ msg += """\n
+ ======== #test_dir crashed =========
+ -------- stderr: ---------
+ #{exec_result.msg}
+ -------------------------------------
+ """
+ else
+ process.stdout.write(\X)
+ fail_count += 1
+ msg += """\n
+ ======== #test_dir compile error ====
+ -------- stderr: ---------
+ #{exec_result.msg}
+ -------------------------------------
+ """
+
+ testDone!
+
+ async.map files, doTest, !->
+ if msg.length > 0
+ process.stdout.write(msg)
+ process.stdout.write "\n#pass_count passed, #fail_count failed.\n"
+ fs.unlink tmp_js_file
+ if fail_count > 0
+ process.exit(1)
View
@@ -1,96 +0,0 @@
-{exec} = require("child_process")
-fs = require("fs")
-path = require("path")
-async = require("async")
-temp = require("temp")
-
-tests_dir = "./tests/"
-tmp_js_file = "./.test_out.tmp.js"
-msg = ""
-pass_count = 0
-fail_count = 0
-fs.readdir tests_dir, (err, files) ->
- doTest = (test_dir, testDone) ->
- main_file = path.join(tests_dir, test_dir, "test")
- expect_file = path.join(tests_dir, test_dir, "expected.txt")
- switches_file = path.join(tests_dir, test_dir, "switches.txt")
-
- do (exec_result=null, expected_output=null) ->
- execTest = (cb) ->
- fs.readFile switches_file, 'utf8', (err, switches) ->
- switches = (switches || "").replace(/\n/g, " ")
- temp.open "", (err, tmp_js_file) ->
- cmdline = "bin/jspackage #{switches} #{main_file} #{tmp_js_file.path}"
- exec cmdline, (err, stdout, stderr) ->
- if stderr.length > 0
- exec_result =
- compile: false
- msg: stderr
- cb()
-
- exec "node #{tmp_js_file.path}", (err, stdout, stderr) ->
- fs.close tmp_js_file.fd, ->
- fs.unlink tmp_js_file.path
-
- if stderr.length > 0
- exec_result =
- compile: true
- run: false
- msg: stderr
- else
- exec_result =
- compile: true
- run: true
- output: stdout
- cb()
-
- readExpected = (cb) -> fs.readFile expect_file, 'utf8', (err, out) ->
- expected_output = out
- cb()
-
- async.parallel [execTest, readExpected], ->
- if exec_result.compile
- if exec_result.run
- if exec_result.output is expected_output
- process.stdout.write(".")
- pass_count += 1
- else
- process.stdout.write("F")
- fail_count += 1
- msg += """\n
- ======== #{test_dir} failed =========
- -------- Expected Output: ---------
- #{expected_output}
- -------------------------------------
- -------- Actual Output: ---------
- #{exec_result.output}
- -------------------------------------
- """
- else
- process.stdout.write("E")
- fail_count += 1
- msg += """\n
- ======== #{test_dir} crashed ========
- -------- stderr: ---------
- #{exec_result.msg}
- -------------------------------------
- """
- else
- process.stdout.write("X")
- fail_count += 1
- msg += """\n
- ======== #{test_dir} compile error ==
- -------- stderr: ---------
- #{exec_result.msg}
- -------------------------------------
- """
-
- testDone()
-
- async.map files, doTest, ->
- if msg.length > 0
- process.stdout.write(msg)
- process.stdout.write "\n#{pass_count} passed, #{fail_count} failed.\n"
- fs.unlink tmp_js_file
- if fail_count > 0
- process.exit(1)

0 comments on commit 7addbe3

Please sign in to comment.