FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
basic-cli/tests/file.roc at main · codegod100/basic-cli · GitHub
codegod100
/
basic-cli
Public
forked from
roc-lang/basic-cli
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
basic-cli
/
tests
/
file.roc
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
270 lines (201 loc) · 9.17 KB
Breadcrumbs
basic-cli
/
tests
/
file.roc
Copy path
File metadata and controls
executable file
·
270 lines (201 loc) · 9.17 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
app
[main!] {
pf: platform
"
../platform/main.roc
"
,
json:
"
https://github.com/lukewilliamboswell/roc-json/releases/download/0.13.0/RqendgZw5e1RsQa3kFhgtnMP8efWoqGRsAvubx4-zus.tar.br
"
,
}
import
pf.
Stdout
import
pf.
Stderr
import
pf.
File
import
pf.
Arg
exposing
[
Arg
]
import
pf.
Cmd
import
json.
Json
main
! :
List
Arg
=
>
Result
{}
_
main
! = |
_args
|
when
run_tests!({})
is
Ok
(
_
) ->
cleanup_test_files!(
FilesNeedToExist
)
Err
(err) ->
_
= cleanup_test_files!(
FilesMaybeExist
)
Err
(
Exit
(
1
,
"
Test run failed:
\n\t
${Inspect.to_str(err)}
"
))
run_tests
! : {} =
>
Result
{}
_
run_tests
! = |{}|
Stdout
.
line
!("
Testing
some
File
functions...
"
)?
Stdout.line!(
"
This
will create and manipulate test files in the current directory.
"
)?
Stdout.line!(
"
"
)?
# Test basic file operations
test_basic_file_operations!({})?
# Test file type checking
test_file_type_checking!({})?
# Test file reader with capacity
test_file_reader_with_capacity!({})?
# Test hard link creation
test_hard_link!({})?
# Test file rename
test_file_rename!({})?
# Test file exists
test_file_exists!({})?
Stdout.line!(
"
\nI ran all file function tests.
"
)
test_basic_file_operations! : {} => Result {} _
test_basic_file_operations! = |{}|
Stdout.line!(
"
Testing
File
.
write_bytes
! and
File
.
read_bytes
!:")?
test_bytes = [
72
,
101
,
108
,
108
,
111
,
44
,
32
,
87
,
111
,
114
,
108
,
100
,
33
]
# "Hello, World!" in bytes
File
.
write_bytes
!(
test_bytes
,
"
test_bytes.txt
"
)?
file_content_bytes =
File
.
read_bytes
!("
test_bytes
.
txt
")?
Stdout
.
line
!("
Bytes
in test_bytes
.
txt
: ${
Inspect
.
to_str
(
file_content_bytes
)}")?
Stdout
.
line
!("\
nTesting
File
.
write
!:")?
File
.
write
!({ some:
"
json stuff
"
},
"
test_write.json
"
,
Json
.
utf8
)?
json_file_content =
File
.
read_utf8
!("
test_write
.
json
")?
Stdout
.
line
!("
Content
of test_write
.
json
: ${json_file_content}
"
)?
Ok({})
test_file_type_checking! : {} => Result {} _
test_file_type_checking! = |{}|
Stdout.line!(
"
\nTesting
File
.
is_file
!:")?
is_file_result =
File
.
is_file
!("
test_bytes
.
txt
")?
if
is_file_result
then
Stdout
.
line
!("✓ test_bytes
.
txt
is
confirmed to be a file
"
)?
else
Stderr.line!(
"
✗ test_bytes
.
txt
is
not recognized
as
a file
"
)?
Stdout.line!(
"
\nTesting
File
.
is_sym_link
!:")?
is_symlink_one =
File
.
is_sym_link
!("
test_bytes
.
txt
")?
if
is_symlink_one
then
Stderr
.
line
!("✗ test_bytes
.
txt
is
a symbolic link
"
)?
else
Stdout.line!(
"
✓ test_bytes
.
txt
is
not a symbolic link
"
)?
Cmd.exec!(
"
ln
"
,[
"
-
s
"
,
"
test_bytes
.
txt
"
,
"
test_symlink
.
txt
"
])?
is_symlink_two = File.is_sym_link!(
"
test_symlink
.
txt
"
)?
if is_symlink_two then
Stdout.line!(
"
✓ test_symlink
.
txt
is
a symbolic link
"
)?
else
Stderr.line!(
"
✗ test_symlink
.
txt
is
not a symbolic link
"
)?
Stdout.line!(
"
\nTesting
File
.
type
!:")?
file_type_file =
File
.
type
!("
test_bytes
.
txt
")?
Stdout
.
line
!("
test_bytes
.
txt
file type: ${
Inspect
.
to_str
(
file_type_file
)}")?
file_type_dir =
File
.
type
!(".")?
Stdout
.
line
!(". file type: ${
Inspect
.
to_str
(
file_type_dir
)}")?
file_type_symlink =
File
.
type
!("
test_symlink
.
txt
")?
Stdout
.
line
!("
test_symlink
.
txt
file type: ${
Inspect
.
to_str
(
file_type_symlink
)}")?
Ok
({})
test_file_reader_with_capacity
! : {} =
>
Result
{}
_
test_file_reader_with_capacity
! = |{}|
Stdout
.
line
!("\
nTesting
File
.
open_reader_with_capacity
!:")?
# First, create a multi-line test file
multi_line_content =
"
First line
\n
Second line
\n
Third line
\n
"
File
.
write_utf8
!(
multi_line_content
,
"
test_multiline.txt
"
)?
# Open reader with custom capacity
reader_buf_size =
3
reader =
File
.
open_reader_with_capacity
!("
test_multiline
.
txt
", reader_buf_size)?
Stdout
.
line
!("✓
Successfully
opened reader with ${
Num
.
to_str
(
reader_buf_size
)} byte capacity
"
)?
# Read lines one by one
Stdout.line!(
"
\nReading lines from file:
"
)?
line1_bytes = File.read_line!(reader)?
line1_str = Str.from_utf8(line1_bytes) ? |_| LineOneInvalidUtf8
Stdout.line!(
"
Line
1
: ${line1_str}
"
)?
line2_bytes = File.read_line!(reader)?
line2_str = Str.from_utf8(line2_bytes) ? |_| LineTwoInvalidUtf8
Stdout.line!(
"
Line
2
: ${line2_str}
"
)?
Ok({})
test_hard_link! : {} => Result {} _
test_hard_link! = |{}|
Stdout.line!(
"
\nTesting
File
.
hard_link
!:")?
# Create original file
File
.
write_utf8
!("
Original
file content for hard link test
"
,
"
test_original_file
.
txt
"
)?
# Create hard link
when File.hard_link!(
"
test_original_file
.
txt
"
,
"
test_link_to_original
.
txt
"
) is
Ok({}) ->
Stdout.line!(
"
✓
Successfully
created hard link: test_link_to_original
.
txt
"
)?
ls_li_output =
Cmd.new(
"
ls
"
)
|> Cmd.args([
"
-
li
"
,
"
test_original_file
.
txt
"
,
"
test_link_to_original
.
txt
"
])
|> Cmd.exec_output!()?
inodes =
Str.split_on(ls_li_output.stdout_utf8,
"
\n
"
)
|> List.map(|line|
Str.split_on(line,
"
"
)
|> List.take_first(1)
)
first_inode = List.get(inodes, 0) ? |_| FirstInodeNotFound
second_inode = List.get(inodes, 1) ? |_| SecondInodeNotFound
Stdout.line!(
"
Hard
link inodes should be equal: ${
Inspect
.
to_str
(
first_inode
==
second_inode)}
"
)?
# Verify both files exist and have same content
original_content = File.read_utf8!(
"
test_original_file
.
txt
"
)?
link_content = File.read_utf8!(
"
test_link_to_original
.
txt
"
)?
if original_content == link_content then
Stdout.line!(
"
✓
Hard
link contains same content
as
original
"
)
else
Stderr.line!(
"
✗
Hard
link content differs from original
"
)
Err(err) ->
Stderr.line!(
"
✗
Hard
link creation failed: ${
Inspect
.
to_str
(
err
)}")
test_file_rename
! : {} =
>
Result
{}
_
test_file_rename
! = |{}|
Stdout
.
line
!("\
nTesting
File
.
rename
!:")?
# Create original file
original_name =
"
test_rename_original.txt
"
new_name =
"
test_rename_new.txt
"
File
.
write_utf8
!("
Content
for rename test
"
, original_name)?
# Rename the file
when File.rename!(original_name, new_name) is
Ok({}) ->
Stdout.line!(
"
✓
Successfully
renamed ${original_name} to ${new_name}
"
)?
# Verify original file no longer exists
original_exists_after =
when File.is_file!(original_name) is
Ok(exists) -> exists
Err(_) -> Bool.false
if original_exists_after then
Stderr.line!(
"
✗
Original
file ${original_name} still exists after rename
"
)?
else
Stdout.line!(
"
✓
Original
file ${original_name} no longer exists
"
)?
# Verify new file exists and has correct content
new_exists = File.is_file!(new_name)?
if new_exists then
Stdout.line!(
"
✓
Renamed
file ${new_name} exists
"
)?
content = File.read_utf8!(new_name)?
if content ==
"
Content
for rename test
"
then
Stdout.line!(
"
✓
Renamed
file has correct content
"
)?
else
Stderr.line!(
"
✗
Renamed
file has incorrect content
"
)?
else
Stderr.line!(
"
✗
Renamed
file ${new_name} does not exist
"
)?
Err(err) ->
Stderr.line!(
"
✗
File
rename failed: ${
Inspect
.
to_str
(
err
)}")?
Ok
({})
test_file_exists
! : {} =
>
Result
{}
_
test_file_exists
! = |{}|
Stdout
.
line
!("\
nTesting
File
.
exists
!:")?
# Test that a file that exists returns true
filename =
"
test_exists.txt
"
File
.
write_utf8
!("", filename)?
test_file_exists =
File
.
exists
!(
filename
) ?
FileExistsCheckFailed
if
test_file_exists
then
Stdout
.
line
!("✓
File
.
exists
! returns true for a file that exists
"
)?
else
Stderr.line!(
"
✗
File
.
exists
! returned false for a file that exists
"
)?
# Test that a file that does not exist returns false
File.delete!(filename)?
test_file_exists_after_delete = File.exists!(filename) ? FileExistsCheckAfterDeleteFailed
if test_file_exists_after_delete then
Stderr.line!(
"
✗
File
.
exists
! returned true for a file that does not exist
"
)?
else
Stdout.line!(
"
✓
File
.
exists
! returns false for a file that does not exist
"
)?
Ok({})
cleanup_test_files! : [FilesNeedToExist, FilesMaybeExist] => Result {} _
cleanup_test_files! = |files_requirement|
Stdout.line!(
"
\nCleaning up test files...
"
)?
test_files = [
"
test_bytes
.
txt
"
,
"
test_symlink
.
txt
"
,
"
test_write
.
json
"
,
"
test_multiline
.
txt
"
,
"
test_original_file
.
txt
"
,
"
test_link_to_original
.
txt
"
,
"
test_rename_new
.
txt
"
,
]
delete_result = List.for_each_try!(
test_files,
|filename| File.delete!(filename)
)
when files_requirement is
FilesNeedToExist ->
delete_result ? FileDeletionFailed
FilesMaybeExist ->
Ok({})?
Stdout.line!(
"
✓
Deleted
all files.
"
)
Back
|
FazBrowse Home
|
New Git URL