Skip to content

Commit

Permalink
Adjust formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
daun committed Sep 7, 2023
1 parent b3282eb commit 6b8d934
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 270 deletions.
8 changes: 3 additions & 5 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"useTabs": true,
"semi": false,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"quoteProps": "consistent",
"trailingComma": "none",
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "avoid",
"arrowParens": "always",
"proseWrap": "always",
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
Expand Down
6 changes: 3 additions & 3 deletions __tests__/icons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { expect } from '@jest/globals'
import { renderIcon } from '../src/icons'

describe('icons.ts', () => {
it('returns a string', async () => {
expect(renderIcon('something')).toBe('')
})
it('returns a string', async () => {
expect(renderIcon('something')).toBe('')
})
})
98 changes: 49 additions & 49 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,59 @@ const runMock = jest.spyOn(index, 'run')
const timeRegex = /^\d{2}:\d{2}:\d{2}/

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
})
beforeEach(() => {
jest.clearAllMocks()
})

it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'milliseconds':
return '500'
default:
return ''
}
})
it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'milliseconds':
return '500'
default:
return ''
}
})

await index.run()
expect(runMock).toHaveReturned()
await index.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
)
expect(setOutputMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
)
})
// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...')
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
)
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
)
expect(setOutputMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
)
})

it('sets a failed status', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
default:
return ''
}
})
it('sets a failed status', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation((name: string): string => {
switch (name) {
case 'milliseconds':
return 'this is not a number'
default:
return ''
}
})

await index.run()
expect(runMock).toHaveReturned()
await index.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
)
})
// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
)
})
})
60 changes: 30 additions & 30 deletions src/formatting.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
export function renderMarkdownTable(
rows: string[][],
headers: string[] = []
rows: string[][],
headers: string[] = []
): string {
if (!rows.length) {
return ''
}
const align = [':---', ':---:', ':---:', ':---:'].slice(0, rows[0].length)
const lines = [headers, align, ...rows].filter(Boolean)
return lines.map(columns => `| ${columns.join(' | ')} |`).join('\n')
if (!rows.length) {
return ''
}
const align = [':---', ':---:', ':---:', ':---:'].slice(0, rows[0].length)
const lines = [headers, align, ...rows].filter(Boolean)
return lines.map((columns) => `| ${columns.join(' | ')} |`).join('\n')
}

export function formatDuration(milliseconds: number): string {
const SECOND = 1000
const MINUTE = 60 * SECOND
const HOUR = 60 * MINUTE
const DAY = 24 * HOUR
const SECOND = 1000
const MINUTE = 60 * SECOND
const HOUR = 60 * MINUTE
const DAY = 24 * HOUR

let remaining = milliseconds
let remaining = milliseconds

const days = Math.floor(remaining / DAY)
remaining %= DAY
const days = Math.floor(remaining / DAY)
remaining %= DAY

const hours = Math.floor(remaining / HOUR)
remaining %= HOUR
const hours = Math.floor(remaining / HOUR)
remaining %= HOUR

const minutes = Math.floor(remaining / MINUTE)
remaining %= MINUTE
const minutes = Math.floor(remaining / MINUTE)
remaining %= MINUTE

const seconds = +(remaining / SECOND).toFixed(1)
const seconds = +(remaining / SECOND).toFixed(1)

return [
days && `${days} ${n('day', days)}`,
hours && `${hours} ${n('hour', hours)}`,
minutes && `${minutes} ${n('minute', minutes)}`,
seconds && `${seconds} ${n('second', seconds)}`
]
.filter(Boolean)
.join(', ')
return [
days && `${days} ${n('day', days)}`,
hours && `${hours} ${n('hour', hours)}`,
minutes && `${minutes} ${n('minute', minutes)}`,
seconds && `${seconds} ${n('second', seconds)}`
]
.filter(Boolean)
.join(', ')
}

export function upperCaseFirst(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1)
return str.charAt(0).toUpperCase() + str.slice(1)
}

export function n(str: string, count: number): string {
return count === 1 ? str : `${str}s`
return count === 1 ? str : `${str}s`
}
14 changes: 7 additions & 7 deletions src/fs.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import fs from 'fs/promises'

export async function fileExists(filename: string): Promise<boolean> {
try {
await fs.access(filename, fs.constants.F_OK)
return true
} catch (e) {
return false
}
try {
await fs.access(filename, fs.constants.F_OK)
return true
} catch (e) {
return false
}
}

export async function readFile(path: string): Promise<string> {
return await fs.readFile(path, { encoding: 'utf8' })
return await fs.readFile(path, { encoding: 'utf8' })
}
84 changes: 42 additions & 42 deletions src/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@ const iconSize = 14
const defaultIconStyle = 'octicons'

export const icons: Record<string, IconSet> = {
octicons: {
failed: 'stop',
passed: 'check-circle',
flaky: 'alert',
skipped: 'skip',
stats: 'pulse',
duration: 'clock',
link: 'link-external',
report: 'package',
commit: 'git-pull-request'
},
emojis: {
failed: '❌',
passed: '✅',
flaky: '⚠️',
skipped: '⏭️',
stats: '',
duration: '',
link: '',
report: '',
commit: ''
}
octicons: {
failed: 'stop',
passed: 'check-circle',
flaky: 'alert',
skipped: 'skip',
stats: 'pulse',
duration: 'clock',
link: 'link-external',
report: 'package',
commit: 'git-pull-request'
},
emojis: {
failed: '❌',
passed: '✅',
flaky: '⚠️',
skipped: '⏭️',
stats: '',
duration: '',
link: '',
report: '',
commit: ''
}
}

const iconColors: IconColors = {
failed: 'da3633',
passed: '3fb950',
flaky: 'd29922',
skipped: '0967d9',
icon: 'abb4bf'
failed: 'da3633',
passed: '3fb950',
flaky: 'd29922',
skipped: '0967d9',
icon: 'abb4bf'
}

export function renderIcon(
status: string,
{ iconStyle = defaultIconStyle }: { iconStyle?: keyof typeof icons } = {}
status: string,
{ iconStyle = defaultIconStyle }: { iconStyle?: keyof typeof icons } = {}
): string {
if (iconStyle === 'emojis') {
return icons.emojis[status] || ''
} else {
const color = iconColors[status] || iconColors.icon
return createOcticonUrl(icons.octicons[status], { label: status, color })
}
if (iconStyle === 'emojis') {
return icons.emojis[status] || ''
} else {
const color = iconColors[status] || iconColors.icon
return createOcticonUrl(icons.octicons[status], { label: status, color })
}
}

function createOcticonUrl(
icon: string,
{ label = 'icon', color = iconColors.icon, size = iconSize } = {}
icon: string,
{ label = 'icon', color = iconColors.icon, size = iconSize } = {}
): string {
if (icon) {
return `![${label}](https://icongr.am/octicons/${icon}.svg?size=${size}&color=${color})`
} else {
return ''
}
if (icon) {
return `![${label}](https://icongr.am/octicons/${icon}.svg?size=${size}&color=${color})`
} else {
return ''
}
}
Loading

0 comments on commit 6b8d934

Please sign in to comment.