filewatcher 2.0.0.beta5 → 2.0.0.beta6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4552a19cac521012fb0cca2c232a14f501530262e02639ed017c0897e5f985f
4
- data.tar.gz: f3d98c14badcbd439a9dca5ce289b40cc80aecb1c377d8ec08f80e76c398aaf5
3
+ metadata.gz: 2db0cc5cc4a588900541eef784783e1548a596b29f7359108ff2f2fc9f6c6149
4
+ data.tar.gz: ac81956d8882c15525de3e3f4747b8e9b62631fa092e19441e925e16814a7a72
5
5
  SHA512:
6
- metadata.gz: 85e030c3e12237aff4dffddfb80fdf4b288b462a35e21a63d254e1aea4ca40cd2d5ba27cb21e39e3847f16936d4291398a106bd55a3ef40205f35d75c401603c
7
- data.tar.gz: 7cec237639d7c760b963fe42b7c7d6ef40b989dde617308434e7d29037f73aad3f6fc87f971f60b4e47f420aec779e36ab76e5a6a2f27cf4f1c0849719b0b286
6
+ metadata.gz: 1fdefea6d108c43026ee922706b8259bf6a7df32019a48c8bf88aa7cf06fb7dcf2942a7ca308c80d834201159af25d804948ca0d5b5b22155140ee0e15bc4305
7
+ data.tar.gz: 86bb17e7dd94046f4c357b80c264d9c4a9a720933ed1437e37e1a9547a4884ed0a5ae97f54ef6ca26cedfe76288212fa5053b1e8a5825cfdec6393981fbf155a
@@ -30,30 +30,53 @@ class Filewatcher
30
30
 
31
31
  # Class for one file from snapshot
32
32
  class SnapshotFile
33
- STATS = %i[mtime].freeze
33
+ class << self
34
+ def stats
35
+ @stats ||= populate_stats %i[mtime]
36
+ end
37
+
38
+ def populate_stats(stats)
39
+ defined?(super) ? super(stats) : stats
40
+ end
41
+
42
+ def subtractions
43
+ @subtractions ||= populate_subtractions(
44
+ created: ->(other) { other.nil? },
45
+ updated: ->(other) { mtime && mtime > other.mtime }
46
+ )
47
+ end
34
48
 
35
- attr_reader(*STATS)
49
+ def populate_subtractions(hash)
50
+ hash = super(hash) if defined?(super)
51
+ hash
52
+ end
53
+ end
54
+
55
+ attr_reader :mtime
36
56
 
37
57
  def initialize(filename)
38
58
  @filename = filename
39
- STATS.each do |stat|
59
+ self.class.stats.each do |stat|
40
60
  time = File.public_send(stat, filename) if File.exist?(filename)
41
61
  instance_variable_set :"@#{stat}", time || Time.new(0)
42
62
  end
43
63
  end
44
64
 
45
65
  def -(other)
46
- if other.nil?
47
- :created
48
- elsif other.mtime < mtime
49
- :updated
50
- end
66
+ self.class.subtractions.find do |_event, block|
67
+ instance_exec(other, &block)
68
+ end&.first
51
69
  end
52
70
 
53
71
  def inspect
72
+ stats_string =
73
+ self.class.stats
74
+ .map { |stat| "#{stat}=#{public_send(stat)&.strftime('%F %T.%9N')&.inspect}" }
75
+ .join(', ')
76
+
54
77
  <<~OUTPUT
55
78
  #<Filewatcher::Snapshot::SnapshotFile:#{object_id}
56
- @filename=#{@filename.inspect}, mtime=#{mtime.strftime('%F %T.%9N').inspect}
79
+ @filename=#{@filename.inspect}, #{stats_string}
57
80
  >
58
81
  OUTPUT
59
82
  end
@@ -1,8 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'watch_run'
4
+
3
5
  class Filewatcher
4
6
  module SpecHelper
