Skip to content

Commit 0d8b41b

Browse files
committed
Added test for creating a new working files
1 parent 9ff8cbe commit 0d8b41b

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

app.py

+1-19
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,7 @@ def branch_edit_add(branch, path=None):
184184
b.checkout()
185185
c = r.commit()
186186

187-
file_path = join((path or '').rstrip('/'), request.form['path'])
188-
full_path = join(r.working_dir, file_path)
189-
190-
head, dirs = split(file_path)[0], []
191-
192-
while head:
193-
head, dir = split(head)
194-
dirs.insert(0, dir)
195-
196-
if '..' in dirs:
197-
raise Exception('None of that now')
198-
199-
stuff = []
200-
201-
for i in range(len(dirs)):
202-
dir_path = join(r.working_dir, '/'.join(dirs[:i+1]))
203-
204-
if not isdir(dir_path):
205-
mkdir(dir_path)
187+
file_path, full_path = bizarro.repo.make_working_file(r, path, request.form['path'])
206188

207189
if not exists(full_path):
208190
with open(full_path, 'w') as file:

bizarro/repo.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,53 @@
1+
import os, logging
2+
from os.path import join, split
3+
14
def start_branch(clone, default_branch_name, new_branch_name):
25
''' Start a new repository branch, push it to origin and return it.
36
'''
47
clone.remotes.origin.fetch()
58

69
if 'origin/' + new_branch_name in clone.refs:
7-
start_point = clone.refs['origin/' + new_branch_name]
10+
start_point = clone.refs['origin/' + new_branch_name].commit
811
else:
9-
start_point = clone.branches[default_branch_name]
12+
start_point = clone.branches[default_branch_name].commit
1013

14+
logging.debug('start_branch() start_point is %s' % repr(start_point))
1115
branch = clone.create_head(new_branch_name, commit=start_point)
1216
clone.remotes.origin.push(new_branch_name)
1317

1418
return branch
1519

20+
def make_working_file(clone, dir, path):
21+
''' Create a new working file, return its local git and real absolute paths.
22+
'''
23+
repo_path = join((dir or '').rstrip('/'), path)
24+
real_path = join(clone.working_dir, repo_path)
25+
26+
#
27+
# Build a full directory path.
28+
#
29+
head, dirs = split(repo_path)[0], []
30+
31+
while head:
32+
head, dir = split(head)
33+
dirs.insert(0, dir)
34+
35+
if '..' in dirs:
36+
raise Exception('None of that now')
37+
38+
#
39+
# Create directory tree.
40+
#
41+
stuff = []
42+
43+
for i in range(len(dirs)):
44+
dir_path = join(clone.working_dir, os.sep.join(dirs[:i+1]))
45+
46+
if not isdir(dir_path):
47+
mkdir(dir_path)
48+
49+
return repo_path, real_path
50+
1651
def save_working_file(clone, path, message, base_sha):
1752
''' Save a file in the working dir and push it to origin.
1853
'''

test.py

+44-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from uuid import uuid4
77

88
from git import Repo
9-
import bizarro
9+
import bizarro, jekyll
1010

1111
#
1212
# Tarball of a single-commit Git repo with files index.md and sub/index.md
@@ -35,10 +35,11 @@ def test_repo_features(self):
3535
self.assertEqual(set(branch_names), set(['master', 'title', 'body']))
3636

3737
def test_start_branch(self):
38-
branch1 = bizarro.repo.start_branch(self.clone1, 'master', 'hello')
38+
name = str(uuid4())
39+
branch1 = bizarro.repo.start_branch(self.clone1, 'master', name)
3940

40-
self.assertTrue('hello' in self.clone1.branches)
41-
self.assertTrue('hello' in self.origin.branches)
41+
self.assertTrue(name in self.clone1.branches)
42+
self.assertTrue(name in self.origin.branches)
4243

4344
#
4445
# Make a change to the branch and push it.
@@ -54,12 +55,49 @@ def test_start_branch(self):
5455
#
5556
# See if the branch made it to clone 2
5657
#
57-
branch2 = bizarro.repo.start_branch(self.clone2, 'master', 'hello')
58+
branch2 = bizarro.repo.start_branch(self.clone2, 'master', name)
5859

59-
self.assertTrue('hello' in self.clone2.branches)
60+
self.assertTrue(name in self.clone2.branches)
6061
self.assertEquals(branch2.commit.hexsha, branch1.commit.hexsha)
6162
self.assertEquals(branch2.commit.message, message)
6263

64+
def test_new_file(self):
65+
name = str(uuid4())
66+
branch1 = bizarro.repo.start_branch(self.clone1, 'master', name)
67+
68+
self.assertTrue(name in self.clone1.branches)
69+
self.assertTrue(name in self.origin.branches)
70+
71+
#
72+
# Make a new file in the branch and push it.
73+
#
74+
branch1.checkout()
75+
message = str(uuid4())
76+
77+
repo_path, real_path = bizarro.repo.make_working_file(self.clone1, '', 'hello.md')
78+
79+
with open(real_path, 'w') as file:
80+
jekyll.dump_jekyll_doc(dict(title='Hello'), 'Hello hello.', file)
81+
82+
bizarro.repo.save_working_file(self.clone1, 'hello.md', message, branch1.commit.hexsha)
83+
84+
#
85+
# See if the branch made it to clone 2
86+
#
87+
branch2 = bizarro.repo.start_branch(self.clone2, 'master', name)
88+
89+
self.assertTrue(name in self.clone2.branches)
90+
self.assertEquals(branch2.commit.hexsha, branch1.commit.hexsha)
91+
self.assertEquals(branch2.commit.message, message)
92+
93+
branch2.checkout()
94+
95+
with open(join(self.clone2.working_dir, 'hello.md')) as file:
96+
front, body = jekyll.load_jekyll_doc(file)
97+
98+
self.assertEquals(front['title'], 'Hello')
99+
self.assertEquals(body, 'Hello hello.')
100+
63101
def tearDown(self):
64102
rmtree(self.origin.git_dir)
65103
rmtree(self.clone1.working_dir)

0 commit comments

Comments
 (0)