Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
jcode/scripts/desktop_gallery_golden.py at master · webtekdigitalai-tech/jcode · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
webtekdigitalai-tech
jcode
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
jcode
/
scripts
/
desktop_gallery_golden.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
159 lines (135 loc) · 5.82 KB
master
Breadcrumbs
jcode
/
scripts
/
desktop_gallery_golden.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
159 lines (135 loc) · 5.82 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
#!/usr/bin/env python3
"""Golden-image regression checks for Jcode Desktop gallery captures.
Renders every gallery fixture state headlessly via
`jcode-desktop --capture-gallery-screens` and compares the PNGs against
checked-in baselines.
Usage:
scripts/desktop_gallery_golden.py check # compare against baselines
scripts/desktop_gallery_golden.py update # rewrite baselines
scripts/desktop_gallery_golden.py check --size 640x400
Exit codes: 0 ok, 1 regressions found, 2 setup/usage error.
A state regresses when more than --threshold fraction of pixels differ
by more than --tolerance per channel (defaults: 0.5% of pixels, 8/255).
Diff images for failing states are written next to the captures.
"""
from
__future__
import
annotations
import
argparse
import
json
import
subprocess
import
sys
import
tempfile
from
pathlib
import
Path
try
:
from
PIL
import
Image
,
ImageChops
except
ImportError
:
print
(
"error: pillow is required (pip install pillow)"
,
file
=
sys
.
stderr
)
sys
.
exit
(
2
)
ROOT
=
Path
(
__file__
).
resolve
().
parent
.
parent
DEFAULT_BIN
=
ROOT
/
"target"
/
"debug"
/
"jcode-desktop"
DEFAULT_BASELINE_DIR
=
ROOT
/
"tests"
/
"desktop-gallery-golden"
def
capture
(
binary
:
Path
,
out_dir
:
Path
,
size
:
str
|
None
)
->
list
[
dict
]:
cmd
=
[
str
(
binary
),
"--capture-gallery-screens"
,
str
(
out_dir
)]
if
size
:
cmd
+=
[
"--capture-size"
,
size
]
result
=
subprocess
.
run
(
cmd
,
capture_output
=
True
,
text
=
True
,
timeout
=
300
)
if
result
.
returncode
!=
0
:
print
(
f"error: capture failed:
{
result
.
stderr
.
strip
()
}
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
2
)
manifest
=
json
.
loads
(
result
.
stdout
)
return
manifest
[
"screens"
]
def
compare
(
baseline_path
:
Path
,
candidate_path
:
Path
,
tolerance
:
int
,
threshold
:
float
)
->
tuple
[
bool
,
float
,
Image
.
Image
|
None
]:
baseline
=
Image
.
open
(
baseline_path
).
convert
(
"RGB"
)
candidate
=
Image
.
open
(
candidate_path
).
convert
(
"RGB"
)
if
baseline
.
size
!=
candidate
.
size
:
return
False
,
1.0
,
None
diff
=
ImageChops
.
difference
(
baseline
,
candidate
)
# Count pixels where any channel differs by more than `tolerance`.
mask
=
diff
.
convert
(
"L"
).
point
(
lambda
value
:
255
if
value
>
tolerance
else
0
)
histogram
=
mask
.
histogram
()
differing
=
sum
(
histogram
[
1
:])
total
=
baseline
.
size
[
0
]
*
baseline
.
size
[
1
]
fraction
=
differing
/
total
return
fraction
<=
threshold
,
fraction
,
diff
if
fraction
>
threshold
else
None
def
main
()
->
int
:
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
parser
.
add_argument
(
"mode"
,
choices
=
[
"check"
,
"update"
])
parser
.
add_argument
(
"--binary"
,
type
=
Path
,
default
=
DEFAULT_BIN
)
parser
.
add_argument
(
"--baseline-dir"
,
type
=
Path
,
default
=
DEFAULT_BASELINE_DIR
)
parser
.
add_argument
(
"--size"
,
default
=
None
,
help
=
"WxH render size override"
)
parser
.
add_argument
(
"--tolerance"
,
type
=
int
,
default
=
8
,
help
=
"per-channel delta ignored"
)
parser
.
add_argument
(
"--threshold"
,
type
=
float
,
default
=
0.005
,
help
=
"max fraction of differing pixels before failing"
,
)
args
=
parser
.
parse_args
()
if
not
args
.
binary
.
exists
():
print
(
f"error: desktop binary not found at
{
args
.
binary
}
; build with "
"`cargo build -p jcode-desktop --bin jcode-desktop`"
,
file
=
sys
.
stderr
,
)
return
2
baseline_dir
=
args
.
baseline_dir
if
args
.
size
:
baseline_dir
=
baseline_dir
/
args
.
size
with
tempfile
.
TemporaryDirectory
(
prefix
=
"jcode-gallery-golden-"
)
as
tmp
:
out_dir
=
Path
(
tmp
)
screens
=
capture
(
args
.
binary
,
out_dir
,
args
.
size
)
if
args
.
mode
==
"update"
:
baseline_dir
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
for
screen
in
screens
:
source
=
out_dir
/
screen
[
"file"
]
target
=
baseline_dir
/
screen
[
"file"
]
target
.
write_bytes
(
source
.
read_bytes
())
print
(
f"updated
{
len
(
screens
)
}
baselines in
{
baseline_dir
}
"
)
return
0
if
not
baseline_dir
.
exists
():
print
(
f"error: no baselines at
{
baseline_dir
}
; run "
f"`
{
sys
.
argv
[
0
]
}
update` first"
,
file
=
sys
.
stderr
,
)
return
2
failures
=
[]
for
screen
in
screens
:
name
=
screen
[
"file"
]
baseline_path
=
baseline_dir
/
name
candidate_path
=
out_dir
/
name
if
not
baseline_path
.
exists
():
failures
.
append
((
name
,
"missing baseline"
,
None
))
continue
ok
,
fraction
,
diff
=
compare
(
baseline_path
,
candidate_path
,
args
.
tolerance
,
args
.
threshold
)
if
ok
:
print
(
f"ok
{
name
}
diff=
{
fraction
:.4%
}
"
)
else
:
diff_dir
=
ROOT
/
"target"
/
"gallery-golden-diffs"
diff_dir
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
candidate_copy
=
diff_dir
/
name
candidate_copy
.
write_bytes
(
candidate_path
.
read_bytes
())
diff_path
=
None
if
diff
is
not
None
:
diff_path
=
diff_dir
/
f"diff-
{
name
}
"
diff
.
save
(
diff_path
)
failures
.
append
((
name
,
f"
{
fraction
:.4%
}
pixels differ"
,
diff_path
))
print
(
f"FAIL
{
name
}
diff=
{
fraction
:.4%
}
"
)
if
failures
:
print
(
f"
\n
{
len
(
failures
)
}
state(s) regressed:"
)
for
name
,
reason
,
diff_path
in
failures
:
suffix
=
f" (diff:
{
diff_path
}
)"
if
diff_path
else
""
print
(
f"
{
name
}
:
{
reason
}
{
suffix
}
"
)
print
(
"
\n
If the change is intentional, refresh with: "
f"
{
sys
.
argv
[
0
]
}
update"
+
(
f" --size
{
args
.
size
}
"
if
args
.
size
else
""
)
)
return
1
print
(
f"
\n
all
{
len
(
screens
)
}
gallery states match baselines"
)
return
0
if
__name__
==
"__main__"
:
sys
.
exit
(
main
())
You can’t perform that action at this time.