def assert_roundtrip_golden(self, env, make_view_from_repo_path, golden_file_name=None, extra_invariant_funcs=()): # .write() / .apply() cycle leaves repository (and index in particular) # unchanged def invariant(): diff = env.cmd(["git", "diff"]).stdout_output cached = env.cmd(["git", "diff", "--cached"]).stdout_output diffs = """\ git diff {0} git diff --cached {1} """.format( diff, cached ) if extra_invariant_funcs: extra = "\n".join(str(func()) for func in extra_invariant_funcs) return "\n".join([diffs, extra, ""]) else: return diffs before = invariant() path = trim(env.cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") view = make_view_from_repo_path(path) out = self.make_temp_dir() view.write(env, out) listing = list_tree.ls_tree(out) listing = listing.replace(path, "<repo>") if golden_file_name is not None: self.assert_golden_file(listing, golden_file_name) view.apply(env, out) after = invariant() self.assert_equal_golden(before, after)
def assert_write_golden(self, env, make_view_from_repo_path, golden_file_name): path = trim(env.cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") view = make_view_from_repo_path(path) out = self.make_temp_dir() view.write(env, out) listing = list_tree.ls_tree(out) listing = listing.replace(path, "<repo>") self.assert_golden_file(listing, golden_file_name)
def assert_write_golden(self, env, make_view_from_repo_path, golden_file_name): path = trim(env.cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") view = make_view_from_repo_path(path) out = self.make_temp_dir() view.write(env, out) listing = list_tree.ls_tree(out) listing = listing.replace(path, '<repo>') self.assert_golden_file(listing, golden_file_name)
def check(self, golden_dir, env, prefix=""): repo_path = trim(env.read_cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") self.assert_golden(self.write_diffs(env), os.path.join(golden_dir, "unmelded")) # Modified or untracked files in the working tree working_tree_sources = [ "untracked", "partially_staged", "modified", "modified_staged", "new_staged", "deleted_from_index", "rename_after", ] # Everything in the index that is also modified in working tree # (including new or untracked) -- and therefore has something that # could be staged index_destinations = [ "changed_type", "modified", "modified_staged", "new_staged", "partially_staged", "rename_after", ] def add_prefix(paths): return [prefix + path for path in paths] working_tree_sources = add_prefix(working_tree_sources) index_destinations = add_prefix(index_destinations) # Write a fake meld command that will edit every file in the index to # have the same content as its file name. Files that are fully staged # or unmodified won't be melded so are omitted. edited = self.make_temp_dir() for name in working_tree_sources: content = name + " edited by meld\n" path = os.path.join(edited, name) env.cmd(["mkdir", "-p", os.path.dirname(path)]) env.cmd(write_file_cmd(path, content)) meld_env, get_recorded_listings = self.write_fake_meld(env, edited) work_dir = self.make_temp_dir() work_area = git_meld_index.WorkArea(meld_env, work_dir) left_view = git_meld_index.make_view("working:" + repo_path) right_view = git_meld_index.make_view("index:" + repo_path) work_area.meld(left_view, right_view, tool="meld") left, right = get_recorded_listings() self.assertEqual(sorted(left), sorted(working_tree_sources)) self.assertEqual(sorted(right), sorted(index_destinations)) self.assert_golden(self.write_diffs(env), os.path.join(golden_dir, "melded"))
def check(self, golden_dir, env, prefix=""): repo_path = trim(env.read_cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") self.assert_golden(self.write_diffs(env), os.path.join(golden_dir, "unmelded")) # Modified or untracked files in the working tree working_tree_sources = [ "untracked", "partially_staged", "modified", "modified_staged", "new_staged", "deleted_from_index", "rename_after" ] # Everything in the index that is also modified in working tree # (including new or untracked) -- and therefore has something that # could be staged index_destinations = [ "changed_type", "modified", "modified_staged", "new_staged", "partially_staged", "rename_after" ] def add_prefix(paths): return [prefix + path for path in paths] working_tree_sources = add_prefix(working_tree_sources) index_destinations = add_prefix(index_destinations) # Write a fake meld command that will edit every file in the index to # have the same content as its file name. Files that are fully staged # or unmodified won't be melded so are omitted. edited = self.make_temp_dir() for name in working_tree_sources: content = name + " edited by meld\n" path = os.path.join(edited, name) env.cmd(["mkdir", "-p", os.path.dirname(path)]) env.cmd(write_file_cmd(path, content)) meld_env, get_recorded_listings = self.write_fake_meld(env, edited) work_dir = self.make_temp_dir() work_area = git_meld_index.WorkArea(meld_env, work_dir) left_view = git_meld_index.make_view("working:" + repo_path) right_view = git_meld_index.make_view("index:" + repo_path) work_area.meld(left_view, right_view, tool="meld") left, right = get_recorded_listings() self.assertEqual(sorted(left), sorted(working_tree_sources)) self.assertEqual(sorted(right), sorted(index_destinations)) self.assert_golden(self.write_diffs(env), os.path.join(golden_dir, "melded"))
def test_roundtrip_submodule(self): env = self.make_env() submodule_repo_env = self.make_env() repo = Repo(env) repo.add_unmodified("file", "content\n") submodule_repo = Repo(submodule_repo_env) submodule_repo.add_unmodified("file", "content\n") submodule_path = trim(submodule_repo_env.cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") env.cmd(["git", "submodule", "add", submodule_path, "sub"]) def submodule_status(): return env.cmd(["git", "submodule", "status"]).stdout_output self.assert_roundtrip_golden( env, self.make_view, "test_write_index_or_head_in_progress_submodule", extra_invariant_funcs=(submodule_status,), )
def test_roundtrip_submodule(self): env = self.make_env() submodule_repo_env = self.make_env() repo = Repo(env) repo.add_unmodified("file", "content\n") submodule_repo = Repo(submodule_repo_env) submodule_repo.add_unmodified("file", "content\n") submodule_path = trim(submodule_repo_env.cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") env.cmd(["git", "submodule", "add", submodule_path, "sub"]) def submodule_status(): return env.cmd(["git", "submodule", "status"]).stdout_output self.assert_roundtrip_golden( env, self.make_view, "test_write_index_or_head_in_progress_submodule", extra_invariant_funcs=(submodule_status, ))
def assert_roundtrip_golden(self, env, make_view_from_repo_path, golden_file_name=None, extra_invariant_funcs=()): # .write() / .apply() cycle leaves repository (and index in particular) # unchanged def invariant(): diff = env.cmd(["git", "diff"]).stdout_output cached = env.cmd(["git", "diff", "--cached"]).stdout_output diffs = """\ git diff {0} git diff --cached {1} """.format(diff, cached) if extra_invariant_funcs: extra = "\n".join( str(func()) for func in extra_invariant_funcs) return "\n".join([diffs, extra, ""]) else: return diffs before = invariant() path = trim(env.cmd(["readlink", "-e", "."]).stdout_output, suffix="\n") view = make_view_from_repo_path(path) out = self.make_temp_dir() view.write(env, out) listing = list_tree.ls_tree(out) listing = listing.replace(path, '<repo>') if golden_file_name is not None: self.assert_golden_file(listing, golden_file_name) view.apply(env, out) after = invariant() self.assert_equal_golden(before, after)