import {TestUnitHolder} from './TestUnitHolder'
import {logger} from "../mslib";

export class TestUnitRunner {

    /**
     * 执行当个测试用例
     * @param servicePath
     */
    async run(servicePath: String) {
        const test = TestUnitHolder.get(servicePath)
        const start = new Date().getTime()
        logger.info('准备开始执行---%s', servicePath)
        await test();
        const end = new Date().getTime()
        logger.info('执行完毕[' + (end - start) + 'ms]---%s', servicePath)

    }

    /**
     * 执行全部测试用例
     */
    async runAll() {
        const tests = TestUnitHolder.getAll()
        if (tests.size == 0) {
            logger.info('执行结束，未找到任何单元测试文件！')
            return
        }
        const keys = Array.from(tests.keys())
        let success = 0;
        let servicePath;
        try {
            for (servicePath of keys) {
                await this.run(servicePath);
                success++
            }
            logger.info('执行完毕，共 [%s] 个单元测试，执行成功 [%s] ', keys.length, success)
        } catch (e) {
            logger.error(e)
            logger.error('执行中断，执行单元测试[%s] 发生异常 ', servicePath)
        }
    }
}
