async function runTests()

in src/index.ts [120:184]


async function runTests(
    dirPath: string,
    onlyTestTsNext: boolean,
    expectOnly: boolean,
    tsLocal: string | undefined,
): Promise<void> {
    const isOlderVersion = /^v(0\.)?\d+$/.test(basename(dirPath));

    const indexText = await readFile(joinPaths(dirPath, "index.d.ts"), "utf-8");
    // If this *is* on DefinitelyTyped, types-publisher will fail if it can't parse the header.
    const dt = indexText.includes("// Type definitions for");
    if (dt) {
        // Someone may have copied text from DefinitelyTyped to their type definition and included a header,
        // so assert that we're really on DefinitelyTyped.
        assertPathIsInDefinitelyTyped(dirPath);
        assertPathIsNotBanned(dirPath);
    }

    const typesVersions = await mapDefinedAsync(await readdir(dirPath), async name => {
        if (name === "tsconfig.json" || name === "tslint.json" || name === "tsutils") { return undefined; }
        const version = withoutPrefix(name, "ts");
        if (version === undefined || !(await stat(joinPaths(dirPath, name))).isDirectory()) { return undefined; }

        if (!TypeScriptVersion.isTypeScriptVersion(version)) {
            throw new Error(`There is an entry named ${name}, but ${version} is not a valid TypeScript version.`);
        }
        if (!TypeScriptVersion.isRedirectable(version)) {
            throw new Error(`At ${dirPath}/${name}: TypeScript version directories only available starting with ts3.1.`);
        }
        return version;
    });

    if (dt) {
        await checkPackageJson(dirPath, typesVersions);
    }

    const minVersion = maxVersion(
        getMinimumTypeScriptVersionFromComment(indexText),
        TypeScriptVersion.lowest) as TypeScriptVersion;
    if (onlyTestTsNext || tsLocal) {
        const tsVersion = tsLocal ? "local" : TypeScriptVersion.latest;
        await testTypesVersion(dirPath, tsVersion, tsVersion, isOlderVersion, dt, expectOnly, tsLocal, /*isLatest*/ true);
    } else {
        // For example, typesVersions of [3.2, 3.5, 3.6] will have
        // associated ts3.2, ts3.5, ts3.6 directories, for
        // <=3.2, <=3.5, <=3.6 respectively; the root level is for 3.7 and above.
        // so this code needs to generate ranges [lowest-3.2, 3.3-3.5, 3.6-3.6, 3.7-latest]
        const lows = [TypeScriptVersion.lowest, ...typesVersions.map(next)];
        const his = [...typesVersions, TypeScriptVersion.latest];
        assert.strictEqual(lows.length, his.length);
        for (let i = 0; i < lows.length; i++) {
            const low = maxVersion(minVersion, lows[i]);
            const hi = his[i];
            assert(
                parseFloat(hi) >= parseFloat(low),
                `'// Minimum TypeScript Version: ${minVersion}' in header skips ts${hi} folder.`);
            const isLatest = hi === TypeScriptVersion.latest;
            const versionPath = isLatest ? dirPath : joinPaths(dirPath, `ts${hi}`);
            if (lows.length > 1) {
                console.log("testing from", low, "to", hi, "in", versionPath);
            }
            await testTypesVersion(versionPath, low, hi, isOlderVersion, dt, expectOnly, undefined, isLatest);
        }
    }
}