-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP Generate reports on private API usage
(and upload them) TODO if this is going to go into main, then it needs to handle hooks and test definition still
- Loading branch information
1 parent
22a7caf
commit 38651a9
Showing
15 changed files
with
606 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { stringify as csvStringify } from 'csv-stringify/sync'; | ||
import { writeFileSync, existsSync, mkdirSync } from 'fs'; | ||
import { PrivateApiContextDto, TestStartRecord } from './dto'; | ||
import { ContextGroup } from './grouping'; | ||
import { RuntimeContext } from './runtimeContext'; | ||
import { StaticContext } from './staticContext'; | ||
import path from 'path'; | ||
|
||
function suiteAtLevelForCSV(suites: string[] | null, level: number) { | ||
return suites?.[level] ?? ''; | ||
} | ||
|
||
function suitesColumnsForCSV(suites: string[] | null, maxSuiteLevel: number) { | ||
const result: string[] = []; | ||
for (let i = 0; i < maxSuiteLevel; i++) { | ||
result.push(suiteAtLevelForCSV(suites, i)); | ||
} | ||
return result; | ||
} | ||
|
||
function suitesHeaders(maxSuiteLevel: number) { | ||
const result: string[] = []; | ||
for (let i = 0; i < maxSuiteLevel; i++) { | ||
result.push(`Suite (level ${i + 1})`); | ||
} | ||
return result; | ||
} | ||
|
||
function commonHeaders(maxSuiteLevel: number) { | ||
return ['File', ...suitesHeaders(maxSuiteLevel), 'Description'].filter((val) => val !== null) as string[]; | ||
} | ||
|
||
function writeCSVData(rows: string[][], name: string) { | ||
const outputDirectoryPath = path.join(__dirname, '..', '..', 'private-api-usage-reports'); | ||
|
||
if (!existsSync(outputDirectoryPath)) { | ||
mkdirSync(outputDirectoryPath); | ||
} | ||
|
||
const result = csvStringify(rows); | ||
writeFileSync(path.join(outputDirectoryPath, `${name}.csv`), result); | ||
} | ||
|
||
export function writeRuntimePrivateAPIUsageCSV(contextGroups: ContextGroup<RuntimeContext>[]) { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max#getting_the_maximum_element_of_an_array | ||
const maxSuiteLevel = contextGroups | ||
.map((group) => (group.key.suite ?? []).length) | ||
.reduce((a, b) => Math.max(a, b), -Infinity); | ||
|
||
const columnHeaders = [ | ||
'Context', | ||
...commonHeaders(maxSuiteLevel), | ||
'Via parameterised test helper', | ||
'Via misc. helpers', | ||
'Private API called', | ||
]; | ||
|
||
const csvRows = contextGroups | ||
.map((contextGroup) => { | ||
const runtimeContext = contextGroup.key; | ||
|
||
const contextColumns = [ | ||
runtimeContext.type, | ||
runtimeContext.file ?? '', | ||
...suitesColumnsForCSV(runtimeContext.suite, maxSuiteLevel), | ||
runtimeContext.type === 'definition' ? runtimeContext.label : runtimeContext.title, | ||
]; | ||
|
||
return contextGroup.usages.map((usage) => [ | ||
...contextColumns, | ||
(usage.context.type === 'test' ? usage.context.parameterisedTestTitle : null) ?? '', | ||
[...usage.context.helperStack].reverse().join(' -> '), | ||
usage.privateAPIIdentifier, | ||
]); | ||
}) | ||
.flat(); | ||
|
||
writeCSVData([columnHeaders, ...csvRows], 'runtime-private-api-usage'); | ||
} | ||
|
||
export function writeStaticPrivateAPIUsageCSV(contextGroups: ContextGroup<StaticContext>[]) { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max#getting_the_maximum_element_of_an_array | ||
const maxSuiteLevel = contextGroups | ||
.map((group) => (group.key.type === 'miscHelper' ? [] : group.key.suite).length) | ||
.reduce((a, b) => Math.max(a, b), -Infinity); | ||
|
||
const columnHeaders = ['Context', ...commonHeaders(maxSuiteLevel), 'Private API called']; | ||
|
||
const csvRows = contextGroups | ||
.map((contextGroup) => { | ||
const staticContext = contextGroup.key; | ||
|
||
const contextColumns = [ | ||
staticContext.type, | ||
staticContext.type === 'miscHelper' ? '' : staticContext.file, | ||
...suitesColumnsForCSV(staticContext.type === 'miscHelper' ? null : staticContext.suite, maxSuiteLevel), | ||
staticContext.type === 'test' ? staticContext.title : staticContext.helperName, | ||
]; | ||
|
||
return contextGroup.usages.map((usage) => [...contextColumns, usage.privateAPIIdentifier]); | ||
}) | ||
.flat(); | ||
|
||
writeCSVData([columnHeaders, ...csvRows], 'static-private-api-usage'); | ||
} | ||
|
||
export function writeNoPrivateAPIUsageCSV(testStartRecords: TestStartRecord[]) { | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max#getting_the_maximum_element_of_an_array | ||
const maxSuiteLevel = testStartRecords | ||
.map((record) => record.context.suite.length) | ||
.reduce((a, b) => Math.max(a, b), -Infinity); | ||
|
||
const columnHeaders = commonHeaders(maxSuiteLevel); | ||
|
||
const csvRows = testStartRecords.map((record) => { | ||
const context = record.context; | ||
return [context.file, ...suitesColumnsForCSV(context.suite, maxSuiteLevel), context.title]; | ||
}); | ||
|
||
writeCSVData([columnHeaders, ...csvRows], 'tests-that-do-not-use-private-api'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export type TestPrivateApiContextDto = { | ||
type: 'test'; | ||
title: string; | ||
/** | ||
* null means that either the test isn’t parameterised or that this usage is unique to the specific parameter | ||
*/ | ||
parameterisedTestTitle: string | null; | ||
helperStack: string[]; | ||
file: string; | ||
suite: string[]; | ||
}; | ||
|
||
export type HookPrivateApiContextDto = { | ||
type: 'hook'; | ||
title: string; | ||
helperStack: string[]; | ||
file: string; | ||
suite: string[]; | ||
}; | ||
|
||
export type RootHookPrivateApiContextDto = { | ||
type: 'hook'; | ||
title: string; | ||
helperStack: string[]; | ||
file: null; | ||
suite: null; | ||
}; | ||
|
||
export type TestDefinitionPrivateApiContextDto = { | ||
type: 'definition'; | ||
label: string; | ||
helperStack: string[]; | ||
file: string; | ||
suite: string[]; | ||
}; | ||
|
||
export type PrivateApiContextDto = | ||
| TestPrivateApiContextDto | ||
| HookPrivateApiContextDto | ||
| RootHookPrivateApiContextDto | ||
| TestDefinitionPrivateApiContextDto; | ||
|
||
export type PrivateApiUsageDto = { | ||
context: PrivateApiContextDto; | ||
privateAPIIdentifier: string; | ||
}; | ||
|
||
export type TestStartRecord = { | ||
context: TestPrivateApiContextDto; | ||
privateAPIIdentifier: null; | ||
}; | ||
|
||
export type Record = PrivateApiUsageDto | TestStartRecord; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { PrivateApiUsageDto } from './dto'; | ||
|
||
type ExclusionRule = { | ||
privateAPIIdentifier: string; | ||
// i.e. only ignore when called from within this helper | ||
helper?: string; | ||
}; | ||
|
||
export function applyingExclusions(usageDtos: PrivateApiUsageDto[]) { | ||
const exclusionRules: ExclusionRule[] = [ | ||
// This is all helper stuff that we could pull into the test suite, and which for now we could just continue using the version privately exposed by ably-js, even in the UTS. | ||
{ privateAPIIdentifier: 'call.BufferUtils.areBuffersEqual' }, | ||
{ privateAPIIdentifier: 'call.BufferUtils.base64Decode' }, | ||
{ privateAPIIdentifier: 'call.BufferUtils.base64Encode' }, | ||
{ privateAPIIdentifier: 'call.BufferUtils.hexEncode' }, | ||
{ privateAPIIdentifier: 'call.BufferUtils.isBuffer' }, | ||
{ privateAPIIdentifier: 'call.BufferUtils.toArrayBuffer' }, | ||
{ privateAPIIdentifier: 'call.BufferUtils.utf8Encode' }, | ||
{ privateAPIIdentifier: 'call.Utils.copy' }, | ||
{ privateAPIIdentifier: 'call.Utils.inspectError' }, | ||
{ privateAPIIdentifier: 'call.Utils.keysArray' }, | ||
{ privateAPIIdentifier: 'call.Utils.mixin' }, | ||
{ privateAPIIdentifier: 'call.Utils.toQueryString' }, | ||
{ privateAPIIdentifier: 'call.msgpack.decode' }, | ||
{ privateAPIIdentifier: 'call.msgpack.encode' }, | ||
{ privateAPIIdentifier: 'call.http.doUri', helper: 'getJWT' }, | ||
|
||
// I’m going to exclude this use of nextTick because I think it’s one where maybe the logic applies cross-platform (but it could turn out I’m wrong about that one, in which case we’ll need to re-assess) | ||
{ privateAPIIdentifier: 'call.Platform.nextTick', helper: 'callbackOnClose' }, | ||
]; | ||
|
||
return usageDtos.filter( | ||
(usageDto) => | ||
!exclusionRules.some( | ||
(exclusionRule) => | ||
exclusionRule.privateAPIIdentifier === usageDto.privateAPIIdentifier && | ||
(!('helper' in exclusionRule) || usageDto.context.helperStack.includes(exclusionRule.helper!)), | ||
), | ||
); | ||
} |
Oops, something went wrong.