File: test_yaml_manager.py

package info (click to toggle)
azure-functions-devops-build 0.0.22-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556 kB
  • sloc: python: 2,422; sh: 12; makefile: 8
file content (228 lines) | stat: -rw-r--r-- 10,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import json
import logging
import unittest
from azure_functions_devops_build.yaml.yaml_manager import YamlManager
from azure_functions_devops_build.constants import (
    PYTHON,
    NODE,
    DOTNET,
    POWERSHELL,
    LINUX_CONSUMPTION,
    LINUX_DEDICATED,
    WINDOWS
)
from ._helpers import id_generator

class TestYamlManager(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        logging.disable(logging.CRITICAL)
        TestYamlManager.backup_file("requirements.txt")
        TestYamlManager.backup_file("package.json")

    @classmethod
    def tearDownClass(cls):
        logging.disable(logging.NOTSET)
        TestYamlManager.restore_file("requirements.txt")
        TestYamlManager.restore_file("package.json")

    def setUp(self):
        self._current_directory = os.path.dirname(os.path.realpath(__file__))

    def tearDown(self):
        if os.path.isfile("azure-pipelines.yml"):
            os.remove("azure-pipelines.yml")

        if os.path.isfile("requirements.txt"):
            os.remove("requirements.txt")

        if os.path.isfile("package.json"):
            os.remove("package.json")

        if os.path.isfile("extensions.csproj"):
            os.remove("extensions.csproj")

    def test_create_yaml_python_linux(self):
        yaml_manager = YamlManager(language=PYTHON, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "python_linux.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_python_linux_pip(self):
        # Create a temporary requirements.txt
        open("requirements.txt", "a").close()
        yaml_manager = YamlManager(language=PYTHON, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "python_linux_pip.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_python_linux_pip_extensions(self):
        # Create a temporary requirements.txt
        # Create a temporary extensions.csproj
        open("requirements.txt", "a").close()
        with open("extensions.csproj", "w") as f:
            f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')

        yaml_manager = YamlManager(language=PYTHON, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "python_linux_pip_extensions.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_dotnet_linux(self):
        yaml_manager = YamlManager(language=DOTNET, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "dotnet_linux.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_dotnet_windows(self):
        yaml_manager = YamlManager(language=DOTNET, app_type=WINDOWS)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "dotnet_windows.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_linux(self):
        yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_linux_install(self):
        # Create a temporary requirements.txt
        with open("package.json", "w") as f:
            json.dump(obj={}, fp=f)
        yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux_install.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_linux_install_build(self):
        # Create a temporary requirements.txt and make a build script in it
        with open("package.json", "w") as f:
            json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
        yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux_install_build.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_linux_install_build_extensions(self):
        # Create a temporary requirements.txt and make a build script in it
        # Create a temporary extensions.csproj
        with open("package.json", "w") as f:
            json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
        with open("extensions.csproj", "w") as f:
            f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')

        yaml_manager = YamlManager(language=NODE, app_type=LINUX_CONSUMPTION)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_linux_install_build_extensions.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_windows(self):
        yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_windows_install(self):
        # Create a temporary requirements.txt
        with open("package.json", "w") as f:
            json.dump(obj={}, fp=f)
        yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows_install.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_windows_install_build(self):
        # Create a temporary requirements.txt and make a build script in it
        with open("package.json", "w") as f:
            json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
        yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows_install_build.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_node_windows_install_build_extensions(self):
        # Create a temporary requirements.txt and make a build script in it
        # Create a temporary extensions.csproj
        with open("package.json", "w") as f:
            json.dump(obj={"scripts": {"build": "echo test"}}, fp=f)
        with open("extensions.csproj", "w") as f:
            f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')

        yaml_manager = YamlManager(language=NODE, app_type=WINDOWS)
        yaml_manager.create_yaml()

        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "node_windows_install_build_extensions.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_powershell_windows(self):
        yaml_manager = YamlManager(language=POWERSHELL, app_type=WINDOWS)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "powershell_windows.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    def test_create_yaml_powershell_windows_extensions(self):
        # Create a temporary extensions.csproj
        with open("extensions.csproj", "w") as f:
            f.write('<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"></Project>')

        yaml_manager = YamlManager(language=POWERSHELL, app_type=WINDOWS)
        yaml_manager.create_yaml()
        yaml_path = os.path.join(self._current_directory, "pipeline_scripts", "powershell_windows_extensions.yml")
        result = TestYamlManager.is_two_files_equal("azure-pipelines.yml", yaml_path)
        self.assertTrue(result)

    @staticmethod
    def is_two_files_equal(filepath1, filepath2):
        with open(filepath1, "r") as f1:
            content1 = f1.read()
        with open(filepath2, "r") as f2:
            content2 = f2.read()

        return content1 == content2

    @staticmethod
    def backup_file(filepath):
        backup_filepath = "{}.bak".format(filepath)
        if os.path.isfile(filepath):
            if os.path.isfile(backup_filepath):
                os.remove(backup_filepath)
            os.rename(filepath, backup_filepath)

    @staticmethod
    def restore_file(filepath):
        backup_filepath = "{}.bak".format(filepath)
        if os.path.isfile(backup_filepath):
            if os.path.isfile(filepath):
                os.remove(filepath)
            os.rename(backup_filepath, filepath)

if __name__ == '__main__':
    unittest.main()