Skip to content

Update test_docs.py #773

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

Merged
merged 2 commits into from
Jul 15, 2018
Merged
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
33 changes: 16 additions & 17 deletions git/test/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from git.test.lib import TestBase
from git.test.lib.helper import with_rw_directory

import os.path as osp
import os.path


class Tutorials(TestBase):
Expand All @@ -25,7 +25,6 @@ def tearDown(self):
def test_init_repo_object(self, rw_dir):
# [1-test_init_repo_object]
from git import Repo
join = osp.join

# rorepo is a Repo instance pointing to the git-python repository.
# For all you know, the first argument to Repo is a path to the repository
Expand All @@ -35,7 +34,7 @@ def test_init_repo_object(self, rw_dir):
# ![1-test_init_repo_object]

# [2-test_init_repo_object]
bare_repo = Repo.init(join(rw_dir, 'bare-repo'), bare=True)
bare_repo = Repo.init(os.path.join(rw_dir, 'bare-repo'), bare=True)
assert bare_repo.bare
# ![2-test_init_repo_object]

Expand All @@ -52,19 +51,19 @@ def test_init_repo_object(self, rw_dir):
# ![4-test_init_repo_object]

# [5-test_init_repo_object]
cloned_repo = repo.clone(join(rw_dir, 'to/this/path'))
cloned_repo = repo.clone(os.path.join(rw_dir, 'to/this/path'))
assert cloned_repo.__class__ is Repo # clone an existing repository
assert Repo.init(join(rw_dir, 'path/for/new/repo')).__class__ is Repo
assert Repo.init(os.path.join(rw_dir, 'path/for/new/repo')).__class__ is Repo
# ![5-test_init_repo_object]

# [6-test_init_repo_object]
with open(join(rw_dir, 'repo.tar'), 'wb') as fp:
with open(os.path.join(rw_dir, 'repo.tar'), 'wb') as fp:
repo.archive(fp)
# ![6-test_init_repo_object]

# repository paths
# [7-test_init_repo_object]
assert osp.isdir(cloned_repo.working_tree_dir) # directory with your work files
assert os.path.isdir(cloned_repo.working_tree_dir) # directory with your work files
assert cloned_repo.git_dir.startswith(cloned_repo.working_tree_dir) # directory containing the git repository
assert bare_repo.working_tree_dir is None # bare repositories have no working tree
# ![7-test_init_repo_object]
Expand Down Expand Up @@ -148,7 +147,7 @@ def update(self, op_code, cur_count, max_count=None, message=''):
self.assertEqual(new_branch.checkout(), cloned_repo.active_branch) # checking out branch adjusts the wtree
self.assertEqual(new_branch.commit, past.commit) # Now the past is checked out

new_file_path = osp.join(cloned_repo.working_tree_dir, 'my-new-file')
new_file_path = os.path.join(cloned_repo.working_tree_dir, 'my-new-file')
open(new_file_path, 'wb').close() # create new file in working tree
cloned_repo.index.add([new_file_path]) # add it to the index
# Commit the changes to deviate masters history
Expand All @@ -164,7 +163,7 @@ def update(self, op_code, cur_count, max_count=None, message=''):
# now new_branch is ahead of master, which probably should be checked out and reset softly.
# note that all these operations didn't touch the working tree, as we managed it ourselves.
# This definitely requires you to know what you are doing :) !
assert osp.basename(new_file_path) in new_branch.commit.tree # new file is now in tree
assert os.path.basename(new_file_path) in new_branch.commit.tree # new file is now in tree
master.commit = new_branch.commit # let master point to most recent commit
cloned_repo.head.reference = master # we adjusted just the reference, not the working tree or index
# ![13-test_init_repo_object]
Expand Down Expand Up @@ -194,7 +193,7 @@ def update(self, op_code, cur_count, max_count=None, message=''):
def test_references_and_objects(self, rw_dir):
# [1-test_references_and_objects]
import git
repo = git.Repo.clone_from(self._small_repo_url(), osp.join(rw_dir, 'repo'), branch='master')
repo = git.Repo.clone_from(self._small_repo_url(), os.path.join(rw_dir, 'repo'), branch='master')

heads = repo.heads
master = heads.master # lists can be accessed by name for convenience
Expand Down Expand Up @@ -266,7 +265,7 @@ def test_references_and_objects(self, rw_dir):

# [11-test_references_and_objects]
hct.blobs[0].data_stream.read() # stream object to read data from
hct.blobs[0].stream_data(open(osp.join(rw_dir, 'blob_data'), 'wb')) # write data to given stream
hct.blobs[0].stream_data(open(os.path.join(rw_dir, 'blob_data'), 'wb')) # write data to given stream
# ![11-test_references_and_objects]

# [12-test_references_and_objects]
Expand Down Expand Up @@ -352,11 +351,11 @@ def test_references_and_objects(self, rw_dir):
# Access blob objects
for (path, stage), entry in index.entries.items(): # @UnusedVariable
pass
new_file_path = osp.join(repo.working_tree_dir, 'new-file-name')
new_file_path = os.path.join(repo.working_tree_dir, 'new-file-name')
open(new_file_path, 'w').close()
index.add([new_file_path]) # add a new file to the index
index.remove(['LICENSE']) # remove an existing one
assert osp.isfile(osp.join(repo.working_tree_dir, 'LICENSE')) # working tree is untouched
assert os.path.isfile(os.path.join(repo.working_tree_dir, 'LICENSE')) # working tree is untouched

self.assertEqual(index.commit("my commit message").type, 'commit') # commit changed index
repo.active_branch.commit = repo.commit('HEAD~1') # forget last commit
Expand All @@ -375,11 +374,11 @@ def test_references_and_objects(self, rw_dir):
# merge two trees three-way into memory
merge_index = IndexFile.from_tree(repo, 'HEAD~10', 'HEAD', repo.merge_base('HEAD~10', 'HEAD'))
# and persist it
merge_index.write(osp.join(rw_dir, 'merged_index'))
merge_index.write(os.path.join(rw_dir, 'merged_index'))
# ![24-test_references_and_objects]

# [25-test_references_and_objects]
empty_repo = git.Repo.init(osp.join(rw_dir, 'empty'))
empty_repo = git.Repo.init(os.path.join(rw_dir, 'empty'))
origin = empty_repo.create_remote('origin', repo.remotes.origin.url)
assert origin.exists()
assert origin == empty_repo.remotes.origin == empty_repo.remotes['origin']
Expand Down Expand Up @@ -482,8 +481,8 @@ def test_submodules(self):
def test_add_file_and_commit(self, rw_dir):
import git

repo_dir = osp.join(rw_dir, 'my-new-repo')
file_name = osp.join(repo_dir, 'new-file')
repo_dir = os.path.join(rw_dir, 'my-new-repo')
file_name = os.path.join(repo_dir, 'new-file')

r = git.Repo.init(repo_dir)
# This function just creates an empty file ...
Expand Down