Skip to content

Commit

Permalink
Update for more flexibility and address CR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mariaines committed Aug 2, 2023
1 parent df741f0 commit 140b1f4
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 73 deletions.
147 changes: 127 additions & 20 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import {expect, test} from '@jest/globals'
import {expect, describe, test, beforeAll} from '@jest/globals'
import {getStatsDiff} from '../src/get-stats-diff'
import {getChunkModuleDiff} from '../src/get-chunk-module-diff'
import {
printAssetTablesByGroup,
printChunkModulesTable,
printTotalAssetTable
} from '../src/print-markdown'
import {AssetDiff} from '../src/types'
import {
AssetDiff,
DescribeAssetsOptions,
DescribeAssetsSection,
WebpackStatsDiff,
describeAssetsSections
} from '../src/types'
import {readFile} from 'node:fs/promises'
import {resolve} from 'node:path'
import {StatsCompilation} from 'webpack'
import {getDescribeAssetsOptions} from '../src/main'
import {fail} from 'node:assert'

async function readJsonFile(path: string): Promise<StatsCompilation> {
const data = await readFile(resolve(__dirname, path), 'utf8')
Expand Down Expand Up @@ -122,25 +130,124 @@ test('does not display module information when it does not exist', async () => {
expect(printChunkModulesTable(statsDiff)).toMatchSnapshot()
})

test('does not describe assets when requested', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
describe('printAssetTablesByGroup describes asset sections as requested', () => {
// generate all combinations of sections
const cases: DescribeAssetsOptions[] = []
for (let i = 0; i < Math.pow(2, describeAssetsSections.length); i++) {
const options = {} as DescribeAssetsOptions
for (let n = 0; n < describeAssetsSections.length; n++) {
if ((i >> n) & 1) {
options[describeAssetsSections[n]] = true
} else {
options[describeAssetsSections[n]] = false
}
}
cases.push(options)
}

let statsDiff: WebpackStatsDiff
beforeAll(async () => {
statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/new-stats-assets.json')
)
})

test.each(cases)(
'printAssetTablesByGroup: %j',
(options: DescribeAssetsOptions) => {
const assetTables = printAssetTablesByGroup(statsDiff, options)
for (const [section, included] of Object.entries(options)) {
const sectionHeader = `**${section[0].toUpperCase()}${section.slice(
1
)}**`
if (included) {
expect(assetTables).toContain(sectionHeader)
} else {
expect(assetTables).not.toContain(sectionHeader)
}
}
if (Object.entries(options).every(([, included]) => included === false)) {
expect(assetTables).toBe('')
}
}
)

expect(printAssetTablesByGroup(statsDiff, 'none')).toBe('')
})

test('skips unchanged assets when requested', async () => {
const statsDiff = getStatsDiff(
await readJsonFile('./__mocks__/old-stats-assets.json'),
await readJsonFile('./__mocks__/old-stats-assets.json')
)

const changedOnlyAssetStats = printAssetTablesByGroup(
statsDiff,
'changed-only'
)
expect(changedOnlyAssetStats).not.toContain('**Unchanged**')
expect(changedOnlyAssetStats).toContain('**Added**')
describe('getDescribeAssetsOptions', () => {
test(`getDescribeAssetsOptions: "all"`, () => {
const generatedOptions = getDescribeAssetsOptions('all')
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(true)
}
})

test(`getDescribeAssetsOptions: "none"`, () => {
const generatedOptions = getDescribeAssetsOptions('none')
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(false)
}
})

test(`getDescribeAssetsOptions: "changed-only"`, () => {
const generatedOptions = getDescribeAssetsOptions('changed-only')
for (const section of describeAssetsSections) {
if (section === 'unchanged') {
expect(generatedOptions[section]).toBe(false)
} else {
expect(generatedOptions[section]).toBe(true)
}
}
})

test('getDescribeAssetsOptions: handles keyword with spaces', () => {
const generatedOptions = getDescribeAssetsOptions(' all ')
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(true)
}
})

test('getDescribeAssetsOptions: unsupported option throws', () => {
try {
getDescribeAssetsOptions('unsupported options')
fail('should fail with unsupported options')
} catch (e) {
// pass
}
})

// generate all combinations of sections as string
const cases: string[] = []
for (let i = 0; i < Math.pow(2, describeAssetsSections.length); i++) {
const options: string[] = []
for (let n = 0; n < describeAssetsSections.length; n++) {
if ((i >> n) & 1) {
options.push(describeAssetsSections[n])
}
}
if (options.length > 0) {
cases.push(options.join(' '))
}
}

test.each(cases)(`getDescribeAssetsOptions: %j`, (optionString: string) => {
const generatedOptions = getDescribeAssetsOptions(optionString)
const providedOptions = optionString.split(' ')
for (const section of providedOptions) {
expect(generatedOptions[section as DescribeAssetsSection]).toBe(true)
}
for (const section of describeAssetsSections.filter(
s => !providedOptions.includes(s)
)) {
expect(generatedOptions[section]).toBe(false)
}
})

test('getDescribeAssetsOptions: handles sections with spaces', () => {
const optionString = ' added removed bigger'
const generatedOptions = getDescribeAssetsOptions(optionString)
for (const section of describeAssetsSections) {
expect(generatedOptions[section]).toBe(optionString.includes(section))
}
})
})
12 changes: 11 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ inputs:
description: 'An optional addition to the title, which also helps key comments, useful if running more than 1 copy of this action'
required: false
describe-assets:
description: 'Option for asset description output. One of "all" (default), "changed-only", or "none".'
description: |
Optional specification describing asset changes. May be one of the convenience
keywords "all", "none", or "changed-only" (for all except the unchanged section), OR
a string of space-separated section names, e.g. "added bigger unchanged", where
all sections are:
- added
- removed
- bigger
- smaller
- unchanged
If not provided, "all" is used (equivalent to "added removed bigger smaller unchanged")
required: false
default: 'all'

