filewatcher 2.0.0.beta4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e9ecdeee0c20df3055e1078a67e8194040361fbcea9f1c633f2b33a8177f250
4
- data.tar.gz: ba5abe65b765b0cc4c4630aae0cf1e0751c7ea4dec5f39911a9de9510fff7e9e
3
+ metadata.gz: 59a457d937b483f6b936e5741df3ac777ef58fe43983c36de2fc00f9475e3e32
4
+ data.tar.gz: aca66c67a07d0d596afa06af04984a42f2d2fe83a6353bf84f11b34199e8379a
5
5
  SHA512:
6
- metadata.gz: 287d42ea2fb239be62a4a0701f5b84440c409b6dc93c3b8caf5a5c8c87958ab12f4885e146e679f988dc2d2ef9c7d47d77be79ce4b8dd6d0ea97d71d63de9116
7
- data.tar.gz: 2a296164258b091771802bbeb7d33690160e235220d30644743abe441d7801259dcad5bcd4216655c1e57549b9052edb45f1b1440a23ba1d84e4981f278a4e90
6
+ metadata.gz: 384400d7f817cd4d403b977a9f7a52eca5b331a217829c013c42227d468e673f462b8072fae057ca60bf0048a9505994278aab0449f41620172ea97f443ef141
7
+ data.tar.gz: 17f903b43e2fd857d2b6b2f48bb81a51ebc175bb631089fda561ed082c29dda38e945ef9c6bbd21825310de9690f63021fc4c0bdce1aff132ce97d5323124871
@@ -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)
@@ -1,32 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
4
-
5
3
  class Filewatcher
6
4
  module SpecHelper
7
- ## Base class for Filewatcher runners in specs
8
- class WatchRun
9
- extend Forwardable
5
+ ## Base module for Filewatcher runners in specs
6
+ module WatchRun
7
+ include Filewatcher::SpecHelper
10
8
 
11
9
  TMP_DIR = "#{Dir.getwd}/spec/tmp"
12
10
 
13
- def_delegators Filewatcher::SpecHelper, :debug, :wait, :system_stat
14
-
15
11
  attr_reader :filename
16
12
 
17
- def initialize(filename:, action:, directory:)
18
- @filename =
19
- if filename.match? %r{^(/|~|[A-Z]:)} then filename
20
- else File.join(TMP_DIR, filename)
21
- end
22
- @directory = directory
13
+ def initialize(filename:, action:)
14
+ @filename = filename.match?(%r{^(/|~|[A-Z]:)}) ? filename : File.join(TMP_DIR, filename)
23
15
  @action = action
24
16
  debug "action = #{action}"
25
17
  end
26
18
 
27
19
  def start
28
20
  debug 'start'
29
- File.write(@filename, 'content1') unless @action == :create
21
+ File.write(@filename, 'content1') unless %i[create create_dir].include? @action
30
22
 
31
23
  wait seconds: 1
32
24
  end
@@ -50,19 +42,26 @@ class Filewatcher
50
42
 
51
43
  private
52
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
+
53
59
  def make_changes
54
60
  debug "make changes, @action = #{@action}, @filename = #{@filename}"
55
61
 
56
- if @action == :delete
57
- FileUtils.remove(@filename)
58
- elsif @directory
59
- FileUtils.mkdir_p(@filename)
60
- else
61
- ## There is no `File.write` because of strange difference in parallel `File.mtime`
62
- ## https://cirrus-ci.com/task/6107605053472768?command=test#L497-L511
63
- system "echo 'content2' > #{@filename}"
64
- debug_file_mtime
65
- end
62
+ action = self.class::ACTIONS.fetch(@action) { raise "Unknown action `#{@action}`" }
63
+
64
+ instance_exec(&action)
66
65
 
67
66
  wait seconds: 1
68
67
  end
@@ -9,22 +9,23 @@ 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
- module_function
17
+ ENVIRONMENT_SPECS_COEFFICIENTS = {
18
+ -> { ENV.fetch('CI', false) } => 1,
19
+ -> { RUBY_PLATFORM == 'java' } => 1,
20
+ -> { Gem::Platform.local.os == 'darwin' } => 1
21
+ }.freeze
17
22
 
18
23
  def logger
19
24
  @logger ||= Logger.new($stdout, level: :debug)
20
25
  end
21
26
 
22
27
  def environment_specs_coefficients
23
- @environment_specs_coefficients ||= {
24
- -> { ENV['CI'] } => 1,
25
- -> { RUBY_PLATFORM == 'java' } => 1,
26
- -> { Gem::Platform.local.os == 'darwin' } => 1
27
- }
28
+ ENVIRONMENT_SPECS_COEFFICIENTS
28
29
  end
29
30
 
30
31
  def wait(seconds: 1, interval: 1, &block)
@@ -62,7 +63,7 @@ class Filewatcher
62
63
 
63
64
  def system_stat(filename)
64
65
  case (host_os = RbConfig::CONFIG['host_os'])
65
- when 'linux'
66
+ when /linux(-gnu)?/
66
67
  `stat --printf 'Modification: %y, Change: %z\n' #{filename}`
67
68
  when /darwin\d*/
68
69
  `stat #{filename}`
@@ -80,5 +81,10 @@ class Filewatcher
80
81
  # debug command
81
82
  `#{command}`
82
83
  end
84
+
85
+ ## https://github.com/rubocop/ruby-style-guide/issues/556#issuecomment-828672008
86
+ # rubocop:disable Style/ModuleFunction
87
+ extend self
88
+ # rubocop:enable Style/ModuleFunction
83
89
  end
84
90
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Filewatcher
4
- VERSION = '2.0.0.beta4'
4
+ VERSION = '2.0.0'
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.beta4
4
+ version: 2.0.0
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-28 00:00:00.000000000 Z
12
+ date: 2022-09-08 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.36.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.36.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,17 +170,17 @@ 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'
176
177
  required_rubygems_version: !ruby/object:Gem::Requirement
177
178
  requirements:
178
- - - ">"
179
+ - - ">="
179
180
  - !ruby/object:Gem::Version
180
- version: 1.3.1
181
+ version: '0'
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.