Skip to content

Commit 48dc861

Browse files
authored
refactor: Fix tests on Windows
2 parents 7ffae09 + 1cec3e9 commit 48dc861

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

spec/file_watcher_spec.cr

+38-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
require "./spec_helper"
22

3-
TMP_DIR = "spec/tmp"
3+
TMP_DIR = File.join("spec", "tmp")
44

55
describe FileWatcher do
66
around_each do |example|
77
FileUtils.rm_rf(TMP_DIR)
88

9-
with_timeout(2.second) do
9+
with_timeout(1.second) do
1010
example.run
1111
end
1212
rescue TimeoutError
@@ -21,7 +21,9 @@ describe FileWatcher do
2121
create_file(File.join(TMP_DIR, "example.txt"))
2222
end
2323

24-
FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 0.01.seconds) do |event|
24+
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s
25+
26+
FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
2527
event.type.added?.should be_true
2628
event.path.should eq File.join(TMP_DIR, "example.txt")
2729
break
@@ -35,7 +37,9 @@ describe FileWatcher do
3537
File.delete(File.join(TMP_DIR, "example.txt"))
3638
end
3739

38-
FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 0.01.seconds) do |event|
40+
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s
41+
42+
FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
3943
event.type.deleted?.should be_true
4044
event.path.should eq File.join(TMP_DIR, "example.txt")
4145
break
@@ -49,7 +53,9 @@ describe FileWatcher do
4953
FileUtils.touch(File.join(TMP_DIR, "example.txt"))
5054
end
5155

52-
FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 0.01.seconds) do |event|
56+
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s
57+
58+
FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
5359
event.type.changed?.should be_true
5460
event.path.should eq File.join(TMP_DIR, "example.txt")
5561
break
@@ -63,7 +69,9 @@ describe FileWatcher do
6369
create_file(File.join(TMP_DIR, "text.txt"))
6470
end
6571

66-
FileWatcher.watch(File.join(TMP_DIR, "**", "*.json"), interval: 0.01.seconds) do |event|
72+
pattern : String = Path[TMP_DIR, "**", "*.json"].to_posix.to_s
73+
74+
FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
6775
event.type.added?.should be_true
6876
event.path.should eq File.join(TMP_DIR, "data.json")
6977
break
@@ -75,7 +83,9 @@ describe FileWatcher do
7583
create_file(File.join(TMP_DIR, "example.txt"))
7684
end
7785

78-
FileWatcher.watch(Path[TMP_DIR, "**", "*"], interval: 0.01.seconds) do |event|
86+
pattern : Path = Path[TMP_DIR, "**", "*"].to_posix
87+
88+
FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
7989
event.type.added?.should be_true
8090
event.path.should eq File.join(TMP_DIR, "example.txt")
8191
break
@@ -90,30 +100,27 @@ describe FileWatcher do
90100

91101
events = [] of FileWatcher::Event
92102

93-
FileWatcher.watch(
94-
Path[TMP_DIR, "folder_a", "*.txt"],
95-
File.join(TMP_DIR, "folder_b/*.txt"),
96-
interval: 0.01.seconds
97-
) do |event|
103+
pattern_a : Path = Path[TMP_DIR, "folder_a", "*.txt"].to_posix
104+
pattern_b : String = Path[TMP_DIR, "folder_b", "*.txt"].to_posix.to_s
105+
106+
FileWatcher.watch(pattern_a, pattern_b, interval: 0.01.seconds) do |event|
98107
events << event
99108

100109
break if events.size == 2
101110
end
102111

103-
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_a/foo.txt"), :added)
104-
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_b/bar.txt"), :added)
112+
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_a", "foo.txt"), :added)
113+
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_b", "bar.txt"), :added)
105114
end
106115

107116
it "accepts match_option" do
108117
spawn do
109118
create_file(File.join(TMP_DIR, ".dotfile"))
110119
end
111120

112-
FileWatcher.watch(
113-
Path[TMP_DIR, "**", "*"],
114-
interval: 0.01.seconds,
115-
match_option: File::MatchOptions::DotFiles
116-
) do |event|
121+
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s
122+
123+
FileWatcher.watch(pattern, interval: 0.01.seconds, match_option: File::MatchOptions::DotFiles) do |event|
117124
event.type.added?.should be_true
118125
event.path.should eq File.join(TMP_DIR, ".dotfile")
119126
break
@@ -128,12 +135,17 @@ describe FileWatcher do
128135
end
129136

130137
spawn do
131-
create_file(File.join(TMP_DIR, "original/example.txt"))
138+
create_file(File.join(TMP_DIR, "original", "example.txt"))
132139
end
133140

134-
FileWatcher.watch("spec/tmp/**/*.txt", interval: 0.01.seconds, follow_symlinks: true) do |event|
141+
pattern : String = Path[TMP_DIR, "**", "*.txt"].to_posix.to_s
142+
143+
FileWatcher.watch(pattern, interval: 0.01.seconds, follow_symlinks: true) do |event|
144+
# ignores if original file event is emitted first
145+
next if event.path == File.join(TMP_DIR, "original", "example.txt")
146+
135147
event.type.added?.should be_true
136-
event.path.should eq File.join(TMP_DIR, "symlink/example.txt")
148+
event.path.should eq File.join(TMP_DIR, "symlink", "example.txt")
137149
break
138150
end
139151
end
@@ -143,14 +155,16 @@ describe FileWatcher do
143155
create_file(File.join(TMP_DIR, "example.txt"))
144156
end
145157

158+
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s
159+
146160
started_at = Time.utc
147161

148-
FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 1.second) do |event|
162+
FileWatcher.watch(pattern, interval: 0.5.seconds) do |event|
149163
event.type.added?.should be_true
150164
event.path.should eq File.join(TMP_DIR, "example.txt")
151165

152166
now = Time.utc
153-
now.should be_close(started_at + 1.second, 0.1.seconds)
167+
now.should be_close(started_at + 0.5.seconds, 0.1.seconds)
154168

155169
break
156170
end

spec/spec_helper.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ def create_file(file_name : String | Path)
2727

2828
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
2929

30-
File.write(file_name, "")
30+
File.write(file_name, "hello")
3131
end

0 commit comments

Comments
 (0)