Skip to content

Commit

Permalink
Fix filesystem sensor for directories (#21729)
Browse files Browse the repository at this point in the history
Fix walking through wildcarded directory in `FileSensor.poke` method

(cherry picked from commit 6b0ca64)
  • Loading branch information
mixilchenko authored and ephraimbuddy committed Mar 22, 2022
1 parent faab2bd commit 8c2ac24
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion airflow/sensors/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def poke(self, context):
self.log.info('Found File %s last modified: %s', str(path), str(mod_time))
return True

for _, _, files in os.walk(full_path):
for _, _, files in os.walk(path):
if len(files) > 0:
return True
return False
36 changes: 34 additions & 2 deletions tests/sensors/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# under the License.
#

import os.path
import os
import shutil
import tempfile
import unittest
Expand Down Expand Up @@ -131,6 +131,38 @@ def test_wildcard_file(self):
task._hook = self.hook
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)

def test_wildcard_empty_directory(self):
with tempfile.TemporaryDirectory() as temp_dir:
with tempfile.TemporaryDirectory(suffix='subdir', dir=temp_dir):
task = FileSensor(
task_id='test',
filepath=os.path.join(temp_dir, '*dir'),
fs_conn_id='fs_default',
dag=self.dag,
timeout=0,
)
task._hook = self.hook

# No files in dir
with pytest.raises(AirflowSensorTimeout):
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)

def test_wildcard_directory_with_files(self):
with tempfile.TemporaryDirectory() as temp_dir:
with tempfile.TemporaryDirectory(suffix='subdir', dir=temp_dir) as subdir:
task = FileSensor(
task_id='test',
filepath=os.path.join(temp_dir, '*dir'),
fs_conn_id='fs_default',
dag=self.dag,
timeout=0,
)
task._hook = self.hook

# `touch` the file in subdir
open(os.path.join(subdir, 'file'), 'a').close()
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)

def test_wildcared_directory(self):
temp_dir = tempfile.mkdtemp()
subdir = tempfile.mkdtemp(dir=temp_dir)
Expand All @@ -146,7 +178,7 @@ def test_wildcared_directory(self):
task._hook = self.hook

try:
# `touch` the dir
# `touch` the file in subdir
open(subdir + "/file", "a").close()
task.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
finally:
Expand Down

0 comments on commit 8c2ac24

Please sign in to comment.