1
0
mirror of https://github.com/benjlevesque/short-sha.git synced 2025-12-07 18:27:50 +01:00

short-sha

This commit is contained in:
Benjamin LEVESQUE
2020-03-09 21:40:47 +01:00
parent 6e8dcce3fd
commit 9f96a4f558
13 changed files with 29878 additions and 6881 deletions

View File

@@ -1,27 +1,15 @@
import {wait} from '../src/wait'
import * as process from 'process'
import * as cp from 'child_process'
import * as path from 'path'
test('throws invalid number', async () => {
const input = parseInt('foo', 10)
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
})
test('wait 500 ms', async () => {
const start = new Date()
await wait(500)
const end = new Date()
var delta = Math.abs(end.getTime() - start.getTime())
expect(delta).toBeGreaterThan(450)
})
// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
process.env['INPUT_MILLISECONDS'] = '500'
process.env['GITHUB_SHA'] = '6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1'
process.env['INPUT_LENGTH'] = '6'
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecSyncOptions = {
env: process.env
}
console.log(cp.execSync(`node ${ip}`, options).toString())
const output = cp.execSync(`node ${ip}`, options).toString()
expect(output).toContain('::set-output name=sha::6e8dcc')
})

39
__tests__/shorten.test.ts Normal file
View File

@@ -0,0 +1,39 @@
import {shorten} from '../src/shorten'
test('normal case', () => {
expect(shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', 7)).toBe('6e8dcce')
})
test('already short', () => {
expect(shorten('6e8dcce', 7)).toBe('6e8dcce')
})
test('too short', () => {
expect(() => shorten('6e8dcc', 7)).toThrowError('input is too short')
})
test('undefined', () => {
expect(() => shorten(undefined as any, 7)).toThrowError('sha must be defined')
})
test('null', () => {
expect(() => shorten(null as any, 7)).toThrowError('sha must be defined')
})
test('invalid length', () => {
expect(() =>
shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', 'x' as any)
).toThrowError('length is invalid')
})
test('negative length', () => {
expect(() =>
shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', -1)
).toThrowError('length is invalid')
})
test('zero length', () => {
expect(() =>
shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', 0)
).toThrowError('length is invalid')
})