示例#1
0
 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())
示例#2
0
 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")
示例#3
0
 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))
示例#4
0
    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")
示例#5
0
    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'))
示例#6
0
 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())
示例#7
0
 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)
示例#9
0
 def test_no_header_for_plain_diff_with_single_file(self):
     pto = fromfile(join(TESTS, "03trail_fname.patch"))
     self.assertEqual(pto.items[0].header, [])
示例#10
0
 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)
示例#12
0
 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"))
示例#13
0
 def test_single_false_on_other_file(self):
     pto3 = fromfile("03trail_fname.patch")
     self.assertFalse(pto3.can_patch("03trail_fname.from"))
示例#14
0
 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))
示例#15
0
 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)
示例#16
0
 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"))
示例#17
0
 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'====='))
示例#18
0
 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)
示例#19
0
 def test_hunk_desc(self):
     pto = fromfile(testfile('git-changed-file.diff'))
     self.assertEqual(pto.items[0].hunks[0].desc,
                      b'class JSONPluginMgr(object):')
示例#20
0
 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")
示例#21
0
 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"))
示例#22
0
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)