5
- class RubyWatchRun < WatchRun
7
+ ## Ruby API watcher for specs
8
+ class RubyWatchRun
9
+ include WatchRun
10
+
6
11
  attr_reader :filewatcher, :thread, :watched, :processed
7
12
 
8
13
  def initialize(filewatcher:, **args)
@@ -2,27 +2,23 @@
2
2
 
3
3
  class Filewatcher
4
4
  module SpecHelper
5
- ## Base class for Filewatcher runners in specs
6
- class WatchRun
5
+ ## Base module for Filewatcher runners in specs
6
+ module WatchRun
7
7
  include Filewatcher::SpecHelper
8
8
 
9
9
  TMP_DIR = "#{Dir.getwd}/spec/tmp"
10
10
 
11
11
  attr_reader :filename
12
12
 
13
- def initialize(filename:, action:, directory:)
14
- @filename =
15
- if filename.match? %r{^(/|~|[A-Z]:)} then filename
16
- else File.join(TMP_DIR, filename)
17
- end
18
- @directory = directory
13
+ def initialize(filename:, action:)
14
+ @filename = filename.match?(%r{^(/|~|[A-Z]:)}) ? filename : File.join(TMP_DIR, filename)
19
15
  @action = action
20
16
  debug "action = #{action}"
21
17
  end
22
18
 
23
19
  def start
24
20
  debug 'start'
25
- File.write(@filename, 'content1') unless @action == :create
21
+ File.write(@filename, 'content1') unless %i[create create_dir].include? @action
26
22
 
27
23
  wait seconds: 1
28
24
  end
@@ -46,19 +42,26 @@ class Filewatcher
46
42
 
47
43
  private
48
44
 
45
+ create_update_action = lambda do
46
+ ## There is no `File.write` because of strange difference in parallel `File.mtime`
47
+ ## https://cirrus-ci.com/task/6107605053472768?command=test#L497-L511
48
+ system "echo 'content2' > #{@filename}"
49
+ debug_file_mtime
50
+ end.freeze
51
+
52
+ ACTIONS = {
53
+ create: create_update_action,
54
+ update: create_update_action,
55
+ create_dir: -> { FileUtils.mkdir_p(@filename) },
56
+ delete: -> { FileUtils.remove(@filename) }
57
+ }.freeze
58
+
49
59
  def make_changes
50
60
  debug "make changes, @action = #{@action}, @filename = #{@filename}"
51
61
 
52
- if @action == :delete
53
- FileUtils.remove(@filename)
54
- elsif @directory
55
- FileUtils.mkdir_p(@filename)
56
- else
57
- ## There is no `File.write` because of strange difference in parallel `File.mtime`
58
- ## https://cirrus-ci.com/task/6107605053472768?command=test#L497-L511
59
- system "echo 'content2' > #{@filename}"
60
- debug_file_mtime
61
- end
62
+ action = self.class::ACTIONS.fetch(@action) { raise "Unknown action `#{@action}`" }
63
+
64
+ instance_exec(&action)
62
65
 
63
66
  wait seconds: 1
64
67
  end
@@ -9,12 +9,13 @@ rescue LoadError
9
9
  end
10
10
 
11
11
  require_relative 'spec_helper/watch_run'
12
+ require_relative 'spec_helper/ruby_watch_run'
12
13
 
13
14
  class Filewatcher
14
15
  ## Helper for common spec features between plugins
15
16
  module SpecHelper
16
17
  ENVIRONMENT_SPECS_COEFFICIENTS = {
17
- -> { ENV['CI'] } => 1,
18
+ -> { ENV.fetch('CI', false) } => 1,
18
19
  -> { RUBY_PLATFORM == 'java' } => 1,
19
20
  -> { Gem::Platform.local.os == 'darwin' } => 1
20
21
  }.freeze
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Filewatcher
4
- VERSION = '2.0.0.beta5'
4
+ VERSION = '2.0.0.beta6'
5
5
  end