Expand Down
78 changes: 55 additions & 23 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

50 changes: 42 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@ import {getChunkModuleDiff} from './get-chunk-module-diff'
import {getStatsDiff} from './get-stats-diff'
import {parseStatsFileToJson} from './parse-stats-file-to-json'
import {getCommentBody, getIdentifierComment} from './to-comment-body'
import {isDescribeAssetsOption, DescribeAssetsOptions} from './types'
import {
isDescribeAssetsSection,
DescribeAssetsSection,
DescribeAssetsOptions
} from './types'

export function getDescribeAssetsOptions(
optionString: string
): DescribeAssetsOptions {
optionString = optionString.trim().toLowerCase()
if (optionString === 'all') {
optionString = 'bigger smaller added removed unchanged'
} else if (optionString === 'none') {
optionString = ''
} else if (optionString === 'changed-only') {
optionString = 'bigger smaller added removed'
}
const options = {
added: false,
removed: false,
bigger: false,
smaller: false,
unchanged: false
}
if (optionString) {
const sections = optionString.split(' ').filter(s => !!s)
if (sections.some(s => !isDescribeAssetsSection(s))) {
throw new Error(
`Unsupported options for 'describe-assets': '${optionString}' contains unexpected sections`
)
}
for (const s of sections as DescribeAssetsSection[]) {
options[s] = true
}
}
return options
}

async function run(): Promise<void> {
try {
Expand All @@ -23,12 +59,10 @@ async function run(): Promise<void> {
const token = core.getInput('github-token')
const currentStatsJsonPath = core.getInput('current-stats-json-path')
const baseStatsJsonPath = core.getInput('base-stats-json-path')
const describeAssetsOption = core.getInput('describe-assets')
if (!isDescribeAssetsOption(describeAssetsOption)) {
throw new Error(
`Unsupported options for 'describe-assets': ${describeAssetsOption} is not one of ${DescribeAssetsOptions}`
)
}
const describeAssetsOptionString = core.getInput('describe-assets')
const describeAssetsOptions = getDescribeAssetsOptions(
describeAssetsOptionString
)
const title = core.getInput('title') ?? ''
const {rest} = getOctokit(token)

Expand Down Expand Up @@ -59,7 +93,7 @@ async function run(): Promise<void> {
statsDiff,
chunkModuleDiff,
title,
describeAssetsOption
describeAssetsOptions
)

const promises: Promise<unknown>[] = []
Expand Down
Loading

0 comments on commit 140b1f4

Please sign in to comment.