Debian Codegraph @

tests/test_yaml_manager.py

raw
1 # --------------------------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for license information.
4 # --------------------------------------------------------------------------------------------
5
6 import os
7 import json
8 import logging
9 import unittest
10 from azure_functions_devops_build.yaml.yaml_manager import YamlManager
11 from azure_functions_devops_build.constants import (
12 PYTHON,
13 NODE,
14 DOTNET,
15 POWERSHELL,
16 LINUX_CONSUMPTION,
17 LINUX_DEDICATED,
18 WINDOWS
19 )
20 from ._helpers import id_generator
21
22 class TestYamlManager(unittest.TestCase):
23 @classmethod
24 def setUpClass(cls):
25 logging.disable(logging.CRITICAL)
26 TestYamlManager.backup_file("requirements.txt")
27 TestYamlManager.backup_file("package.json")
28
29 @classmethod
30 def tearDownClass(cls):
31 logging.disable(logging.NOTSET)
32 TestYamlManager.restore_file("requirements.txt")
33 TestYamlManager.restore_file("package.json")
34
35 def setUp(self):
36 self._current_directory = os.path.dirname(os.path.realpath(__file__))
37
38 def tearDown(self):
39 if os.path.isfile("azure-pipelines.yml"):
40 os.remove("azure-pipelines.yml")
41
42 if os.path.isfile("requirements.txt"):
43 os.remove("requirements.txt")
44
45 if os.path.isfile("package.json"):
46 os.remove("package.json")
47
48 if os.path.isfile("extensions.csproj"):
49 os.remove("extensions.csproj")
50
51 def test_create_yaml_python_linux(self):
52 yaml_manager = YamlManager(language=PYTHON, app_type=LINUX_CONSUMPTION)
53 yaml_manager.create_yaml()
54 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "python_linux.yml")
55 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
56 self.assertTrue(result)
57
58 def test_create_yaml_python_linux_pip(self):
59 # Create a temporary requirements.txt
60 open("requirements.txt", "a").close()
61 yaml_manager = YamlManager(language=PYTHON, app_type=LINUX_CONSUMPTION)
62 yaml_manager.create_yaml()
63
64 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "python_linux_pip.yml")
65 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
66 self.assertTrue(result)
67
68 def test_create_yaml_python_linux_pip_extensions(self):
69 # Create a temporary requirements.txt
70 # Create a temporary extensions.csproj
71 open("requirements.txt", "a").close()
72 with open("extensions.csproj", "w") as f:
73 f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')
74
75 yaml_manager = YamlManager(language=PYTHON, app_type=LINUX_CONSUMPTION)
76 yaml_manager.create_yaml()
77
78 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "python_linux_pip_extensions.yml")
79 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
80 self.assertTrue(result)
81
82 def test_create_yaml_dotnet_linux(self):
83 yaml_manager = YamlManager(language=DOTNET, app_type=LINUX_CONSUMPTION)
84 yaml_manager.create_yaml()
85 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "dotnet_linux.yml")
86 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
87 self.assertTrue(result)
88
89 def test_create_yaml_dotnet_windows(self):
90 yaml_manager = YamlManager(language=DOTNET, app_type=WINDOWS)
91 yaml_manager.create_yaml()
92 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "dotnet_windows.yml")
93 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
94 self.assertTrue(result)
95
96 def test_create_yaml_node_linux(self):
97 yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
98 yaml_manager.create_yaml()
99 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux.yml")
100 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
101 self.assertTrue(result)
102
103 def test_create_yaml_node_linux_install(self):
104 # Create a temporary requirements.txt
105 with open("package.json", "w") as f:
106 json.dump(obj={}, fp=f)
107 yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
108 yaml_manager.create_yaml()
109
110 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux_install.yml")
111 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
112 self.assertTrue(result)
113
114 def test_create_yaml_node_linux_install_build(self):
115 # Create a temporary requirements.txt and make a build script in it
116 with open("package.json", "w") as f:
117 json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
118 yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
119 yaml_manager.create_yaml()
120
121 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux_install_build.yml")
122 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
123 self.assertTrue(result)
124
125 def test_create_yaml_node_linux_install_build_extensions(self):
126 # Create a temporary requirements.txt and make a build script in it
127 # Create a temporary extensions.csproj
128 with open("package.json", "w") as f:
129 json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
130 with open("extensions.csproj", "w") as f:
131 f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')
132
133 yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
134 yaml_manager.create_yaml()
135
136 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux_install_build_extensions.yml")
137 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
138 self.assertTrue(result)
139
140 def test_create_yaml_node_windows(self):
141 yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
142 yaml_manager.create_yaml()
143 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows.yml")
144 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
145 self.assertTrue(result)
146
147 def test_create_yaml_node_windows_install(self):
148 # Create a temporary requirements.txt
149 with open("package.json", "w") as f:
150 json.dump(obj={}, fp=f)
151 yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
152 yaml_manager.create_yaml()
153
154 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows_install.yml")
155 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
156 self.assertTrue(result)
157
158 def test_create_yaml_node_windows_install_build(self):
159 # Create a temporary requirements.txt and make a build script in it
160 with open("package.json", "w") as f:
161 json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
162 yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
163 yaml_manager.create_yaml()
164
165 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows_install_build.yml")
166 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
167 self.assertTrue(result)
168
169 def test_create_yaml_node_windows_install_build_extensions(self):
170 # Create a temporary requirements.txt and make a build script in it
171 # Create a temporary extensions.csproj
172 with open("package.json", "w") as f:
173 json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
174 with open("extensions.csproj", "w") as f:
175 f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')
176
177 yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
178 yaml_manager.create_yaml()
179
180 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows_install_build_extensions.yml")
181 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
182 self.assertTrue(result)
183
184 def test_create_yaml_powershell_windows(self):
185 yaml_manager = YamlManager(language=POWERSHELL, app_type=WINDOWS)
186 yaml_manager.create_yaml()
187 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "powershell_windows.yml")
188 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
189 self.assertTrue(result)
190
191 def test_create_yaml_powershell_windows_extensions(self):
192 # Create a temporary extensions.csproj
193 with open("extensions.csproj", "w") as f:
194 f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')
195
196 yaml_manager = YamlManager(language=POWERSHELL, app_type=WINDOWS)
197 yaml_manager.create_yaml()
198 yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "powershell_windows_extensions.yml")
199 result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
200 self.assertTrue(result)
201
202 @staticmethod
203 def is_two_files_equal(filepath1, filepath2):
204 with open(filepath1, "r") as f1:
205 content1 = f1.read()
206 with open(filepath2, "r") as f2:
207 content2 = f2.read()
208
209 return content1 == content2
210
211 @staticmethod
212 def backup_file(filepath):
213 backup_filepath = "{}.bak".format(filepath)
214 if os.path.isfile(filepath):
215 if os.path.isfile(backup_filepath):
216 os.remove(backup_filepath)
217 os.rename(filepath, backup_filepath)
218
219 @staticmethod
220 def restore_file(filepath):
221 backup_filepath = "{}.bak".format(filepath)
222 if os.path.isfile(backup_filepath):
223 if os.path.isfile(filepath):
224 os.remove(filepath)
225 os.rename(backup_filepath, filepath)
226
227 if __name__ == '__main__':
228 unittest.main()