Skip to content

Commit

Permalink
Fix linting errors and disable some linting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
simu committed Nov 1, 2023
1 parent 0157c28 commit 2d18f86
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 230 deletions.
7 changes: 4 additions & 3 deletions .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ rules:
'semi': 'off',
'@typescript-eslint/array-type': 'error',
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/ban-ts-comment': 'error',
'@typescript-eslint/ban-ts-comment': 'warn',
'@typescript-eslint/consistent-type-assertions': 'error',
'@typescript-eslint/explicit-member-accessibility':
['error', { 'accessibility': 'no-public' }],
Expand All @@ -56,7 +56,7 @@ rules:
'@typescript-eslint/func-call-spacing': ['error', 'never'],
'@typescript-eslint/no-array-constructor': 'error',
'@typescript-eslint/no-empty-interface': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-extraneous-class': 'error',
'@typescript-eslint/no-for-in-array': 'error',
'@typescript-eslint/no-inferrable-types': 'error',
Expand All @@ -79,5 +79,6 @@ rules:
'@typescript-eslint/semi': ['error', 'never'],
'@typescript-eslint/space-before-function-paren': 'off',
'@typescript-eslint/type-annotation-spacing': 'error',
'@typescript-eslint/unbound-method': 'error'
'@typescript-eslint/unbound-method': 'error',
'jest/no-conditional-expect': 'off'
}
125 changes: 12 additions & 113 deletions __tests__/bump-labels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as github from '@actions/github'
import * as bump_labels from '../src/bump-labels'
import { ReleaseType } from 'semver'
import { expect } from '@jest/globals'
import { makeOctokitMock } from './helpers'

// Mock the GitHub Actions core library
const getInputMock = jest.spyOn(core, 'getInput')
Expand Down Expand Up @@ -109,7 +110,7 @@ describe('bumpFromLabels', () => {
github.context.eventName = 'discussion'
delete github.context.payload.pull_request

expect(async () => {
await expect(async () => {
await bump_labels.bumpFromLabels(bumpLabels)
}).rejects.toThrow(
new Error(
Expand All @@ -133,29 +134,9 @@ describe('bumpFromLabels', () => {
return ''
}
})
getOctokitMock.mockImplementation((token: string): any => {
expect(token).toBe('mock-token')
return {
rest: {
pulls: {
get: async (req: any) => {
expect(req.owner).toBe('projectsyn')
expect(req.repo).toBe('pr-label-tag-action')
expect(req.pull_number).toBe(123)
return new Promise(resolve => {
resolve({
data: {
labels: [{ name: 'bump:patch' }, { name: 'dependency' }]
}
})
})
}
}
}
}
})
getOctokitMock.mockImplementation(makeOctokitMock('bump:patch'))

expect(bump_labels.bumpFromLabels(bumpLabels)).resolves.toBe(
await expect(bump_labels.bumpFromLabels(bumpLabels)).resolves.toBe(
'patch' as ReleaseType
)
})
Expand All @@ -175,29 +156,9 @@ describe('bumpFromLabels', () => {
return ''
}
})
getOctokitMock.mockImplementation((token: string): any => {
expect(token).toBe('mock-token')
return {
rest: {
pulls: {
get: async (req: any) => {
expect(req.owner).toBe('projectsyn')
expect(req.repo).toBe('pr-label-tag-action')
expect(req.pull_number).toBe(123)
return new Promise(resolve => {
resolve({
data: {
labels: [{ name: 'bump:minor' }, { name: 'dependency' }]
}
})
})
}
}
}
}
})
getOctokitMock.mockImplementation(makeOctokitMock('bump:minor'))

expect(bump_labels.bumpFromLabels(bumpLabels)).resolves.toBe(
await expect(bump_labels.bumpFromLabels(bumpLabels)).resolves.toBe(
'minor' as ReleaseType
)
})
Expand All @@ -217,29 +178,9 @@ describe('bumpFromLabels', () => {
return ''
}
})
getOctokitMock.mockImplementation((token: string): any => {
expect(token).toBe('mock-token')
return {
rest: {
pulls: {
get: async (req: any) => {
expect(req.owner).toBe('projectsyn')
expect(req.repo).toBe('pr-label-tag-action')
expect(req.pull_number).toBe(123)
return new Promise(resolve => {
resolve({
data: {
labels: [{ name: 'bump:major' }, { name: 'dependency' }]
}
})
})
}
}
}
}
})
getOctokitMock.mockImplementation(makeOctokitMock('bump:major'))

expect(bump_labels.bumpFromLabels(bumpLabels)).resolves.toBe(
await expect(bump_labels.bumpFromLabels(bumpLabels)).resolves.toBe(
'major' as ReleaseType
)
})
Expand All @@ -259,27 +200,7 @@ describe('bumpFromLabels', () => {
return ''
}
})
getOctokitMock.mockImplementation((token: string): any => {
expect(token).toBe('mock-token')
return {
rest: {
pulls: {
get: async (req: any) => {
expect(req.owner).toBe('projectsyn')
expect(req.repo).toBe('pr-label-tag-action')
expect(req.pull_number).toBe(123)
return new Promise(resolve => {
resolve({
data: {
labels: [{ name: 'dependency' }]
}
})
})
}
}
}
}
})
getOctokitMock.mockImplementation(makeOctokitMock())

