Codebase list ros-vcstools / upstream/0.1.42 test / test_git_subm.py
upstream/0.1.42

Tree @upstream/0.1.42 (Download .tar.gz)

test_git_subm.py @upstream/0.1.42raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import absolute_import, print_function, unicode_literals

import os
import unittest
import subprocess
import tempfile
import shutil
import tarfile
import filecmp
from contextlib import closing

from vcstools.git import GitClient


class GitClientTestSetups(unittest.TestCase):

    @classmethod
    def setUpClass(self):

        self.root_directory = tempfile.mkdtemp()
        # helpful when setting tearDown to pass
        self.directories = dict(setUp=self.root_directory)
        self.remote_dir = os.path.join(self.root_directory, "remote")
        self.repo_path = os.path.join(self.remote_dir, "repo")
        self.submodule_path = os.path.join(self.remote_dir, "submodule")
        self.subsubmodule_path = os.path.join(self.remote_dir, "subsubmodule")
        self.local_path = os.path.join(self.root_directory, "local")
        self.sublocal_path = os.path.join(self.local_path, "submodule")
        self.sublocal2_path = os.path.join(self.local_path, "submodule2")
        self.subsublocal_path = os.path.join(self.sublocal_path, "subsubmodule")
        self.subsublocal2_path = os.path.join(self.sublocal2_path, "subsubmodule")
        self.export_path = os.path.join(self.root_directory, "export")
        self.subexport_path = os.path.join(self.export_path, "submodule")
        self.subexport2_path = os.path.join(self.export_path, "submodule2")
        self.subsubexport_path = os.path.join(self.subexport_path, "subsubmodule")
        self.subsubexport2_path = os.path.join(self.subexport2_path, "subsubmodule")
        os.makedirs(self.repo_path)
        os.makedirs(self.submodule_path)
        os.makedirs(self.subsubmodule_path)

        # create a "remote" repo
        subprocess.check_call("git init", shell=True, cwd=self.repo_path)
        subprocess.check_call("touch fixed.txt", shell=True, cwd=self.repo_path)
        subprocess.check_call("git add fixed.txt", shell=True, cwd=self.repo_path)
        subprocess.check_call("git commit -m initial", shell=True, cwd=self.repo_path)
        subprocess.check_call("git tag test_tag", shell=True, cwd=self.repo_path)
        subprocess.check_call("git branch initial_branch", shell=True, cwd=self.repo_path)
        po = subprocess.Popen("git log -n 1 --pretty=format:\"%H\"", shell=True,
                              cwd=self.repo_path, stdout=subprocess.PIPE)
        self.version_init = po.stdout.read().decode('UTF-8').rstrip('"').lstrip('"')

        # create a submodule repo
        subprocess.check_call("git init", shell=True, cwd=self.submodule_path)
        subprocess.check_call("touch subfixed.txt", shell=True, cwd=self.submodule_path)
        subprocess.check_call("git add *", shell=True, cwd=self.submodule_path)
        subprocess.check_call("git commit -m initial", shell=True, cwd=self.submodule_path)
        subprocess.check_call("git tag sub_test_tag", shell=True, cwd=self.submodule_path)

        # create a subsubmodule repo
        subprocess.check_call("git init", shell=True, cwd=self.subsubmodule_path)
        subprocess.check_call("touch subsubfixed.txt", shell=True, cwd=self.subsubmodule_path)
        subprocess.check_call("git add *", shell=True, cwd=self.subsubmodule_path)
        subprocess.check_call("git commit -m initial", shell=True, cwd=self.subsubmodule_path)
        subprocess.check_call("git tag subsub_test_tag", shell=True, cwd=self.subsubmodule_path)

        # attach subsubmodule to submodule
        subprocess.check_call("git submodule add %s %s" % (self.subsubmodule_path, "subsubmodule"),
                              shell=True, cwd=self.submodule_path)
        subprocess.check_call("git submodule init", shell=True, cwd=self.submodule_path)
        subprocess.check_call("git submodule update", shell=True, cwd=self.submodule_path)
        subprocess.check_call("git commit -m subsubmodule", shell=True, cwd=self.submodule_path)

        po = subprocess.Popen("git log -n 1 --pretty=format:\"%H\"", shell=True,
                              cwd=self.subsubmodule_path, stdout=subprocess.PIPE)
        self.subsubversion_final = po.stdout.read().decode('UTF-8').rstrip('"').lstrip('"')

        po = subprocess.Popen("git log -n 1 --pretty=format:\"%H\"", shell=True,
                              cwd=self.submodule_path, stdout=subprocess.PIPE)
        self.subversion_final = po.stdout.read().decode('UTF-8').rstrip('"').lstrip('"')

        # attach submodule somewhere, only in test_branch first
        subprocess.check_call("git checkout master -b test_branch", shell=True, cwd=self.repo_path)
        subprocess.check_call("git submodule add %s %s" % (self.submodule_path,
                                                           "submodule2"), shell=True, cwd=self.repo_path)

        # this is needed only if git <= 1.7, during the time when submodules were being introduced (from 1.5.3)
        subprocess.check_call("git submodule init", shell=True, cwd=self.repo_path)
        subprocess.check_call("git submodule update", shell=True, cwd=self.repo_path)

        subprocess.check_call("git commit -m submodule", shell=True, cwd=self.repo_path)

        po = subprocess.Popen("git log -n 1 --pretty=format:\"%H\"", shell=True,
                              cwd=self.repo_path, stdout=subprocess.PIPE)
        self.version_test = po.stdout.read().decode('UTF-8').rstrip('"').lstrip('"')

        # attach submodule using relative url, only in test_sub_relative
        subprocess.check_call("git checkout master -b test_sub_relative", shell=True, cwd=self.repo_path)
        subprocess.check_call("git submodule add %s %s" % (os.path.join('..', os.path.basename(self.submodule_path)),
                                                           "submodule"), shell=True, cwd=self.repo_path)

        # this is needed only if git <= 1.7, during the time when submodules were being introduced (from 1.5.3)
        subprocess.check_call("git submodule init", shell=True, cwd=self.repo_path)
        subprocess.check_call("git submodule update", shell=True, cwd=self.repo_path)

        subprocess.check_call("git commit -m submodule", shell=True, cwd=self.repo_path)

        po = subprocess.Popen("git log -n 1 --pretty=format:\"%H\"", shell=True,
                              cwd=self.repo_path, stdout=subprocess.PIPE)
        self.version_relative = po.stdout.read().decode('UTF-8').rstrip('"').lstrip('"')

        # attach submodule to remote on master. CAREFUL : submodule2 is still in working tree (git does not clean it)
        subprocess.check_call("git checkout master", shell=True, cwd=self.repo_path)
        subprocess.check_call("git submodule add %s %s" % (self.submodule_path, "submodule"),
                              shell=True, cwd=self.repo_path)

        # this is needed only if git <= 1.7, during the time when submodules were being introduced (from 1.5.3)
        subprocess.check_call("git submodule init", shell=True, cwd=self.repo_path)
        subprocess.check_call("git submodule update", shell=True, cwd=self.repo_path)

        subprocess.check_call("git commit -m submodule", shell=True, cwd=self.repo_path)

        po = subprocess.Popen("git log -n 1 --pretty=format:\"%H\"", shell=True,
                              cwd=self.repo_path, stdout=subprocess.PIPE)
        self.version_final = po.stdout.read().decode('UTF-8').rstrip('"').lstrip('"')
        subprocess.check_call("git tag last_tag", shell=True, cwd=self.repo_path)

        print("setup done\n\n")

    @classmethod
    def tearDownClass(self):
        for d in self.directories:
            shutil.rmtree(self.directories[d])

    def tearDown(self):
        if os.path.exists(self.local_path):
            shutil.rmtree(self.local_path)
        if os.path.exists(self.export_path):
            shutil.rmtree(self.export_path)


class GitClientTest(GitClientTestSetups):

    def test_checkout_master_with_subs(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subsubclient = GitClient(self.subsublocal_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(self.version_final, client.get_version())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subclient.detect_presence())
        self.assertEqual(self.subversion_final, subclient.get_version())
        self.assertTrue(subsubclient.path_exists())
        self.assertTrue(subsubclient.detect_presence())
        self.assertEqual(self.subsubversion_final, subsubclient.get_version())

    def test_export_master(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subsubclient = GitClient(self.subsublocal_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertFalse(os.path.exists(self.export_path))
        self.assertTrue(client.checkout(url))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        tarpath = client.export_repository("master", self.export_path)
        self.assertEqual(tarpath, self.export_path + '.tar.gz')
        os.mkdir(self.export_path)
        with closing(tarfile.open(tarpath, "r:gz")) as tarf:
            tarf.extractall(self.export_path)
        subsubdirdiff = filecmp.dircmp(self.subsubexport_path, self.subsublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subsubdirdiff.left_only, [])
        self.assertEqual(subsubdirdiff.right_only, [])
        self.assertEqual(subsubdirdiff.diff_files, [])
        subdirdiff = filecmp.dircmp(self.subexport_path, self.sublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subdirdiff.left_only, [])
        self.assertEqual(subdirdiff.right_only, [])
        self.assertEqual(subdirdiff.diff_files, [])
        dirdiff = filecmp.dircmp(self.export_path, self.local_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(dirdiff.left_only, [])
        self.assertEqual(dirdiff.right_only, [])
        self.assertEqual(dirdiff.diff_files, [])

    def test_export_relative(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subsubclient = GitClient(self.subsublocal_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertFalse(os.path.exists(self.export_path))
        self.assertTrue(client.checkout(url, "test_sub_relative"))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        #subprocess.call(["tree", self.root_directory])
        tarpath = client.export_repository("test_sub_relative", self.export_path)
        self.assertEqual(tarpath, self.export_path + '.tar.gz')
        os.mkdir(self.export_path)
        with closing(tarfile.open(tarpath, "r:gz")) as tarf:
            tarf.extractall(self.export_path)
        subsubdirdiff = filecmp.dircmp(self.subsubexport_path, self.subsublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subsubdirdiff.left_only, [])
        self.assertEqual(subsubdirdiff.right_only, [])
        self.assertEqual(subsubdirdiff.diff_files, [])
        subdirdiff = filecmp.dircmp(self.subexport_path, self.sublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subdirdiff.left_only, [])
        self.assertEqual(subdirdiff.right_only, [])
        self.assertEqual(subdirdiff.diff_files, [])
        dirdiff = filecmp.dircmp(self.export_path, self.local_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(dirdiff.left_only, [])
        self.assertEqual(dirdiff.right_only, [])
        self.assertEqual(dirdiff.diff_files, [])

    def test_export_branch(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient = GitClient(self.subsublocal_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertFalse(os.path.exists(self.export_path))
        self.assertTrue(client.checkout(url, version='master'))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertFalse(subclient2.path_exists())
        self.assertFalse(subsubclient2.path_exists())
        # we need first to retrieve locally the branch we want to export
        self.assertTrue(client.update(version='test_branch'))
        self.assertTrue(client.path_exists())
        # git leaves old submodule around by default
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        # new submodule should be there
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())

        tarpath = client.export_repository("test_branch", self.export_path)
        self.assertEqual(tarpath, self.export_path + '.tar.gz')
        os.mkdir(self.export_path)
        with closing(tarfile.open(tarpath, "r:gz")) as tarf:
            tarf.extractall(self.export_path)

        # Checking that we have only submodule2 in our export
        self.assertFalse(os.path.exists(self.subexport_path))
        self.assertFalse(os.path.exists(self.subsubexport_path))
        self.assertTrue(os.path.exists(self.subexport2_path))
        self.assertTrue(os.path.exists(self.subsubexport2_path))

        # comparing with test_branch version ( currently checked-out )
        subsubdirdiff = filecmp.dircmp(self.subsubexport2_path, self.subsublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subsubdirdiff.left_only, [])  # same subsubfixed.txt in both subsubmodule/
        self.assertEqual(subsubdirdiff.right_only, [])
        self.assertEqual(subsubdirdiff.diff_files, [])
        subdirdiff = filecmp.dircmp(self.subexport2_path, self.sublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subdirdiff.left_only, [])
        self.assertEqual(subdirdiff.right_only, [])
        self.assertEqual(subdirdiff.diff_files, [])
        dirdiff = filecmp.dircmp(self.export_path, self.local_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(dirdiff.left_only, [])
        # submodule is still there on local_path (git default behavior)
        self.assertEqual(dirdiff.right_only, ['submodule'])
        self.assertEqual(dirdiff.diff_files, [])

    def test_export_hash(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient = GitClient(self.subsublocal_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertFalse(os.path.exists(self.export_path))
        self.assertTrue(client.checkout(url, version='master'))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertFalse(subclient2.path_exists())
        self.assertFalse(subsubclient2.path_exists())
        # we need first to retrieve locally the hash we want to export
        self.assertTrue(client.update(version=self.version_test))
        self.assertTrue(client.path_exists())
        # git leaves old submodule around by default
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        # new submodule should be there
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())

        tarpath = client.export_repository(self.version_test, self.export_path)
        self.assertEqual(tarpath, self.export_path + '.tar.gz')
        os.mkdir(self.export_path)
        with closing(tarfile.open(tarpath, "r:gz")) as tarf:
            tarf.extractall(self.export_path)

        # Checking that we have only submodule2 in our export
        self.assertFalse(os.path.exists(self.subexport_path))
        self.assertFalse(os.path.exists(self.subsubexport_path))
        self.assertTrue(os.path.exists(self.subexport2_path))
        self.assertTrue(os.path.exists(self.subsubexport2_path))

        # comparing with version_test ( currently checked-out )
        subsubdirdiff = filecmp.dircmp(self.subsubexport2_path, self.subsublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subsubdirdiff.left_only, [])  # same subsubfixed.txt in both subsubmodule/
        self.assertEqual(subsubdirdiff.right_only, [])
        self.assertEqual(subsubdirdiff.diff_files, [])
        subdirdiff = filecmp.dircmp(self.subexport2_path, self.sublocal_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(subdirdiff.left_only, [])
        self.assertEqual(subdirdiff.right_only, [])
        self.assertEqual(subdirdiff.diff_files, [])
        dirdiff = filecmp.dircmp(self.export_path, self.local_path, ignore=['.git', '.gitmodules'])
        self.assertEqual(dirdiff.left_only, [])
        # submodule is still there on local_path (git default behavior)
        self.assertEqual(dirdiff.right_only, ['submodule'])
        self.assertEqual(dirdiff.diff_files, [])

    def test_checkout_branch_without_subs(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subsubclient = GitClient(self.subsublocal_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url, version='initial_branch'))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(self.version_init, client.get_version())
        self.assertFalse(subclient.path_exists())
        self.assertFalse(subsubclient.path_exists())

    def test_checkout_test_branch_with_subs(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subsubclient = GitClient(self.subsublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url, version='test_branch'))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(self.version_test, client.get_version())
        self.assertFalse(subclient.path_exists())
        self.assertFalse(subsubclient.path_exists())
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())

    def test_checkout_master_with_subs2(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subsubclient = GitClient(self.subsublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url, version='master'))
        self.assertTrue(client.path_exists())
        self.assertTrue(client.detect_presence())
        self.assertEqual(self.version_final, client.get_version())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertFalse(subclient2.path_exists())
        self.assertFalse(subsubclient2.path_exists())

    def test_switch_branches(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient = GitClient(self.subsublocal_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertFalse(subclient2.path_exists())
        new_version = "test_branch"
        self.assertTrue(client.update(new_version))
        # checking that update doesnt make submodule disappear (git default behavior)
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        oldnew_version = "master"
        self.assertTrue(client.update(oldnew_version))
        # checking that update doesnt make submodule2 disappear (git default behavior)
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())

    def test_switch_branches_retrieve_local_subcommit(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        subclient = GitClient(self.sublocal_path)
        subclient2 = GitClient(self.sublocal2_path)
        subsubclient = GitClient(self.subsublocal_path)
        subsubclient2 = GitClient(self.subsublocal2_path)
        self.assertFalse(client.path_exists())
        self.assertFalse(client.detect_presence())
        self.assertTrue(client.checkout(url))
        self.assertTrue(client.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertFalse(subclient2.path_exists())
        new_version = "test_branch"
        self.assertTrue(client.update(new_version))
        # checking that update doesnt make submodule disappear (git default behavior)
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        subprocess.check_call("touch submodif.txt", shell=True, cwd=self.sublocal2_path)
        subprocess.check_call("git add submodif.txt", shell=True, cwd=self.sublocal2_path)
        subprocess.check_call("git commit -m submodif", shell=True, cwd=self.sublocal2_path)
        subprocess.check_call("git add submodule2", shell=True, cwd=self.local_path)
        subprocess.check_call("git commit -m submodule2_modif", shell=True, cwd=self.local_path)
        oldnew_version = "master"
        self.assertTrue(client.update(oldnew_version))
        # checking that update doesnt make submodule2 disappear (git default behavior)
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertTrue(client.update(new_version))
        # checking that update still has submodule with submodif
        self.assertTrue(subclient2.path_exists())
        self.assertTrue(subsubclient2.path_exists())
        self.assertTrue(subclient.path_exists())
        self.assertTrue(subsubclient.path_exists())
        self.assertTrue(os.path.exists(os.path.join(self.sublocal2_path, "submodif.txt")))

    def test_status(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        self.assertTrue(client.checkout(url))
        output = client.get_status(porcelain=True)  # porcelain=True ensures stable format
        self.assertEqual('', output, "Expected empty string, got `{0}`".format(output))

        with open(os.path.join(self.local_path, 'fixed.txt'), 'a') as f:
            f.write('0123456789abcdef')
        subprocess.check_call("touch new.txt", shell=True, cwd=self.local_path)
        with open(os.path.join(self.sublocal_path, 'subfixed.txt'), 'a') as f:
            f.write('abcdef0123456789')
        subprocess.check_call("touch subnew.txt", shell=True, cwd=self.sublocal_path)
        with open(os.path.join(self.subsublocal_path, 'subsubfixed.txt'), 'a') as f:
            f.write('012345cdef')
        subprocess.check_call("touch subsubnew.txt", shell=True, cwd=self.subsublocal_path)

        output = client.get_status(porcelain=True)  # porcelain=True ensures stable format
        self.assertEqual('''\
 M ./fixed.txt
 M ./submodule
 M ./subfixed.txt
 M ./subsubmodule
 M ./subsubfixed.txt''', output.rstrip())

        output = client.get_status(untracked=True, porcelain=True)
        self.assertEqual('''\
 M ./fixed.txt
 M ./submodule
?? ./new.txt
 M ./subfixed.txt
 M ./subsubmodule
?? ./subnew.txt
 M ./subsubfixed.txt
?? ./subsubnew.txt''', output.rstrip())

        output = client.get_status(
            basepath=os.path.dirname(self.local_path),
            untracked=True,
            porcelain=True)
        self.assertEqual('''\
 M local/fixed.txt
 M local/submodule
?? local/new.txt
 M local/subfixed.txt
 M local/subsubmodule
?? local/subnew.txt
 M local/subsubfixed.txt
?? local/subsubnew.txt''', output.rstrip())

    def test_diff(self):
        url = self.repo_path
        client = GitClient(self.local_path)
        self.assertTrue(client.checkout(url))
        output = client.get_diff()
        self.assertEqual('', output, output)

        with open(os.path.join(self.local_path, 'fixed.txt'), 'a') as f:
            f.write('0123456789abcdef')
        subprocess.check_call("touch new.txt", shell=True, cwd=self.local_path)
        with open(os.path.join(self.sublocal_path, 'subfixed.txt'), 'a') as f:
            f.write('abcdef0123456789')
        subprocess.check_call("touch subnew.txt", shell=True, cwd=self.sublocal_path)
        with open(os.path.join(self.subsublocal_path, 'subsubfixed.txt'), 'a') as f:
            f.write('012345cdef')
        subprocess.check_call("touch subsubnew.txt", shell=True, cwd=self.subsublocal_path)

        output = client.get_diff()
        self.assertEqual(1094, len(output))
        self.assertTrue('''\
diff --git ./fixed.txt ./fixed.txt
index e69de29..454f6b3 100644
--- ./fixed.txt
+++ ./fixed.txt
@@ -0,0 +1 @@
+0123456789abcdef
\\ No newline at end of file''' in output)
        self.assertTrue('''\
diff --git ./submodule/subsubmodule/subsubfixed.txt ./submodule/subsubmodule/subsubfixed.txt
index e69de29..1a332dc 100644
--- ./submodule/subsubmodule/subsubfixed.txt
+++ ./submodule/subsubmodule/subsubfixed.txt
@@ -0,0 +1 @@
+012345cdef
\\ No newline at end of file''' in output)

        output = client.get_diff(basepath=os.path.dirname(self.local_path))
        self.assertEqual(1174, len(output))
        self.assertTrue('''\
diff --git local/fixed.txt local/fixed.txt
index e69de29..454f6b3 100644
--- local/fixed.txt
+++ local/fixed.txt
@@ -0,0 +1 @@
+0123456789abcdef
\ No newline at end of file''' in output, output)
        self.assertTrue('''
diff --git local/submodule/subsubmodule/subsubfixed.txt local/submodule/subsubmodule/subsubfixed.txt
index e69de29..1a332dc 100644
--- local/submodule/subsubmodule/subsubfixed.txt
+++ local/submodule/subsubmodule/subsubfixed.txt
@@ -0,0 +1 @@
+012345cdef
\ No newline at end of file''' in output, output)