Skip to content

Commit 77700f4

Browse files
committed
Fix for failing extractcode tests on Windowss
1 parent 5c8d28a commit 77700f4

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

src/extractcode/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,12 @@ def new_name(location, is_dir=False):
160160
the extension unchanged.
161161
"""
162162
assert location
163+
163164
location = location.rstrip('\\/')
164165
name = fileutils.file_name(location).strip()
165-
if not name or name == '.':
166+
if (not name or name == '.'
167+
# windows bare drive path as in c: or z:
168+
or (name and len(name)==2 and name.endswith(':'))):
166169
name = 'file'
167170

168171
parent = fileutils.parent_directory(location)

src/extractcode/libarchive2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,19 @@ def write(self, target_dir, transform_path=lambda x: x):
280280
# skip links and special files
281281
if not (self.isfile or self.isdir):
282282
return
283-
283+
abs_target_dir = os.path.abspath(os.path.expanduser(target_dir))
284284
# TODO: return some warning when original path has been transformed
285285
clean_path = transform_path(self.path)
286286
if self.isdir:
287-
dir_path = posixpath.join(target_dir, clean_path)
287+
dir_path = posixpath.join(abs_target_dir, clean_path)
288288
fileutils.create_dir(dir_path)
289289
return dir_path
290290

291291
# isfile
292292
try:
293293
# create parent directories if needed
294294
# TODO: also rename directories, segment by segment?
295-
target_path = os.path.join(target_dir, clean_path)
295+
target_path = os.path.join(abs_target_dir, clean_path)
296296
parent_path = posixpath.dirname(target_path)
297297
fileutils.create_dir(parent_path)
298298

tests/commoncode/test_fileutils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
from commoncode import filetype
4040
from commoncode import fileutils
41+
from commoncode.fileutils import as_posixpath
4142

4243

4344
class TestPermissions(FileBasedTesting):
@@ -317,7 +318,7 @@ def test_os_walk_with_unicode_path(self):
317318
def test_fileutils_walk(self):
318319
test_dir = self.get_test_loc('fileutils/walk')
319320
base = self.get_test_loc('fileutils')
320-
result = [(t.replace(base, ''), d, f,) for t, d, f in fileutils.walk(test_dir)]
321+
result = [(as_posixpath(t.replace(base, '')), d, f,) for t, d, f in fileutils.walk(test_dir)]
321322
expected = [
322323
('/walk', ['d1'], ['f', 'unicode.zip']),
323324
('/walk/d1', ['d2'], ['f1']),
@@ -357,7 +358,7 @@ def test_fileutils_walk_can_walk_an_empty_dir(self):
357358
def test_file_iter(self):
358359
test_dir = self.get_test_loc('fileutils/walk')
359360
base = self.get_test_loc('fileutils')
360-
result = [f.replace(base, '') for f in fileutils.file_iter(test_dir)]
361+
result = [as_posixpath(f.replace(base, '')) for f in fileutils.file_iter(test_dir)]
361362
expected = [
362363
'/walk/f',
363364
'/walk/unicode.zip',
@@ -369,7 +370,7 @@ def test_file_iter(self):
369370

370371
def test_file_iter_can_iterate_a_single_file(self):
371372
test_file = self.get_test_loc('fileutils/walk/f')
372-
result = list(fileutils.file_iter(test_file))
373+
result = [as_posixpath(f) for f in fileutils.file_iter(test_file)]
373374
expected = [test_file]
374375
assert expected == result
375376

tests/extractcode/test_extract.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
from commoncode.testcase import FileBasedTesting
3131
from commoncode import fileutils
32+
from commoncode.fileutils import as_posixpath
3233

3334
import extractcode
3435
from extractcode_assert_utils import check_files
@@ -857,7 +858,7 @@ def test_walk_can_be_extended_while_walking(self):
857858
self.touch(os.path.join(test_dir, 'dir', 'otherarch.gz'))
858859
allpaths = []
859860
for top, dirs, files in self.extract_walker(test_dir):
860-
allpaths.extend([os.path.join(top, d).replace(test_dir, '') for d in dirs + files])
861+
allpaths.extend([as_posixpath(os.path.join(top, d).replace(test_dir, '')) for d in dirs + files])
861862

862863
expected = [
863864
'/arch.gzextract/extracted_file',

tests/scancode/test_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from commoncode.testcase import FileBasedTesting
3232
from scancode import cli
33+
from commoncode.fileutils import as_posixpath
3334

3435

3536
class TestCommandLine(FileBasedTesting):
@@ -97,7 +98,7 @@ def test_extract_option_works_with_relative_paths(self):
9798
assert not 'WARNING' in result.output
9899
assert not 'ERROR' in result.output
99100
expected = ['/c/a/a.txt', '/c/b/a.txt', '/c/c/a.txt']
100-
file_result = [f.replace(test_tgt_dir, '') for f in fileutils.file_iter(test_tgt_dir)]
101+
file_result = [as_posixpath(f.replace(test_tgt_dir, '')) for f in fileutils.file_iter(test_tgt_dir)]
101102
assert sorted(expected)==sorted(file_result)
102103

103104
def test_copyright_option_detects_copyrights(self):

0 commit comments

Comments
 (0)