Beispiel #1
0
    def test_update_file_binary_file(self, mock_stdout):
        self.args.year = '2020'
        self.args.changed = False
        self.args.licence = 'AGPL-3.0-or-later'

        test_file = self.path / "test.py"
        if test_file.exists():
            test_file.unlink()

        # create a Binary file ...
        # https://stackoverflow.com/a/30148554
        with open(test_file, 'wb') as f:
            f.write(struct.pack('>if', 42, 2.71828182846))

        with self.assertRaises(UnicodeDecodeError):
            code = update_file(file=test_file,
                               regex=self.regex,
                               args=self.args)
            self.assertEqual(code, 1)

        ret = mock_stdout.getvalue()
        self.assertEqual(
            ret,
            f"{test_file}: Ignoring binary file.\n",
        )

        test_file.unlink()
Beispiel #2
0
    def test_update_file_not_existing(self, mock_stdout):
        self.args.year = '2020'
        self.args.changed = False
        self.args.licence = 'AGPL-3.0-or-later'

        test_file = self.path / "test.py"
        if test_file.exists():
            test_file.unlink()

        with self.assertRaises(FileNotFoundError):
            update_file(file=test_file, regex=self.regex, args=self.args)

        ret = mock_stdout.getvalue()
        self.assertEqual(
            ret,
            f"{test_file}: File is not existing.\n",
        )
Beispiel #3
0
    def test_update_header_in_file(self, mock_stdout):
        self.args.year = '2021'
        self.args.changed = False
        self.args.licence = 'AGPL-3.0-or-later'

        header = """# -*- coding: utf-8 -*-
# Copyright (C) 2020 Greenbone Networks GmbH
#
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

        test_file = self.path / "test.py"
        if test_file.exists():
            test_file.unlink()

        test_file.write_text(header)

        code = update_file(file=test_file, regex=self.regex, args=self.args)

        self.assertEqual(code, 0)
        ret = mock_stdout.getvalue()
        self.assertEqual(
            ret,
            f"{test_file}: Changed Licence Header "
            "Copyright Year None -> 2021\n",
        )
        self.assertIn(
            '# Copyright (C) 2020-2021 Greenbone Networks GmbH',
            test_file.read_text(),
        )

        test_file.unlink()
Beispiel #4
0
    def test_update_file_suffix_invalid(self, mock_stdout):
        self.args.year = '2020'
        self.args.changed = False
        self.args.licence = 'AGPL-3.0-or-later'

        test_file = self.path / "test.pppy"
        test_file.touch()

        code = update_file(file=test_file, regex=self.regex, args=self.args)
        self.assertEqual(code, 1)

        ret = mock_stdout.getvalue()
        self.assertEqual(
            ret,
            f"{test_file}: No licence header for the format .pppy found.\n",
        )

        test_file.unlink()
Beispiel #5
0
    def test_update_file_wrong_licence(self, mock_stdout):
        self.args.year = '2020'
        self.args.changed = False
        self.args.licence = 'AAAGPL-3.0-or-later'

        test_file = self.path / "test.py"
        test_file.touch()

        code = update_file(file=test_file, regex=self.regex, args=self.args)
        self.assertEqual(code, 1)

        ret = mock_stdout.getvalue()
        self.assertEqual(
            ret,
            f"{test_file}: Licence file for "
            "AAAGPL-3.0-or-later is not existing.\n",
        )

        test_file.unlink()
Beispiel #6
0
    def test_update_file_changed(self, mock_stdout):
        self.args.year = '1995'
        self.args.changed = True
        self.args.licence = 'AGPL-3.0-or-later'

        test_file = self.path / "test.py"
        if test_file.exists():
            test_file.unlink()

        with self.assertRaises(FileNotFoundError):
            code = update_file(file=test_file,
                               regex=self.regex,
                               args=self.args)
            self.assertEqual(code, 1)

        ret = mock_stdout.getvalue()

        self.assertEqual(
            ret,
            f"{test_file}: Could not get date of last "
            f"modification using git, using {self.args.year} instead.\n"
            f"{test_file}: File is not existing.\n",
        )