Skip to content

Commit

Permalink
Another addition to #302 (we can now access the test runner args insi…
Browse files Browse the repository at this point in the history
…de the TestCase) and #319 (The stupid xplane_config.debug/log is now deleted)
  • Loading branch information
tngreene committed Aug 13, 2018
1 parent ed288ad commit 4252dad
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 89 deletions.
5 changes: 3 additions & 2 deletions io_xplane2blender/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class XPlaneTestCase(unittest.TestCase):
expected_logger_warnings = 0

def setUp(self, useLogger = True):
if '--debug' in sys.argv:
setDebug(True)
dd_index = sys.argv.index('--')
blender_args, xplane_args = sys.argv[:dd_index],sys.argv[dd_index+1:]
setDebug('--force-xplane-debug' in xplane_args)

if useLogger:
self.useLogger()
Expand Down
53 changes: 4 additions & 49 deletions io_xplane2blender/xplane_config.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
# File: xplane_config.py
# Holds config variables that are used throughout the addon.

import bpy
from io_xplane2blender import bl_info
from io_xplane2blender import xplane_constants

# Variable: debug
# Set to True for debugging output using <debugger>. Default is True, as we are still dealing with a development release.
# ##############################################################################
# # NOTICE!!!! #
# ##############################################################################
# If you need debug output in a test you must enable call setDebug in your
# test. Otherwise WAYYYYY too much debug output gets put in the test.
# During a test, it is set to False unless the --debug flag is used.
#
# TODO: Having "Debug" in the .blend file then not produce debug output in the
# test has caused me to re-debug and discover this anti pattern at least twice
# now. This is stupid. Remove this stupid variable from the addon
#
# TODO: This is a duplicate for bpy.context.scene.xplane.debug, no matter what you set here it won't matter. In addition,
# at most of it's call sites, it is an unused variable. This really aught to be cleaned one day
debug = False

# Variable: log
# Set to True, to log debug output in a file.
# TODO: This is a duplicate for bpy.context.scene.xplane.log, no matter what you set here it won't matter. In addition,
# at most of it's call sites, it is a barely used variable. This redundent global state variable really aught to be cleaned
log = False

# We make a copy here so as not to cause a circular dependency in xplane_props and other places
CURRENT_ADDON_VERSION = bl_info["version"]

Expand All @@ -52,30 +29,8 @@
# The build number, hardcoded by the build script when there is one, otherwise it is xplane_constants.BUILD_NUMBER_NONE
CURRENT_BUILD_NUMBER = xplane_constants.BUILD_NUMBER_NONE

def initConfig():
global debug
global log
import bpy

if hasattr(bpy.context.scene, "xplane") and bpy.context.scene.xplane.debug:
debug = True

if bpy.context.scene.xplane.log:
log = True
else:
log = False
else:
debug = False
log = False

def getDebug():
global debug
return debug

def getLog():
global log
return log
return bpy.context.scene.xplane.debug

def setDebug(d):
global debug
debug = d
def setDebug(debug:bool)->None:
bpy.context.scene.xplane.debug = debug
8 changes: 3 additions & 5 deletions io_xplane2blender/xplane_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import sys
from .xplane_helpers import XPlaneLogger, logger
from .xplane_types import xplane_file
from .xplane_config import getDebug, getLog, initConfig
from .xplane_config import getDebug
from bpy_extras.io_utils import ImportHelper, ExportHelper
import io_xplane2blender

Expand Down Expand Up @@ -53,8 +53,6 @@ class ExportXPlane(bpy.types.Operator, ExportHelper):
# Parameters:
# context - Blender context object.
def execute(self, context):
initConfig()
log = getLog()
# prepare logging
self._startLogging()

Expand Down Expand Up @@ -143,7 +141,6 @@ def execute(self, context):
return {'FINISHED'}

def _startLogging(self):
log = getLog()
debug = getDebug()
logLevels = ['error', 'warning']

Expand All @@ -162,9 +159,10 @@ def _startLogging(self):
logger.addTransport(XPlaneLogger.ConsoleTransport(), logLevels)

# log out to a file if logging is enabled
if log:
if debug and bpy.context.scene.xplane.log:
if bpy.context.blend_data.filepath != '':
filepath = os.path.dirname(bpy.context.blend_data.filepath)
#Something this? self.logfile = os.path.join(dir,name+'_'+time.strftime("%y-%m-%d-%H-%M-%S")+'_xplane2blender.log')
self.logFile = open(os.path.join(filepath, 'xplane2blender.log'), 'w')
logger.addTransport(XPlaneLogger.FileTransport(self.logFile), logLevels)
else:
Expand Down
73 changes: 40 additions & 33 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,45 @@ def clean_tmp_folder():

