ScriptUtilsTest.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. ***************************************************************************
  3. ScriptUtilsTest
  4. ---------------------
  5. Date : February 2019
  6. Copyright : (C) 2019 by Luigi Pirelli
  7. Email : luipir at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Luigi Pirelli'
  18. __date__ = 'February 2019'
  19. __copyright__ = '(C) 2019, Luigi Pirelli'
  20. import os
  21. import shutil
  22. import tempfile
  23. from qgis.core import NULL, QgsApplication
  24. import unittest
  25. from qgis.testing import start_app, QgisTestCase
  26. from processing.script import ScriptUtils
  27. testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')
  28. start_app()
  29. class ScriptUtilsTest(QgisTestCase):
  30. @classmethod
  31. def setUpClass(cls):
  32. cls.cleanup_paths = []
  33. @classmethod
  34. def tearDownClass(cls):
  35. for path in cls.cleanup_paths:
  36. shutil.rmtree(path)
  37. def testResetScriptFolder(self):
  38. # if folder exist
  39. defaultScriptFolder = ScriptUtils.defaultScriptsFolder()
  40. folder = ScriptUtils.resetScriptFolder(defaultScriptFolder)
  41. self.assertEqual(folder, defaultScriptFolder)
  42. folder = ScriptUtils.resetScriptFolder('.')
  43. self.assertEqual(folder, '.')
  44. # if folder does not exist and not absolute
  45. folder = ScriptUtils.resetScriptFolder('fake')
  46. self.assertEqual(folder, None)
  47. # if absolute but not relative to QgsApplication.qgisSettingsDirPath()
  48. folder = os.path.join(tempfile.gettempdir(), 'fakePath')
  49. newFolder = ScriptUtils.resetScriptFolder(folder)
  50. self.assertEqual(newFolder, folder)
  51. # if absolute profile but poiting somewhere
  52. # reset the path as pointing to profile into the current settings
  53. folder = QgsApplication.qgisSettingsDirPath()
  54. # modify default profile changing absolute path pointing somewhere
  55. paths = folder.split(os.sep)
  56. paths[0] = '/'
  57. paths[1] = 'fakelocation'
  58. folder = os.path.join(*paths)
  59. folder = ScriptUtils.resetScriptFolder(folder)
  60. self.assertEqual(folder, QgsApplication.qgisSettingsDirPath())
  61. if __name__ == '__main__':
  62. unittest.main()