File: test_main.py

package info (click to toggle)
pontos 26.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,808 kB
  • sloc: python: 44,771; makefile: 21; sh: 10; xml: 3
file content (312 lines) | stat: -rw-r--r-- 10,737 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# Copyright (C) 2023 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

import unittest
from contextlib import redirect_stderr, redirect_stdout
from datetime import datetime
from io import StringIO

from pontos.testing import temp_directory
from pontos.version._main import VersionExitCode, main


class MainTestCase(unittest.TestCase):
    def test_no_command(self):
        with self.assertRaises(SystemExit), redirect_stderr(StringIO()):
            main([])

    def test_no_project(self):
        with (
            temp_directory(change_into=True),
            self.assertRaises(SystemExit) as cm,
            redirect_stderr(StringIO()),
        ):
            main(["show"])

        self.assertEqual(cm.exception.code, VersionExitCode.NO_PROJECT)

    def test_update_success(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["update", "2.0.0"])

        self.assertEqual(
            out.getvalue(), "Updated version from 1.2.3 to 2.0.0.\n"
        )

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)

    def test_update_failure(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stderr(StringIO()),
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                "{}",
                encoding="utf8",
            )
            main(["update", "2.0.0"])

        self.assertEqual(cm.exception.code, VersionExitCode.UPDATE_ERROR)

    def test_update_already_up_to_date(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["update", "1.2.3"])

        self.assertEqual(out.getvalue(), "Version is already up-to-date.\n")

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)

    def test_show(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["show"])

        self.assertEqual(out.getvalue(), "1.2.3\n")

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)

    def test_show_error(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stderr(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "abc"}',
                encoding="utf8",
            )
            main(["show"])

        self.assertEqual(out.getvalue(), "Invalid version: 'abc'\n")

        self.assertEqual(
            cm.exception.code, VersionExitCode.CURRENT_VERSION_ERROR
        )

    def test_verify_failure(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stderr(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["verify", "1.2.4"])

        self.assertEqual(
            out.getvalue(),
            "Provided version 1.2.4 does not match the current version "
            f"1.2.3 in {version_file.resolve()}.\n",
        )

        self.assertEqual(cm.exception.code, VersionExitCode.VERIFY_ERROR)

    def test_verify_success(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["verify", "1.2.3"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)

    def test_verify_current(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["verify", "current"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)

    def test_next_invalid_current(self):
        with (
            redirect_stderr(StringIO()),
            temp_directory(change_into=True) as temp_dir,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.tt"}',
                encoding="utf8",
            )
            main(["next", "dev", "--versioning-scheme", "pep440"])

        self.assertEqual(
            cm.exception.code, VersionExitCode.CURRENT_VERSION_ERROR
        )

    def test_next_dev(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "dev", "--versioning-scheme", "pep440"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "1.2.4.dev1\n")

    def test_next_patch(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "patch", "--versioning-scheme", "pep440"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "1.2.4\n")

    def test_next_minor(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "minor", "--versioning-scheme", "pep440"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "1.3.0\n")

    def test_next_major(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "major", "--versioning-scheme", "pep440"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "2.0.0\n")

    def test_next_alpha(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "alpha", "--versioning-scheme", "semver"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "1.2.4-alpha1\n")

    def test_next_beta(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "beta", "--versioning-scheme", "semver"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "1.2.4-beta1\n")

    def test_next_rc(self):
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "rc", "--versioning-scheme", "semver"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(out.getvalue(), "1.2.4-rc1\n")

    def test_next_calendar(self):
        today = datetime.today()
        with (
            temp_directory(change_into=True) as temp_dir,
            redirect_stdout(StringIO()) as out,
            self.assertRaises(SystemExit) as cm,
        ):
            version_file = temp_dir / "package.json"
            version_file.write_text(
                '{"name": "foo", "version": "1.2.3"}',
                encoding="utf8",
            )
            main(["next", "calendar", "--versioning-scheme", "pep440"])

        self.assertEqual(cm.exception.code, VersionExitCode.SUCCESS)
        self.assertEqual(
            out.getvalue(), f"{today.year % 100}.{today.month}.0\n"
        )