Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.4KB

  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. PyNSMClient - A New Session Manager Client-Library in one file.
  5. The Non-Session-Manager by Jonathan Moore Liles <male@tuxfamily.org>: http://non.tuxfamily.org/nsm/
  6. New Session Manager by Nils Hilbricht et al https://new-session-manager.jackaudio.org
  7. With help from code fragments from https://github.com/attwad/python-osc ( DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE v2 )
  8. MIT License
  9. Copyright (c) since 2014: Laborejo Software Suite <info@laborejo.org>, All rights reserved.
  10. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  11. associated documentation files (the "Software"), to deal in the Software without restriction,
  12. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  13. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in all copies or
  16. substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  18. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  20. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  21. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. """
  23. #Test file for the resource import function in nsmclient.py
  24. if __name__ == "__main__":
  25. from nsmclient import NSMClient
  26. #We do not start nsmclient, just the the function with temporary files
  27. importResource = NSMClient.importResource
  28. import os, os.path
  29. import logging
  30. logging.basicConfig(level=logging.INFO)
  31. from inspect import currentframe
  32. def get_linenumber():
  33. cf = currentframe()
  34. return cf.f_back.f_lineno
  35. from tempfile import mkdtemp
  36. class self(object):
  37. ourPath = mkdtemp()
  38. ourClientNameUnderNSM = "Loader Test"
  39. assert os.path.isdir(self.ourPath)
  40. #First a meta test to see if our system is working:
  41. assert os.path.exists("/etc/hostname")
  42. try:
  43. result = importResource(self, "/etc/hostname") #should not fail!
  44. except FileNotFoundError:
  45. pass
  46. else:
  47. print (f"Meta Test System works as of line {get_linenumber()}")
  48. print ("""You should not see any "Test Error" messages""")
  49. print ("Working in", self.ourPath)
  50. print (f"Removing {result} for a clean test environment")
  51. os.remove(result)
  52. print()
  53. #Real tests
  54. try:
  55. importResource(self, "/floot/nonexistent") #should fail
  56. except FileNotFoundError:
  57. pass
  58. else:
  59. print (f"Test Error in line {get_linenumber()}")
  60. try:
  61. importResource(self, "////floot//nonexistent/") #should fail
  62. except FileNotFoundError:
  63. pass
  64. else:
  65. print (f"Test Error in line {get_linenumber()}")
  66. try:
  67. importResource(self, "/etc/shadow") #reading not possible
  68. except PermissionError:
  69. pass
  70. else:
  71. print (f"Test Error in line {get_linenumber()}")
  72. assert os.path.exists("/etc/hostname")
  73. try:
  74. org = self.ourPath
  75. self.ourPath = "/" #writing not possible
  76. importResource(self, "/etc/hostname")
  77. except PermissionError:
  78. self.ourPath = org
  79. else:
  80. print (f"Test Error in line {get_linenumber()}")
  81. from tempfile import NamedTemporaryFile
  82. tmpf = NamedTemporaryFile()
  83. assert os.path.exists("/etc/hostname")
  84. try:
  85. org = self.ourPath
  86. self.ourPath = tmpf.name #writable, but not a dir
  87. importResource(self, "/etc/hostname")
  88. except NotADirectoryError:
  89. self.ourPath = org
  90. else:
  91. print (f"Test Error in line {get_linenumber()}")
  92. #Test the real purpose
  93. result = importResource(self, "/etc/hostname")
  94. print ("imported to", result)
  95. #Test what happens if we try to import already imported resource again
  96. result = importResource(self, result)
  97. print ("imported to", result)
  98. #Test what happens if we try to import a resource that would result in a name collision
  99. result = importResource(self, "/etc/hostname")
  100. print ("imported to", result)
  101. #Count the number of resulting files.
  102. assert len(os.listdir(self.ourPath)) == 2