• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

andrzej-woof / stuntman / 8908932552

01 May 2024 11:48AM CUT coverage: 81.27%. Remained the same
8908932552

push

github

andrzej-woof
and again

611 of 668 branches covered (91.47%)

Branch coverage included in aggregate %.

2218 of 2813 relevant lines covered (78.85%)

13.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.36
/packages/server/src/rules/loadRules.ts
1
import fs from 'fs';
1✔
2
import { globSync } from 'glob';
1✔
3
import * as tsImport from 'ts-import';
1✔
4
import { catchAllRule } from './catchAll';
1✔
5
import { echoRule } from './echo';
1✔
6
import { stuntmanConfig, logger, errorToLog } from '@stuntman/shared';
1✔
7
import type * as Stuntman from '@stuntman/shared';
1✔
8

1✔
9
export const DEFAULT_RULES: Stuntman.DeployedRule[] = [catchAllRule, echoRule];
1✔
10

1✔
11
export const loadRules = async (): Promise<Stuntman.DeployedRule[]> => {
1✔
12
    const customRules: Stuntman.DeployedRule[] = [];
2✔
13
    if (!stuntmanConfig.mock.rulesPath || !fs.existsSync(stuntmanConfig.mock.rulesPath)) {
2✔
14
        logger.debug({ rulesPath: stuntmanConfig.mock.rulesPath }, `additional rules directory not found`);
2✔
15
        return [];
2✔
16
    }
2✔
17
    logger.debug({ rulesPath: stuntmanConfig.mock.rulesPath }, `loading additional rules`);
×
18
    const filePaths = globSync('*.[tj]s', { absolute: true, cwd: stuntmanConfig.mock.rulesPath });
×
19
    const tsImportCompiledFileNames = filePaths.map((p) => p.replace(/\.[^/.]+$/u, '.js'));
×
20
    const tsImportDuplicatedCompiledFileNames = tsImportCompiledFileNames.filter(
×
21
        (currentValue, currentIndex) => tsImportCompiledFileNames.indexOf(currentValue) !== currentIndex
×
22
    );
×
23
    if (tsImportDuplicatedCompiledFileNames.length > 0) {
×
24
        logger.error(
×
25
            { tsImportDuplicatedCompiledFileNames },
×
26
            'duplicated compiled file names https://github.com/radarsu/ts-import/issues/32'
×
27
        );
×
28
        throw new Error('duplicated compiled file names https://github.com/radarsu/ts-import/issues/32');
×
29
    }
×
30
    for (const filePath of filePaths) {
×
31
        // TODO add .ts rule support
×
32
        try {
×
33
            const loadedFile = await tsImport.load(filePath, { useCache: false });
×
34
            // eslint-disable-next-line @typescript-eslint/no-var-requires
×
35
            const exportedRules = (Object.values(loadedFile) as Stuntman.DeployedRule[]).filter((rule) => {
×
36
                if (!rule || !rule.id || typeof rule.matches !== 'function') {
×
37
                    logger.error({ filePath, rule }, 'invalid exported rule');
×
38
                    return false;
×
39
                }
×
40
                return true;
×
41
            });
×
42
            customRules.push(...exportedRules);
×
43
        } catch (error) {
×
44
            logger.error({ filePath, error: errorToLog(error as Error) }, 'error importing rule');
×
45
        }
×
46
    }
×
47
    const ruleIds = [...DEFAULT_RULES, ...customRules].map((rule) => rule.id);
×
48
    const duplicatedRuleIds = ruleIds.filter((currentValue, currentIndex) => ruleIds.indexOf(currentValue) !== currentIndex);
×
49
    if (duplicatedRuleIds.length > 0) {
×
50
        logger.error({ duplicatedRuleIds }, 'duplicated rule ids');
×
51
        throw new Error('duplicated rule ids');
×
52
    }
×
53
    return customRules;
×
54
};
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc