FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
orm/scripts/lint-casts.test.mjs at main · prisma/orm · GitHub
prisma
/
orm
Public
Notifications
You must be signed in to change notification settings
Fork
2.5k
Star
47.7k
Code
Issues
2.5k
Pull requests
134
Discussions
Actions
Projects
Security and quality
1
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
orm
/
scripts
/
lint-casts.test.mjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
171 lines (142 loc) · 5.7 KB
Breadcrumbs
orm
/
scripts
/
lint-casts.test.mjs
Copy path
File metadata and controls
171 lines (142 loc) · 5.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import
assert
from
'node:assert/strict'
;
import
{
execFileSync
,
spawnSync
}
from
'node:child_process'
;
import
{
mkdirSync
,
mkdtempSync
,
rmSync
,
writeFileSync
}
from
'node:fs'
;
import
{
tmpdir
}
from
'node:os'
;
import
{
dirname
,
join
}
from
'node:path'
;
import
{
execPath
}
from
'node:process'
;
import
{
afterEach
,
beforeEach
,
describe
,
it
}
from
'node:test'
;
import
{
fileURLToPath
}
from
'node:url'
;
import
{
filterNoBarecastDiags
}
from
'./lint-casts.mjs'
;
const
SCRIPT_PATH
=
join
(
fileURLToPath
(
new
URL
(
'.'
,
import
.
meta
.
url
)
)
,
'lint-casts.mjs'
)
;
// A single bare `as` cast that the plugin recognises.
const
FILE_WITH_CAST
=
'declare const value: unknown;\nexport const x = value as string;\n'
;
// No `as` casts.
const
FILE_WITHOUT_CAST
=
'export const x = 1;\n'
;
let
repo
;
function
git
(
...
args
)
{
return
execFileSync
(
'git'
,
args
,
{
cwd
:
repo
,
encoding
:
'utf-8'
,
stdio
:
[
'ignore'
,
'pipe'
,
'pipe'
]
,
}
)
.
trim
(
)
;
}
function
writeRepoFile
(
relPath
,
content
)
{
const
full
=
join
(
repo
,
relPath
)
;
mkdirSync
(
dirname
(
full
)
,
{
recursive
:
true
}
)
;
writeFileSync
(
full
,
content
)
;
}
function
commitAll
(
message
)
{
git
(
'add'
,
'-A'
)
;
git
(
'commit'
,
'-m'
,
message
)
;
}
function
setOriginMain
(
sha
)
{
// Materialize refs/remotes/origin/main without needing a real remote.
git
(
'update-ref'
,
'refs/remotes/origin/main'
,
sha
)
;
}
function
runScript
(
)
{
return
spawnSync
(
execPath
,
[
SCRIPT_PATH
]
,
{
cwd
:
repo
,
encoding
:
'utf-8'
}
)
;
}
beforeEach
(
(
)
=>
{
repo
=
mkdtempSync
(
join
(
tmpdir
(
)
,
'pn-lint-casts-'
)
)
;
git
(
'init'
,
'--quiet'
,
'--initial-branch=main'
)
;
git
(
'config'
,
'user.email'
,
'test@example.com'
)
;
git
(
'config'
,
'user.name'
,
'Test'
)
;
git
(
'config'
,
'commit.gpgsign'
,
'false'
)
;
}
)
;
afterEach
(
(
)
=>
{
rmSync
(
repo
,
{
recursive
:
true
,
force
:
true
}
)
;
}
)
;
describe
(
'filterNoBarecastDiags'
,
(
)
=>
{
it
(
'keeps plugin diagnostics with the no-bare-cast prefix'
,
(
)
=>
{
const
diags
=
[
{
category
:
'plugin'
,
message
:
'no-bare-cast: bare `as` cast'
}
,
{
category
:
'lint'
,
message
:
'no-bare-cast: should be ignored'
}
,
{
category
:
'plugin'
,
message
:
'other-plugin: something else'
}
,
]
;
const
kept
=
filterNoBarecastDiags
(
diags
)
;
assert
.
equal
(
kept
.
length
,
1
)
;
assert
.
equal
(
kept
[
0
]
.
category
,
'plugin'
)
;
assert
.
match
(
kept
[
0
]
.
message
,
/
^
n
o
-
b
a
r
e
-
c
a
s
t
:
/
)
;
}
)
;
it
(
'returns empty array when no diagnostics match'
,
(
)
=>
{
assert
.
deepEqual
(
filterNoBarecastDiags
(
[
]
)
,
[
]
)
;
assert
.
deepEqual
(
filterNoBarecastDiags
(
[
{
category
:
'lint'
,
message
:
'something'
}
]
)
,
[
]
)
;
}
)
;
}
)
;
describe
(
'lint-casts — skip on main'
,
(
)
=>
{
it
(
'exits 0 and prints a skip message when HEAD is at merge-base'
,
(
)
=>
{
writeRepoFile
(
'src/app.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'initial'
)
;
setOriginMain
(
git
(
'rev-parse'
,
'HEAD'
)
)
;
// HEAD == merge-base → should skip
const
result
=
runScript
(
)
;
assert
.
equal
(
result
.
status
,
0
,
`expected exit 0; stderr=
${
result
.
stderr
}
`
)
;
assert
.
match
(
result
.
stdout
,
/
S
k
i
p
p
i
n
g
/
i
)
;
}
)
;
}
)
;
describe
(
'lint-casts — zero delta'
,
(
)
=>
{
it
(
'exits 0 and reports delta=0 when cast count is unchanged'
,
(
)
=>
{
writeRepoFile
(
'src/app.ts'
,
FILE_WITH_CAST
)
;
commitAll
(
'base: one cast'
)
;
setOriginMain
(
git
(
'rev-parse'
,
'HEAD'
)
)
;
writeRepoFile
(
'src/other.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'feature: add unrelated file'
)
;
const
result
=
runScript
(
)
;
assert
.
equal
(
result
.
status
,
0
,
`expected exit 0; stderr=
${
result
.
stderr
}
`
)
;
assert
.
match
(
result
.
stdout
,
/
d
e
l
t
a
=
0
/
)
;
}
)
;
}
)
;
describe
(
'lint-casts — negative delta'
,
(
)
=>
{
it
(
'exits 0 and reports a negative delta when a cast is removed'
,
(
)
=>
{
writeRepoFile
(
'src/app.ts'
,
FILE_WITH_CAST
)
;
commitAll
(
'base: one cast'
)
;
setOriginMain
(
git
(
'rev-parse'
,
'HEAD'
)
)
;
writeRepoFile
(
'src/app.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'feature: remove cast'
)
;
const
result
=
runScript
(
)
;
assert
.
equal
(
result
.
status
,
0
,
`expected exit 0; stderr=
${
result
.
stderr
}
`
)
;
assert
.
match
(
result
.
stdout
,
/
d
e
l
t
a
=
-
1
/
)
;
}
)
;
}
)
;
describe
(
'lint-casts — positive delta'
,
(
)
=>
{
it
(
'exits 1 and prints added site(s) when a cast is introduced'
,
(
)
=>
{
writeRepoFile
(
'src/app.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'base: no casts'
)
;
setOriginMain
(
git
(
'rev-parse'
,
'HEAD'
)
)
;
writeRepoFile
(
'src/app.ts'
,
FILE_WITH_CAST
)
;
commitAll
(
'feature: add cast'
)
;
const
result
=
runScript
(
)
;
assert
.
equal
(
result
.
status
,
1
,
`expected exit 1; stdout=
${
result
.
stdout
}
`
)
;
assert
.
match
(
result
.
stdout
,
/
d
e
l
t
a
=
\+
1
/
)
;
assert
.
match
(
result
.
stderr
,
/
n
o
-
b
a
r
e
-
c
a
s
t
|
a
s
.
*
c
a
s
t
/
i
)
;
assert
.
match
(
result
.
stderr
,
/
b
l
i
n
d
C
a
s
t
|
c
a
s
t
A
s
/
)
;
}
)
;
it
(
'lists each new cast site with file:line'
,
(
)
=>
{
writeRepoFile
(
'src/app.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'base: no casts'
)
;
setOriginMain
(
git
(
'rev-parse'
,
'HEAD'
)
)
;
writeRepoFile
(
'src/app.ts'
,
FILE_WITH_CAST
)
;
commitAll
(
'feature: add cast'
)
;
const
result
=
runScript
(
)
;
assert
.
equal
(
result
.
status
,
1
)
;
// The cast is on line 2 of FILE_WITH_CAST
assert
.
match
(
result
.
stderr
,
/
s
r
c
\/
a
p
p
\.
t
s
:
2
/
)
;
}
)
;
}
)
;
describe
(
'lint-casts — worktree cleanup'
,
(
)
=>
{
it
(
'leaves no stray worktrees after a successful run'
,
(
)
=>
{
writeRepoFile
(
'src/app.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'base'
)
;
setOriginMain
(
git
(
'rev-parse'
,
'HEAD'
)
)
;
writeRepoFile
(
'src/other.ts'
,
FILE_WITHOUT_CAST
)
;
commitAll
(
'feature'
)
;
runScript
(
)
;
const
worktreeList
=
execFileSync
(
'git'
,
[
'worktree'
,
'list'
,
'--porcelain'
]
,
{
cwd
:
repo
,
encoding
:
'utf-8'
,
}
)
;
// Only the main worktree should remain (one `worktree <path>` entry)
const
worktreeCount
=
worktreeList
.
split
(
'\n'
)
.
filter
(
(
l
)
=>
l
.
startsWith
(
'worktree '
)
)
.
length
;
assert
.
equal
(
worktreeCount
,
1
,
`expected 1 worktree; got:\n
${
worktreeList
}
`
)
;
}
)
;
}
)
;
Back
|
FazBrowse Home
|
New Git URL