Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcut for building patch sets from strings #34

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,47 @@
if not PY2:
unicode = str

TESTS_DIR = os.path.dirname(os.path.realpath(__file__))
SAMPLES_DIR = os.path.join(TESTS_DIR, "samples")

class TestPatchSet(unittest.TestCase):
"""Tests for the PatchSet class."""

samples_dir = SAMPLES_DIR

def setUp(self):
super(TestPatchSet, self).setUp()
self.sample_filename = os.path.join(self.samples_dir, 'sample0.diff')
with open(self.sample_filename) as diff_file:
self.sample_text = diff_file.read()

def test_from_filename(self):
"""Test the PatchSet.from_filename classmethod"""
with open(self.sample_filename) as diff_file:
expected = PatchSet(diff_file)
actual = PatchSet.from_filename(self.sample_filename)
self.assertEqual(expected, actual)

def test_from_string(self):
"""Test the PatchSet.from_string classmethod"""
with open(self.sample_filename) as diff_file:
expected = PatchSet(diff_file)
actual = PatchSet.from_string(self.sample_text)
self.assertEqual(expected, actual)


class TestUnidiffParser(unittest.TestCase):
"""Tests for Unified Diff Parser."""

samples_dir = SAMPLES_DIR

def setUp(self):
super(TestUnidiffParser, self).setUp()
self.samples_dir = os.path.dirname(os.path.realpath(__file__))
self.sample_file = os.path.join(
self.samples_dir, 'samples/sample0.diff')
self.sample_bad_file = os.path.join(
self.samples_dir, 'samples/sample1.diff')
self.sample_file = os.path.join(self.samples_dir, 'sample0.diff')
self.sample_bad_file = os.path.join(self.samples_dir, 'sample1.diff')

def test_missing_encoding(self):
utf8_file = os.path.join(self.samples_dir, 'samples/sample3.diff')
utf8_file = os.path.join(self.samples_dir, 'sample3.diff')
# read bytes
with open(utf8_file, 'rb') as diff_file:
if PY2:
Expand All @@ -59,7 +87,7 @@ def test_missing_encoding(self):
self.assertRaises(TypeError, PatchSet, diff_file)

def test_encoding_param(self):
utf8_file = os.path.join(self.samples_dir, 'samples/sample3.diff')
utf8_file = os.path.join(self.samples_dir, 'sample3.diff')
with open(utf8_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')

Expand All @@ -69,7 +97,7 @@ def test_encoding_param(self):
self.assertEqual(added_unicode_line.value, 'holá mundo!\n')

def test_no_newline_at_end_of_file(self):
utf8_file = os.path.join(self.samples_dir, 'samples/sample3.diff')
utf8_file = os.path.join(self.samples_dir, 'sample3.diff')
with open(utf8_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')

Expand All @@ -83,7 +111,7 @@ def test_no_newline_at_end_of_file(self):
self.assertEqual(added_unicode_line.value, ' No newline at end of file\n')

def test_preserve_dos_line_endings(self):
utf8_file = os.path.join(self.samples_dir, 'samples/sample4.diff')
utf8_file = os.path.join(self.samples_dir, 'sample4.diff')
with open(utf8_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')

Expand Down Expand Up @@ -210,12 +238,12 @@ def test_diff_lines_linenos(self):
class TestVCSSamples(unittest.TestCase):
"""Tests for real examples from VCS."""

samples_dir = SAMPLES_DIR
samples = ['bzr.diff', 'git.diff', 'hg.diff', 'svn.diff']

def test_samples(self):
tests_dir = os.path.dirname(os.path.realpath(__file__))
for fname in self.samples:
file_path = os.path.join(tests_dir, 'samples', fname)
file_path = os.path.join(self.samples_dir, fname)
with codecs.open(file_path, 'r', encoding='utf-8') as diff_file:
res = PatchSet(diff_file)

Expand Down
5 changes: 5 additions & 0 deletions unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import codecs
import sys
from io import StringIO

from unidiff.constants import (
DEFAULT_ENCODING,
Expand Down Expand Up @@ -344,6 +345,10 @@ def from_filename(cls, filename, encoding=DEFAULT_ENCODING, errors=None):
instance = cls(f)
return instance

@classmethod
def from_string(cls, s, *args, **kwargs):
return cls(StringIO(unicode(s)), *args, **kwargs)

@property
def added_files(self):
"""Return patch added files as a list."""
Expand Down