Module: Filewatcher::SpecHelper

Extended by:
SpecHelper
Included in:
SpecHelper, WatchRun
Defined in:
lib/filewatcher/spec_helper.rb,
lib/filewatcher/spec_helper/watch_run.rb,
lib/filewatcher/spec_helper/ruby_watch_run.rb

Overview

Helper for common spec features between plugins

Defined Under Namespace

Modules: WatchRun Classes: RubyWatchRun

Constant Summary collapse

ENVIRONMENT_SPECS_COEFFICIENTS =
{
  -> { ENV.fetch('CI', false) } => 1,
  -> { RUBY_PLATFORM == 'java' } => 1,
  -> { Gem::Platform.local.os == 'darwin' } => 1
}.freeze

Instance Method Summary collapse

Instance Method Details

#debug(string) ⇒ Object



60
61
62
# File 'lib/filewatcher/spec_helper.rb', line 60

def debug(string)
  logger.debug "Thread ##{Thread.current.object_id} #{string}"
end

#environment_specs_coefficientsObject



27
28
29
# File 'lib/filewatcher/spec_helper.rb', line 27

def environment_specs_coefficients
  ENVIRONMENT_SPECS_COEFFICIENTS
end

#loggerObject



23
24
25
# File 'lib/filewatcher/spec_helper.rb', line 23

def logger
  @logger ||= Logger.new($stdout, level: :debug)
end

#system_stat(filename) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/filewatcher/spec_helper.rb', line 64

def system_stat(filename)
  case (host_os = RbConfig::CONFIG['host_os'])
  when /linux(-gnu)?/
    `stat --printf 'Modification: %y, Change: %z\n' #{filename}`
  when /darwin\d*/
    `stat #{filename}`
  when *Gem::WIN_PATTERNS
    system_stat_windows filename
  else
    "Unknown OS `#{host_os}` for system's `stat` command"
  end
end

#system_stat_windows(filename) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/filewatcher/spec_helper.rb', line 77

def system_stat_windows(filename)
  filename = filename.gsub('/', '\\\\\\')
  properties = 'CreationDate,InstallDate,LastModified,LastAccessed'
  command = "wmic datafile where Name=\"#{filename}\" get #{properties}"
  # debug command
  `#{command}`
end

#wait(seconds: 1, interval: 1, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/filewatcher/spec_helper.rb', line 31

def wait(seconds: 1, interval: 1, &block)
  environment_specs_coefficients.each do |condition, coefficient|
    next unless instance_exec(&condition)

    interval *= coefficient
    seconds *= coefficient
  end

  if block
    wait_with_block seconds, interval, &block
  else
    wait_without_block seconds
  end
end

#wait_with_block(seconds, interval, &_block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/filewatcher/spec_helper.rb', line 46

def wait_with_block(seconds, interval, &_block)
  (seconds / interval).ceil.times do
    break if yield

    debug "sleep interval #{interval}"
    sleep interval
  end
end

#wait_without_block(seconds) ⇒ Object



55
56
57
58
# File 'lib/filewatcher/spec_helper.rb', line 55

def wait_without_block(seconds)
  debug "sleep without intervals #{seconds}"
  sleep seconds
end