📄 diff_tests.py
字号:
#!/usr/bin/env python## diff_tests.py: some basic diff tests## Subversion is a tool for revision control. # See http://subversion.tigris.org for more information.# # ====================================================================# Copyright (c) 2000-2006 CollabNet. All rights reserved.## This software is licensed as described in the file COPYING, which# you should have received as part of this distribution. The terms# are also available at http://subversion.tigris.org/license-1.html.# If newer versions of this license are posted there, you may use a# newer version instead, at your option.######################################################################## General modulesimport string, sys, re, os.path# Our testing moduleimport svntestfrom svntest import SVNAnyOutput# (abbreviation)Skip = svntest.testcase.SkipXFail = svntest.testcase.XFailItem = svntest.wc.StateItem####################################################################### Diff output checker## Looks for the correct filenames and a suitable number of +/- lines# depending on whether this is an addition, modification or deletion.def check_diff_output(diff_output, name, diff_type): "check diff output"# On Windows, diffs still display / rather than \ in paths if svntest.main.windows == 1: name = string.replace(name, '\\', '/') i_re = re.compile('^Index:') d_re = re.compile('^Index: (\\./)?' + name) p_re = re.compile('^--- (\\./)?' + name) add_re = re.compile('^\\+') sub_re = re.compile('^-') i = 0 while i < len(diff_output) - 4: # identify a possible diff if (d_re.match(diff_output[i]) and p_re.match(diff_output[i+2])): # count lines added and deleted i += 4 add_lines = 0 sub_lines = 0 while i < len(diff_output) and not i_re.match(diff_output[i]): if add_re.match(diff_output[i][0]): add_lines += 1 if sub_re.match(diff_output[i][0]): sub_lines += 1 i += 1 #print "add:", add_lines #print "sub:", sub_lines # check if this looks like the right sort of diff if add_lines > 0 and sub_lines == 0 and diff_type == 'A': return 0 if sub_lines > 0 and add_lines == 0 and diff_type == 'D': return 0 if add_lines > 0 and sub_lines > 0 and diff_type == 'M': return 0 else: i += 1 # no suitable diff found return 1def count_diff_output(diff_output): "count the number of file diffs in the output" i_re = re.compile('Index:') diff_count = 0 i = 0 while i < len(diff_output) - 4: if i_re.match(diff_output[i]): i += 4 diff_count += 1 else: i += 1 return diff_countdef verify_expected_output(diff_output, expected): "verify given line exists in diff output" for line in diff_output: if line.find(expected) != -1: break else: raise svntest.Failuredef verify_excluded_output(diff_output, excluded): "verify given line does not exist in diff output as diff line" for line in diff_output: if re.match("^(\\+|-)%s" % re.escape(excluded), line): print 'Sought: %s' % excluded print 'Found: %s' % line raise svntest.Failuredef extract_diff_path(line): l2 = line[(line.find("(")+1):] l3 = l2[0:(l2.find(")"))] return l3####################################################################### diff on a repository subset and check the outputdef diff_check_repo_subset(wc_dir, repo_subset, check_fn, do_diff_r): "diff and check for part of the repository" was_cwd = os.getcwd() os.chdir(wc_dir) try: diff_output, err_output = svntest.main.run_svn(None, 'diff', repo_subset) if check_fn(diff_output): return 1 if do_diff_r: diff_output, err_output = svntest.main.run_svn(None, 'diff', '-r', 'HEAD', repo_subset) if check_fn(diff_output): return 1 finally: os.chdir(was_cwd) return 0####################################################################### Changes makers and change checkersdef update_a_file(): "update a file" open(os.path.join('A', 'B', 'E', 'alpha'), 'w').write("new atext") # svntest.main.file_append(, "new atext") return 0def check_update_a_file(diff_output): "check diff for update a file" return check_diff_output(diff_output, os.path.join('A', 'B', 'E', 'alpha'), 'M')def diff_check_update_a_file_repo_subset(wc_dir): "diff and check update a file for a repository subset" repo_subset = os.path.join('A', 'B') if diff_check_repo_subset(wc_dir, repo_subset, check_update_a_file, 1): return 1 repo_subset = os.path.join('A', 'B', 'E', 'alpha') if diff_check_repo_subset(wc_dir, repo_subset, check_update_a_file, 1): return 1 return 0 #----------------------------------------------------------------------def add_a_file(): "add a file" svntest.main.file_append(os.path.join('A', 'B', 'E', 'theta'), "theta") svntest.main.run_svn(None, 'add', os.path.join('A', 'B', 'E', 'theta')) return 0def check_add_a_file(diff_output): "check diff for add a file" return check_diff_output(diff_output, os.path.join('A', 'B', 'E', 'theta'), 'A')def check_add_a_file_reverse(diff_output): "check diff for add a file" return check_diff_output(diff_output, os.path.join('A', 'B', 'E', 'theta'), 'D')def diff_check_add_a_file_repo_subset(wc_dir): "diff and check add a file for a repository subset" repo_subset = os.path.join('A', 'B') if diff_check_repo_subset(wc_dir, repo_subset, check_add_a_file, 1): return 1 repo_subset = os.path.join('A', 'B', 'E', 'theta') ### TODO: diff -r HEAD doesn't work for added file if diff_check_repo_subset(wc_dir, repo_subset, check_add_a_file, 0): return 1def update_added_file(): svntest.main.file_append(os.path.join('A', 'B', 'E', 'theta'), "net ttext") "update added file" return 0def check_update_added_file(diff_output): "check diff for update of added file" return check_diff_output(diff_output, os.path.join('A', 'B', 'E', 'theta'), 'M')#----------------------------------------------------------------------def add_a_file_in_a_subdir(): "add a file in a subdir" os.mkdir(os.path.join('A', 'B', 'T')) svntest.main.run_svn(None, 'add', os.path.join('A', 'B', 'T')) svntest.main.file_append(os.path.join('A', 'B', 'T', 'phi'), "phi") svntest.main.run_svn(None, 'add', os.path.join('A', 'B', 'T', 'phi')) return 0def check_add_a_file_in_a_subdir(diff_output): "check diff for add a file in a subdir" return check_diff_output(diff_output, os.path.join('A', 'B', 'T', 'phi'), 'A')def check_add_a_file_in_a_subdir_reverse(diff_output): "check diff for add a file in a subdir" return check_diff_output(diff_output, os.path.join('A', 'B', 'T', 'phi'), 'D')def diff_check_add_a_file_in_a_subdir_repo_subset(wc_dir): "diff and check add a file in a subdir for a repository subset" repo_subset = os.path.join('A', 'B', 'T') ### TODO: diff -r HEAD doesn't work for added subdir if diff_check_repo_subset(wc_dir, repo_subset, check_add_a_file_in_a_subdir, 0): return 1 repo_subset = os.path.join('A', 'B', 'T', 'phi') ### TODO: diff -r HEAD doesn't work for added file in subdir if diff_check_repo_subset(wc_dir, repo_subset, check_add_a_file_in_a_subdir, 0): return 1#----------------------------------------------------------------------def replace_a_file(): "replace a file" svntest.main.run_svn(None, 'rm', os.path.join('A', 'D', 'G', 'rho')) svntest.main.file_append(os.path.join('A', 'D', 'G', 'rho'), "new rho") svntest.main.run_svn(None, 'add', os.path.join('A', 'D', 'G', 'rho')) return 0def check_replace_a_file(diff_output): "check diff for replace a file" return check_diff_output(diff_output, os.path.join('A', 'D', 'G', 'rho'), 'M') #----------------------------------------------------------------------def update_three_files(): "update three files" open(os.path.join('A', 'D', 'gamma'), 'w').write("new gamma") open(os.path.join('A', 'D', 'G', 'tau'), 'w').write("new tau") open(os.path.join('A', 'D', 'H', 'psi'), 'w').write("new psi") return 0def check_update_three_files(diff_output): "check update three files" if check_diff_output(diff_output, os.path.join('A', 'D', 'gamma'), 'M'): return 1 if check_diff_output(diff_output, os.path.join('A', 'D', 'G', 'tau'), 'M'): return 1 if check_diff_output(diff_output, os.path.join('A', 'D', 'H', 'psi'), 'M'): return 1 return 0 ####################################################################### make a change, check the diff, commit the change, check the diffdef change_diff_commit_diff(wc_dir, revision, change_fn, check_fn): "make a change, diff, commit, update and diff again" was_cwd = os.getcwd() os.chdir(wc_dir) try: svntest.main.run_svn(None, 'up', '-r', 'HEAD') change_fn() # diff without revision doesn't use an editor diff_output, err_output = svntest.main.run_svn(None, 'diff') if check_fn(diff_output): raise svntest.Failure # diff with revision runs an editor diff_output, err_output = svntest.main.run_svn(None, 'diff', '-r', 'HEAD') if check_fn(diff_output): raise svntest.Failure svntest.main.run_svn(None, 'ci', '-m', 'log msg') svntest.main.run_svn(None, 'up') diff_output, err_output = svntest.main.run_svn(None, 'diff', '-r', revision) if check_fn(diff_output): raise svntest.Failure finally: os.chdir(was_cwd)####################################################################### check the diffdef just_diff(wc_dir, rev_check, check_fn): "update and check that the given diff is seen" was_cwd = os.getcwd() os.chdir(wc_dir) try: diff_output, err_output = svntest.main.run_svn(None, 'diff', '-r', rev_check) if check_fn(diff_output): raise svntest.Failure finally: os.chdir(was_cwd)####################################################################### update, check the diffdef update_diff(wc_dir, rev_up, rev_check, check_fn): "update and check that the given diff is seen" was_cwd = os.getcwd() os.chdir(wc_dir) try: svntest.main.run_svn(None, 'up', '-r', rev_up) finally: os.chdir(was_cwd) just_diff(wc_dir, rev_check, check_fn)####################################################################### check a pure repository rev1:rev2 diffdef repo_diff(wc_dir, rev1, rev2, check_fn): "check that the given pure repository diff is seen"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -