4
4
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
5
5
6
6
from io import BytesIO
7
-
8
- from git .objects import Tree , Blob
9
- from test .lib import TestBase
10
-
11
- import os
12
7
import os .path as osp
8
+ from pathlib import Path
13
9
import subprocess
14
10
11
+ from git .objects import Tree , Blob
12
+ from git .util import cwd
13
+ from test .lib import TestBase , with_rw_directory
14
+
15
15
16
16
class TestTree (TestBase ):
17
17
def test_serializable (self ):
@@ -42,28 +42,39 @@ def test_serializable(self):
42
42
testtree ._deserialize (stream )
43
43
# END for each item in tree
44
44
45
- def test_tree_modifier_ordering (self ):
46
- def setup_git_repository_and_get_ordered_files ():
47
- os .mkdir ("tmp" )
48
- os .chdir ("tmp" )
45
+ @with_rw_directory
46
+ def _get_git_ordered_files (self , rw_dir ):
47
+ """Get files as git orders them, to compare in test_tree_modifier_ordering."""
48
+ # Create directory contents.
49
+ Path (rw_dir , "file" ).mkdir ()
50
+ for filename in (
51
+ "bin" ,
52
+ "bin.d" ,
53
+ "file.to" ,
54
+ "file.toml" ,
55
+ "file.toml.bin" ,
56
+ "file0" ,
57
+ ):
58
+ Path (rw_dir , filename ).touch ()
59
+ Path (rw_dir , "file" , "a" ).touch ()
60
+
61
+ with cwd (rw_dir ):
62
+ # Prepare the repository.
49
63
subprocess .run (["git" , "init" , "-q" ], check = True )
50
- os .mkdir ("file" )
51
- for filename in [
52
- "bin" ,
53
- "bin.d" ,
54
- "file.to" ,
55
- "file.toml" ,
56
- "file.toml.bin" ,
57
- "file0" ,
58
- "file/a" ,
59
- ]:
60
- open (filename , "a" ).close ()
61
-
62
64
subprocess .run (["git" , "add" , "." ], check = True )
63
65
subprocess .run (["git" , "commit" , "-m" , "c1" ], check = True )
64
- tree_hash = subprocess .check_output (["git" , "rev-parse" , "HEAD^{tree}" ]).decode ().strip ()
65
- cat_file_output = subprocess .check_output (["git" , "cat-file" , "-p" , tree_hash ]).decode ()
66
- return [line .split ()[- 1 ] for line in cat_file_output .split ("\n " ) if line ]
66
+
67
+ # Get git output from which an ordered file list can be parsed.
68
+ rev_parse_command = ["git" , "rev-parse" , "HEAD^{tree}" ]
69
+ tree_hash = subprocess .check_output (rev_parse_command ).decode ().strip ()
70
+ cat_file_command = ["git" , "cat-file" , "-p" , tree_hash ]
71
+ cat_file_output = subprocess .check_output (cat_file_command ).decode ()
72
+
73
+ return [line .split ()[- 1 ] for line in cat_file_output .split ("\n " ) if line ]
74
+
75
+ def test_tree_modifier_ordering (self ):
76
+ """TreeModifier.set_done() sorts files in the same order git does."""
77
+ git_file_names_in_order = self ._get_git_ordered_files ()
67
78
68
79
hexsha = "6c1faef799095f3990e9970bc2cb10aa0221cf9c"
69
80
roottree = self .rorepo .tree (hexsha )
@@ -92,9 +103,6 @@ def names_in_mod_cache():
92
103
here = file_names_in_order ()
93
104
return [e for e in a if e in here ]
94
105
95
- git_file_names_in_order = setup_git_repository_and_get_ordered_files ()
96
- os .chdir (".." )
97
-
98
106
mod .set_done ()
99
107
assert names_in_mod_cache () == git_file_names_in_order , "set_done() performs git-sorting"
100
108
0 commit comments