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
3e5fea45
Commit
3e5fea45
authored
Oct 31, 2022
by
Maurelian
Committed by
Matthew Slipper
Dec 02, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ctb: Add simple script for enforcing test conventions
parent
7300a7ca
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
81 additions
and
0 deletions
+81
-0
forge-test-names.ts
packages/contracts-bedrock/scripts/forge-test-names.ts
+81
-0
No files found.
packages/contracts-bedrock/scripts/forge-test-names.ts
0 → 100644
View file @
3e5fea45
import
fs
from
'
fs
'
import
path
from
'
path
'
const
testPath
=
'
./contracts/test
'
const
testFiles
=
fs
.
readdirSync
(
testPath
)
// Given a test function name, ensures it matches the expected format
const
handleFunctionName
=
(
name
:
string
)
=>
{
if
(
!
name
.
startsWith
(
'
test
'
))
{
return
}
const
parts
=
name
.
split
(
'
_
'
)
parts
.
forEach
((
part
)
=>
{
// Good enough approximation for camelCase
if
(
part
[
0
]
!==
part
[
0
].
toLowerCase
())
{
throw
new
Error
(
`Invalid test name:
${
name
}
.\n Test name parts should be in camelCase`
)
}
})
if
(
parts
.
length
<
3
||
parts
.
length
>
4
)
{
throw
new
Error
(
`Invalid test name:
${
name
}
.\n Test names should have either 3 or 4 parts, each separated by underscores`
)
}
if
(
!
[
'
test
'
,
'
testFuzz
'
,
'
testDiff
'
].
includes
(
parts
[
0
]))
{
throw
new
Error
(
`Invalid test name:
${
name
}
.\n Names should begin with either "test" or "testFuzz"`
)
}
if
(
!
[
'
succeeds
'
,
'
reverts
'
,
'
fails
'
,
'
benchmark
'
,
'
works
'
].
includes
(
parts
[
parts
.
length
-
1
]
)
&&
parts
[
parts
.
length
-
2
]
!==
'
benchmark
'
)
{
throw
new
Error
(
`Invalid test name:
${
name
}
.\n Test names should end with either "succeeds", "reverts", "fails", "differential" or "benchmark[_num]"`
)
}
if
(
[
'
reverts
'
,
'
fails
'
].
includes
(
parts
[
parts
.
length
-
1
])
&&
parts
.
length
<
4
)
{
throw
new
Error
(
`Invalid test name:
${
name
}
.\n Failure tests should have 4 parts. The third part should indicate the reason for failure.`
)
}
}
// Todo: define this function for validating contract names
// Given a test contract name, ensures it matches the expected format
const
handleContractName
=
(
name
:
string
)
=>
{
name
}
for
(
const
testFile
of
testFiles
)
{
const
filePath
=
path
.
join
(
testPath
,
testFile
)
const
lines
=
fs
.
readFileSync
(
filePath
,
'
utf-8
'
)
.
split
(
'
\n
'
)
.
map
((
l
)
=>
l
.
trim
())
let
currentContract
:
string
for
(
const
line
of
lines
)
{
if
(
line
.
startsWith
(
'
contract
'
))
{
currentContract
=
line
.
split
(
'
'
)[
1
]
handleContractName
(
line
)
continue
}
else
if
(
line
.
startsWith
(
'
function
'
))
{
const
funcName
=
line
.
split
(
'
'
)[
1
].
split
(
'
(
'
)[
0
]
try
{
handleFunctionName
(
funcName
)
}
catch
(
error
)
{
throw
new
Error
(
`In
${
filePath
}
::
${
currentContract
}
:\n
${
error
.
message
}
`
)
}
continue
}
}
}
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