More tests

This commit is contained in:
Kovid Goyal 2023-07-20 21:24:24 +05:30
parent 39f7f2d7b4
commit 350af446d3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 32 additions and 10 deletions

View File

@ -1251,7 +1251,9 @@ func send_loop(opts *Options, files []*File) (err error, rc int) {
}
rc = 1
}
if lp.ExitCode() != 0 {
rc = lp.ExitCode()
}
return
}

View File

@ -307,12 +307,13 @@ class PTY:
def wait_till_child_exits(self, timeout=10, require_exit_code=None):
end_time = time.monotonic() + timeout
while time.monotonic() <= end_time:
status = os.waitid(os.P_PID, self.child_pid, os.WNOHANG | os.WEXITED)
if status is not None and status.si_pid == self.child_pid:
si_pid, status = os.waitpid(self.child_pid, os.WNOHANG)
if si_pid == self.child_pid and os.WIFEXITED(status):
ec = os.waitstatus_to_exitcode(status)
self.child_waited_for = True
if require_exit_code is not None and os.waitstatus_to_exitcode(status.si_status) != require_exit_code:
if require_exit_code is not None and ec != require_exit_code:
raise AssertionError(
f'Child exited with exit status: {status} code: {os.waitstatus_to_exitcode(status.si_status)} != {require_exit_code}.'
f'Child exited with exit status: {status} code: {ec} != {require_exit_code}.'
f' Screen contents:\n{self.screen_contents()}')
return status
self.process_input_from_child(timeout=0.02)

View File

@ -165,6 +165,7 @@ class PtyFileTransmission(FileTransmission):
def write_ftc_to_child(self, payload: FileTransmissionCommand, appendleft: bool = False, use_pending: bool = True) -> bool:
self.pty.write_to_child('\x1b]' + payload.serialize(prefix_with_osc_code=True) + '\x1b\\', flush=False)
return True
class TransferPTY(PTY):
@ -467,12 +468,12 @@ class TestFileTransmission(BaseTest):
self.assertEqual(h128.hexdigest(), '8d6b60383dfa90c21be79eecd1b1353d')
@contextmanager
def run_kitten(self, cmd, home_dir=''):
def run_kitten(self, cmd, home_dir='', allow=True):
homedir_ephemeral = not home_dir
home_dir = self.home_dir = home_dir or os.path.realpath(tempfile.mkdtemp())
cmd = [kitten_exe(), 'transfer'] + cmd
try:
pty = TransferPTY(cmd, cwd=home_dir)
pty = TransferPTY(cmd, cwd=home_dir, allow=allow)
i = 10
while i > 0 and not pty.screen_contents().strip():
pty.process_input_from_child()
@ -484,8 +485,26 @@ class TestFileTransmission(BaseTest):
def test_transfer_send(self):
src = os.path.join(self.tdir, 'src')
self.src_data = os.urandom(9137)
with open(src, 'wb') as s:
s.write(os.urandom(9137))
s.write(self.src_data)
dest = os.path.join(self.tdir, 'dest')
with self.run_kitten([src, dest]) as pty:
pty.wait_till_child_exits(require_exit_code=0)
with self.run_kitten([src, dest], allow=False) as pty:
pty.wait_till_child_exits(require_exit_code=1)
self.assertFalse(os.path.exists(dest))
def single_file(*cmd):
with self.run_kitten(list(cmd) + [src, dest]) as pty:
pty.wait_till_child_exits(require_exit_code=0)
with open(dest, 'rb') as f:
self.assertEqual(self.src_data, f.read())
single_file()
single_file()
single_file('--transmit-deltas')
with open(dest, 'wb') as d:
d.write(os.urandom(1023))
single_file('--transmit-deltas')
os.remove(dest)
single_file('--transmit-deltas')