def test_apply_returns_false_on_failure(self): self.tmpcopy([ 'data/failing/non-empty-patch-for-empty-file.diff', 'data/failing/upload.py' ]) pto = fromfile('non-empty-patch-for-empty-file.diff') self.assertFalse(pto.apply())
def test_autofixed_parent_path(self): # [ ] exception vs return codes for error recovery # [x] separate return code when patch lib compensated the error # (implemented as warning count) pto = fromfile(join(TESTS, "data/autofix/parent-path.diff")) self.assertEqual(pto.errors, 0) self.assertEqual(pto.warnings, 2) self.assertEqual(pto.items[0].source, b"patch.py")
def test_apply_strip(self): treeroot = join(self.tmpdir, 'rootparent') shutil.copytree(join(TESTS, '06nested'), treeroot) pto = fromfile(join(TESTS, '06nested/06nested.patch')) for p in pto: p.source = b'nasty/prefix/' + p.source p.target = b'nasty/prefix/' + p.target self.assertTrue(pto.apply(strip=2, root=treeroot))
def test_diffstat(self): output = """\ updatedlg.cpp | 20 ++++++++++++++++++-- updatedlg.h | 1 + manifest.xml | 15 ++++++++------- conf.cpp | 23 +++++++++++++++++------ conf.h | 7 ++++--- 5 files changed, 48 insertions(+), 18 deletions(-), +1203 bytes""" pto = fromfile(join(TESTS, "01uni_multi/01uni_multi.patch")) self.assertEqual(pto.diffstat(), output, "Output doesn't match")
def test_revert(self): def get_file_content(filename): with open(filename, 'rb') as f: return f.read() self.tmpcopy(['03trail_fname.patch', '03trail_fname.from']) pto = fromfile('03trail_fname.patch') self.assertTrue(pto.apply()) self.assertNotEqual( get_file_content(self.tmpdir + '/03trail_fname.from'), get_file_content(TESTS + '/03trail_fname.from')) self.assertTrue(pto.revert()) self.assertEqual(get_file_content(self.tmpdir + '/03trail_fname.from'), get_file_content(TESTS + '/03trail_fname.from'))
def test_apply_returns_true_on_success(self): self.tmpcopy(['03trail_fname.patch', '03trail_fname.from']) pto = fromfile('03trail_fname.patch') self.assertTrue(pto.apply())
def test_can_patch_single_source(self): pto2 = fromfile("02uni_newline.patch") self.assertTrue(pto2.can_patch(b"02uni_newline.from"))
def test(self): pto = fromfile(join(TESTDATA, file_name)) self.assertEqual(pto.type, patchtype)
def test_no_header_for_plain_diff_with_single_file(self): pto = fromfile(join(TESTS, "03trail_fname.patch")) self.assertEqual(pto.items[0].header, [])
def test_can_patch_checks_source_filename_even_if_target_can_be_patched( self): pto2 = fromfile("04can_patch.patch") self.assertFalse(pto2.can_patch("04can_patch.to"))
def test_svn_detected(self): pto = fromfile(join(TESTS, "01uni_multi/01uni_multi.patch")) self.assertEqual(pto.type, PatchSetTypes.SVN)
def test_multiline_false_on_other_file(self): pto = fromfile("01uni_multi/01uni_multi.patch") os.chdir(join(TESTS, "01uni_multi")) self.assertFalse(pto.can_patch(b"updatedlg.cpp"))
def test_single_false_on_other_file(self): pto3 = fromfile("03trail_fname.patch") self.assertFalse(pto3.can_patch("03trail_fname.from"))
def test_apply_root(self): treeroot = join(self.tmpdir, 'rootparent') shutil.copytree(join(TESTS, '06nested'), treeroot) pto = fromfile(join(TESTS, '06nested/06nested.patch')) self.assertTrue(pto.apply(root=treeroot))
def test_fromfile(self): pst = fromfile(join(TESTS, "01uni_multi/01uni_multi.patch")) self.assertNotEqual(pst, False) self.assertEqual(len(pst), 5) ps2 = fromfile(testfile("failing/not-a-patch.log")) self.assertFalse(ps2)
def test_patched_multipatch(self): pto = fromfile("01uni_multi/01uni_multi.patch") os.chdir(join(TESTS, "01uni_multi", "[result]")) self.assertTrue(pto.can_patch(b"updatedlg.cpp"))
def test_header_for_second_file_in_svn_diff(self): pto = fromfile(join(TESTS, "01uni_multi/01uni_multi.patch")) self.assertEqual(pto.items[1].header[0], b'Index: updatedlg.h\r\n') self.assertTrue(pto.items[1].header[1].startswith(b'====='))
def test_autofixed_stripped_trailing_whitespace(self): pto = fromfile( join(TESTS, "data/autofix/stripped-trailing-whitespace.diff")) self.assertEqual(pto.errors, 0) self.assertEqual(pto.warnings, 4)
def test_hunk_desc(self): pto = fromfile(testfile('git-changed-file.diff')) self.assertEqual(pto.items[0].hunks[0].desc, b'class JSONPluginMgr(object):')
def test_autofixed_absolute_path(self): pto = fromfile(join(TESTS, "data/autofix/absolute-path.diff")) self.assertEqual(pto.errors, 0) self.assertEqual(pto.warnings, 2) self.assertEqual(pto.items[0].source, b"winnt/tests/run_tests.py")
def test_can_patch_fails_on_target_file(self): pto3 = fromfile("03trail_fname.patch") self.assertEqual(None, pto3.can_patch(b"03trail_fname.to")) self.assertEqual(None, pto3.can_patch(b"not_in_source.also"))
def main(): from optparse import OptionParser from os.path import exists import sys opt = OptionParser(usage="1. %prog [options] unified.diff\n" " 2. %prog [options] http://host/patch\n" " 3. %prog [options] -- < unified.diff", version="python-patch %s" % __version__) opt.add_option("-q", "--quiet", action="store_const", dest="verbosity", const=0, help="print only warnings and errors", default=1) opt.add_option("-v", "--verbose", action="count", dest="verbosity", default=0, help="verbosity level (use up to 3 times)") opt.add_option("--diffstat", action="store_true", dest="diffstat", help="print diffstat and exit") opt.add_option("-d", "--directory", metavar='DIR', help="specify root directory for applying patch") opt.add_option("-p", "--strip", type="int", metavar='N', default=0, help="strip N path components from filenames") opt.add_option("--revert", action="store_true", help="apply patch in reverse order (unpatch)") (options, args) = opt.parse_args() if not args and sys.argv[-1:] != ['--']: opt.print_version() opt.print_help() sys.exit() readstdin = (sys.argv[-1:] == ['--'] and not args) setup_logging(options.verbosity) if readstdin: patch = PatchSet(sys.stdin) else: patchfile = args[0] urltest = patchfile.split(':')[0] if ':' in patchfile and urltest.isalpha() and len(urltest) > 1: # one char before : is a windows drive letter patch = fromurl(patchfile) else: if not exists(patchfile) or not isfile(patchfile): sys.exit("patch file does not exist - %s" % patchfile) patch = fromfile(patchfile) if options.diffstat: print(patch.diffstat()) sys.exit(0) if options.revert: patch.revert(options.strip, root=options.directory) or sys.exit(-1) else: patch.apply(options.strip, root=options.directory) or sys.exit(-1)