1313# This can be removed when all tests support wasm.
1414def pytest_ignore_collect (collection_path : pathlib .Path ) -> bool :
1515 return collection_path .name not in [
16+ "test_add.py" ,
17+ "test_branch.py" ,
18+ "test_checkout.py"
1619 "test_clone.py" ,
20+ "test_commit.py" ,
21+ "test_config.py" ,
1722 "test_fixtures.py" ,
1823 "test_git.py" ,
1924 "test_init.py" ,
25+ "test_log.py" ,
26+ "test_merge.py" ,
27+ "test_rebase.py" ,
28+ "test_remote.py" ,
29+ "test_reset.py" ,
30+ "test_revlist.py" ,
31+ "test_revparse.py" ,
32+ "test_stash.py" ,
33+ "test_status.py" ,
2034 ]
2135
2236
@@ -48,6 +62,10 @@ def os_getcwd():
4862 return subprocess .run (["pwd" ], capture_output = True , check = True , text = True ).stdout .strip ()
4963
5064
65+ def os_remove (file : str ):
66+ return subprocess .run (["rm" , str (file )], capture_output = True , check = True , text = True )
67+
68+
5169class MockPath (pathlib .Path ):
5270 def __init__ (self , path : str = "" ):
5371 super ().__init__ (path )
@@ -69,6 +87,23 @@ def iterdir(self):
6987 for f in filter (lambda f : f not in ['' , '.' , '..' ], re .split (r"\r?\n" , p .stdout )):
7088 yield MockPath (self / f )
7189
90+ def mkdir (self ):
91+ subprocess .run (["mkdir" , str (self )], capture_output = True , text = True , check = True )
92+
93+ def read_text (self ) -> str :
94+ p = subprocess .run (["cat" , str (self )], capture_output = True , text = True , check = True )
95+ text = p .stdout
96+ if text .endswith ("\n " ):
97+ text = text [:- 1 ]
98+ return text
99+
100+ def write_text (self , data : str ):
101+ # Note that in general it is not valid to direct output of a subprocess.run call to a file,
102+ # but we get away with it here as the command arguments are passed straight through to
103+ # cockle without being checked.
104+ p = subprocess .run (["echo" , data , ">" , str (self )], capture_output = True , text = True )
105+ assert p .returncode == 0
106+
72107 def __truediv__ (self , other ):
73108 if isinstance (other , str ):
74109 return MockPath (f"{ self } /{ other } " )
@@ -82,25 +117,39 @@ def subprocess_run(
82117 capture_output : bool = False ,
83118 check : bool = False ,
84119 cwd : str | MockPath | None = None ,
120+ input : str | None = None ,
85121 text : bool | None = None
86122) -> subprocess .CompletedProcess :
87- shell_run = "async cmd => await window.cockle.shellRun(cmd)"
123+ shell_run = "async obj => await window.cockle.shellRun(obj. cmd, obj.input )"
88124
89125 # Set cwd.
90126 if cwd is not None :
91- proc = page .evaluate (shell_run , " pwd" )
127+ proc = page .evaluate (shell_run , { "cmd" : " pwd" } )
92128 if proc ['returncode' ] != 0 :
93129 raise RuntimeError ("Error getting pwd" )
94130 old_cwd = proc ['stdout' ].strip ()
95131 if old_cwd == str (cwd ):
96132 # cwd is already correct.
97133 cwd = None
98134 else :
99- proc = page .evaluate (shell_run , f"cd { cwd } " )
135+ proc = page .evaluate (shell_run , { "cmd" : f"cd { cwd } " } )
100136 if proc ['returncode' ] != 0 :
101137 raise RuntimeError (f"Error setting cwd to { cwd } " )
102138
103- proc = page .evaluate (shell_run , " " .join (cmd ))
139+ def maybe_wrap_arg (s : str | MockPath ) -> str :
140+ # An argument containing spaces needs to be wrapped in quotes if it is not already, due
141+ # to how the command is passed to cockle as a single string.
142+ # Could do better here.
143+ s = str (s )
144+ if ' ' in s and not s .endswith ("'" ):
145+ return "'" + s + "'"
146+ return s
147+
148+ shell_run_args = {
149+ "cmd" : " " .join ([maybe_wrap_arg (s ) for s in cmd ]),
150+ "input" : input
151+ }
152+ proc = page .evaluate (shell_run , shell_run_args )
104153
105154 # TypeScript object is auto converted to Python dict.
106155 # Want to return subprocess.CompletedProcess, consider namedtuple if this fails in future.
@@ -112,7 +161,7 @@ def subprocess_run(
112161
113162 # Reset cwd.
114163 if cwd is not None :
115- proc = page .evaluate (shell_run , " cd " + old_cwd )
164+ proc = page .evaluate (shell_run , { "cmd" : " cd " + old_cwd } )
116165 if proc ['returncode' ] != 0 :
117166 raise RuntimeError (f"Error setting cwd to { old_cwd } " )
118167
@@ -142,3 +191,4 @@ def mock_subprocess_run(page: Page, monkeypatch):
142191 monkeypatch .setattr (subprocess , "run" , partial (subprocess_run , page ))
143192 monkeypatch .setattr (os , "chdir" , os_chdir )
144193 monkeypatch .setattr (os , "getcwd" , os_getcwd )
194+ monkeypatch .setattr (os , "remove" , os_remove )
0 commit comments