File: test_code.py

package info (click to toggle)
duplicity 3.0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,260 kB
  • sloc: python: 25,089; sh: 934; ansic: 392; makefile: 83
file content (102 lines) | stat: -rw-r--r-- 3,348 bytes parent folder | download
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
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
#
# Copyright 2014 Michael Terry
#
# This file is part of duplicity.
#
# Duplicity is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# Duplicity 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with duplicity; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


import glob
import os
import pycodestyle
import sys
from subprocess import (
    Popen,
    PIPE,
    STDOUT,
)
import unittest

import pytest

from . import _top_dir, DuplicityTestCase

files_to_test = []
files_to_test.extend(glob.glob(os.path.join(_top_dir, "duplicity/**/*.py"), recursive=True))
files_to_test.extend(glob.glob(os.path.join(_top_dir, "testing/functional/*.py")))
files_to_test.extend(glob.glob(os.path.join(_top_dir, "testing/unit/*.py")))
files_to_test.extend(glob.glob(os.path.join(_top_dir, "testing/*.py")))

# don't test argparse311.py.  not really ours.
files_to_test.remove(os.path.join(_top_dir, "duplicity/argparse311.py"))

# TODO: remove when pylint AST builder is fixed.
files_to_test.remove(os.path.join(_top_dir, "duplicity/backends/giobackend.py"))


@unittest.skipIf(os.environ.get("USER", "") == "buildd", "Skip test on Launchpad")
class CodeTest(DuplicityTestCase):
    def run_checker(self, cmd, returncodes=None):
        if returncodes is None:
            returncodes = [0]
        process = Popen(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
        output = process.communicate()[0]
        if len(output):
            for line in output.split("\n"):
                print(line, file=sys.stderr)
            output = ""
        self.assertTrue(
            process.returncode in returncodes,
            f"Test failed: returncode = {process.returncode}",
        )

    def test_black(self):
        """Black check for out of format files"""
        print()
        self.run_checker(
            [
                "black",
                "--check",
            ]
            + files_to_test,
        )

    def test_pep8(self):
        """Test that we conform to PEP-8 using pycodestyle."""
        # Note that the settings, ignores etc for pycodestyle are set in pyproject.toml, not here
        print()
        style = pycodestyle.StyleGuide(config_file=os.path.join(_top_dir, "setup.cfg"))
        result = style.check_files(files_to_test)
        self.assertEqual(
            result.total_errors,
            0,
            f"Found {result.total_errors} code style errors (and warnings).",
        )

    def test_pylint(self):
        """Pylint test (requires pylint to be installed to pass)"""
        print()
        self.run_checker(
            [
                "pylint",
                f"--rcfile={os.path.join(_top_dir, 'pyproject.toml')}",
            ]
            + files_to_test
        )


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