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-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(
|
|
|
|
|
`
|
|
|
|
|
import './test/run.js'
|
|
|
|
|
`,
|
|
|
|
|
{project_dir: dir}
|
|
|
|
|
)
|
|
|
|
|
assert_equal(i.loading_external_imports_state != null, true)
|
2022-09-10 02:48:13 +08:00
|
|
|
|
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
|
|
|
|
|
const state = COMMANDS.eval_modules_finished(loaded , result, s.node, s.toplevel)
|
2022-09-10 02:48:13 +08:00
|
|
|
|
2022-12-19 15:46:13 +08:00
|
|
|
assert_equal(root_calltree_node(state).ok, true)
|
|
|
|
|
|
|
|
|
|
console.timeEnd('run')
|