@@ -28,7 +28,6 @@ describe Filewatcher do
28
28
 
29
29
  let(:filename) { 'tmp_file.txt' }
30
30
  let(:action) { :update }
31
- let(:directory) { false }
32
31
  ## TODO: Check its needless
33
32
  let(:every) { false }
34
33
  let(:immediate) { false }
@@ -41,8 +40,7 @@ describe Filewatcher do
41
40
 
42
41
  let(:watch_run) do
43
42
  Filewatcher::SpecHelper::RubyWatchRun.new(
44
- filename: filename, filewatcher: filewatcher, action: action,
45
- directory: directory
43
+ filename: filename, filewatcher: filewatcher, action: action
46
44
  )
47
45
  end
48
46
 
@@ -146,52 +144,59 @@ describe Filewatcher do
146
144
  end
147
145
 
148
146
  describe '#watch' do
149
- before do
150
- FileUtils.mkdir_p subdirectory if defined? subdirectory
147
+ context 'when action is known' do
148
+ before do
149
+ FileUtils.mkdir_p subdirectory if defined? subdirectory
151
150
 
152
- watch_run.run
153
- end
151
+ watch_run.run
152
+ end
154
153
 
155
- describe 'detecting file deletions' do
156
- let(:action) { :delete }
154
+ context 'when there are file deletions' do
155
+ let(:action) { :delete }
157
156
 
158
- it { is_expected.to eq [{ watch_run.filename => :deleted }] }
159
- end
157
+ it { is_expected.to eq [{ watch_run.filename => :deleted }] }
158
+ end
160
159
 
161
- context 'when there are file additions' do
162
- let(:action) { :create }
160
+ context 'when there are file additions' do
161
+ let(:action) { :create }
163
162
 
164
- it { is_expected.to eq [{ watch_run.filename => :created }] }
165
- end
163
+ it { is_expected.to eq [{ watch_run.filename => :created }] }
164
+ end
166
165
 
167
- context 'when there are file updates' do
168
- let(:action) { :update }
166
+ context 'when there are file updates' do
167
+ let(:action) { :update }
169
168
 
170
- it { is_expected.to eq [{ watch_run.filename => :updated }] }
171
- end
169
+ it { is_expected.to eq [{ watch_run.filename => :updated }] }
170
+ end
171
+
172
+ context 'when there are new files in subdirectories' do
173
+ let(:subdirectory) { File.expand_path('spec/tmp/new_sub_directory') }
172
174
 
173
- context 'when there are new files in subdirectories' do
174
- let(:subdirectory) { File.expand_path('spec/tmp/new_sub_directory') }
175
+ let(:filename) { File.join(subdirectory, 'file.txt') }
176
+ let(:action) { :create }
177
+ let(:every) { true }
178
+ ## https://github.com/filewatcher/filewatcher/pull/115#issuecomment-674581595
179
+ let(:interval) { 0.4 }
180
+
181
+ it do
182
+ expect(processed).to eq [
183
+ { subdirectory => :updated, watch_run.filename => :created }
184
+ ]
185
+ end
186
+ end
175
187
 
176
- let(:filename) { File.join(subdirectory, 'file.txt') }
177
- let(:action) { :create }
178
- let(:every) { true }
179
- ## https://github.com/filewatcher/filewatcher/pull/115#issuecomment-674581595
180
- let(:interval) { 0.4 }
188
+ context 'when there are new subdirectories' do
189
+ let(:filename) { 'new_sub_directory' }
190
+ let(:action) { :create_dir }
181
191
 
182
- it do
183
- expect(processed).to eq [
184
- { subdirectory => :updated, watch_run.filename => :created }
185
- ]
192
+ it { is_expected.to eq [{ watch_run.filename => :created }] }
186
193
  end
187
194
  end
188
195
 
