diff --git a/mkcodes.py b/mkcodes.py index 51388aa..9602e94 100644 --- a/mkcodes.py +++ b/mkcodes.py @@ -109,5 +109,9 @@ def main(inputs, output, github, safe): inputname = os.path.splitext(os.path.basename(filename))[0] outputfilename = output.format(name=inputname) + outputdir = os.path.dirname(outputfilename) + if not os.path.exists(outputdir): + os.makedirs(outputdir) + with open(outputfilename, 'w') as outputfile: outputfile.write(code) diff --git a/tests/test.py b/tests/test.py index 07f1e34..e28bec1 100644 --- a/tests/test.py +++ b/tests/test.py @@ -1,4 +1,5 @@ import os +import shutil import textwrap import unittest import subprocess @@ -94,6 +95,20 @@ def test_multiple(self): self.remove('tests/some.py') self.remove('tests/other.py') + def test_unexistant_output_directory(self): + try: + subprocess.call([ + 'mkcodes', '--output', 'tests/unexistant/{name}.py', + '--github', 'tests/data/some.md']) + self.assertFileEqual('tests/unexistant/some.py', """\ + bar = False + + + backticks = range(5, 7) + """) + finally: + shutil.rmtree('tests/unexistant') + @unittest.skip def test_glob(self): raise NotImplementedError