scikit-learn / scikit-learn

@@ -1,70 +1,8 @@
Loading
1 1
"""Tests for making sure experimental imports work as expected."""
2 2
3 -
import sys
4 -
import os
5 -
import os.path as op
6 3
import textwrap
7 -
import tempfile
8 -
from subprocess import check_output, STDOUT, CalledProcessError
9 -
from subprocess import TimeoutExpired
10 4
11 -
12 -
def assert_run_python_script(source_code, timeout=60):
13 -
    """Utility to check assertions in an independent Python subprocess.
14 -
15 -
    The script provided in the source code should return 0 and not print
16 -
    anything on stderr or stdout.
17 -
18 -
    This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle
19 -
20 -
    Parameters
21 -
    ----------
22 -
    source_code : str
23 -
        The Python source code to execute.
24 -
    timeout : int
25 -
        Time in seconds before timeout.
26 -
    """
27 -
    def _make_cwd_env():
28 -
        """Helper to prepare environment for the child processes"""
29 -
        sklearn_repo_folder = op.normpath(
30 -
            op.join(op.dirname(__file__), '../..'))
31 -
        env = os.environ.copy()
32 -
        pythonpath = "{src}{sep}tests{pathsep}{src}".format(
33 -
            src=sklearn_repo_folder, sep=os.sep, pathsep=os.pathsep)
34 -
        env['PYTHONPATH'] = pythonpath
35 -
        return sklearn_repo_folder, env
36 -
37 -
    fd, source_file = tempfile.mkstemp(suffix='_src_test_sklearn.py')
38 -
    os.close(fd)
39 -
    try:
40 -
        with open(source_file, 'wb') as f:
41 -
            f.write(source_code.encode('utf-8'))
42 -
        cmd = [sys.executable, source_file]
43 -
        cwd, env = _make_cwd_env()
44 -
        kwargs = {
45 -
            'cwd': cwd,
46 -
            'stderr': STDOUT,
47 -
            'env': env,
48 -
        }
49 -
        # If coverage is running, pass the config file to the subprocess
50 -
        coverage_rc = os.environ.get("COVERAGE_PROCESS_START")
51 -
        if coverage_rc:
52 -
            kwargs['env']['COVERAGE_PROCESS_START'] = coverage_rc
53 -
54 -
        kwargs['timeout'] = timeout
55 -
        try:
56 -
            try:
57 -
                out = check_output(cmd, **kwargs)
58 -
            except CalledProcessError as e:
59 -
                raise RuntimeError(u"script errored with output:\n%s"
60 -
                                   % e.output.decode('utf-8'))
61 -
            if out != b"":
62 -
                raise AssertionError(out.decode('utf-8'))
63 -
        except TimeoutExpired as e:
64 -
            raise RuntimeError(u"script timeout, output so far:\n%s"
65 -
                               % e.output.decode('utf-8'))
66 -
    finally:
67 -
        os.unlink(source_file)
5 +
from sklearn.utils.testing import assert_run_python_script
68 6
69 7
70 8
def test_imports_strategies():

@@ -17,6 +17,9 @@
Loading
17 17
import warnings
18 18
import sys
19 19
import functools
20 +
import tempfile
21 +
from subprocess import check_output, STDOUT, CalledProcessError
22 +
from subprocess import TimeoutExpired
20 23
21 24
22 25
import scipy as sp
@@ -83,7 +86,8 @@
Loading
83 86
           "assert_array_almost_equal", "assert_array_less",
84 87
           "assert_less", "assert_less_equal",
85 88
           "assert_greater", "assert_greater_equal",
86 -
           "assert_approx_equal", "assert_allclose", "SkipTest"]
89 +
           "assert_approx_equal", "assert_allclose",
90 +
           "assert_run_python_script", "SkipTest"]
87 91
__all__.extend(additional_names_in_all)
88 92
89 93
_dummy = TestCase('__init__')
@@ -971,3 +975,61 @@
Loading
971 975
            if n1 != n2:
972 976
                incorrect += [func_name + ' ' + n1 + ' != ' + n2]
973 977
    return incorrect
978 +
979 +
980 +
def assert_run_python_script(source_code, timeout=60):
981 +
    """Utility to check assertions in an independent Python subprocess.
982 +
983 +
    The script provided in the source code should return 0 and not print
984 +
    anything on stderr or stdout.
985 +
986 +
    This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle
987 +
988 +
    Parameters
989 +
    ----------
990 +
    source_code : str
991 +
        The Python source code to execute.
992 +
    timeout : int
993 +
        Time in seconds before timeout.
994 +
    """
995 +
    def _make_cwd_env():
996 +
        """Helper to prepare environment for the child processes"""
997 +
        sklearn_repo_folder = op.normpath(
998 +
            op.join(op.dirname(__file__), '../..'))
999 +
        env = os.environ.copy()
1000 +
        pythonpath = "{src}{sep}tests{pathsep}{src}".format(
1001 +
            src=sklearn_repo_folder, sep=os.sep, pathsep=os.pathsep)
1002 +
        env['PYTHONPATH'] = pythonpath
1003 +
        return sklearn_repo_folder, env
1004 +
1005 +
    fd, source_file = tempfile.mkstemp(suffix='_src_test_sklearn.py')
1006 +
    os.close(fd)
1007 +
    try:
1008 +
        with open(source_file, 'wb') as f:
1009 +
            f.write(source_code.encode('utf-8'))
1010 +
        cmd = [sys.executable, source_file]
1011 +
        cwd, env = _make_cwd_env()
1012 +
        kwargs = {
1013 +
            'cwd': cwd,
1014 +
            'stderr': STDOUT,
1015 +
            'env': env,
1016 +
        }
1017 +
        # If coverage is running, pass the config file to the subprocess
1018 +
        coverage_rc = os.environ.get("COVERAGE_PROCESS_START")
1019 +
        if coverage_rc:
1020 +
            kwargs['env']['COVERAGE_PROCESS_START'] = coverage_rc
1021 +
1022 +
        kwargs['timeout'] = timeout
1023 +
        try:
1024 +
            try:
1025 +
                out = check_output(cmd, **kwargs)
1026 +
            except CalledProcessError as e:
1027 +
                raise RuntimeError(u"script errored with output:\n%s"
1028 +
                                   % e.output.decode('utf-8'))
1029 +
            if out != b"":
1030 +
                raise AssertionError(out.decode('utf-8'))
1031 +
        except TimeoutExpired as e:
1032 +
            raise RuntimeError(u"script timeout, output so far:\n%s"
1033 +
                               % e.output.decode('utf-8'))
1034 +
    finally:
1035 +
        os.unlink(source_file)
Files Coverage
sklearn 95.68%
Project Totals (383 files) 95.68%
Untitled
1
comment: false
2

3
coverage:
4
  status:
5
    project:
6
      default:
7
        # Commits pushed to master should not make the overall
8
        # project coverage decrease by more than 1%:
9
        target: auto
10
        threshold: 1%
11
    patch:
12
      default:
13
        # Be tolerant on slight code coverage diff on PRs to limit
14
        # noisy red coverage status on github PRs.
15
        # Note The coverage stats are still uploaded
16
        # to codecov so that PR reviewers can see uncovered lines
17
        # in the github diff if they install the codecov browser
18
        # extension:
19
        # https://github.com/codecov/browser-extension
20
        target: auto
21
        threshold: 1%
22

23
ignore:
24
- "sklearn/externals"
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading