Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
rtk/tests/diff_byte_accuracy_test.rs at develop · rtk-ai/rtk · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
rtk-ai
rtk
Repository navigation
Code
Issues
900
(900)
Pull requests
725
(725)
Discussions
Actions
Projects
Security and quality
3
(3)
Insights
More
items
Files
Expand file tree
develop
Breadcrumbs
rtk
/
tests
/
diff_byte_accuracy_test.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
185 lines (162 loc) · 6.25 KB
develop
Breadcrumbs
rtk
/
tests
/
diff_byte_accuracy_test.rs
Copy path
Top
File metadata and controls
Code
Blame
185 lines (162 loc) · 6.25 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use
std
::
fs
;
use
std
::
path
::
Path
;
use
std
::
process
::
Output
;
mod
common
;
const
DIFF_SUBCOMMAND
:
&
str
=
"diff"
;
const
LF_FILE
:
&
str
=
"lf.txt"
;
const
CRLF_FILE
:
&
str
=
"crlf.txt"
;
const
LF_CONTENT
:
&
str
=
"alpha
\n
beta
\n
"
;
const
CRLF_CONTENT
:
&
str
=
"alpha
\r
\n
beta
\r
\n
"
;
const
ONE_LINE_LF_CONTENT
:
&
str
=
"x
\n
"
;
const
ONE_LINE_CRLF_CONTENT
:
&
str
=
"x
\r
\n
"
;
const
BOTH_FILES_SEPARATOR
:
&
str
=
"
\n
---
\n
"
;
const
WHITESPACE_ONLY_MESSAGE
:
&
str
=
"whitespace or line endings"
;
const
IDENTICAL_MESSAGE
:
&
str
=
"[ok] Files are identical"
;
const
DIFF_EXIT_CODE
:
i32
=
1
;
fn
run_rtk_diff
(
file1
:
&
Path
,
file2
:
&
Path
)
->
Output
{
let
file1 = file1
.
display
(
)
.
to_string
(
)
;
let
file2 = file2
.
display
(
)
.
to_string
(
)
;
common
::
rtk_command
(
)
.
args
(
[
DIFF_SUBCOMMAND
,
&
file1
,
&
file2
]
)
.
output
(
)
.
expect
(
"run rtk diff"
)
}
fn
assert_explains_invisible_difference
(
lf_content
:
&
str
,
crlf_content
:
&
str
)
{
let
dir = tempfile
::
tempdir
(
)
.
expect
(
"tempdir"
)
;
let
lf = dir
.
path
(
)
.
join
(
LF_FILE
)
;
let
crlf = dir
.
path
(
)
.
join
(
CRLF_FILE
)
;
fs
::
write
(
&
lf
,
lf_content
)
.
expect
(
"write LF fixture"
)
;
fs
::
write
(
&
crlf
,
crlf_content
)
.
expect
(
"write CRLF fixture"
)
;
let
output =
run_rtk_diff
(
&
lf
,
&
crlf
)
;
let
stdout =
String
::
from_utf8
(
output
.
stdout
)
.
expect
(
"stdout utf8"
)
;
assert_eq
!
(
output
.
status
.
code
(
)
,
Some
(
DIFF_EXIT_CODE
)
,
"{stdout}"
)
;
assert
!
(
stdout
.
contains
(
WHITESPACE_ONLY_MESSAGE
)
,
"CRLF-vs-LF diff should explain the byte-only difference:
\n
{stdout}"
)
;
assert
!
(
!stdout
.
contains
(
BOTH_FILES_SEPARATOR
)
,
"the two indistinguishable blobs must not replace the explanation:
\n
{stdout}"
)
;
assert
!
(
!stdout
.
contains
(
IDENTICAL_MESSAGE
)
,
"byte-different files must not be reported identical:
\n
{stdout}"
)
;
}
#
[
test
]
fn
small_crlf_vs_lf_diff_prints_whitespace_message
(
)
{
assert_explains_invisible_difference
(
LF_CONTENT
,
CRLF_CONTENT
)
;
}
#
[
test
]
fn
one_line_crlf_vs_lf_diff_prints_whitespace_message
(
)
{
// The message is ~20 tokens and a one-line pair ~2, so any fixed allowance
// above raw drops it here. It is shown regardless of size.
assert_explains_invisible_difference
(
ONE_LINE_LF_CONTENT
,
ONE_LINE_CRLF_CONTENT
)
;
}
#
[
test
]
fn
missing_operand_exits_two_and_names_the_failed_path
(
)
{
let
dir = tempfile
::
tempdir
(
)
.
unwrap
(
)
;
let
existing = dir
.
path
(
)
.
join
(
"existing.txt"
)
;
let
missing = dir
.
path
(
)
.
join
(
"missing.txt"
)
;
fs
::
write
(
&
existing
,
"content
\n
"
)
.
unwrap
(
)
;
for
(
left
,
right
)
in
[
(
&
missing
,
&
existing
)
,
(
&
existing
,
&
missing
)
]
{
let
output =
run_rtk_diff
(
left
,
right
)
;
let
stderr =
String
::
from_utf8_lossy
(
&
output
.
stderr
)
;
assert_eq
!
(
output
.
status
.
code
(
)
,
Some
(
2
)
,
"{stderr}"
)
;
assert
!
(
output
.
stdout
.
is_empty
(
)
,
"read errors belong on stderr"
)
;
assert
!
(
stderr
.
contains
(
&
*
missing
.
to_string_lossy
(
)
)
,
"{stderr}"
)
;
}
}
#
[
test
]
fn
non_utf8_files_are_compared_instead_of_reported_as_io_errors
(
)
{
let
dir = tempfile
::
tempdir
(
)
.
unwrap
(
)
;
let
left = dir
.
path
(
)
.
join
(
"left.bin"
)
;
let
right = dir
.
path
(
)
.
join
(
"right.bin"
)
;
fs
::
write
(
&
left
,
b"
\xff
"
)
.
unwrap
(
)
;
// Reading succeeds even though UTF-8 conversion fails.
for
(
content
,
expected
)
in
[
(
b"
\xff
"
,
0
)
,
(
b"
\xfe
"
,
1
)
]
{
fs
::
write
(
&
right
,
content
)
.
unwrap
(
)
;
let
output =
run_rtk_diff
(
&
left
,
&
right
)
;
assert_eq
!
(
output
.
status
.
code
(
)
,
Some
(
expected
)
)
;
assert
!
(
output
.
stderr
.
is_empty
(
)
)
;
if
expected ==
1
{
let
stdout =
String
::
from_utf8
(
output
.
stdout
)
.
unwrap
(
)
;
assert
!
(
stdout
.
contains
(
"Binary files"
)
,
"{stdout}"
)
;
assert
!
(
stdout
.
contains
(
"differ"
)
,
"{stdout}"
)
;
}
}
}
#
[
test
]
fn
one_file_operand_is_a_usage_error_not_a_stdin_condense
(
)
{
let
dir = tempfile
::
tempdir
(
)
.
unwrap
(
)
;
let
file = dir
.
path
(
)
.
join
(
"o.txt"
)
;
fs
::
write
(
&
file
,
"a
\n
"
)
.
unwrap
(
)
;
let
output = common
::
rtk_command
(
)
.
args
(
[
DIFF_SUBCOMMAND
,
&
file
.
display
(
)
.
to_string
(
)
]
)
.
stdin
(
std
::
process
::
Stdio
::
null
(
)
)
.
output
(
)
.
expect
(
"run rtk diff with one operand"
)
;
let
stderr =
String
::
from_utf8_lossy
(
&
output
.
stderr
)
;
assert_eq
!
(
output
.
status
.
code
(
)
,
Some
(
2
)
,
"a single file operand is a usage error, not a diff to condense: {stderr}"
)
;
assert
!
(
stderr
.
contains
(
"missing operand after"
)
,
"the usage error must name the form, like diff does: {stderr}"
)
;
}
#
[
test
]
fn
one_missing_operand_is_a_usage_error_before_any_read
(
)
{
let
dir = tempfile
::
tempdir
(
)
.
expect
(
"tempdir"
)
;
let
piped = dir
.
path
(
)
.
join
(
"piped.diff"
)
;
fs
::
write
(
&
piped
,
"--- a/f
\n
+++ b/f
\n
@@ -1 +1 @@
\n
-x
\n
+y
\n
"
)
.
expect
(
"write piped diff"
)
;
let
missing = dir
.
path
(
)
.
join
(
"missing.txt"
)
;
let
output = common
::
rtk_command
(
)
.
args
(
[
DIFF_SUBCOMMAND
,
&
missing
.
display
(
)
.
to_string
(
)
]
)
.
stdin
(
fs
::
File
::
open
(
&
piped
)
.
expect
(
"open piped diff"
)
)
.
output
(
)
.
expect
(
"run rtk diff with one missing operand"
)
;
let
stderr =
String
::
from_utf8_lossy
(
&
output
.
stderr
)
;
assert_eq
!
(
output
.
status
.
code
(
)
,
Some
(
2
)
,
"{stderr}"
)
;
assert
!
(
stderr
.
contains
(
"missing operand after"
)
,
"the usage error comes before the operand is opened: {stderr}"
)
;
assert
!
(
!stderr
.
contains
(
"rtk diff:"
)
,
"the missing operand is not read: {stderr}"
)
;
assert
!
(
output
.
stdout
.
is_empty
(
)
,
"stdin is not condensed on a usage error: {}"
,
String
::
from_utf8_lossy
(
&
output
.
stdout
)
)
;
}
#
[
test
]
fn
explicit_stdin_dash_still_condenses
(
)
{
use
std
::
io
::
Write
as
_
;
use
std
::
process
::
Stdio
;
let
mut
child = common
::
rtk_command
(
)
.
args
(
[
DIFF_SUBCOMMAND
,
"-"
]
)
.
stdin
(
Stdio
::
piped
(
)
)
.
stdout
(
Stdio
::
piped
(
)
)
.
stderr
(
Stdio
::
piped
(
)
)
.
spawn
(
)
.
expect
(
"spawn rtk diff -"
)
;
child
.
stdin
.
take
(
)
.
expect
(
"stdin pipe"
)
.
write_all
(
b"--- a/f
\n
+++ b/f
\n
@@ -1 +1 @@
\n
-x
\n
+y
\n
"
)
.
expect
(
"write diff to stdin"
)
;
let
output = child
.
wait_with_output
(
)
.
expect
(
"wait for rtk diff -"
)
;
assert_eq
!
(
output
.
status
.
code
(
)
,
Some
(
0
)
,
"the `-` form is the stdin mode"
)
;
let
stdout =
String
::
from_utf8_lossy
(
&
output
.
stdout
)
;
assert
!
(
stdout
.
contains
(
"-x"
)
|| stdout
.
contains
(
"+y"
)
,
"{stdout}"
)
;
}
You can’t perform that action at this time.