Skip to content

Commit

Permalink
chore: sort snapshots by key
Browse files Browse the repository at this point in the history
  • Loading branch information
eoinsha committed Dec 1, 2023
1 parent 42f5b4b commit 6e98ebb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cf-macro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { setLogger } from 'slic-watch-core/logging'
import { type SlicWatchConfig, resolveSlicWatchConfig } from 'slic-watch-core/inputs/general-config'
import { type Template } from 'cloudform-types'

const logger = pino({ name: 'macroHandler', level: process.env.DEBUG_LEVEL ?? 'DEBUG' })
const logger = pino({ name: 'macroHandler', level: process.env.DEBUG_LEVEL ?? 'debug' })

setLogger(logger)

Expand Down
2 changes: 1 addition & 1 deletion test-utils/sls-test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pino from 'pino'
const extras = ['levels', 'silent', 'onChild', 'trace', 'debug', 'info', 'warn', 'error', 'fatal']
const pinoLogger = pino()
export const dummyLogger = Object.fromEntries(
Object.entries(pinoLogger).filter(([key]) => !extras.includes(key))
Object.entries(pinoLogger).filter(([key]) => !extras.includes(key as string))
)

export const pluginUtils = { log: dummyLogger }
Expand Down
20 changes: 18 additions & 2 deletions test-utils/snapshot-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ import { type Test } from 'tap'
* @returns The formatted snapshot
*/
export function formatSnapshot (template: Template): string {
return JSON.stringify(template, null, ' ')
return JSON.stringify(sortObject(template), null, ' ')
}

/**
* Create a copy of the object with every nested property sorted alphabetically for easier comparison
*/
export function sortObject (obj: object): object {
const sortedObj: Record<string, any> = {}
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
const obj = value as any
sortedObj[key] = sortObject(typeof obj.toJSON === 'function' ? obj.toJSON() : obj)
} else {
sortedObj[key] = value
}
}
return sortedObj as object
}

/**
Expand All @@ -27,7 +43,7 @@ export function cleanSnapshot (snapshot: string): string {
([key, value]) => {
const resource = value
if (resource.Type === 'AWS::CloudWatch::Dashboard') {
const dashBody = (resource.Properties as DashboardProperties).DashboardBody
const dashBody = (resource.Properties as DashboardProperties).DashboardBody as any
const subValue = dashBody['Fn::Sub']
const parsedValue = JSON.parse(subValue)
return [key, {
Expand Down

0 comments on commit 6e98ebb

Please sign in to comment.