|
32 | 32 | _find_copyright as find_copyright, |
33 | 33 | _add_header as add_header, |
34 | 34 | _update_file as update_file, |
| 35 | + _parse_args as parse_args, |
| 36 | + main, |
35 | 37 | ) |
36 | 38 |
|
37 | 39 |
|
@@ -349,3 +351,114 @@ def test_update_header_in_file(self, mock_stdout): |
349 | 351 | ) |
350 | 352 |
|
351 | 353 | test_file.unlink() |
| 354 | + |
| 355 | + @patch('sys.stdout', new_callable=StringIO) |
| 356 | + def test_update_header_ok_in_file(self, mock_stdout): |
| 357 | + self.args.year = '2021' |
| 358 | + self.args.changed = False |
| 359 | + self.args.licence = 'AGPL-3.0-or-later' |
| 360 | + |
| 361 | + header = """# -*- coding: utf-8 -*- |
| 362 | +# Copyright (C) 2021 Greenbone Networks GmbH |
| 363 | +# |
| 364 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 365 | +# |
| 366 | +# This program is free software: you can redistribute it and/or modify |
| 367 | +# it under the terms of the GNU Affero General Public License as |
| 368 | +# published by the Free Software Foundation, either version 3 of the |
| 369 | +# License, or (at your option) any later version. |
| 370 | +# |
| 371 | +# This program is distributed in the hope that it will be useful, |
| 372 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 373 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 374 | +# GNU Affero General Public License for more details. |
| 375 | +# |
| 376 | +# You should have received a copy of the GNU Affero General Public License |
| 377 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 378 | +""" |
| 379 | + |
| 380 | + test_file = self.path / "test.py" |
| 381 | + if test_file.exists(): |
| 382 | + test_file.unlink() |
| 383 | + |
| 384 | + test_file.write_text(header) |
| 385 | + |
| 386 | + code = update_file(file=test_file, regex=self.regex, args=self.args) |
| 387 | + |
| 388 | + self.assertEqual(code, 0) |
| 389 | + ret = mock_stdout.getvalue() |
| 390 | + self.assertEqual( |
| 391 | + ret, |
| 392 | + f"{test_file}: Licence Header is ok.\n", |
| 393 | + ) |
| 394 | + self.assertIn( |
| 395 | + '# Copyright (C) 2021 Greenbone Networks GmbH', |
| 396 | + test_file.read_text(), |
| 397 | + ) |
| 398 | + |
| 399 | + test_file.unlink() |
| 400 | + |
| 401 | + def test_argparser_files(self): |
| 402 | + self.args.year = '2021' |
| 403 | + self.args.changed = False |
| 404 | + self.args.licence = 'AGPL-3.0-or-later' |
| 405 | + |
| 406 | + args = ['-f', 'test.py', '-y', '2021', '-l', 'AGPL-3.0-or-later'] |
| 407 | + |
| 408 | + args = parse_args(args) |
| 409 | + self.assertIsNotNone(args) |
| 410 | + self.assertEqual(args.company, self.args.company) |
| 411 | + self.assertEqual(args.files, ['test.py']) |
| 412 | + self.assertEqual(args.year, self.args.year) |
| 413 | + self.assertEqual(args.licence, self.args.licence) |
| 414 | + |
| 415 | + def test_argparser_dir(self): |
| 416 | + self.args.year = '2020' |
| 417 | + self.args.changed = False |
| 418 | + self.args.licence = 'AGPL-3.0-or-later' |
| 419 | + |
| 420 | + args = ['-d', '.', '-c', '-l', 'AGPL-3.0-or-later'] |
| 421 | + |
| 422 | + args = parse_args(args) |
| 423 | + self.assertIsNotNone(args) |
| 424 | + self.assertEqual(args.company, self.args.company) |
| 425 | + self.assertEqual(args.directory, '.') |
| 426 | + self.assertTrue(args.changed) |
| 427 | + self.assertEqual(args.year, '2021') |
| 428 | + self.assertEqual(args.licence, self.args.licence) |
| 429 | + |
| 430 | + @patch('pontos.updateheader.updateheader._parse_args') |
| 431 | + def test_main(self, argparser_mock): |
| 432 | + self.args.year = '2021' |
| 433 | + self.args.changed = False |
| 434 | + self.args.licence = 'AGPL-3.0-or-later' |
| 435 | + self.args.files = ['test.py'] |
| 436 | + self.args.directory = None |
| 437 | + |
| 438 | + argparser_mock.return_value = self.args |
| 439 | + |
| 440 | + code = main() |
| 441 | + |
| 442 | + # I have no idea how or why test main ... |
| 443 | + self.assertEqual(code, 0) |
| 444 | + |
| 445 | + @patch('sys.stdout', new_callable=StringIO) |
| 446 | + @patch('pontos.updateheader.updateheader._parse_args') |
| 447 | + def test_main_never_happen(self, argparser_mock, mock_stdout): |
| 448 | + self.args.year = '2021' |
| 449 | + self.args.changed = False |
| 450 | + self.args.licence = 'AGPL-3.0-or-later' |
| 451 | + self.args.files = None |
| 452 | + self.args.directory = None |
| 453 | + |
| 454 | + argparser_mock.return_value = self.args |
| 455 | + |
| 456 | + # I have no idea how or why test main ... |
| 457 | + with self.assertRaises(SystemExit): |
| 458 | + main() |
| 459 | + |
| 460 | + ret = mock_stdout.getvalue() |
| 461 | + self.assertEqual( |
| 462 | + ret, |
| 463 | + "Specify files to update!\n", |
| 464 | + ) |
0 commit comments