Parcourir la source

Merge branch 'master' into ruby-3.2

Akinori MUSHA il y a 1 an
Parent
commit
bf076b8241

+ 3 - 3
app/models/agents/dropbox_watch_agent.rb

@@ -53,9 +53,9 @@ module Agents
     private
 
     def ls(dir_to_watch)
-      dropbox.ls(dir_to_watch).map { |file|
-        { 'path' => file.path, 'rev' => file.rev, 'modified' => file.server_modified }
-      }
+      dropbox.ls(dir_to_watch)
+        .select { |entry| entry.respond_to?(:rev) }
+        .map { |file| { 'path' => file.path, 'rev' => file.rev, 'modified' => file.server_modified } }
     end
 
     def previous_contents

+ 3 - 1
app/models/agents/local_file_agent.rb

@@ -114,9 +114,11 @@ module Agents
 
       incoming_events.each do |event|
         mo = interpolated(event)
-        File.open(File.expand_path(mo['path']), boolify(mo['append']) ? 'a' : 'w') do |file|
+        expanded_path = File.expand_path(mo['path'])
+        File.open(expanded_path, boolify(mo['append']) ? 'a' : 'w') do |file|
           file.write(mo['data'])
         end
+        create_event payload: get_file_pointer(expanded_path)
       end
     end
 

+ 18 - 2
spec/models/agents/dropbox_watch_agent_spec.rb

@@ -50,7 +50,15 @@ describe Agents::DropboxWatchAgent do
 
   describe '#check' do
 
-    let(:first_result) { Dropbox::API::Object.convert([{ 'path_display' => '1.json', 'rev' => '1', 'server_modified' => '01-01-01' }], nil) }
+    let(:first_result) do
+      Dropbox::API::Object.convert(
+        [
+          { 'path_display' => '1.json', 'rev' => '1', 'server_modified' => '01-01-01' },
+          { 'path_display' => 'sub_dir_1', '.tag' => 'folder' }
+        ],
+        nil
+      )
+    end
 
     before(:each) do
       allow(Dropbox::API::Client).to receive(:new) do
@@ -77,7 +85,15 @@ describe Agents::DropboxWatchAgent do
 
     context 'subsequent calls' do
 
-      let(:second_result) { Dropbox::API::Object.convert([{ 'path_display' => '2.json', 'rev' => '1', 'server_modified' => '02-02-02' }], nil) }
+      let(:second_result) do
+        Dropbox::API::Object.convert(
+          [
+            { 'path_display' => '2.json', 'rev' => '1', 'server_modified' => '02-02-02' },
+            { 'path_display' => 'sub_dir_2', '.tag' => 'folder' }
+          ],
+          nil
+        )
+      end
 
       before(:each) do
         @agent.memory = { 'contents' => 'not_empty' }

+ 9 - 0
spec/models/agents/local_file_agent_spec.rb

@@ -187,6 +187,15 @@ describe Agents::LocalFileAgent do
       expect { @checker.receive([]) }.to change(AgentLog, :count).by(1)
       ENV['ENABLE_INSECURE_AGENTS'] = 'true'
     end
+
+    it "emits an event containing the file pointer" do
+      expect(@file_mock).to receive(:write).with('hello world')
+      event = Event.new(payload: {'data' => 'hello world'})
+      expect(File).to receive(:open).with(File.join(Rails.root, 'tmp', 'spec'), 'w').and_yield(@file_mock)
+
+      expect { @checker.receive([event]) }.to change(Event, :count).by(1)
+      expect(Event.last.payload.has_key?('file_pointer')).to be_truthy
+    end
   end
 
   describe describe Agents::LocalFileAgent::Worker do