2010-02-21 11:14:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-07-19 17:29:01 +00:00
|
|
|
# Copyright (C) 2010-2015 Bastian Kleineidam
|
2010-02-21 11:14:57 +00:00
|
|
|
#
|
|
|
|
# This program 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 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import os
|
2012-11-19 19:58:42 +00:00
|
|
|
import sys
|
2010-02-21 11:14:57 +00:00
|
|
|
import patoolib
|
2012-11-14 19:24:34 +00:00
|
|
|
import pytest
|
2013-02-22 17:37:28 +00:00
|
|
|
import importlib
|
2012-05-24 21:09:37 +00:00
|
|
|
|
2010-02-21 11:14:57 +00:00
|
|
|
basedir = os.path.dirname(__file__)
|
|
|
|
datadir = os.path.join(basedir, 'data')
|
2013-02-25 20:04:02 +00:00
|
|
|
patool_cmd = os.path.join(os.path.dirname(basedir), "patool")
|
2010-02-21 11:14:57 +00:00
|
|
|
|
2012-11-19 19:58:42 +00:00
|
|
|
# Python 3.x renamed the function name attribute
|
|
|
|
if sys.version_info[0] > 2:
|
|
|
|
fnameattr = '__name__'
|
|
|
|
else:
|
|
|
|
fnameattr = 'func_name'
|
2012-05-24 21:09:37 +00:00
|
|
|
|
2013-02-22 17:37:28 +00:00
|
|
|
def _need_func(testfunc, name, description):
|
|
|
|
"""Decorator skipping test if given testfunc returns False."""
|
|
|
|
def check_func(func):
|
|
|
|
def newfunc(*args, **kwargs):
|
|
|
|
if not testfunc(name):
|
|
|
|
raise pytest.skip("%s %r is not available" % (description, name))
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
setattr(newfunc, fnameattr, getattr(func, fnameattr))
|
2010-03-08 21:04:27 +00:00
|
|
|
return newfunc
|
2013-02-22 17:37:28 +00:00
|
|
|
return check_func
|
2010-03-08 21:04:27 +00:00
|
|
|
|
|
|
|
|
2013-02-22 17:37:28 +00:00
|
|
|
def needs_os(name):
|
|
|
|
"""Decorator skipping test if given operating system is not available."""
|
|
|
|
return _need_func(lambda x: os.name == x, name, 'operating system')
|
|
|
|
|
|
|
|
|
|
|
|
def needs_program(name):
|
2010-02-22 20:30:47 +00:00
|
|
|
"""Decorator skipping test if given program is not available."""
|
2013-02-22 17:37:28 +00:00
|
|
|
return _need_func(lambda x: patoolib.util.find_program(x), name, 'program')
|
2010-02-22 20:55:29 +00:00
|
|
|
|
|
|
|
|
2013-02-22 17:37:28 +00:00
|
|
|
def needs_one_program(programs):
|
2010-03-11 17:33:58 +00:00
|
|
|
"""Decorator skipping test if not one of given programs are available."""
|
2013-02-22 17:37:28 +00:00
|
|
|
return _need_func(lambda x: all(map(patoolib.util.find_program, x)), programs, 'programs')
|
|
|
|
|
|
|
|
|
|
|
|
def needs_module(name):
|
|
|
|
"""Decorator skipping test if given module is not available."""
|
|
|
|
def has_module(module):
|
|
|
|
try:
|
|
|
|
importlib.import_module(module)
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
return _need_func(has_module, name, 'Python module')
|
2010-03-11 17:33:58 +00:00
|
|
|
|
|
|
|
|
2010-02-22 20:55:29 +00:00
|
|
|
def needs_codec (program, codec):
|
|
|
|
"""Decorator skipping test if given program codec is not available."""
|
|
|
|
def check_prog (f):
|
|
|
|
def newfunc (*args, **kwargs):
|
2010-03-01 15:10:17 +00:00
|
|
|
if not patoolib.util.find_program(program):
|
2012-11-14 19:24:34 +00:00
|
|
|
raise pytest.skip("program `%s' not available" % program)
|
2010-03-01 15:18:36 +00:00
|
|
|
if not has_codec(program, codec):
|
2012-11-14 19:24:34 +00:00
|
|
|
raise pytest.skip("codec `%s' for program `%s' not available" % (codec, program))
|
2010-02-22 20:55:29 +00:00
|
|
|
return f(*args, **kwargs)
|
2012-11-19 19:58:42 +00:00
|
|
|
setattr(newfunc, fnameattr, getattr(f, fnameattr))
|
2010-02-22 20:55:29 +00:00
|
|
|
return newfunc
|
|
|
|
return check_prog
|
|
|
|
|
|
|
|
|
2010-03-01 15:18:36 +00:00
|
|
|
def has_codec (program, codec):
|
2010-03-03 19:13:03 +00:00
|
|
|
"""Test if program supports given codec."""
|
2010-02-22 20:55:29 +00:00
|
|
|
if program == '7z' and codec == 'rar':
|
2010-03-01 15:05:32 +00:00
|
|
|
return patoolib.util.p7zip_supports_rar()
|
2012-05-23 03:19:14 +00:00
|
|
|
if patoolib.program_supports_compression(program, codec):
|
|
|
|
return True
|
|
|
|
return patoolib.util.find_program(codec)
|
2013-02-23 08:01:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
def skip_on_travis():
|
|
|
|
"""Skip test if TRAVIS build environment is detected."""
|
|
|
|
def check_func(func):
|
|
|
|
def newfunc(*args, **kwargs):
|
|
|
|
if "TRAVIS" in os.environ:
|
|
|
|
raise pytest.skip("Skip on TRAVIS CI build.")
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
setattr(newfunc, fnameattr, getattr(func, fnameattr))
|
|
|
|
return newfunc
|
|
|
|
return check_func
|