Files
leporello-js/test/self_hosted_test.js

92 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-12-19 15:46:13 +08:00
/*
Loads Leporello then runs tests inside Leporello.
Benchmarks how fast test suite is executed inside leporello
*/
2022-09-10 02:48:13 +08:00
import fs from 'fs'
2022-12-19 15:46:13 +08:00
import * as pathlib from 'path'
import {COMMANDS} from '../src/cmd.js'
import {root_calltree_node} from '../src/calltree.js'
import { assert_equal, test_initial_state, } from './utils.js'
2022-12-23 03:34:09 +08:00
import {tests} from './test.js'
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
// Should work same as src/filesystem.js:load_dir
const load_dir = path => {
const kind = fs.statSync(path).isDirectory() ? 'directory' : 'file'
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
const props = {
path,
name: pathlib.basename(path),
kind,
}
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
if(kind == 'file') {
return {...props, contents: fs.readFileSync(path, 'utf8')}
} else {
return {
...props,
children: fs.readdirSync(path)
.filter(f => !f.startsWith('.'))
.map(file =>
load_dir(pathlib.join(path, file))
)
}
}
}
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
// Convert path to modules relative to '.' into path relative to this file
const adjust_path = path => {
return pathlib.join(
pathlib.relative(
pathlib.dirname(import.meta.url.replace('file://', '')),
pathlib.resolve('.'),
),
path
)
}
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
const load_external_modules = async state => {
const urls = state.loading_external_imports_state.external_imports
const results = await Promise.all(
urls.map(u => import(adjust_path(u)))
)
return Object.fromEntries(
results.map((module, i) => (
[
urls[i],
{
ok: true,
module,
}
]
))
)
}
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
const dir = load_dir('.')
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
console.time('run')
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
const i = test_initial_state(
2022-12-22 22:14:36 +08:00
{}, // files
{project_dir: dir, entrypoint: 'test/run.js'}
2022-12-19 15:46:13 +08:00
)
2022-09-10 02:48:13 +08:00
2022-12-25 16:39:20 +08:00
assert_equal(i.loading_external_imports_state != null, true)
2022-12-19 15:46:13 +08:00
const external_imports = await load_external_modules(i)
const loaded = COMMANDS.external_imports_loaded(i, i, external_imports)
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
assert_equal(loaded.eval_modules_state != null, true)
const s = loaded.eval_modules_state
const result = await s.promise
2023-01-03 01:27:43 +08:00
const state = COMMANDS.eval_modules_finished(loaded, loaded, result, s.node, s.toplevel)
2022-12-22 22:14:36 +08:00
const root = root_calltree_node(state)
const run = root.children[0]
2022-09-10 02:48:13 +08:00
2022-12-19 15:46:13 +08:00
assert_equal(root_calltree_node(state).ok, true)
2022-12-23 02:14:38 +08:00
// Assert that run children are tests
2022-12-23 03:34:09 +08:00
assert_equal(run.children.length, tests.length)
2022-12-23 02:14:38 +08:00
2022-12-19 15:46:13 +08:00
console.timeEnd('run')