Browse Source

Use variable number of arguments for `chdir`

pull/172/head
Louis 9 years ago
parent
commit
b1eb5a0f87
  1. 6
      patacrep/files.py
  2. 15
      test/test_chordpro/test_parser.py

6
patacrep/files.py

@ -61,17 +61,17 @@ def path2posix(string):
) )
@contextmanager @contextmanager
def chdir(path): def chdir(*path):
"""Locally change dir """Locally change dir
Can be used as: Can be used as:
with chdir("some/directory"): with chdir("some", "directory"):
do_stuff() do_stuff()
""" """
olddir = os.getcwd() olddir = os.getcwd()
if path: if path:
os.chdir(path) os.chdir(os.path.join(*path))
yield yield
os.chdir(olddir) os.chdir(olddir)
else: else:

15
test/test_chordpro/test_parser.py

@ -44,10 +44,10 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
@staticmethod @staticmethod
@contextlib.contextmanager @contextlib.contextmanager
def chdir(): def chdir(*path):
"""Context to temporarry change current directory to this file directory """Context to temporarry change current directory, relative to this file directory
""" """
with files.chdir(resource_filename(__name__, "")): with files.chdir(resource_filename(__name__, ""), *path):
yield yield
def assertRender(self, base, destformat): # pylint: disable=invalid-name def assertRender(self, base, destformat): # pylint: disable=invalid-name
@ -91,7 +91,7 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
cls._create_test(base, dest), cls._create_test(base, dest),
) )
with files.chdir('errors'): with cls.chdir('errors'):
for source in sorted(glob.glob('*.source')): for source in sorted(glob.glob('*.source')):
base = source[:-len(".source")] base = source[:-len(".source")]
yield ( yield (
@ -117,10 +117,9 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
def test_parse_failure(self): def test_parse_failure(self):
"""Test that `base` parsing fails.""" """Test that `base` parsing fails."""
sourcename = "{}.source".format(base) sourcename = "{}.source".format(base)
with self.chdir(): with self.chdir('errors'):
with files.chdir('errors'): parser = self.song_plugins[LANGUAGES['sgc']]['sgc']
parser = self.song_plugins[LANGUAGES['sgc']]['sgc'] self.assertRaises(SyntaxError, parser, sourcename, self.config)
self.assertRaises(SyntaxError, parser, sourcename, self.config)
test_parse_failure.__doc__ = ( test_parse_failure.__doc__ = (
"Test that '{base}' parsing fails." "Test that '{base}' parsing fails."

Loading…
Cancel
Save