-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverage.ts
118 lines (99 loc) · 4.81 KB
/
coverage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) 2021, jayree
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import {
SfCommand,
requiredOrgFlagWithDeprecations,
orgApiVersionFlagWithDeprecations,
} from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
// eslint-disable-next-line no-underscore-dangle
const __filename = fileURLToPath(import.meta.url);
// eslint-disable-next-line no-underscore-dangle
const __dirname = dirname(__filename);
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@jayree/sfdx-plugin-org', 'flowtestcoverage');
export default class FlowTestCoverage extends SfCommand<AnyJson> {
public static readonly summary = messages.getMessage('commandDescription');
public static readonly examples = [
`$ sfdx jayree:flowtestcoverage
=== Flow Test Coverage
Coverage: 82%
...
`,
];
public static readonly flags = {
'target-org': requiredOrgFlagWithDeprecations,
'api-version': orgApiVersionFlagWithDeprecations,
};
public static readonly deprecateAliases = true;
public static readonly aliases = ['jayree:flowtestcoverage'];
public async run(): Promise<AnyJson> {
const { flags } = await this.parse(FlowTestCoverage);
const org = flags['target-org'];
const conn = org.getConnection(flags['api-version']);
const query1 = await conn.tooling.query<{ expr0: number }>(
"SELECT count_distinct(DefinitionId) FROM Flow WHERE Status = 'Active' AND(ProcessType = 'AutolaunchedFlow' OR ProcessType = 'Workflow' OR ProcessType = 'CustomEvent' OR ProcessType = 'InvocableProcess')",
);
const numberOfActiveAutolaunchedFlowsAndProcesses = query1.records[0]['expr0'];
const query2 = await conn.tooling.query<{ expr0: number }>(
'SELECT count_distinct(FlowVersionId) FROM FlowTestCoverage',
);
const numberOfCoveredActiveAutolaunchedFlowsAndProcesses = query2.records[0]['expr0'];
const query3 = await conn.tooling.query<{ Definition: { DeveloperName: string } }>(
"SELECT Definition.DeveloperName FROM Flow WHERE Status = 'Active' AND(ProcessType = 'AutolaunchedFlow' OR ProcessType = 'Workflow' OR ProcessType = 'CustomEvent' OR ProcessType = 'InvocableProcess') AND Id NOT IN(SELECT FlowVersionId FROM FlowTestCoverage)",
);
const uncovered = query3.records.map((value) => value.Definition.DeveloperName);
const query4 = await conn.tooling.query<{ DeveloperName: string }>(
'SELECT FlowVersion.Definition.DeveloperName FROM FlowTestCoverage GROUP BY FlowVersion.Definition.DeveloperName',
);
const covered = query4.records.map((value) => value.DeveloperName);
this.styledHeader('Flow Test Coverage');
this.styledObject({
'number of active autolaunched flows and processes': numberOfActiveAutolaunchedFlowsAndProcesses,
'number of covered active autolaunched flows and processes': numberOfCoveredActiveAutolaunchedFlowsAndProcesses,
Coverage: `${Math.floor(
(numberOfCoveredActiveAutolaunchedFlowsAndProcesses / numberOfActiveAutolaunchedFlowsAndProcesses) * 100,
)}%'`,
});
const x = [];
const length = uncovered.length > covered.length ? uncovered.length : covered.length;
for (let i = 0; i < length; i++) {
x.push({
'all active autolaunched flows and processes that don’t have test coverage': uncovered[i],
'all flows and processes that have test coverage': covered[i],
});
}
this.table(x, {
TESTCOVERAGE: {
header: 'all flows and processes that have test coverage',
get: (row) => row['all flows and processes that have test coverage'],
},
NOTESTCOVERAGE: {
header: 'all active autolaunched flows and processes that don’t have test coverage',
get: (row) => row['all active autolaunched flows and processes that don’t have test coverage'],
},
});
if (covered.length !== numberOfCoveredActiveAutolaunchedFlowsAndProcesses) {
this.warn(
'Error in the FlowTestCoverage table found, please delete all records in the FlowTestCoverage table, then run all tests, and use this command to check the Flow Test Coverage again',
);
}
return {
orgId: org.getOrgId(),
Coverage: `${Math.floor(
(numberOfCoveredActiveAutolaunchedFlowsAndProcesses / numberOfActiveAutolaunchedFlowsAndProcesses) * 100,
)}%'`,
numberOfActiveAutolaunchedFlowsAndProcesses,
numberOfCoveredActiveAutolaunchedFlowsAndProcesses,
'all active autolaunched flows and processes that don’t have test coverage': uncovered,
'all flows and processes that have test coverage': covered,
};
}
}