forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile.jl
156 lines (135 loc) · 3.78 KB
/
file.jl
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
#############################################
# Create some temporary files & directories #
#############################################
# This first section may not run for non-UNIX people.
# If so, create the directories and files manually, and comment out this section
# (Or fix up the code to support such operations on Windows!)
dir = mktempdir()
file = joinpath(dir, "afile.txt")
touch(file)
#######################################################################
# This section tests some of the features of the stat-based file info #
#######################################################################
@test isdir(dir) == true
@test isfile(dir) == false
@test islink(dir) == false
@test isdir(file) == false
@test isfile(file) == true
@test islink(file) == false
@test isreadable(file) == true
@test iswritable(file) == true
# Here's something else that might be UNIX-specific?
run(`chmod -w $file`)
@test iswritable(file) == false
run(`chmod +w $file`)
@test isexecutable(file) == false
@test filesize(file) == 0
# On windows the filesize of a folder is the accumulation of all the contained
# files and is thus zero in this case.
@windows_only begin
@test filesize(dir) == 0
end
@unix_only begin
@test filesize(dir) > 0
end
@test mtime(file) >= mtime(dir)
# rename file
newfile = joinpath(dir, "bfile.txt")
mv(file, newfile)
@test ispath(file) == false
@test isfile(newfile) == true
file = newfile
#######################################################################
# This section tests file watchers. #
#######################################################################
function test_file_poll(channel,timeout_ms)
rc = poll_file(file, iround(timeout_ms/10), timeout_ms)
put(channel,rc)
end
function test_timeout(tval)
tic()
channel = RemoteRef()
@async test_file_poll(channel,tval)
tr = take(channel)
t_elapsed = toq()
@test tr == 0
tdiff = t_elapsed * 1000
@test tval <= tdiff
end
function test_touch(slval)
tval = slval+100
channel = RemoteRef()
@async test_file_poll(channel,iround(tval))
sleep(slval/10_000) # ~one poll period
f = open(file,"a")
write(f,"Hello World\n")
close(f)
tr = take(channel)
# @test tr == 1
end
function test_monitor(slval)
FsMonitorPassed = false
fm = FileMonitor(file) do args...
FsMonitorPassed = true
end
sleep(slval/10_000)
f = open(file,"a")
write(f,"Hello World\n")
close(f)
sleep(9slval/10_000)
@test FsMonitorPassed
close(fm)
end
# Commented out the tests below due to issues 3015, 3016 and 3020
test_timeout(0.1)
test_timeout(1)
test_touch(0.1)
test_touch(1)
test_monitor(1)
test_monitor(0.1)
##########
# mmap #
##########
s = open(file, "w")
write(s, "Hello World\n")
close(s)
s = open(file, "r")
@test isreadonly(s) == true
c = mmap_array(Uint8, (11,), s)
@test c == "Hello World".data
close(s)
s = open(file, "r+")
@test isreadonly(s) == false
c = mmap_array(Uint8, (11,), s)
c[5] = uint8('x')
msync(c)
close(s)
s = open(file, "r")
str = readline(s)
close(s)
@test beginswith(str, "Hellx World")
#######################################################################
# This section tests temporary file and directory creation. #
#######################################################################
# my_tempdir = tempdir()
# @test isdir(my_tempdir) == true
# path = tempname()
# @test ispath(path) == false
# (file, f) = mktemp()
# print(f, "Here is some text")
# close(f)
# @test isfile(file) == true
# @test readall(file) == "Here is some text"
emptyfile = joinpath(dir, "empty")
touch(emptyfile)
emptyf = open(emptyfile)
@test isempty(readlines(emptyf))
close(emptyf)
rm(emptyfile)
############
# Clean up #
############
rm(file)
rmdir(dir)
@test ispath(file) == false
@test ispath(dir) == false