plug-ins, tests: fix #11408 test-file-plug-ins: permission error ...

for gimp-tests.log

On Linux we may not have permission to create a log at our default
location, so we need to check for that.

First we should not init the log when our plug-in is asked for info.
Only create it when we actually run the import or export tests.

Second, try to catch the permission error and write a message either
in GIMP or in the terminal for non-interactive.
This commit is contained in:
Jacob Boerema 2024-04-27 12:49:10 -04:00
parent b5c6b48bee
commit 97671dbab7
2 changed files with 44 additions and 26 deletions

View File

@ -36,6 +36,7 @@ class GimpLogger (object):
self.verbose = verbose
self.debugging = debugging
self.enabled = True
if debugging:
log_level = logging.DEBUG
@ -46,15 +47,25 @@ class GimpLogger (object):
else:
log_filemode = 'w'
logging.basicConfig(
filename=logfile,
filemode=log_filemode,
encoding='utf-8',
level=log_level,
format='%(asctime)s | %(name)s | %(levelname)s | %(message)s')
try:
logging.basicConfig(
filename=logfile,
filemode=log_filemode,
encoding='utf-8',
level=log_level,
format='%(asctime)s | %(name)s | %(levelname)s | %(message)s')
logging.debug("Starting logger...")
logging.debug("Log file: %s", os.path.abspath(logfile))
logging.debug("Starting logger...")
logging.debug("Log file: %s", os.path.abspath(logfile))
except PermissionError:
self.enabled = False
msg = ("We do not have permission to create a log file at " +
os.path.abspath(logfile) + ". " +
"Please use env var GIMP_TESTS_LOG_FILE to set up a location.")
if interactive:
Gimp.message(msg)
else:
print(msg)
def set_interactive(self, interactive):
if self.interactive != interactive:

View File

@ -45,13 +45,13 @@ PRINT_VERBOSE = False
LOG_APPEND = False
test_cfg = GimpConfig()
log = GimpLogger(False, test_cfg.log_file, LOG_APPEND, PRINT_VERBOSE, DEBUGGING)
class PythonTest (Gimp.PlugIn):
def __init__(self):
Gimp.PlugIn.__init__(self)
self.test_cfg = None
self.log = None
## GimpPlugIn virtual methods ##
def do_set_i18n(self, _name):
# We don't support internationalization here...
@ -93,31 +93,38 @@ class PythonTest (Gimp.PlugIn):
YEARS) #year
return procedure
def init_logging(self):
self.test_cfg = GimpConfig()
self.log = GimpLogger(True, self.test_cfg.log_file, LOG_APPEND, PRINT_VERBOSE, DEBUGGING)
def run_import_tests(self, procedure, _run_mode, _image,
_n_drawables, _drawable, _config, _data):
log.set_interactive(True)
self.init_logging()
runner = GimpTestRunner(log, "import", test_cfg)
runner.run_tests()
if self.log.enabled:
runner = GimpTestRunner(self.log, "import", self.test_cfg)
runner.run_tests()
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())
def run_export_tests(self, procedure, _run_mode, _image,
_n_drawables, _drawable, _config, _data):
log.set_interactive(True)
self.init_logging()
runner = GimpExportTestRunner(log, "export", test_cfg)
if not runner:
log.error("Failed to create export test runner!")
else:
runner.load_test_configs()
if self.log.enabled:
runner = GimpExportTestRunner(self.log, "export", self.test_cfg)
if not runner:
self.log.error("Failed to create export test runner!")
else:
runner.load_test_configs()
bmp_tests = BmpExportTests("bmp", log)
runner.add_test(bmp_tests)
bmp_tests = BmpExportTests("bmp", self.log)
runner.add_test(bmp_tests)
# Add additional tests here
# Add additional tests here
runner.run_tests()
runner.run_tests()
return procedure.new_return_values(Gimp.PDBStatusType.SUCCESS, GLib.Error())