From c55e3d7227fe1453869e309025996b9d75256d5d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 17 Dec 2021 11:58:15 +0100 Subject: Move test functions outside extra-utils --- shared/core-utils/common/array.ts | 13 +++++++++++ shared/core-utils/common/crypto.ts | 20 ++++++++++++++++ shared/core-utils/common/env.ts | 30 ++++++++++++++++++++++++ shared/core-utils/common/index.ts | 8 ++++++- shared/core-utils/common/miscs.ts | 41 --------------------------------- shared/core-utils/common/object.ts | 30 ++++++++++++++++++++++++ shared/core-utils/common/path.ts | 46 +++++++++++++++++++++++++++++++++++++ shared/core-utils/common/time.ts | 7 ++++++ shared/core-utils/common/version.ts | 20 ++++++++++++++++ shared/core-utils/crypto.ts | 14 ----------- shared/core-utils/index.ts | 3 --- shared/core-utils/path.ts | 34 --------------------------- shared/core-utils/utils/array.ts | 13 ----------- shared/core-utils/utils/index.ts | 2 -- shared/core-utils/utils/object.ts | 15 ------------ shared/core-utils/uuid.ts | 32 -------------------------- shared/core-utils/videos/index.ts | 1 + shared/core-utils/videos/uuid.ts | 32 ++++++++++++++++++++++++++ 18 files changed, 206 insertions(+), 155 deletions(-) create mode 100644 shared/core-utils/common/array.ts create mode 100644 shared/core-utils/common/crypto.ts create mode 100644 shared/core-utils/common/env.ts delete mode 100644 shared/core-utils/common/miscs.ts create mode 100644 shared/core-utils/common/object.ts create mode 100644 shared/core-utils/common/path.ts create mode 100644 shared/core-utils/common/time.ts create mode 100644 shared/core-utils/common/version.ts delete mode 100644 shared/core-utils/crypto.ts delete mode 100644 shared/core-utils/path.ts delete mode 100644 shared/core-utils/utils/array.ts delete mode 100644 shared/core-utils/utils/index.ts delete mode 100644 shared/core-utils/utils/object.ts delete mode 100644 shared/core-utils/uuid.ts create mode 100644 shared/core-utils/videos/uuid.ts (limited to 'shared/core-utils') diff --git a/shared/core-utils/common/array.ts b/shared/core-utils/common/array.ts new file mode 100644 index 000000000..9e326a5aa --- /dev/null +++ b/shared/core-utils/common/array.ts @@ -0,0 +1,13 @@ +function findCommonElement (array1: T[], array2: T[]) { + for (const a of array1) { + for (const b of array2) { + if (a === b) return a + } + } + + return null +} + +export { + findCommonElement +} diff --git a/shared/core-utils/common/crypto.ts b/shared/core-utils/common/crypto.ts new file mode 100644 index 000000000..1a583f1a0 --- /dev/null +++ b/shared/core-utils/common/crypto.ts @@ -0,0 +1,20 @@ +import { BinaryToTextEncoding, createHash } from 'crypto' + +function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { + return createHash('sha256').update(str).digest(encoding) +} + +function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { + return createHash('sha1').update(str).digest(encoding) +} + +// high excluded +function randomInt (low: number, high: number) { + return Math.floor(Math.random() * (high - low) + low) +} + +export { + randomInt, + sha256, + sha1 +} diff --git a/shared/core-utils/common/env.ts b/shared/core-utils/common/env.ts new file mode 100644 index 000000000..38c96b152 --- /dev/null +++ b/shared/core-utils/common/env.ts @@ -0,0 +1,30 @@ +function parallelTests () { + return process.env.MOCHA_PARALLEL === 'true' +} + +function isGithubCI () { + return !!process.env.GITHUB_WORKSPACE +} + +function areHttpImportTestsDisabled () { + const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true' + + if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled') + + return disabled +} + +function areObjectStorageTestsDisabled () { + const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true' + + if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled') + + return disabled +} + +export { + parallelTests, + isGithubCI, + areHttpImportTestsDisabled, + areObjectStorageTestsDisabled +} diff --git a/shared/core-utils/common/index.ts b/shared/core-utils/common/index.ts index 5d3512148..f9ba10d5e 100644 --- a/shared/core-utils/common/index.ts +++ b/shared/core-utils/common/index.ts @@ -1,5 +1,11 @@ +export * from './array' +export * from './crypto' export * from './date' -export * from './miscs' +export * from './env' +export * from './object' +export * from './path' export * from './regexp' +export * from './time' export * from './promises' export * from './url' +export * from './version' diff --git a/shared/core-utils/common/miscs.ts b/shared/core-utils/common/miscs.ts deleted file mode 100644 index bc65dc338..000000000 --- a/shared/core-utils/common/miscs.ts +++ /dev/null @@ -1,41 +0,0 @@ -// high excluded -function randomInt (low: number, high: number) { - return Math.floor(Math.random() * (high - low) + low) -} - -// Thanks https://stackoverflow.com/a/16187766 -function compareSemVer (a: string, b: string) { - const regExStrip0 = /(\.0+)+$/ - const segmentsA = a.replace(regExStrip0, '').split('.') - const segmentsB = b.replace(regExStrip0, '').split('.') - - const l = Math.min(segmentsA.length, segmentsB.length) - - for (let i = 0; i < l; i++) { - const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10) - - if (diff) return diff - } - - return segmentsA.length - segmentsB.length -} - -function sortObjectComparator (key: string, order: 'asc' | 'desc') { - return (a: any, b: any) => { - if (a[key] < b[key]) { - return order === 'asc' ? -1 : 1 - } - - if (a[key] > b[key]) { - return order === 'asc' ? 1 : -1 - } - - return 0 - } -} - -export { - randomInt, - compareSemVer, - sortObjectComparator -} diff --git a/shared/core-utils/common/object.ts b/shared/core-utils/common/object.ts new file mode 100644 index 000000000..88d6b7514 --- /dev/null +++ b/shared/core-utils/common/object.ts @@ -0,0 +1,30 @@ +function pick (object: O, keys: K[]): Pick { + const result: any = {} + + for (const key of keys) { + if (Object.prototype.hasOwnProperty.call(object, key)) { + result[key] = object[key] + } + } + + return result +} + +function sortObjectComparator (key: string, order: 'asc' | 'desc') { + return (a: any, b: any) => { + if (a[key] < b[key]) { + return order === 'asc' ? -1 : 1 + } + + if (a[key] > b[key]) { + return order === 'asc' ? 1 : -1 + } + + return 0 + } +} + +export { + pick, + sortObjectComparator +} diff --git a/shared/core-utils/common/path.ts b/shared/core-utils/common/path.ts new file mode 100644 index 000000000..ec507538b --- /dev/null +++ b/shared/core-utils/common/path.ts @@ -0,0 +1,46 @@ +import { basename, extname, isAbsolute, join, resolve } from 'path' + +let rootPath: string + +function root () { + if (rootPath) return rootPath + + rootPath = __dirname + + if (basename(rootPath) === 'common') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..') + if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..') + + return rootPath +} + +function buildPath (path: string) { + if (isAbsolute(path)) return path + + return join(root(), path) +} + +function getLowercaseExtension (filename: string) { + const ext = extname(filename) || '' + + return ext.toLowerCase() +} + +function buildAbsoluteFixturePath (path: string, customCIPath = false) { + if (isAbsolute(path)) return path + + if (customCIPath && process.env.GITHUB_WORKSPACE) { + return join(process.env.GITHUB_WORKSPACE, 'fixtures', path) + } + + return join(root(), 'server', 'tests', 'fixtures', path) +} + +export { + root, + buildPath, + buildAbsoluteFixturePath, + getLowercaseExtension +} diff --git a/shared/core-utils/common/time.ts b/shared/core-utils/common/time.ts new file mode 100644 index 000000000..2992609ca --- /dev/null +++ b/shared/core-utils/common/time.ts @@ -0,0 +1,7 @@ +function wait (milliseconds: number) { + return new Promise(resolve => setTimeout(resolve, milliseconds)) +} + +export { + wait +} diff --git a/shared/core-utils/common/version.ts b/shared/core-utils/common/version.ts new file mode 100644 index 000000000..8a64f8c4d --- /dev/null +++ b/shared/core-utils/common/version.ts @@ -0,0 +1,20 @@ +// Thanks https://stackoverflow.com/a/16187766 +function compareSemVer (a: string, b: string) { + const regExStrip0 = /(\.0+)+$/ + const segmentsA = a.replace(regExStrip0, '').split('.') + const segmentsB = b.replace(regExStrip0, '').split('.') + + const l = Math.min(segmentsA.length, segmentsB.length) + + for (let i = 0; i < l; i++) { + const diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10) + + if (diff) return diff + } + + return segmentsA.length - segmentsB.length +} + +export { + compareSemVer +} diff --git a/shared/core-utils/crypto.ts b/shared/core-utils/crypto.ts deleted file mode 100644 index d6d1150d0..000000000 --- a/shared/core-utils/crypto.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { BinaryToTextEncoding, createHash } from 'crypto' - -function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { - return createHash('sha256').update(str).digest(encoding) -} - -function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') { - return createHash('sha1').update(str).digest(encoding) -} - -export { - sha256, - sha1 -} diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index ee5cd4412..8daaa2d04 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts @@ -1,10 +1,7 @@ export * from './abuse' export * from './common' export * from './i18n' -export * from './path' export * from './plugins' export * from './renderer' export * from './users' -export * from './utils' export * from './videos' -export * from './uuid' diff --git a/shared/core-utils/path.ts b/shared/core-utils/path.ts deleted file mode 100644 index b1a45d69b..000000000 --- a/shared/core-utils/path.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { basename, extname, isAbsolute, join, resolve } from 'path' - -let rootPath: string - -function root () { - if (rootPath) return rootPath - - rootPath = __dirname - - if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..') - if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..') - if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..') - if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..') - - return rootPath -} - -function buildPath (path: string) { - if (isAbsolute(path)) return path - - return join(root(), path) -} - -function getLowercaseExtension (filename: string) { - const ext = extname(filename) || '' - - return ext.toLowerCase() -} - -export { - root, - buildPath, - getLowercaseExtension -} diff --git a/shared/core-utils/utils/array.ts b/shared/core-utils/utils/array.ts deleted file mode 100644 index 9e326a5aa..000000000 --- a/shared/core-utils/utils/array.ts +++ /dev/null @@ -1,13 +0,0 @@ -function findCommonElement (array1: T[], array2: T[]) { - for (const a of array1) { - for (const b of array2) { - if (a === b) return a - } - } - - return null -} - -export { - findCommonElement -} diff --git a/shared/core-utils/utils/index.ts b/shared/core-utils/utils/index.ts deleted file mode 100644 index 8d16365a8..000000000 --- a/shared/core-utils/utils/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './array' -export * from './object' diff --git a/shared/core-utils/utils/object.ts b/shared/core-utils/utils/object.ts deleted file mode 100644 index 9a8a98f9b..000000000 --- a/shared/core-utils/utils/object.ts +++ /dev/null @@ -1,15 +0,0 @@ -function pick (object: O, keys: K[]): Pick { - const result: any = {} - - for (const key of keys) { - if (Object.prototype.hasOwnProperty.call(object, key)) { - result[key] = object[key] - } - } - - return result -} - -export { - pick -} diff --git a/shared/core-utils/uuid.ts b/shared/core-utils/uuid.ts deleted file mode 100644 index f3c80e046..000000000 --- a/shared/core-utils/uuid.ts +++ /dev/null @@ -1,32 +0,0 @@ -import short, { uuid } from 'short-uuid' - -const translator = short() - -function buildUUID () { - return uuid() -} - -function uuidToShort (uuid: string) { - if (!uuid) return uuid - - return translator.fromUUID(uuid) -} - -function shortToUUID (shortUUID: string) { - if (!shortUUID) return shortUUID - - return translator.toUUID(shortUUID) -} - -function isShortUUID (value: string) { - if (!value) return false - - return value.length === translator.maxLength -} - -export { - buildUUID, - uuidToShort, - shortToUUID, - isShortUUID -} diff --git a/shared/core-utils/videos/index.ts b/shared/core-utils/videos/index.ts index 620e3a716..8f6736d39 100644 --- a/shared/core-utils/videos/index.ts +++ b/shared/core-utils/videos/index.ts @@ -1,2 +1,3 @@ export * from './bitrate' export * from './privacy' +export * from './uuid' diff --git a/shared/core-utils/videos/uuid.ts b/shared/core-utils/videos/uuid.ts new file mode 100644 index 000000000..f3c80e046 --- /dev/null +++ b/shared/core-utils/videos/uuid.ts @@ -0,0 +1,32 @@ +import short, { uuid } from 'short-uuid' + +const translator = short() + +function buildUUID () { + return uuid() +} + +function uuidToShort (uuid: string) { + if (!uuid) return uuid + + return translator.fromUUID(uuid) +} + +function shortToUUID (shortUUID: string) { + if (!shortUUID) return shortUUID + + return translator.toUUID(shortUUID) +} + +function isShortUUID (value: string) { + if (!value) return false + + return value.length === translator.maxLength +} + +export { + buildUUID, + uuidToShort, + shortToUUID, + isShortUUID +} -- cgit v1.2.3