File: shared_test_code.py

package info (click to toggle)
fdroidserver 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,948 kB
  • sloc: python: 34,139; xml: 2,186; sh: 1,362; java: 293; makefile: 54; javascript: 23
file content (90 lines) | stat: -rw-r--r-- 2,663 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
#
# Copyright (C) 2017, Michael Poehn <michael.poehn@fsfe.org>
#
# 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/>.

import os
import sys
import tempfile
import unittest
import unittest.mock

from pathlib import Path


GP_FINGERPRINT = 'B7C2EEFD8DAC7806AF67DFCD92EB18126BC08312A7F2D6F3862E46013C7A6135'


class VerboseFalseOptions:
    verbose = False


class TmpCwd:
    """Context-manager for temporarily changing the current working directory."""

    def __init__(self, new_cwd):
        self.new_cwd = new_cwd

    def __enter__(self):
        self.orig_cwd = os.getcwd()
        os.chdir(self.new_cwd)

    def __exit__(self, a, b, c):
        os.chdir(self.orig_cwd)


class TmpPyPath:
    """Context-manager for temporarily adding a directory to Python path."""

    def __init__(self, additional_path):
        self.additional_path = additional_path

    def __enter__(self):
        sys.path.append(self.additional_path)

    def __exit__(self, a, b, c):
        sys.path.remove(self.additional_path)


def mock_open_to_str(mock):
    """For accessing all data written into a unittest.mock.mock_open() instance as a string."""

    return "".join(
        [x.args[0] for x in mock.mock_calls if str(x).startswith("call().write(")]
    )


def mkdtemp():
    if sys.version_info < (3, 10):  # ignore_cleanup_errors was added in 3.10
        return tempfile.TemporaryDirectory()
    else:
        return tempfile.TemporaryDirectory(ignore_cleanup_errors=True)


def mkdir_testfiles(localmodule, test):
    """Keep the test files in a labeled test dir for easy reference"""
    testroot = Path(localmodule) / '.testfiles'
    testroot.mkdir(exist_ok=True)
    testdir = testroot / unittest.TestCase.id(test)
    testdir.mkdir(exist_ok=True)
    return Path(tempfile.mkdtemp(dir=testdir))


def mock_urlopen(status=200, body=None):
    resp = unittest.mock.MagicMock()
    resp.getcode.return_value = status
    resp.read.return_value = body
    resp.__enter__.return_value = resp
    return unittest.mock.Mock(return_value=resp)