test_index.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. # -*- coding: utf-8 -*-
  2. # test_index.py -- Tests for the git index
  3. # encoding: utf-8
  4. # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
  5. #
  6. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  7. # General Public License as public by the Free Software Foundation; version 2.0
  8. # or (at your option) any later version. You can redistribute it and/or
  9. # modify it under the terms of either of these two licenses.
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # You should have received a copy of the licenses; if not, see
  18. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  19. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  20. # License, Version 2.0.
  21. #
  22. """Tests for the index."""
  23. from io import BytesIO
  24. import os
  25. import shutil
  26. import stat
  27. import struct
  28. import sys
  29. import tempfile
  30. from dulwich.index import (
  31. Index,
  32. build_index_from_tree,
  33. cleanup_mode,
  34. commit_tree,
  35. get_unstaged_changes,
  36. index_entry_from_stat,
  37. read_index,
  38. read_index_dict,
  39. validate_path_element_default,
  40. validate_path_element_ntfs,
  41. write_cache_time,
  42. write_index,
  43. write_index_dict,
  44. _tree_to_fs_path,
  45. _fs_to_tree_path,
  46. )
  47. from dulwich.object_store import (
  48. MemoryObjectStore,
  49. )
  50. from dulwich.objects import (
  51. Blob,
  52. Commit,
  53. Tree,
  54. S_IFGITLINK,
  55. )
  56. from dulwich.repo import Repo
  57. from dulwich.tests import (
  58. TestCase,
  59. skipIf,
  60. )
  61. class IndexTestCase(TestCase):
  62. datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
  63. def get_simple_index(self, name):
  64. return Index(os.path.join(self.datadir, name))
  65. class SimpleIndexTestCase(IndexTestCase):
  66. def test_len(self):
  67. self.assertEqual(1, len(self.get_simple_index("index")))
  68. def test_iter(self):
  69. self.assertEqual([b'bla'], list(self.get_simple_index("index")))
  70. def test_getitem(self):
  71. self.assertEqual(((1230680220, 0), (1230680220, 0), 2050, 3761020,
  72. 33188, 1000, 1000, 0,
  73. b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
  74. self.get_simple_index("index")[b"bla"])
  75. def test_empty(self):
  76. i = self.get_simple_index("notanindex")
  77. self.assertEqual(0, len(i))
  78. self.assertFalse(os.path.exists(i._filename))
  79. def test_against_empty_tree(self):
  80. i = self.get_simple_index("index")
  81. changes = list(i.changes_from_tree(MemoryObjectStore(), None))
  82. self.assertEqual(1, len(changes))
  83. (oldname, newname), (oldmode, newmode), (oldsha, newsha) = changes[0]
  84. self.assertEqual(b'bla', newname)
  85. self.assertEqual(b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', newsha)
  86. class SimpleIndexWriterTestCase(IndexTestCase):
  87. def setUp(self):
  88. IndexTestCase.setUp(self)
  89. self.tempdir = tempfile.mkdtemp()
  90. def tearDown(self):
  91. IndexTestCase.tearDown(self)
  92. shutil.rmtree(self.tempdir)
  93. def test_simple_write(self):
  94. entries = [(b'barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
  95. 33188, 1000, 1000, 0,
  96. b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
  97. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  98. with open(filename, 'wb+') as x:
  99. write_index(x, entries)
  100. with open(filename, 'rb') as x:
  101. self.assertEqual(entries, list(read_index(x)))
  102. class ReadIndexDictTests(IndexTestCase):
  103. def setUp(self):
  104. IndexTestCase.setUp(self)
  105. self.tempdir = tempfile.mkdtemp()
  106. def tearDown(self):
  107. IndexTestCase.tearDown(self)
  108. shutil.rmtree(self.tempdir)
  109. def test_simple_write(self):
  110. entries = {b'barbla': ((1230680220, 0), (1230680220, 0), 2050, 3761020,
  111. 33188, 1000, 1000, 0,
  112. b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)}
  113. filename = os.path.join(self.tempdir, 'test-simple-write-index')
  114. with open(filename, 'wb+') as x:
  115. write_index_dict(x, entries)
  116. with open(filename, 'rb') as x:
  117. self.assertEqual(entries, read_index_dict(x))
  118. class CommitTreeTests(TestCase):
  119. def setUp(self):
  120. super(CommitTreeTests, self).setUp()
  121. self.store = MemoryObjectStore()
  122. def test_single_blob(self):
  123. blob = Blob()
  124. blob.data = b"foo"
  125. self.store.add_object(blob)
  126. blobs = [(b"bla", blob.id, stat.S_IFREG)]
  127. rootid = commit_tree(self.store, blobs)
  128. self.assertEqual(rootid, b"1a1e80437220f9312e855c37ac4398b68e5c1d50")
  129. self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid][b"bla"])
  130. self.assertEqual(set([rootid, blob.id]), set(self.store._data.keys()))
  131. def test_nested(self):
  132. blob = Blob()
  133. blob.data = b"foo"
  134. self.store.add_object(blob)
  135. blobs = [(b"bla/bar", blob.id, stat.S_IFREG)]
  136. rootid = commit_tree(self.store, blobs)
  137. self.assertEqual(rootid, b"d92b959b216ad0d044671981196781b3258fa537")
  138. dirid = self.store[rootid][b"bla"][1]
  139. self.assertEqual(dirid, b"c1a1deb9788150829579a8b4efa6311e7b638650")
  140. self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid][b"bla"])
  141. self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid][b"bar"])
  142. self.assertEqual(set([rootid, dirid, blob.id]),
  143. set(self.store._data.keys()))
  144. class CleanupModeTests(TestCase):
  145. def test_file(self):
  146. self.assertEqual(0o100644, cleanup_mode(0o100000))
  147. def test_executable(self):
  148. self.assertEqual(0o100755, cleanup_mode(0o100711))
  149. def test_symlink(self):
  150. self.assertEqual(0o120000, cleanup_mode(0o120711))
  151. def test_dir(self):
  152. self.assertEqual(0o040000, cleanup_mode(0o40531))
  153. def test_submodule(self):
  154. self.assertEqual(0o160000, cleanup_mode(0o160744))
  155. class WriteCacheTimeTests(TestCase):
  156. def test_write_string(self):
  157. f = BytesIO()
  158. self.assertRaises(TypeError, write_cache_time, f, "foo")
  159. def test_write_int(self):
  160. f = BytesIO()
  161. write_cache_time(f, 434343)
  162. self.assertEqual(struct.pack(">LL", 434343, 0), f.getvalue())
  163. def test_write_tuple(self):
  164. f = BytesIO()
  165. write_cache_time(f, (434343, 21))
  166. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  167. def test_write_float(self):
  168. f = BytesIO()
  169. write_cache_time(f, 434343.000000021)
  170. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  171. class IndexEntryFromStatTests(TestCase):
  172. def test_simple(self):
  173. st = os.stat_result((16877, 131078, 64769,
  174. 154, 1000, 1000, 12288,
  175. 1323629595, 1324180496, 1324180496))
  176. entry = index_entry_from_stat(st, "22" * 20, 0)
  177. self.assertEqual(entry, (
  178. 1324180496,
  179. 1324180496,
  180. 64769,
  181. 131078,
  182. 16384,
  183. 1000,
  184. 1000,
  185. 12288,
  186. '2222222222222222222222222222222222222222',
  187. 0))
  188. def test_override_mode(self):
  189. st = os.stat_result((stat.S_IFREG + 0o644, 131078, 64769,
  190. 154, 1000, 1000, 12288,
  191. 1323629595, 1324180496, 1324180496))
  192. entry = index_entry_from_stat(st, "22" * 20, 0,
  193. mode=stat.S_IFREG + 0o755)
  194. self.assertEqual(entry, (
  195. 1324180496,
  196. 1324180496,
  197. 64769,
  198. 131078,
  199. 33261,
  200. 1000,
  201. 1000,
  202. 12288,
  203. '2222222222222222222222222222222222222222',
  204. 0))
  205. class BuildIndexTests(TestCase):
  206. def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha):
  207. self.assertEqual(index_entry[4], mode) # mode
  208. self.assertEqual(index_entry[7], filesize) # filesize
  209. self.assertEqual(index_entry[8], sha) # sha
  210. def assertFileContents(self, path, contents, symlink=False):
  211. if symlink:
  212. self.assertEqual(os.readlink(path), contents)
  213. else:
  214. with open(path, 'rb') as f:
  215. self.assertEqual(f.read(), contents)
  216. def test_empty(self):
  217. repo_dir = tempfile.mkdtemp()
  218. self.addCleanup(shutil.rmtree, repo_dir)
  219. with Repo.init(repo_dir) as repo:
  220. tree = Tree()
  221. repo.object_store.add_object(tree)
  222. build_index_from_tree(repo.path, repo.index_path(),
  223. repo.object_store, tree.id)
  224. # Verify index entries
  225. index = repo.open_index()
  226. self.assertEqual(len(index), 0)
  227. # Verify no files
  228. self.assertEqual(['.git'], os.listdir(repo.path))
  229. def test_git_dir(self):
  230. repo_dir = tempfile.mkdtemp()
  231. self.addCleanup(shutil.rmtree, repo_dir)
  232. with Repo.init(repo_dir) as repo:
  233. # Populate repo
  234. filea = Blob.from_string(b'file a')
  235. filee = Blob.from_string(b'd')
  236. tree = Tree()
  237. tree[b'.git/a'] = (stat.S_IFREG | 0o644, filea.id)
  238. tree[b'c/e'] = (stat.S_IFREG | 0o644, filee.id)
  239. repo.object_store.add_objects([(o, None)
  240. for o in [filea, filee, tree]])
  241. build_index_from_tree(repo.path, repo.index_path(),
  242. repo.object_store, tree.id)
  243. # Verify index entries
  244. index = repo.open_index()
  245. self.assertEqual(len(index), 1)
  246. # filea
  247. apath = os.path.join(repo.path, '.git', 'a')
  248. self.assertFalse(os.path.exists(apath))
  249. # filee
  250. epath = os.path.join(repo.path, 'c', 'e')
  251. self.assertTrue(os.path.exists(epath))
  252. self.assertReasonableIndexEntry(index[b'c/e'],
  253. stat.S_IFREG | 0o644, 1, filee.id)
  254. self.assertFileContents(epath, b'd')
  255. def test_nonempty(self):
  256. repo_dir = tempfile.mkdtemp()
  257. self.addCleanup(shutil.rmtree, repo_dir)
  258. with Repo.init(repo_dir) as repo:
  259. # Populate repo
  260. filea = Blob.from_string(b'file a')
  261. fileb = Blob.from_string(b'file b')
  262. filed = Blob.from_string(b'file d')
  263. tree = Tree()
  264. tree[b'a'] = (stat.S_IFREG | 0o644, filea.id)
  265. tree[b'b'] = (stat.S_IFREG | 0o644, fileb.id)
  266. tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id)
  267. repo.object_store.add_objects([(o, None)
  268. for o in [filea, fileb, filed, tree]])
  269. build_index_from_tree(repo.path, repo.index_path(),
  270. repo.object_store, tree.id)
  271. # Verify index entries
  272. index = repo.open_index()
  273. self.assertEqual(len(index), 3)
  274. # filea
  275. apath = os.path.join(repo.path, 'a')
  276. self.assertTrue(os.path.exists(apath))
  277. self.assertReasonableIndexEntry(index[b'a'],
  278. stat.S_IFREG | 0o644, 6, filea.id)
  279. self.assertFileContents(apath, b'file a')
  280. # fileb
  281. bpath = os.path.join(repo.path, 'b')
  282. self.assertTrue(os.path.exists(bpath))
  283. self.assertReasonableIndexEntry(index[b'b'],
  284. stat.S_IFREG | 0o644, 6, fileb.id)
  285. self.assertFileContents(bpath, b'file b')
  286. # filed
  287. dpath = os.path.join(repo.path, 'c', 'd')
  288. self.assertTrue(os.path.exists(dpath))
  289. self.assertReasonableIndexEntry(index[b'c/d'],
  290. stat.S_IFREG | 0o644, 6, filed.id)
  291. self.assertFileContents(dpath, b'file d')
  292. # Verify no extra files
  293. self.assertEqual(['.git', 'a', 'b', 'c'],
  294. sorted(os.listdir(repo.path)))
  295. self.assertEqual(['d'],
  296. sorted(os.listdir(os.path.join(repo.path, 'c'))))
  297. @skipIf(not getattr(os, 'sync', None), 'Requires sync support')
  298. def test_norewrite(self):
  299. repo_dir = tempfile.mkdtemp()
  300. self.addCleanup(shutil.rmtree, repo_dir)
  301. with Repo.init(repo_dir) as repo:
  302. # Populate repo
  303. filea = Blob.from_string(b'file a')
  304. filea_path = os.path.join(repo_dir, 'a')
  305. tree = Tree()
  306. tree[b'a'] = (stat.S_IFREG | 0o644, filea.id)
  307. repo.object_store.add_objects([(o, None)
  308. for o in [filea, tree]])
  309. # First Write
  310. build_index_from_tree(repo.path, repo.index_path(),
  311. repo.object_store, tree.id)
  312. # Use sync as metadata can be cached on some FS
  313. os.sync()
  314. mtime = os.stat(filea_path).st_mtime
  315. # Test Rewrite
  316. build_index_from_tree(repo.path, repo.index_path(),
  317. repo.object_store, tree.id)
  318. os.sync()
  319. self.assertEqual(mtime, os.stat(filea_path).st_mtime)
  320. # Modify content
  321. with open(filea_path, 'wb') as fh:
  322. fh.write(b'test a')
  323. os.sync()
  324. mtime = os.stat(filea_path).st_mtime
  325. # Test rewrite
  326. build_index_from_tree(repo.path, repo.index_path(),
  327. repo.object_store, tree.id)
  328. os.sync()
  329. with open(filea_path, 'rb') as fh:
  330. self.assertEqual(b'file a', fh.read())
  331. @skipIf(not getattr(os, 'symlink', None), 'Requires symlink support')
  332. def test_symlink(self):
  333. repo_dir = tempfile.mkdtemp()
  334. self.addCleanup(shutil.rmtree, repo_dir)
  335. with Repo.init(repo_dir) as repo:
  336. # Populate repo
  337. filed = Blob.from_string(b'file d')
  338. filee = Blob.from_string(b'd')
  339. tree = Tree()
  340. tree[b'c/d'] = (stat.S_IFREG | 0o644, filed.id)
  341. tree[b'c/e'] = (stat.S_IFLNK, filee.id) # symlink
  342. repo.object_store.add_objects([(o, None)
  343. for o in [filed, filee, tree]])
  344. build_index_from_tree(repo.path, repo.index_path(),
  345. repo.object_store, tree.id)
  346. # Verify index entries
  347. index = repo.open_index()
  348. # symlink to d
  349. epath = os.path.join(repo.path, 'c', 'e')
  350. self.assertTrue(os.path.exists(epath))
  351. self.assertReasonableIndexEntry(
  352. index[b'c/e'], stat.S_IFLNK,
  353. 0 if sys.platform == 'win32' else 1,
  354. filee.id)
  355. self.assertFileContents(epath, 'd', symlink=True)
  356. def test_no_decode_encode(self):
  357. repo_dir = tempfile.mkdtemp()
  358. repo_dir_bytes = repo_dir.encode(sys.getfilesystemencoding())
  359. self.addCleanup(shutil.rmtree, repo_dir)
  360. with Repo.init(repo_dir) as repo:
  361. # Populate repo
  362. file = Blob.from_string(b'foo')
  363. tree = Tree()
  364. latin1_name = u'À'.encode('latin1')
  365. utf8_name = u'À'.encode('utf8')
  366. tree[latin1_name] = (stat.S_IFREG | 0o644, file.id)
  367. tree[utf8_name] = (stat.S_IFREG | 0o644, file.id)
  368. repo.object_store.add_objects(
  369. [(o, None) for o in [file, tree]])
  370. build_index_from_tree(
  371. repo.path, repo.index_path(),
  372. repo.object_store, tree.id)
  373. # Verify index entries
  374. index = repo.open_index()
  375. latin1_path = os.path.join(repo_dir_bytes, latin1_name)
  376. self.assertTrue(os.path.exists(latin1_path))
  377. utf8_path = os.path.join(repo_dir_bytes, utf8_name)
  378. self.assertTrue(os.path.exists(utf8_path))
  379. def test_git_submodule(self):
  380. repo_dir = tempfile.mkdtemp()
  381. self.addCleanup(shutil.rmtree, repo_dir)
  382. with Repo.init(repo_dir) as repo:
  383. filea = Blob.from_string(b'file alalala')
  384. subtree = Tree()
  385. subtree[b'a'] = (stat.S_IFREG | 0o644, filea.id)
  386. c = Commit()
  387. c.tree = subtree.id
  388. c.committer = c.author = b'Somebody <somebody@example.com>'
  389. c.commit_time = c.author_time = 42342
  390. c.commit_timezone = c.author_timezone = 0
  391. c.parents = []
  392. c.message = b'Subcommit'
  393. tree = Tree()
  394. tree[b'c'] = (S_IFGITLINK, c.id)
  395. repo.object_store.add_objects(
  396. [(o, None) for o in [tree]])
  397. build_index_from_tree(repo.path, repo.index_path(),
  398. repo.object_store, tree.id)
  399. # Verify index entries
  400. index = repo.open_index()
  401. self.assertEqual(len(index), 1)
  402. # filea
  403. apath = os.path.join(repo.path, 'c/a')
  404. self.assertFalse(os.path.exists(apath))
  405. # dir c
  406. cpath = os.path.join(repo.path, 'c')
  407. self.assertTrue(os.path.isdir(cpath))
  408. self.assertEqual(index[b'c'][4], S_IFGITLINK) # mode
  409. self.assertEqual(index[b'c'][8], c.id) # sha
  410. def test_git_submodule_exists(self):
  411. repo_dir = tempfile.mkdtemp()
  412. self.addCleanup(shutil.rmtree, repo_dir)
  413. with Repo.init(repo_dir) as repo:
  414. filea = Blob.from_string(b'file alalala')
  415. subtree = Tree()
  416. subtree[b'a'] = (stat.S_IFREG | 0o644, filea.id)
  417. c = Commit()
  418. c.tree = subtree.id
  419. c.committer = c.author = b'Somebody <somebody@example.com>'
  420. c.commit_time = c.author_time = 42342
  421. c.commit_timezone = c.author_timezone = 0
  422. c.parents = []
  423. c.message = b'Subcommit'
  424. tree = Tree()
  425. tree[b'c'] = (S_IFGITLINK, c.id)
  426. os.mkdir(os.path.join(repo_dir, 'c'))
  427. repo.object_store.add_objects(
  428. [(o, None) for o in [tree]])
  429. build_index_from_tree(repo.path, repo.index_path(),
  430. repo.object_store, tree.id)
  431. # Verify index entries
  432. index = repo.open_index()
  433. self.assertEqual(len(index), 1)
  434. # filea
  435. apath = os.path.join(repo.path, 'c/a')
  436. self.assertFalse(os.path.exists(apath))
  437. # dir c
  438. cpath = os.path.join(repo.path, 'c')
  439. self.assertTrue(os.path.isdir(cpath))
  440. self.assertEqual(index[b'c'][4], S_IFGITLINK) # mode
  441. self.assertEqual(index[b'c'][8], c.id) # sha
  442. class GetUnstagedChangesTests(TestCase):
  443. def test_get_unstaged_changes(self):
  444. """Unit test for get_unstaged_changes."""
  445. repo_dir = tempfile.mkdtemp()
  446. self.addCleanup(shutil.rmtree, repo_dir)
  447. with Repo.init(repo_dir) as repo:
  448. # Commit a dummy file then modify it
  449. foo1_fullpath = os.path.join(repo_dir, 'foo1')
  450. with open(foo1_fullpath, 'wb') as f:
  451. f.write(b'origstuff')
  452. foo2_fullpath = os.path.join(repo_dir, 'foo2')
  453. with open(foo2_fullpath, 'wb') as f:
  454. f.write(b'origstuff')
  455. repo.stage(['foo1', 'foo2'])
  456. repo.do_commit(b'test status', author=b'', committer=b'')
  457. with open(foo1_fullpath, 'wb') as f:
  458. f.write(b'newstuff')
  459. # modify access and modify time of path
  460. os.utime(foo1_fullpath, (0, 0))
  461. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  462. self.assertEqual(list(changes), [b'foo1'])
  463. def test_get_unstaged_deleted_changes(self):
  464. """Unit test for get_unstaged_changes."""
  465. repo_dir = tempfile.mkdtemp()
  466. self.addCleanup(shutil.rmtree, repo_dir)
  467. with Repo.init(repo_dir) as repo:
  468. # Commit a dummy file then remove it
  469. foo1_fullpath = os.path.join(repo_dir, 'foo1')
  470. with open(foo1_fullpath, 'wb') as f:
  471. f.write(b'origstuff')
  472. repo.stage(['foo1'])
  473. repo.do_commit(b'test status', author=b'', committer=b'')
  474. os.unlink(foo1_fullpath)
  475. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  476. self.assertEqual(list(changes), [b'foo1'])
  477. class TestValidatePathElement(TestCase):
  478. def test_default(self):
  479. self.assertTrue(validate_path_element_default(b"bla"))
  480. self.assertTrue(validate_path_element_default(b".bla"))
  481. self.assertFalse(validate_path_element_default(b".git"))
  482. self.assertFalse(validate_path_element_default(b".giT"))
  483. self.assertFalse(validate_path_element_default(b".."))
  484. self.assertTrue(validate_path_element_default(b"git~1"))
  485. def test_ntfs(self):
  486. self.assertTrue(validate_path_element_ntfs(b"bla"))
  487. self.assertTrue(validate_path_element_ntfs(b".bla"))
  488. self.assertFalse(validate_path_element_ntfs(b".git"))
  489. self.assertFalse(validate_path_element_ntfs(b".giT"))
  490. self.assertFalse(validate_path_element_ntfs(b".."))
  491. self.assertFalse(validate_path_element_ntfs(b"git~1"))
  492. class TestTreeFSPathConversion(TestCase):
  493. def test_tree_to_fs_path(self):
  494. tree_path = u'délwíçh/foo'.encode('utf8')
  495. fs_path = _tree_to_fs_path(b'/prefix/path', tree_path)
  496. self.assertEqual(
  497. fs_path,
  498. os.path.join(u'/prefix/path', u'délwíçh', u'foo').encode('utf8'))
  499. def test_fs_to_tree_path_str(self):
  500. fs_path = os.path.join(os.path.join(u'délwíçh', u'foo'))
  501. tree_path = _fs_to_tree_path(fs_path, "utf-8")
  502. self.assertEqual(tree_path, u'délwíçh/foo'.encode("utf-8"))
  503. def test_fs_to_tree_path_bytes(self):
  504. fs_path = os.path.join(os.path.join(u'délwíçh', u'foo').encode('utf8'))
  505. tree_path = _fs_to_tree_path(fs_path, "utf-8")
  506. self.assertEqual(tree_path, u'délwíçh/foo'.encode('utf8'))