aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-17 11:58:15 +0100
committerChocobozzz <me@florianbigard.com>2021-12-17 12:24:03 +0100
commitc55e3d7227fe1453869e309025996b9d75256d5d (patch)
tree08e9b0ca210d75c82c8606fef0852eca020e8e0e /shared/core-utils/common
parentbf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (diff)
downloadPeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.gz
PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.tar.zst
PeerTube-c55e3d7227fe1453869e309025996b9d75256d5d.zip
Move test functions outside extra-utils
Diffstat (limited to 'shared/core-utils/common')
-rw-r--r--shared/core-utils/common/array.ts13
-rw-r--r--shared/core-utils/common/crypto.ts20
-rw-r--r--shared/core-utils/common/env.ts30
-rw-r--r--shared/core-utils/common/index.ts8
-rw-r--r--shared/core-utils/common/object.ts30
-rw-r--r--shared/core-utils/common/path.ts46
-rw-r--r--shared/core-utils/common/time.ts7
-rw-r--r--shared/core-utils/common/version.ts (renamed from shared/core-utils/common/miscs.ts)23
8 files changed, 154 insertions, 23 deletions
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 @@
1function findCommonElement <T> (array1: T[], array2: T[]) {
2 for (const a of array1) {
3 for (const b of array2) {
4 if (a === b) return a
5 }
6 }
7
8 return null
9}
10
11export {
12 findCommonElement
13}
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 @@
1import { BinaryToTextEncoding, createHash } from 'crypto'
2
3function sha256 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
4 return createHash('sha256').update(str).digest(encoding)
5}
6
7function sha1 (str: string | Buffer, encoding: BinaryToTextEncoding = 'hex') {
8 return createHash('sha1').update(str).digest(encoding)
9}
10
11// high excluded
12function randomInt (low: number, high: number) {
13 return Math.floor(Math.random() * (high - low) + low)
14}
15
16export {
17 randomInt,
18 sha256,
19 sha1
20}
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 @@
1function parallelTests () {
2 return process.env.MOCHA_PARALLEL === 'true'
3}
4
5function isGithubCI () {
6 return !!process.env.GITHUB_WORKSPACE
7}
8
9function areHttpImportTestsDisabled () {
10 const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true'
11
12 if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled')
13
14 return disabled
15}
16
17function areObjectStorageTestsDisabled () {
18 const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true'
19
20 if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled')
21
22 return disabled
23}
24
25export {
26 parallelTests,
27 isGithubCI,
28 areHttpImportTestsDisabled,
29 areObjectStorageTestsDisabled
30}
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 @@
1export * from './array'
2export * from './crypto'
1export * from './date' 3export * from './date'
2export * from './miscs' 4export * from './env'
5export * from './object'
6export * from './path'
3export * from './regexp' 7export * from './regexp'
8export * from './time'
4export * from './promises' 9export * from './promises'
5export * from './url' 10export * from './url'
11export * from './version'
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 @@
1function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> {
2 const result: any = {}
3
4 for (const key of keys) {
5 if (Object.prototype.hasOwnProperty.call(object, key)) {
6 result[key] = object[key]
7 }
8 }
9
10 return result
11}
12
13function sortObjectComparator (key: string, order: 'asc' | 'desc') {
14 return (a: any, b: any) => {
15 if (a[key] < b[key]) {
16 return order === 'asc' ? -1 : 1
17 }
18
19 if (a[key] > b[key]) {
20 return order === 'asc' ? 1 : -1
21 }
22
23 return 0
24 }
25}
26
27export {
28 pick,
29 sortObjectComparator
30}
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 @@
1import { basename, extname, isAbsolute, join, resolve } from 'path'
2
3let rootPath: string
4
5function root () {
6 if (rootPath) return rootPath
7
8 rootPath = __dirname
9
10 if (basename(rootPath) === 'common') rootPath = resolve(rootPath, '..')
11 if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..')
12 if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..')
13 if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..')
14 if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..')
15
16 return rootPath
17}
18
19function buildPath (path: string) {
20 if (isAbsolute(path)) return path
21
22 return join(root(), path)
23}
24
25function getLowercaseExtension (filename: string) {
26 const ext = extname(filename) || ''
27
28 return ext.toLowerCase()
29}
30
31function buildAbsoluteFixturePath (path: string, customCIPath = false) {
32 if (isAbsolute(path)) return path
33
34 if (customCIPath && process.env.GITHUB_WORKSPACE) {
35 return join(process.env.GITHUB_WORKSPACE, 'fixtures', path)
36 }
37
38 return join(root(), 'server', 'tests', 'fixtures', path)
39}
40
41export {
42 root,
43 buildPath,
44 buildAbsoluteFixturePath,
45 getLowercaseExtension
46}
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 @@
1function wait (milliseconds: number) {
2 return new Promise(resolve => setTimeout(resolve, milliseconds))
3}
4
5export {
6 wait
7}
diff --git a/shared/core-utils/common/miscs.ts b/shared/core-utils/common/version.ts
index bc65dc338..8a64f8c4d 100644
--- a/shared/core-utils/common/miscs.ts
+++ b/shared/core-utils/common/version.ts
@@ -1,8 +1,3 @@
1// high excluded
2function randomInt (low: number, high: number) {
3 return Math.floor(Math.random() * (high - low) + low)
4}
5
6// Thanks https://stackoverflow.com/a/16187766 1// Thanks https://stackoverflow.com/a/16187766
7function compareSemVer (a: string, b: string) { 2function compareSemVer (a: string, b: string) {
8 const regExStrip0 = /(\.0+)+$/ 3 const regExStrip0 = /(\.0+)+$/
@@ -20,22 +15,6 @@ function compareSemVer (a: string, b: string) {
20 return segmentsA.length - segmentsB.length 15 return segmentsA.length - segmentsB.length
21} 16}
22 17
23function sortObjectComparator (key: string, order: 'asc' | 'desc') {
24 return (a: any, b: any) => {
25 if (a[key] < b[key]) {
26 return order === 'asc' ? -1 : 1
27 }
28
29 if (a[key] > b[key]) {
30 return order === 'asc' ? 1 : -1
31 }
32
33 return 0
34 }
35}
36
37export { 18export {
38 randomInt, 19 compareSemVer
39 compareSemVer,
40 sortObjectComparator
41} 20}