def _make_argparse():
parser = argparse.ArgumentParser(description="Runs the XPlane2Blender test suite")
parser.add_argument("-f", "--filter",
help="Filter test files with a regular expression",
test_selection = parser.add_argument_group("Test Selection And Control")
test_selection.add_argument("-f", "--filter",
help="Filter test files with a regular expression",
type=str)#[regex]
parser.add_argument("--exclude",
help="Exclude test files with a regular expression",
test_selection.add_argument("--exclude",
help="Exclude test files with a regular expression",
type=str)#[regex]
parser.add_argument("-c", "--continue",
test_selection.add_argument("-c", "--continue",
help="Keep running after a test failure",
default=False,
action="store_true",
dest="keep_going")
parser.add_argument("-q", "--quiet",

output_control = parser.add_argument_group("Output Control")
output_control.add_argument("-q", "--quiet",
default=False,
help="Only output if tests pass or fail",
action="store_true")
parser.add_argument("-p", "--print-fails",
output_control.add_argument("-p", "--print-fails",
default=False,
help="Like --quiet, but also prints the output of failed tests",
action="store_true")
parser.add_argument("--debug",
help="Enable Blender debugging", #Hopefully it could one day also enable pydev
action="store_true")
parser.add_argument("-n", "--no-factory-startup",
help="Run Blender with current prefs rather than factory prefs",
action="store_true")
'''
python's use of folder-as-module-name makes this much harder.
Possible solutions would be messing with sys.modules, or copying and renaming and etc the src
folder
parser.add_argument("--addon",
default="io_xplane2blender",
type=str,
help="Provide alternative path to addon to test (such as io_xplane2blender_build)")
'''
parser.add_argument("--blender",
default="blender",#Use the blender in the system path
# Hopefully it could one day also enable pydev, and we can move this to a --verbose argument
output_control.add_argument("--force-xplane-debug",
default=False,
help="Shows verbose(!) debug info and turns on Scene's Debug if not set in Blend file",
action='store_true')

blender_options = parser.add_argument_group("Blender Options")
blender_options.add_argument("--blender",
default="blender",# Use the blender in the system path
type=str,
help="Provide alternative path to blender executable")
blender_options .add_argument("--force-blender-debug",
help="Turn on Blender's --debug flag",
action="store_true")
blender_options.add_argument("-n", "--no-factory-startup",
help="Run Blender with current prefs rather than factory prefs",
action="store_true")
return parser

def main(argv=None):
Expand Down Expand Up @@ -107,29 +108,35 @@ def inFilter(filepath):
if not (argv.quiet or argv.print_fails):
printTestBeginning("Running file " + pyFile)

args = [argv.blender, '--addons', 'io_xplane2blender', '--factory-startup', '-noaudio', '-b']
blender_args = [argv.blender, '--addons', 'io_xplane2blender', '--factory-startup', '-noaudio', '-b']

if argv.no_factory_startup:
args.remove('--factory-startup')
blender_args.remove('--factory-startup')

if os.path.exists(blendFile):
args.append(blendFile)
blender_args.append(blendFile)
else:
if not (argv.quiet or argv.print_fails):
print("WARNING: Blender file " + blendFile + " does not exist")
printTestEnd()

args.append('--python')
args.append(pyFile)
blender_args.extend(['--python', pyFile])

if argv.force_blender_debug:
blender_args.append('--debug')

if argv.debug:
args.append('--debug')
# Small Hack!
# Blender stops parsing after '--', so we can append the test runner
# args and bridge the gap without anything fancy!
blender_args.extend(['--']+sys.argv[1:])

if not argv.quiet and\
(argv.force_blender_debug or argv.force_xplane_debug):
# print the command used to execute the script
# to be able to easily re-run it manually to get better error output
print(' '.join(args))
print(' '.join(blender_args))

out = subprocess.check_output(args, stderr = subprocess.STDOUT)
out = subprocess.check_output(blender_args, stderr = subprocess.STDOUT)

if sys.version_info >= (3, 0):
out = out.decode('utf-8')
Expand Down

0 comments on commit 4252dad

Please sign in to comment.