Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
b4165299
Commit
b4165299
authored
Feb 08, 2022
by
Lord of the dance
Committed by
smartcontracts
Feb 08, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test: adds tests and docstrings for core-utils misc functions
parent
ed234a8d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
99 additions
and
2 deletions
+99
-2
eighty-comics-push.md
.changeset/eighty-comics-push.md
+5
-0
misc.ts
packages/core-utils/src/common/misc.ts
+13
-1
misc.spec.ts
packages/core-utils/test/misc.spec.ts
+81
-1
No files found.
.changeset/eighty-comics-push.md
0 → 100644
View file @
b4165299
---
'
@eth-optimism/core-utils'
:
patch
---
Added tests and docstrings to misc functions
packages/core-utils/src/common/misc.ts
View file @
b4165299
...
...
@@ -11,7 +11,12 @@ export const sleep = async (ms: number): Promise<void> => {
})
}
// Returns a copy of an object
/**
* Returns a clone of the object.
*
* @param obj Object to clone.
* @returns Clone of the object.
*/
export
const
clone
=
(
obj
:
any
):
any
=>
{
if
(
typeof
obj
===
'
undefined
'
)
{
throw
new
Error
(
`Trying to clone undefined object`
)
...
...
@@ -33,6 +38,13 @@ export const reqenv = (name: string): string => {
return
value
}
/**
* Loads a variable from the environment and returns a fallback if not found.
*
* @param name Name of the variable to load.
* @param [fallback] Optional value to be returned as fallback.
* @returns Value of the variable as a string, fallback or undefined.
*/
export
const
getenv
=
(
name
:
string
,
fallback
?:
string
):
string
|
undefined
=>
{
return
process
.
env
[
name
]
||
fallback
}
packages/core-utils/test/misc.spec.ts
View file @
b4165299
/* Imports: Internal */
import
{
expect
}
from
'
./setup
'
import
{
sleep
}
from
'
../src
'
import
{
sleep
,
clone
,
reqenv
,
getenv
}
from
'
../src
'
describe
(
'
sleep
'
,
async
()
=>
{
it
(
'
should return wait input amount of ms
'
,
async
()
=>
{
...
...
@@ -10,3 +10,83 @@ describe('sleep', async () => {
expect
(
startTime
+
1000
<=
endTime
).
to
.
deep
.
equal
(
true
)
})
})
describe
(
'
clone
'
,
async
()
=>
{
it
(
'
should return a cloned object
'
,
async
()
=>
{
const
exampleObject
=
{
example
:
'
Example
'
}
const
clonedObject
=
clone
(
exampleObject
)
expect
(
clonedObject
).
to
.
not
.
equal
(
exampleObject
)
expect
(
JSON
.
stringify
(
clonedObject
)).
to
.
equal
(
JSON
.
stringify
(
exampleObject
))
})
})
describe
(
'
reqenv
'
,
async
()
=>
{
let
cachedEnvironment
const
temporaryEnvironmentKey
=
'
testVariable
'
const
temporaryEnvironment
=
{
[
temporaryEnvironmentKey
]:
'
This is an environment variable
'
,
}
before
(()
=>
{
cachedEnvironment
=
process
.
env
process
.
env
=
temporaryEnvironment
})
it
(
'
should return an existent environment variable
'
,
async
()
=>
{
const
requiredEnvironmentValue
=
reqenv
(
temporaryEnvironmentKey
)
expect
(
requiredEnvironmentValue
).
to
.
equal
(
temporaryEnvironment
[
temporaryEnvironmentKey
]
)
})
it
(
'
should throw an error trying to return a variable that does not exist
'
,
async
()
=>
{
const
undeclaredVariableName
=
'
undeclaredVariable
'
const
failedReqenv
=
()
=>
reqenv
(
undeclaredVariableName
)
expect
(
failedReqenv
).
to
.
throw
()
})
after
(()
=>
{
process
.
env
=
cachedEnvironment
})
})
describe
(
'
getenv
'
,
async
()
=>
{
let
cachedEnvironment
const
temporaryEnvironmentKey
=
'
testVariable
'
const
temporaryEnvironment
=
{
[
temporaryEnvironmentKey
]:
'
This is an environment variable
'
,
}
const
fallback
=
'
fallback
'
before
(()
=>
{
cachedEnvironment
=
process
.
env
process
.
env
=
temporaryEnvironment
})
it
(
'
should return an existent environment variable
'
,
async
()
=>
{
const
environmentVariable
=
getenv
(
temporaryEnvironmentKey
)
expect
(
environmentVariable
).
to
.
equal
(
temporaryEnvironment
[
temporaryEnvironmentKey
]
)
})
it
(
'
should return an existent environment variable even if fallback is passed
'
,
async
()
=>
{
const
environmentVariable
=
getenv
(
temporaryEnvironmentKey
,
fallback
)
expect
(
environmentVariable
).
to
.
equal
(
temporaryEnvironment
[
temporaryEnvironmentKey
]
)
})
it
(
'
should return fallback if variable is not defined
'
,
async
()
=>
{
const
undeclaredVariableName
=
'
undeclaredVariable
'
expect
(
getenv
(
undeclaredVariableName
,
fallback
)).
to
.
equal
(
fallback
)
})
it
(
'
should return undefined if no fallback is passed and variable is not defined
'
,
async
()
=>
{
expect
(
getenv
(
'
undeclaredVariable
'
)).
to
.
be
.
undefined
})
after
(()
=>
{
process
.
env
=
cachedEnvironment
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment