Skip to content

Commit

Permalink
test: finish pr #22, relative &filelist
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Nov 24, 2023
1 parent 420de0d commit bd76df4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ Unreleased

- Dropped support for Python 2.7, 3.5, and 3.6, and added 3.11 and 3.12.

- Files listing other files to process can now be specified as
``&files_to_cog.txt`` to interpret the file names relative to the location of
the list file. The existing ``@files_to_cog.txt`` syntax interprets file
names relative to the current working directory. Thanks, Phil Kirkpatrick.

- Removed the ``cog.py`` installed file. Use the ``cog`` command, or ``python
-m cogapp`` to run cog.

Expand Down
10 changes: 4 additions & 6 deletions cogapp/cogapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import types

from .whiteutils import commonPrefix, reindentBlock, whitePrefix
from .utils import NumberedFileReader, Redirectable, md5
from .utils import NumberedFileReader, Redirectable, change_dir, md5

__version__ = "4.0.0.dev2"

Expand Down Expand Up @@ -724,12 +724,10 @@ def processArguments(self, args):
self.processFileList(args[0][1:])
elif args[0][0] == '&':
if self.options.sOutputName:
raise CogUsageError("Can't use -o with @file")
saved_cwd = os.getcwd()
raise CogUsageError("Can't use -o with &file")
file_list = args[0][1:]
os.chdir(os.path.dirname(file_list))
self.processFileList(os.path.basename(file_list))
os.chdir(saved_cwd)
with change_dir(os.path.dirname(file_list)):
self.processFileList(os.path.basename(file_list))
else:
self.processWildcards(args[0])

Expand Down
48 changes: 41 additions & 7 deletions cogapp/test_cogapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,16 +808,15 @@ def testArgumentFailure(self):
self.cog.callableMain(['argv0', '-j'])

def testNoDashOAndAtFile(self):
d = {
'cogfiles.txt': """\
# Please run cog
"""
}

makeFiles(d)
makeFiles({"cogfiles.txt": "# Please run cog"})
with self.assertRaisesRegex(CogUsageError, r"^Can't use -o with @file$"):
self.cog.callableMain(['argv0', '-o', 'foo', '@cogfiles.txt'])

def testNoDashOAndAmpFile(self):
makeFiles({"cogfiles.txt": "# Please run cog"})
with self.assertRaisesRegex(CogUsageError, r"^Can't use -o with &file$"):
self.cog.callableMain(['argv0', '-o', 'foo', '&cogfiles.txt'])

def testDashV(self):
self.assertEqual(self.cog.main(['argv0', '-v']), 0)
output = self.output.getvalue()
Expand Down Expand Up @@ -1346,6 +1345,41 @@ def fix_backslashes(files_txt):
self.assertFilesSame('subdir/subback.cog', 'subback.out')
self.assertFilesSame('subdir/subfwd.cog', 'subfwd.out')

def testAmpFile(self):
d = {
'code': {
'files_to_cog': """\
# A locally resolved file name.
test.cog
""",

'test.cog': """\
//[[[cog
import myampsubmodule
//]]]
//[[[end]]]
""",

'test.out': """\
//[[[cog
import myampsubmodule
//]]]
Hello from myampsubmodule
//[[[end]]]
""",

'myampsubmodule.py': """\
import cog
cog.outl("Hello from myampsubmodule")
"""
}
}

makeFiles(d)
print(os.path.abspath("code/test.out"))
self.cog.callableMain(['argv0', '-r', '&code/files_to_cog'])
self.assertFilesSame('code/test.cog', 'code/test.out')

def run_with_verbosity(self, verbosity):
d = {
'unchanged.cog': """\
Expand Down
18 changes: 18 additions & 0 deletions cogapp/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
""" Utilities for cog.
"""

import contextlib
import functools
import hashlib
import os
import sys


Expand Down Expand Up @@ -51,3 +53,19 @@ def readline(self):

def linenumber(self):
return self.n


@contextlib.contextmanager
def change_dir(new_dir):
"""Change directory, and then change back.
Use as a context manager, it will return to the original
directory at the end of the block.
"""
old_dir = os.getcwd()
os.chdir(str(new_dir))
try:
yield
finally:
os.chdir(old_dir)
6 changes: 5 additions & 1 deletion docs/running.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ with an ``@``:
$ cog @files_to_cog.txt
These @-files can be nested, and each line can contain switches as well as a
File names in the list file are relative to the current directory. You can also
use ``&files_to_cog.txt`` and the file names will be relative to the location
of the list file.

These list files can be nested, and each line can contain switches as well as a
file to process. For example, you can create a file cogfiles.txt:

.. code-block:: text
Expand Down

0 comments on commit bd76df4

Please sign in to comment.