Skip to content

Commit

Permalink
refactor: Fix tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Sep 7, 2024
2 parents 7ffae09 + 1cec3e9 commit 48dc861
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
62 changes: 38 additions & 24 deletions spec/file_watcher_spec.cr
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require "./spec_helper"

TMP_DIR = "spec/tmp"
TMP_DIR = File.join("spec", "tmp")

describe FileWatcher do
around_each do |example|
FileUtils.rm_rf(TMP_DIR)

with_timeout(2.second) do
with_timeout(1.second) do
example.run
end
rescue TimeoutError
Expand All @@ -21,7 +21,9 @@ describe FileWatcher do
create_file(File.join(TMP_DIR, "example.txt"))
end

FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 0.01.seconds) do |event|
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s

FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
event.type.added?.should be_true
event.path.should eq File.join(TMP_DIR, "example.txt")
break
Expand All @@ -35,7 +37,9 @@ describe FileWatcher do
File.delete(File.join(TMP_DIR, "example.txt"))
end

FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 0.01.seconds) do |event|
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s

FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
event.type.deleted?.should be_true
event.path.should eq File.join(TMP_DIR, "example.txt")
break
Expand All @@ -49,7 +53,9 @@ describe FileWatcher do
FileUtils.touch(File.join(TMP_DIR, "example.txt"))
end

FileWatcher.watch(File.join(TMP_DIR, "**", "*"), interval: 0.01.seconds) do |event|
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s

FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
event.type.changed?.should be_true
event.path.should eq File.join(TMP_DIR, "example.txt")
break
Expand All @@ -63,7 +69,9 @@ describe FileWatcher do
create_file(File.join(TMP_DIR, "text.txt"))
end

FileWatcher.watch(File.join(TMP_DIR, "**", "*.json"), interval: 0.01.seconds) do |event|
pattern : String = Path[TMP_DIR, "**", "*.json"].to_posix.to_s

FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
event.type.added?.should be_true
event.path.should eq File.join(TMP_DIR, "data.json")
break
Expand All @@ -75,7 +83,9 @@ describe FileWatcher do
create_file(File.join(TMP_DIR, "example.txt"))
end

FileWatcher.watch(Path[TMP_DIR, "**", "*"], interval: 0.01.seconds) do |event|
pattern : Path = Path[TMP_DIR, "**", "*"].to_posix

FileWatcher.watch(pattern, interval: 0.01.seconds) do |event|
event.type.added?.should be_true
event.path.should eq File.join(TMP_DIR, "example.txt")
break
Expand All @@ -90,30 +100,27 @@ describe FileWatcher do

events = [] of FileWatcher::Event

FileWatcher.watch(
Path[TMP_DIR, "folder_a", "*.txt"],
File.join(TMP_DIR, "folder_b/*.txt"),
interval: 0.01.seconds
) do |event|
pattern_a : Path = Path[TMP_DIR, "folder_a", "*.txt"].to_posix
pattern_b : String = Path[TMP_DIR, "folder_b", "*.txt"].to_posix.to_s

FileWatcher.watch(pattern_a, pattern_b, interval: 0.01.seconds) do |event|
events << event

break if events.size == 2
end

events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_a/foo.txt"), :added)
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_b/bar.txt"), :added)
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_a", "foo.txt"), :added)
events.should contain FileWatcher::Event.new(File.join(TMP_DIR, "folder_b", "bar.txt"), :added)
end

it "accepts match_option" do
spawn do
create_file(File.join(TMP_DIR, ".dotfile"))
end

FileWatcher.watch(
Path[TMP_DIR, "**", "*"],
interval: 0.01.seconds,
match_option: File::MatchOptions::DotFiles
) do |event|
pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s

FileWatcher.watch(pattern, interval: 0.01.seconds, match_option: File::MatchOptions::DotFiles) do |event|
event.type.added?.should be_true
event.path.should eq File.join(TMP_DIR, ".dotfile")
break
Expand All @@ -128,12 +135,17 @@ describe FileWatcher do
end

spawn do
create_file(File.join(TMP_DIR, "original/example.txt"))
create_file(File.join(TMP_DIR, "original", "example.txt"))
end

FileWatcher.watch("spec/tmp/**/*.txt", interval: 0.01.seconds, follow_symlinks: true) do |event|
pattern : String = Path[TMP_DIR, "**", "*.txt"].to_posix.to_s

FileWatcher.watch(pattern, interval: 0.01.seconds, follow_symlinks: true) do |event|
# ignores if original file event is emitted first
next if event.path == File.join(TMP_DIR, "original", "example.txt")

event.type.added?.should be_true
event.path.should eq File.join(TMP_DIR, "symlink/example.txt")
event.path.should eq File.join(TMP_DIR, "symlink", "example.txt")
break
end
end
Expand All @@ -143,14 +155,16 @@ describe FileWatcher do
create_file(File.join(TMP_DIR, "example.txt"))
end

pattern : String = Path[TMP_DIR, "**", "*"].to_posix.to_s

started_at = Time.utc

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

now = Time.utc
now.should be_close(started_at + 1.second, 0.1.seconds)
now.should be_close(started_at + 0.5.seconds, 0.1.seconds)

break
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ def create_file(file_name : String | Path)

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

File.write(file_name, "")
File.write(file_name, "hello")
end

0 comments on commit 48dc861

Please sign in to comment.