Skip to content

Commit

Permalink
Python 3 fixes - fix dirutil, fileutil, and xml_parser tests (pantsbu…
Browse files Browse the repository at this point in the history
…ild#6229)

### Problem
Dirutil tests failing due to unicode vs bytes. 

`dirutil.py` itself is fine. I decided `safe_file_dump()` and `read_file()` should still work with bytes, not unicode.

### Related PRs
To get completely green on Py3, requires pantsbuild#6226 and pantsbuild#6228. This can be merged before them both, though, as it shouldn't break Py2 and we aren't running Py3 yet.
  • Loading branch information
Eric-Arellano authored and Chris Livingston committed Aug 27, 2018
1 parent 89b766b commit 2d10d9a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
24 changes: 12 additions & 12 deletions tests/python/pants_test/util/test_dirutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import os
import time
import unittest
from builtins import str
from contextlib import contextmanager

import mock
from future.utils import text_type

from pants.util import dirutil
from pants.util.contextutil import pushd, temporary_dir
Expand Down Expand Up @@ -120,10 +120,10 @@ def test_safe_walk(self):
# unicode constructor.
with temporary_dir() as tmpdir:
safe_mkdir(os.path.join(tmpdir, '中文'))
if isinstance(tmpdir, text_type):
if isinstance(tmpdir, str):
tmpdir = tmpdir.encode('utf-8')
for _, dirs, _ in dirutil.safe_walk(tmpdir):
self.assertTrue(all(isinstance(dirname, text_type) for dirname in dirs))
self.assertTrue(all(isinstance(dirname, str) for dirname in dirs))

@contextmanager
def tree(self):
Expand All @@ -149,7 +149,7 @@ class Dir(datatype(['path'])):
class File(datatype(['path', 'contents'])):
@classmethod
def empty(cls, path):
return cls(path, contents=b'')
return cls(path, contents='')

@classmethod
def read(cls, root, relpath):
Expand Down Expand Up @@ -194,11 +194,11 @@ def test_mergetree_existing(self):
self.Dir('a/b'),

# Existing overlapping file should be overlayed.
self.File('a/b/1', contents=b'1'),
self.File('a/b/1', contents='1'),

self.File.empty('a/b/2'),
self.Dir('b'),
self.File('b/1', contents=b'1'),
self.File('b/1', contents='1'),
self.File.empty('b/2'),
self.Dir('c'),

Expand Down Expand Up @@ -227,10 +227,10 @@ def test_mergetree_new(self):
self.Dir('a'),
self.File.empty('a/2'),
self.Dir('a/b'),
self.File('a/b/1', contents=b'1'),
self.File('a/b/1', contents='1'),
self.File.empty('a/b/2'),
self.Dir('b'),
self.File('b/1', contents=b'1'),
self.File('b/1', contents='1'),
self.File.empty('b/2'))

def test_mergetree_ignore_files(self):
Expand All @@ -246,7 +246,7 @@ def ignore(root, names):
self.File.empty('a/2'),
self.Dir('a/b'),
self.Dir('b'),
self.File('b/1', contents=b'1'),
self.File('b/1', contents='1'),
self.File.empty('b/2'))

def test_mergetree_ignore_dirs(self):
Expand All @@ -261,7 +261,7 @@ def ignore(root, names):
self.Dir('a'),
self.File.empty('a/2'),
self.Dir('b'),
self.File('b/1', contents=b'1'),
self.File('b/1', contents='1'),
self.File.empty('b/2'))

def test_mergetree_symlink(self):
Expand All @@ -272,7 +272,7 @@ def test_mergetree_symlink(self):
self.Dir('a'),
self.Symlink('a/2'),
self.Dir('a/b'),
self.File('a/b/1', contents=b'1'),
self.File('a/b/1', contents='1'),
self.File.empty('a/b/2'),

# NB: assert_tree does not follow symlinks and so does not descend into the
Expand Down Expand Up @@ -396,7 +396,7 @@ def test_rm_rf_no_such_file_not_an_error(self, file_name='./vanishing_file'):
def test_readwrite_file(self):
with temporary_dir() as td:
test_filename = os.path.join(td, 'test.out')
test_content = '3333'
test_content = b'3333'
safe_file_dump(test_filename, test_content)
self.assertEqual(read_file(test_filename), test_content)

Expand Down
2 changes: 1 addition & 1 deletion tests/python/pants_test/util/test_fileutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class FileutilTest(unittest.TestCase):
def test_atomic_copy(self):
with temporary_file() as src:
src.write(src.name)
src.write(src.name.encode('utf-8'))
src.flush()
with temporary_file() as dst:
atomic_copy(src.name, dst.name)
Expand Down
2 changes: 1 addition & 1 deletion tests/python/pants_test/util/xml_test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def xml_file(self,
android_name_attribute='android:name',
application_name_value='org.pantsbuild.example.hello.HelloWorld'):
"""Represent an .xml file (Here an AndroidManifest.xml is used)."""
with temporary_file() as fp:
with temporary_file(binary_mode=False) as fp:
fp.write(textwrap.dedent(
"""<?xml version="1.0" encoding="utf-8"?>
<{manifest} xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down

0 comments on commit 2d10d9a

Please sign in to comment.