1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-06-03 06:10:10 +02:00
fdroidserver/tests/testcommon.py
Hans-Christoph Steiner 18f3acc32e split out options from read_config()
There is no longer any reason for these to be intertwined.

This deliberately avoids touching some files as much as possible because
they are super tangled and due to be replaced.  Those files are:

* fdroidserver/build.py
* fdroidserver/update.py

# Conflicts:
#	tests/testcommon.py

# Conflicts:
#	fdroidserver/btlog.py
#	fdroidserver/import_subcommand.py
2024-05-08 16:26:46 +02:00

89 lines
2.5 KiB
Python

#!/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
from pathlib import Path
from unittest import mock
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 tempfile.mkdtemp(dir=testdir)
def parse_args_for_test(parser, args):
"""Only send --flags to the ArgumentParser, not test classes, etc."""
from fdroidserver.common import parse_args
flags = []
for arg in args:
if arg[0] == '-':
flags.append(flags)
with mock.patch('sys.argv', flags):
parse_args(parser)