Skip to content

Commit

Permalink
Add a helper for executing test code in the given directory
Browse files Browse the repository at this point in the history
Use a context manager instead of writing try/finally in several places.

No real changes, this just (slightly) simplifies the existing tests and
will make adding new ones less painful.
  • Loading branch information
vadz committed Apr 19, 2018
1 parent 2afdcdc commit 458c13e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
def pytest_configure(config):
import sys, os.path

bkl_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'src'))
sys.path = [bkl_path] + sys.path
tests_path = os.path.dirname(__file__)
bkl_path = os.path.normpath(os.path.join(tests_path, '..', 'src'))
sys.path = [bkl_path, tests_path] + sys.path

import logging
log_level = logging.DEBUG if config.getvalue("debug") else logging.WARNING
Expand Down
35 changes: 35 additions & 0 deletions tests/indir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This file is part of Bakefile (http://bakefile.org)
#
# Copyright (C) 2018 Vadim Zeitlin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#

import os
from contextlib import contextmanager

@contextmanager
def in_directory(path):
"""
Context manager for temporarily changing the current directory.
"""
cwd_orig = os.getcwd()
os.chdir(path)
yield
os.chdir(cwd_orig)
7 changes: 2 additions & 5 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import bkl.parser, bkl.interpreter, bkl.error
import bkl.dumper
from indir import in_directory

@pytest.fixture(scope='session')
def testdirs():
Expand Down Expand Up @@ -59,12 +60,8 @@ def test_builder(testdir, model_file):
input = os.path.splitext(model_file)[0] + '.bkl'

f = input[len(testdir)+1:]
cwd = os.getcwd()
os.chdir(testdir)
try:
with in_directory(testdir):
_do_test_builder_on_file(f, model_file)
finally:
os.chdir(cwd)

def _do_test_builder_on_file(input, model_file):
print 'interpreting %s' % input
Expand Down
13 changes: 5 additions & 8 deletions tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import bkl.parser, bkl.interpreter, bkl.error
import bkl.dumper

from indir import in_directory

@pytest.fixture(scope='session')
def testdir():
import projects
Expand Down Expand Up @@ -64,12 +66,8 @@ def test_full(testdir, project_file):
model_file = os.path.splitext(project_file)[0] + '.model'

f = project_file[len(testdir)+1:]
cwd = os.getcwd()
os.chdir(testdir)
try:
with in_directory(testdir):
_do_test_on_file(f, model_file)
finally:
os.chdir(cwd)

def _do_test_on_file(input, model_file):
print 'interpreting %s' % input
Expand Down Expand Up @@ -116,9 +114,8 @@ def test_unicode_filename():
import tempfile
cwd = os.getcwd()
tmpdir = tempfile.mkdtemp(prefix=u"Üñîçöḍè".encode('utf-8'))
os.chdir(tmpdir)
try:
i.process(t)
with in_directory(tmpdir):
i.process(t)
finally:
os.chdir(cwd)
shutil.rmtree(tmpdir)
7 changes: 2 additions & 5 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from glob import glob

import bkl.parser, bkl.error
from indir import in_directory

@pytest.fixture(scope='session')
def testdir():
Expand All @@ -52,12 +53,8 @@ def test_parser(testdir, ast_file):
input = os.path.splitext(ast_file)[0] + '.bkl'

f = input[len(testdir)+1:]
cwd = os.getcwd()
os.chdir(testdir)
try:
with in_directory(testdir):
_do_test_parser_on_file(f, ast_file)
finally:
os.chdir(cwd)


def _do_test_parser_on_file(input, ast_file):
Expand Down

0 comments on commit 458c13e

Please sign in to comment.