Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 35 additions & 22 deletions bin/results.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ he = require 'he'
class TestRunner
constructor: () ->
@astData = []
@taskIdsEnabled = false

configFile = process.argv[7]
if configFile? and fs.existsSync(configFile)
config = JSON.parse(fs.readFileSync(configFile, 'utf8'))
@taskIdsEnabled = config.custom?['flag.tests.task-per-describe'] is true

read_test_cases: (results) =>
fs.readFile process.argv[2], 'utf8', (err, data) =>
if err
@error_handeling()
@error_handling()
else
@read_ast_tree()
.then(() =>
options =
ignoreAttributes: false
parser = new XMLParser(options)
parser = new XMLParser(ignoreAttributes: false)
@get_test_cases(parser.parse(data))
)
.catch((err) => console.log(err))
Expand All @@ -31,7 +35,7 @@ class TestRunner
if typeof obj[key] is 'object'
search_keys(obj[key])
search_keys(result)

test_cases = []

fs.readFile process.argv[6], 'utf8', (err, data) =>
Expand All @@ -42,13 +46,16 @@ class TestRunner
for test_case in matchingKeys
name = he.decode(test_case['@_name'].replace(/'/g, "\'"));
testInfo = @astData.find (element) => he.decode(element.name) is name
return @error_handeling() if testInfo is undefined
return @error_handling() if testInfo is undefined
testCode = file.slice(testInfo["loc"]["start"]["line"], testInfo["loc"]["end"]["line"])
length = testCode[0].match(/^\s*/)[0].length
testCode = (testCode.map (line) => (line.slice(length))).join("\n")
data =
name: test_case['@_name']
test_code: testCode

if @taskIdsEnabled and testInfo.task_id?
data['task_id'] = testInfo.task_id

if test_case.hasOwnProperty('failure')
data['status'] = 'fail'
Expand All @@ -59,7 +66,7 @@ class TestRunner
test_cases.push(data)
@write_result((if test_cases.some (item) -> item.status is "fail" then "fail" else "pass"), test_cases)

error_handeling: () =>
error_handling: () =>
fs.readFile process.argv[4], 'utf8', (err, data) =>
if err
console.log err
Expand All @@ -78,21 +85,27 @@ class TestRunner
)

get_ast_tree: (ast) =>
search_keys = (obj) =>
nextTaskId = 0
search_keys = (obj, taskId) =>
for key in Object.keys(obj)
if typeof obj[key] is 'object' and obj[key] isnt null
if obj[key]["type"]? and obj[key]["expression"]?["callee"]?["name"]?
if obj[key]["type"] is "ExpressionStatement" and obj[key]["expression"]["callee"]["name"] is "it"
name = @read_test_name(obj[key]["expression"]?["arguments"]?[0])
if name? and obj[key]["expression"]?["arguments"]?[1]?["loc"]?
@astData.push({name: name, loc: obj[key]["expression"]["arguments"][1]["loc"]})
else
console.log("incorrect")
else
search_keys(obj[key])
child = obj[key]
continue unless typeof child is 'object' and child?

calleeName = child["expression"]?["callee"]?["name"]
if child["type"] is "ExpressionStatement" and calleeName is "it"
name = @read_test_name(child["expression"]?["arguments"]?[0])
if name? and child["expression"]?["arguments"]?[1]?["loc"]?
@astData.push({name: name, loc: child["expression"]["arguments"][1]["loc"], task_id: taskId})
else
search_keys(obj[key])
search_keys(ast)
console.log("incorrect")
else if child["type"] is "ExpressionStatement" and calleeName is "describe"
nextTaskId += 1
search_keys(child, nextTaskId)
else
search_keys(child, taskId)

for statement in ast.program?.body ? []
search_keys(statement)

read_test_name: (node) =>
return unless node?
Expand All @@ -107,14 +120,14 @@ class TestRunner

write_result: (status, test_cases = [], message = "") ->
result =
version: 2
version: 3
status: status
tests: test_cases
message: message if message isnt ""



fs.writeFile "#{process.argv[3]}/results.json", JSON.stringify(result, null, "\t"), (err) ->
fs.writeFile "#{process.argv[3]}/results.json", "#{JSON.stringify(result, null, "\t")}\n", (err) ->
if err
console.log err
else
Expand Down
3 changes: 2 additions & 1 deletion bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ input_dir="${2%/}"
output_dir="${3%/}"
tests_file="${input_dir}/${slug}.spec.coffee"
original_tests_file="${input_dir}/${slug}.spec.coffee.original"
config_file="${input_dir}/.meta/config.json"
capture_file="${output_dir}/capture"
ast_file="${output_dir}/ast.json"
results_file="${output_dir}/results.json"
Expand All @@ -57,6 +58,6 @@ file=$(find ${output_dir} -type f -name "*.xml")
if test -f "$file"; then
sed_sub_in_place "'" "\\'" "${file}"
fi
node bin/results.js "${file}" "${output_dir}" "${capture_file}" "${ast_file}" ${tests_file}
node bin/results.js "${file}" "${output_dir}" "${capture_file}" "${ast_file}" "${tests_file}" "${config_file}"

mv -f "${original_tests_file}" "${tests_file}"
4 changes: 2 additions & 2 deletions tests/example-all-fail/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "fail",
"tests": [
{
Expand Down Expand Up @@ -87,4 +87,4 @@
"message": "Expected 'I don't know.' to equal 'Fine. Be that way!'."
}
]
}
}
4 changes: 2 additions & 2 deletions tests/example-empty-file/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 2,
"version": 3,
"status": "error",
"tests": [],
"message": "\u001b[31mF\u001b[0m\n\nFailures:\n\n 1) Bob encountered a declaration exception\n Message:\n \u001b[31mTypeError: Bob is not a constructor\u001b[0m\n Stacktrace:\n TypeError: Bob is not a constructor\n at jasmine.Suite.<anonymous> (/solution/example-empty-file.spec.coffee:3:9)\n at Object.<anonymous> (/solution/example-empty-file.spec.coffee:2:1)\n at Object.<anonymous> (/solution/example-empty-file.spec.coffee:1:1)\n at Module._compile (node:internal/modules/cjs/loader:1956:14)\n at Object.loadFile (/opt/test-runner/node_modules/jasmine-node/node_modules/coffeescript/lib/coffee-script/register.js:16:19)\n at Module.load (/opt/test-runner/node_modules/jasmine-node/node_modules/coffeescript/lib/coffee-script/register.js:45:36)\n at Module._load (node:internal/modules/cjs/loader:1470:12)\n at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)\n at Module.require (node:internal/modules/cjs/loader:1701:12)\n at require (node:internal/modules/helpers:318:16)\n at Object.jasmine.executeSpecsInFolder (/opt/test-runner/node_modules/jasmine-node/lib/jasmine-node/index.js:160:9)\n at Object.<anonymous> (/opt/test-runner/node_modules/jasmine-node/lib/jasmine-node/cli.js:247:9)\n at Module._compile (node:internal/modules/cjs/loader:1956:14)\n at Object..js (node:internal/modules/cjs/loader:2096:10)\n at Module.<anonymous> (node:internal/modules/cjs/loader:1678:32)\n at Module._load (node:internal/modules/cjs/loader:1470:12)\n at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)\n at Module.require (node:internal/modules/cjs/loader:1701:12)\n at require (node:internal/modules/helpers:318:16)\n at Object.<anonymous> (/opt/test-runner/node_modules/jasmine-node/bin/jasmine-node:6:1)\n at Module._compile (node:internal/modules/cjs/loader:1956:14)\n at Object..js (node:internal/modules/cjs/loader:2096:10)\n at Module.<anonymous> (node:internal/modules/cjs/loader:1678:32)\n at Module._load (node:internal/modules/cjs/loader:1470:12)\n at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)\n at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)\n at node:internal/main/run_main_module:33:47\n\n\n\n\u001b[31m1 test, 1 assertion, 1 failure, 0 skipped\n\u001b[0m\n\n"
}
}
4 changes: 2 additions & 2 deletions tests/example-multiline-test-name/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "pass",
"tests": [
{
Expand All @@ -9,4 +9,4 @@
"message": null
}
]
}
}
4 changes: 2 additions & 2 deletions tests/example-partial-fail/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "fail",
"tests": [
{
Expand Down Expand Up @@ -87,4 +87,4 @@
"message": "Expected 'Okay. Be that way!' to equal 'Fine. Be that way!'."
}
]
}
}
4 changes: 2 additions & 2 deletions tests/example-success/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 2,
"version": 3,
"status": "pass",
"tests": [
{
Expand Down Expand Up @@ -87,4 +87,4 @@
"message": null
}
]
}
}
4 changes: 2 additions & 2 deletions tests/example-syntax-error/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 2,
"version": 3,
"status": "error",
"tests": [],
"message": "Exception loading: /solution/example-syntax-error.spec.coffee\n[/solution/example-syntax-error.coffee:1:13: error: unexpected @\nclasSSY Bo%b@\n ^] {\n location: { first_line: 0, first_column: 12, last_line: 0, last_column: 12 },\n toString: [Function: syntaxErrorToString],\n code: 'clasSSY Bo%b@',\n filename: '/solution/example-syntax-error.coffee'\n}\n"
}
}
5 changes: 5 additions & 0 deletions tests/example-task-ids/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"custom": {
"flag.tests.task-per-describe": true
}
}
3 changes: 3 additions & 0 deletions tests/example-task-ids/example-task-ids.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
answer = -> 42

module.exports = answer
14 changes: 14 additions & 0 deletions tests/example-task-ids/example-task-ids.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
answer = require './example-task-ids'

# First task by order of appearance, not label, in the file
describe 'task IDs', ->
describe 'second task', ->
it 'Should appear first', ->
expect(answer()).toEqual 42

xit 'Should appear second', ->
expect(answer()).toEqual 42

describe 'first task', ->
xit 'Should appear third', ->
expect(answer()).toEqual 42
27 changes: 27 additions & 0 deletions tests/example-task-ids/expected_results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": 3,
"status": "pass",
"tests": [
{
"name": "Should appear first",
"test_code": "expect(answer()).toEqual 42",
"task_id": 1,
"status": "pass",
"message": null
},
{
"name": "Should appear second",
"test_code": "expect(answer()).toEqual 42",
"task_id": 1,
"status": "pass",
"message": null
},
{
"name": "Should appear third",
"test_code": "expect(answer()).toEqual 42",
"task_id": 2,
"status": "pass",
"message": null
}
]
}