From 498c548417f0049de5a020f9ec769aa49a7d9f5e Mon Sep 17 00:00:00 2001 From: Pedro Avalos <42501726+pedro-avalos@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:22:13 -0600 Subject: [PATCH] Ignore common file extensions in ignored files warning (BugFix) (#1583) * Add ignore list for ignored files warning * Add unit tests --- .../plainbox/impl/secure/providers/test_v1.py | 20 +++++++++ .../plainbox/impl/secure/providers/v1.py | 43 +++++++++---------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/checkbox-ng/plainbox/impl/secure/providers/test_v1.py b/checkbox-ng/plainbox/impl/secure/providers/test_v1.py index b6b23ba39..9478be3b1 100644 --- a/checkbox-ng/plainbox/impl/secure/providers/test_v1.py +++ b/checkbox-ng/plainbox/impl/secure/providers/test_v1.py @@ -35,6 +35,7 @@ from plainbox.impl.secure.providers.v1 import Provider1 from plainbox.impl.secure.providers.v1 import Provider1Definition from plainbox.impl.secure.providers.v1 import Provider1PlugIn +from plainbox.impl.secure.providers.v1 import ProviderContentLoader from plainbox.impl.secure.providers.v1 import UnitPlugIn from plainbox.impl.secure.providers.v1 import VersionValidator from plainbox.impl.secure.rfc822 import FileTextSource @@ -911,3 +912,22 @@ def test_init_bindtextdomain__not_called(self, mock_gettext): base_dir=self.BASE_DIR, ) self.assertEqual(mock_gettext.bindtextdomain.call_args_list, []) + + +@mock.patch("plainbox.impl.secure.providers.v1.logger") +class ProviderContentLoaderTests(TestCase): + def test__warn_ignored_file_warned(self, logger_mock): + self_mock = mock.MagicMock() + self_mock.provider.units_dir = "/some/path" + ProviderContentLoader._warn_ignored_file( + self_mock, "/some/path/script.py" + ) + self.assertTrue(logger_mock.warning.called) + + def test__warn_ignored_file_ignored(self, logger_mock): + self_mock = mock.MagicMock() + self_mock.provider.units_dir = "/some/path" + ProviderContentLoader._warn_ignored_file( + self_mock, "/some/path/__pycache__/script.pyc" + ) + self.assertFalse(logger_mock.warning.called) diff --git a/checkbox-ng/plainbox/impl/secure/providers/v1.py b/checkbox-ng/plainbox/impl/secure/providers/v1.py index cb5fe8446..5bb6d6a90 100644 --- a/checkbox-ng/plainbox/impl/secure/providers/v1.py +++ b/checkbox-ng/plainbox/impl/secure/providers/v1.py @@ -661,31 +661,28 @@ def load(self, plugin_kwargs): def _warn_ignored_file(self, filename): """ - Print an warning message for each file that is skipped at loading - Do not print warning for all skipped files, do it only for file that is located - inside a official provider folder (bin, data, units, ..). - """ - if ( - ( - self.provider.units_dir - and filename.startswith(self.provider.units_dir) - ) - or ( - self.provider.jobs_dir - and filename.startswith(self.provider.jobs_dir) + Print a warning message for each file that is skipped at loading. + Do not print warning for all skipped files. Ignore common text formats, + compiled bytcode python files, etc. Files to warn about should also be + located inside an official provider folder (bin, data, units, ...). + """ + if all( + not filename.endswith(ext) + for ext in ( + ".md", + ".txt", + ".pyc", ) - or ( - self.provider.data_dir - and filename.startswith(self.provider.data_dir) - ) - or ( - self.provider.bin_dir - and filename.startswith(self.provider.bin_dir) - ) - or ( - self.provider.locale_dir - and filename.startswith(self.provider.locale_dir) + ) and any( + filename.startswith(path) + for path in ( + self.provider.units_dir, + self.provider.jobs_dir, + self.provider.data_dir, + self.provider.bin_dir, + self.provider.locale_dir, ) + if path ): logger.warning("Skipped file: %s", filename)