189
- context 'when there are new subdirectories' do
190
- let(:filename) { 'new_sub_directory' }
191
- let(:directory) { true }
192
- let(:action) { :create }
196
+ context 'when action is unknown' do
197
+ let(:action) { :foo }
193
198
 
194
- it { is_expected.to eq [{ watch_run.filename => :created }] }
199
+ specify { expect { watch_run.run }.to raise_error(RuntimeError, 'Unknown action `foo`') }
195
200
  end
196
201
  end
197
202
 
data/spec/spec_helper.rb CHANGED
@@ -10,8 +10,6 @@ end
10
10
 
11
11
  require_relative '../lib/filewatcher/spec_helper'
12
12
 
13
- require_relative 'spec_helper/ruby_watch_run'
14
-
15
13
  ## For case when required from dumpers
16
14
  if Object.const_defined?(:RSpec)
17
15
  RSpec::Matchers.define :include_all_files do |expected|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filewatcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta5
4
+ version: 2.0.0.beta6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Flemming
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-04-29 00:00:00.000000000 Z
12
+ date: 2022-07-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -31,56 +31,56 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 0.8.0
34
+ version: 0.9.0
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 0.8.0
41
+ version: 0.9.0
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: gem_toys
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: 0.8.0
48
+ version: 0.12.1
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: 0.8.0
55
+ version: 0.12.1
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: toys
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: 0.11.4
62
+ version: 0.13.0
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: 0.11.4
69
+ version: 0.13.0
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: codecov
72
72
  requirement: !ruby/object:Gem::Requirement
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: 0.5.1
76
+ version: 0.6.0
77
77
  type: :development
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: 0.5.1
83
+ version: 0.6.0
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: rspec
86
86
  requirement: !ruby/object:Gem::Requirement
@@ -101,14 +101,14 @@ dependencies:
101
101
  requirements:
102
102
  - - "~>"
103
103
  - !ruby/object:Gem::Version
104
- version: '1.3'
104
+ version: 1.32.0
105
105
  type: :development
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
109
  - - "~>"
110
110
  - !ruby/object:Gem::Version
111
- version: '1.3'
111
+ version: 1.32.0
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: rubocop-performance
114
114
  requirement: !ruby/object:Gem::Requirement
@@ -150,17 +150,18 @@ files:
150
150
  - lib/filewatcher/snapshot.rb
151
151
  - lib/filewatcher/snapshots.rb
152
152
  - lib/filewatcher/spec_helper.rb
153
+ - lib/filewatcher/spec_helper/ruby_watch_run.rb
153
154
  - lib/filewatcher/spec_helper/watch_run.rb
154
155
  - lib/filewatcher/version.rb
155
156
  - spec/filewatcher/snapshot_spec.rb
156
157
  - spec/filewatcher/version_spec.rb
157
158
  - spec/filewatcher_spec.rb
158
159
  - spec/spec_helper.rb
159
- - spec/spec_helper/ruby_watch_run.rb
160
160
  homepage: http://github.com/filewatcher/filewatcher
161
161
  licenses:
162
162
  - MIT
163
- metadata: {}
163
+ metadata:
164
+ rubygems_mfa_required: 'true'
164
165
  post_install_message:
165
166
  rdoc_options: []
166
167
  require_paths:
@@ -169,7 +170,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
169
170
  requirements:
170
171
  - - ">="
171
172
  - !ruby/object:Gem::Version
172
- version: '2.5'
173
+ version: '2.6'
173
174
  - - "<"
174
175
  - !ruby/object:Gem::Version
175
176
  version: '4'
@@ -179,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
180
  - !ruby/object:Gem::Version
180
181
  version: 1.3.1
181
182
  requirements: []
182
- rubygems_version: 3.2.15
183
+ rubygems_version: 3.3.7
183
184
  signing_key:
184
185
  specification_version: 4
185
186
  summary: Lightweight filewatcher.