Skip to content

Commit c981347

Browse files
committed
Add basic unit tests for --file-from and --file-from0
Test the basic functionality of "attic create" with --files-from and --files-from0 options. Note that the tests are not very exhaustive; missing in particular is: - reading from stdin, as the test harness doesn't emulate sys.stdin.buffer. - use of multiple --files-from and/or --files-from0 options.
1 parent bbb53e5 commit c981347

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

attic/archiver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ def do_create(self, args):
118118
pass
119119
for f in args.filelists:
120120
self._process_filelist(archive, cache, skip_inodes, f)
121+
if not (f is sys.stdin or f is getattr(sys.stdin, 'buffer', None)):
122+
f.close()
121123
for path in args.paths:
122124
path = os.path.normpath(path)
123125
if args.dontcross:

attic/testsuite/archiver.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tempfile
1010
import time
1111
import unittest
12+
import itertools
1213
from hashlib import sha256
1314
from attic import xattr
1415
from attic.archive import Archive, ChunkBuffer
@@ -59,6 +60,9 @@ def __exit__(self, *args, **kw):
5960
if v is not None:
6061
os.environ[k] = v
6162

63+
def listdir_recursive(dirname):
64+
return itertools.chain(*[[os.path.normpath(os.path.join(dirpath, f)) for f in filenames]
65+
for dirpath, dirnames, filenames in os.walk(dirname)])
6266

6367
class ArchiverTestCaseBase(AtticTestCase):
6468

@@ -262,6 +266,27 @@ def test_exclude_caches(self):
262266
self.assert_equal(sorted(os.listdir('output/input')), ['cache2', 'file1'])
263267
self.assert_equal(sorted(os.listdir('output/input/cache2')), ['CACHEDIR.TAG'])
264268

269+
def test_files_from(self):
270+
self._test_files_from_option(delim=b'\n', option='--files-from')
271+
272+
def test_files_from0(self):
273+
self._test_files_from_option(delim=b'\0', option='--files-from0')
274+
275+
def _test_files_from_option(self, *, delim, option):
276+
self.attic('init', self.repository_location)
277+
for filename in ['file1', 'non-listed/file', 'listed/file']:
278+
self.create_regular_file(filename, size=1024 * 80)
279+
listed_files = sorted(['file1', 'listed/file'])
280+
self.create_regular_file('filelist',
281+
contents=delim.join([os.path.join('input', f).encode('ascii')
282+
for f in listed_files]))
283+
self.attic('create', option + '=input/filelist', self.repository_location + '::test')
284+
with changedir('output'):
285+
self.attic('extract', self.repository_location + '::test')
286+
with changedir('output/input'):
287+
present_files = sorted(listdir_recursive('.'))
288+
self.assert_equal(present_files, listed_files)
289+
265290
def test_path_normalization(self):
266291
self.attic('init', self.repository_location)
267292
self.create_regular_file('dir1/dir2/file', size=1024 * 80)

0 commit comments

Comments
 (0)