Skip to content

Commit

Permalink
Merge pull request #42 from jenskeiner/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
jenskeiner authored Sep 6, 2024
2 parents a540361 + 641ae5b commit b4dcda1
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 12 deletions.
14 changes: 13 additions & 1 deletion .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,19 @@ rules:
'@typescript-eslint/no-require-imports': 'error',
'@typescript-eslint/no-unnecessary-qualifier': 'error',
'@typescript-eslint/no-unnecessary-type-assertion': 'error',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-unused-vars':
[
'error',
{
'args': 'all',
'argsIgnorePattern': '^_',
'caughtErrors': 'all',
'caughtErrorsIgnorePattern': '^_',
'destructuredArrayIgnorePattern': '^_',
'varsIgnorePattern': '^_',
'ignoreRestSiblings': true
}
],
'@typescript-eslint/no-useless-constructor': 'error',
'@typescript-eslint/no-var-requires': 'error',
'@typescript-eslint/prefer-for-of': 'warn',
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
![CI](https://github.com/jenskeiner/ghcr-container-repository-cleanup-action/actions/workflows/ci.yml/badge.svg?branch=main)
[![Check dist/](https://github.com/jenskeiner/ghcr-container-repository-cleanup-action/actions/workflows/autofix.yml/badge.svg?branch=main)](https://github.com/jenskeiner/ghcr-container-repository-cleanup-action/actions/workflows/autofix.yml)
[![CodeQL](https://github.com/jenskeiner/ghcr-container-repository-cleanup-action/actions/workflows/codeql-analysis.yml/badge.svg?branch=main)](https://github.com/jenskeiner/ghcr-container-repository-cleanup-action/actions/workflows/codeql-analysis.yml)
[![codecov](https://codecov.io/github/jenskeiner/ghcr-container-repository-cleanup-action/branch/main/graph/badge.svg?token=X84LE2UUQT)](https://codecov.io/github/jenskeiner/ghcr-container-repository-cleanup-action)

This GitHub Action deletes tags and versions (container images) from a GitHub
Container Registry (ghcr.io) package repository. It is based on
Expand Down
4 changes: 2 additions & 2 deletions citester/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41032,7 +41032,7 @@ class Config {
return true;
}
},
onSecondaryRateLimit: (retryAfter, options, octokit) => {
onSecondaryRateLimit: (_retryAfter, options, octokit) => {
// does not retry, only logs a warning
octokit.log.warn(`SecondaryRateLimit detected for request ${options.method} ${options.url}`);
}
Expand Down Expand Up @@ -46714,7 +46714,7 @@ class PackageVersionModel {



const Ajv = (jtd_default()).Ajv;
const Ajv = (jtd_default())["default"] || (jtd_default());
const ajv = new Ajv();
const parseManifest0 = ajv.compileParser(manifestSchema);
function parseManifest(jsonString) {
Expand Down
2 changes: 1 addition & 1 deletion citester/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40063,7 +40063,7 @@ class Config {
return true;
}
},
onSecondaryRateLimit: (retryAfter, options, octokit) => {
onSecondaryRateLimit: (_retryAfter, options, octokit) => {
// does not retry, only logs a warning
octokit.log.warn(`SecondaryRateLimit detected for request ${options.method} ${options.url}`);
}
Expand Down Expand Up @@ -45718,7 +45718,7 @@ class PackageVersionModel {



const Ajv = (jtd_default()).Ajv;
const Ajv = (jtd_default())["default"] || (jtd_default());
const ajv = new Ajv();
const parseManifest0 = ajv.compileParser(manifestSchema);
function parseManifest(jsonString) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Config {
}
},
onSecondaryRateLimit: (
retryAfter: number,
_retryAfter: number,
options: EndpointDefaults,
octokit: Octokit
) => {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PackageVersion
} from './models.js'

const Ajv = AjvModule.Ajv
const Ajv = (AjvModule as any).default || AjvModule
const ajv = new Ajv()

const parseManifest0 = ajv.compileParser(manifestSchema)
Expand Down
113 changes: 113 additions & 0 deletions src/tree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { renderTree } from './tree'

describe('renderTree', () => {
// Helper function to capture console.log output
function captureConsoleLog(fn: () => void): string[] {
const logs: string[] = []
const originalLog = console.log
console.log = (message: string) => logs.push(message)
fn()
console.log = originalLog
return logs
}

test('renders a tree with single root and no children', () => {
const root = { value: 'Root' }
const getChildren = (_node: any): any[] => []
const renderNode = (node: any, prefix: string): void =>
console.log(`${prefix}${node.value}`)

const output = captureConsoleLog(() =>
renderTree(root, getChildren, renderNode)
)

expect(output).toEqual(['Root'])
})

test('renders a tree with root and multiple children', () => {
const root = {
value: 'Root',
children: [{ value: 'Child 1' }, { value: 'Child 2' }]
}
const getChildren = (node: any): any[] => node.children
const renderNode = (node: any, prefix: string): void =>
console.log(`${prefix}${node.value}`)

const output = captureConsoleLog(() =>
renderTree(root, getChildren, renderNode)
)

expect(output).toEqual(['Root', ' ├─Child 1', ' └─Child 2'])
})

test('renders a tree with multiple levels of nesting', () => {
const root = {
value: 'Root',
children: [
{
value: 'Child 1',
children: [{ value: 'Grandchild 1.1' }, { value: 'Grandchild 1.2' }]
},
{ value: 'Child 2' }
]
}
const getChildren = (node: any): any[] => node.children
const renderNode = (node: any, prefix: string): void =>
console.log(`${prefix}${node.value}`)

const output = captureConsoleLog(() =>
renderTree(root, getChildren, renderNode)
)

expect(output).toEqual([
'Root',
' ├─Child 1',
' │ ├─Grandchild 1.1',
' │ └─Grandchild 1.2',
' └─Child 2'
])
})

test('renders an empty tree (edge case)', () => {
const root = null
const getChildren = (_node: any): any[] => []
const renderNode = (node: any, prefix: string): void =>
console.log(`${prefix}${node}`)

const output = captureConsoleLog(() =>
renderTree(root, getChildren, renderNode)
)

expect(output).toEqual(['null'])
})

test('renders a tree with custom node rendering', () => {
const getChildren = (node: any): any[] => node.children
const renderNode = (node: any, prefix: string): void =>
console.log(`${prefix}[${node.id}] ${node.name}`)

const tree = {
id: 1,
name: 'Root',
children: [
{ id: 2, name: 'Child 1' },
{
id: 3,
name: 'Child 2',
children: [{ id: 4, name: 'Grandchild 2.1' }]
}
]
}

const output = captureConsoleLog(() =>
renderTree(tree, getChildren, renderNode)
)

expect(output).toEqual([
'[1] Root',
' ├─[2] Child 1',
' └─[3] Child 2',
' └─[4] Grandchild 2.1'
])
})
})
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "node16",
"module": "ES2022",
"rootDir": "./src",
"moduleResolution": "node16",
"moduleResolution": "node",
"baseUrl": "./",
"sourceMap": true,
"outDir": "./dist",
Expand All @@ -15,7 +15,8 @@
"skipLibCheck": true,
"newLine": "lf",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"resolveJsonModule": true
},
"exclude": [
"./dist",
Expand Down

0 comments on commit b4dcda1

Please sign in to comment.