test_utils.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import errno
  17. import os
  18. def create_dir(location, dir_name):
  19. try:
  20. dir_path = os.path.join(location, dir_name)
  21. os.mkdir(dir_path)
  22. except OSError as e:
  23. if e.errno != errno.EEXIST:
  24. raise
  25. def create_file(location, file_name, content, subdir=""):
  26. directory = os.path.join(location, subdir)
  27. try:
  28. os.makedirs(directory)
  29. except OSError as e:
  30. if e.errno != errno.EEXIST:
  31. raise
  32. resource = os.path.join(directory, file_name)
  33. with open(resource, "w", encoding="utf-8") as f:
  34. f.write(content)
  35. expected_response = {
  36. "env_vars": {
  37. "VAR1": "newvalue",
  38. "VAR2": None,
  39. "VAR3": None,
  40. "VAR4": None,
  41. "VAR5": "localhost",
  42. "VAR6": "6",
  43. "VAR7": "value7",
  44. "VAR8": None,
  45. },
  46. "inputs": [],
  47. "outputs": [],
  48. }
  49. expected_response_empty = {"env_vars": {}, "inputs": [], "outputs": []}
  50. text_content = "This is a text file."
  51. notebook_content = {
  52. "cells": [
  53. {
  54. "cell_type": "markdown",
  55. "id": "advanced-touch",
  56. "metadata": {},
  57. "source": [
  58. "# Python Notebook with Environment Variables\n",
  59. "\n",
  60. "This python Notebook contains various environment variables to test the parser functionality.",
  61. ],
  62. },
  63. {
  64. "cell_type": "code",
  65. "execution_count": 0,
  66. "id": "regional-indie",
  67. "metadata": {},
  68. "outputs": [],
  69. "source": [
  70. "import os\n",
  71. "\n",
  72. 'os.getenv("VAR1")\n',
  73. 'os.environ["VAR2"]\n',
  74. 'os.environ.get("VAR3")\n',
  75. "\n",
  76. "print(os.environ['VAR4'])\n",
  77. "print(os.getenv(\"VAR5\", 'localhost'))",
  78. ],
  79. },
  80. {
  81. "cell_type": "code",
  82. "execution_count": 1,
  83. "id": "completed-timothy",
  84. "metadata": {},
  85. "outputs": [],
  86. "source": [
  87. "os.environ['VAR6'] = \"6\"\n",
  88. "print(os.environ.get('VAR7', 'value7'))\n",
  89. "os.getenv('VAR8')\n",
  90. "\n",
  91. 'os.environ["VAR1"] = "newvalue"',
  92. ],
  93. },
  94. ],
  95. "metadata": {
  96. "kernelspec": {
  97. "display_name": "Python 3",
  98. "language": "python",
  99. "name": "python3",
  100. },
  101. "language_info": {
  102. "codemirror_mode": {"name": "ipython", "version": 3},
  103. "file_extension": ".py",
  104. "mimetype": "text/x-python",
  105. "name": "python",
  106. "nbconvert_exporter": "python",
  107. "pygments_lexer": "ipython3",
  108. "version": "3.9.1",
  109. },
  110. },
  111. "nbformat": 4,
  112. "nbformat_minor": 5,
  113. }
  114. python_content = """
  115. import os
  116. os.getenv("VAR1")
  117. os.environ["VAR2"]
  118. os.environ.get("VAR3")
  119. print(os.environ['VAR4'])
  120. print(os.getenv("VAR5", 'localhost'))
  121. os.environ['VAR6'] = "6"
  122. print(os.environ.get('VAR7', 'value7'))
  123. os.getenv('VAR8')
  124. os.environ["VAR1"] = "newvalue"
  125. """
  126. r_content = """
  127. Sys.setenv(VAR1 = "newvalue")
  128. Sys.getenv(VAR2)
  129. Sys.getenv("VAR3")
  130. Sys.getenv('VAR4')
  131. Sys.setenv('VAR5' = 'localhost')
  132. Sys.setenv("VAR6" = 6)
  133. Sys.setenv(VAR7 = "value7")
  134. Sys.getenv('VAR8')
  135. """
  136. empty_notebook_content = {
  137. "cells": [
  138. {
  139. "cell_type": "markdown",
  140. "id": "literary-parts",
  141. "metadata": {},
  142. "source": [
  143. "# Python Notebook with No Environment Variables\n",
  144. "\n",
  145. "This python Notebook contains no environment variables to test the parser functionality.",
  146. ],
  147. },
  148. {
  149. "cell_type": "code",
  150. "execution_count": 0,
  151. "id": "dental-manchester",
  152. "metadata": {},
  153. "outputs": [],
  154. "source": ["import os\n", "\n", "print(os.cwd())"],
  155. },
  156. ],
  157. "metadata": {
  158. "kernelspec": {
  159. "display_name": "Python 3",
  160. "language": "python",
  161. "name": "python3",
  162. },
  163. "language_info": {
  164. "codemirror_mode": {"name": "ipython", "version": 3},
  165. "file_extension": ".py",
  166. "mimetype": "text/x-python",
  167. "name": "python",
  168. "nbconvert_exporter": "python",
  169. "pygments_lexer": "ipython3",
  170. "version": "3.9.1",
  171. },
  172. },
  173. "nbformat": 4,
  174. "nbformat_minor": 5,
  175. }