await expect(async () => {
await bump_labels.bumpFromLabels(bumpLabels)
Expand All @@ -303,31 +224,9 @@ describe('bumpFromLabels', () => {
return ''
}
})
getOctokitMock.mockImplementation((token: string): any => {
expect(token).toBe('mock-token')
return {
rest: {
pulls: {
get: async (req: any) => {
expect(req.owner).toBe('projectsyn')
expect(req.repo).toBe('pr-label-tag-action')
expect(req.pull_number).toBe(123)
return new Promise(resolve => {
resolve({
data: {
labels: [
{ name: 'dependency' },
{ name: 'bump:patch' },
{ name: 'bump:minor' }
]
}
})
})
}
}
}
}
})
getOctokitMock.mockImplementation(
makeOctokitMock('bump:patch', 'bump:minor')
)

await expect(bump_labels.bumpFromLabels(bumpLabels)).rejects.toThrow(
new Error('Unknown version bump null')
Expand Down
74 changes: 74 additions & 0 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as exec from '@actions/exec'
import { expect } from '@jest/globals'

export function makeGitExecMock(
stdout: string
): (
commandLine: string,
args?: string[] | undefined,
options?: exec.ExecOptions | undefined
) => Promise<number> {
return async (
commandLine: string,
args?: string[] | undefined,
options?: exec.ExecOptions | undefined
): Promise<number> => {
expect(commandLine).toBe('git')
expect(args).toStrictEqual(['tag', '--sort=-v:refname'])
expect(options).toBeDefined()
expect(options).not.toBeNull()
if (options) {
expect(options.listeners).toBeDefined()
expect(options.listeners).not.toBeNull()
if (options.listeners) {
expect(options.listeners.stdout).toBeDefined()
expect(options.listeners.stdout).not.toBeNull()
expect(options.listeners.stderr).toBeDefined()
expect(options.listeners.stderr).not.toBeNull()
if (options.listeners.stdout) {
options.listeners.stdout(Buffer.from(stdout))
}
if (options.listeners.stderr) {
options.listeners.stderr(Buffer.from(''))
}
}
}
return new Promise(resolve => {
resolve(0)
})
}
}

export function makeOctokitMock(
...bumpLabel: string[]
): (token: string) => any {

Check warning on line 44 in __tests__/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Unexpected any. Specify a different type

Check warning on line 44 in __tests__/helpers.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
const labels = [{ name: 'dependency' }]
if (bumpLabel !== undefined) {
labels.push(
...bumpLabel.map((l: string) => {
return { name: l }
})
)
}
return (token: string): any => {

Check warning on line 53 in __tests__/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Unexpected any. Specify a different type

Check warning on line 53 in __tests__/helpers.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
expect(token).toBe('mock-token')
return {
rest: {
pulls: {
get: async (req: any) => {

Check warning on line 58 in __tests__/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint Code Base

Unexpected any. Specify a different type

Check warning on line 58 in __tests__/helpers.ts

View workflow job for this annotation

GitHub Actions / TypeScript Tests

Unexpected any. Specify a different type
expect(req.owner).toBe('projectsyn')
expect(req.repo).toBe('pr-label-tag-action')
expect(req.pull_number).toBe(123)
return new Promise(resolve => {
resolve({
data: {
labels
}
})
})
}
}
}
}
}
}
27 changes: 2 additions & 25 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as main from '../src/main'
import { makeGitExecMock } from './helpers'

// Mock the GitHub Actions core library
const debugMock = jest.spyOn(core, 'debug')
Expand All @@ -23,31 +24,7 @@ describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
// Mock `git tag --sort=-v:refname`
execMock.mockImplementation((commandLine, args?, options?) => {
expect(commandLine).toBe('git')
expect(args).toStrictEqual(['tag', '--sort=-v:refname'])
expect(options).not.toBeUndefined()
expect(options).not.toBeNull()
if (options) {
expect(options.listeners).toBeDefined()
expect(options.listeners).not.toBeNull()
if (options.listeners) {
expect(options.listeners.stdout).toBeDefined()
expect(options.listeners.stdout).not.toBeNull()
expect(options.listeners.stderr).toBeDefined()
expect(options.listeners.stderr).not.toBeNull()
if (options.listeners.stdout) {
options.listeners.stdout(Buffer.from('v1.2.3\n'))
}
if (options.listeners.stderr) {
options.listeners.stderr(Buffer.from(''))
}
}
}
return new Promise(resolve => {
resolve(0)
})
})
execMock.mockImplementation(makeGitExecMock('v1.2.3\n'))
})

it('parses the bump labels', async () => {
Expand Down
Loading

0 comments on commit 2d18f86

Please sign in to comment.