def athome(cls, host: H, subdir: str) -> linux.Path[H]: """ Create a new workdir below the users home. Should be used inside the :meth:`~tbot.machine.linux.LinuxMachine.workdir` method, like this:: class MyMachine(...): ... @property def workdir(self) -> "linux.Path[MyMachine]": return linux.Workdir.athome(self, "tbot-work") :param linux.LinuxMachine host: Host for which this workdir should be defined. :param str subdir: Name of the subdirectory in $HOME which should be used as a workdir. :rtype: linux.Path :returns: A tbot-path to the workdir """ home = host.exec0("echo", linux.Env("HOME")).strip("\n") p = linux.Path(host, home) / subdir if not hasattr(host, "_wd_path"): if not p.is_dir(): host.exec0("mkdir", "-p", p) setattr(host, "_wd_path", p) return typing.cast(linux.Path[H], getattr(host, "_wd_path"))
def selftest_machine_shell( m: typing.Union[linux.LinuxMachine, board.UBootMachine] ) -> None: # Capabilities cap = [] if isinstance(m, linux.LinuxMachine): if m.shell == linux.shell.Bash: cap.extend(["printf", "jobs", "control"]) if m.shell == linux.shell.Ash: cap.extend(["printf", "control"]) tbot.log.message("Testing command output ...") out = m.exec0("echo", "Hello World") assert out == "Hello World\n", repr(out) out = m.exec0("echo", "$?", "!#") assert out == "$? !#\n", repr(out) if "printf" in cap: out = m.exec0("printf", "Hello World") assert out == "Hello World", repr(out) out = m.exec0("printf", "Hello\\nWorld") assert out == "Hello\nWorld", repr(out) out = m.exec0("printf", "Hello\nWorld") assert out == "Hello\nWorld", repr(out) s = "_".join(map(lambda i: f"{i:02}", range(80))) out = m.exec0("echo", s) assert out == f"{s}\n", repr(out) tbot.log.message("Testing return codes ...") assert m.test("true") assert not m.test("false") if isinstance(m, linux.LinuxMachine): tbot.log.message("Testing env vars ...") value = "12\nfoo !? # true; exit\n" m.exec0("export", f"TBOT_TEST_ENV_VAR={value}") out = m.env("TBOT_TEST_ENV_VAR") assert out == value, repr(out) tbot.log.message("Testing redirection (and weird paths) ...") f = m.workdir / ".redir test.txt" if f.exists(): m.exec0("rm", f) m.exec0("echo", "Some data\nAnd some more", stdout=f) out = m.exec0("cat", f) assert out == "Some data\nAnd some more\n", repr(out) tbot.log.message("Testing formatting ...") tmp = linux.Path(m, "/tmp/f o/bar") out = m.exec0("echo", linux.F("{}:{}:{}", tmp, linux.Pipe, "foo")) assert out == "/tmp/f o/bar:|:foo\n", repr(out) m.exec0("export", linux.F("NEWPATH={}:{}", tmp, linux.Env("PATH"), quote=False)) out = m.env("NEWPATH") assert out != "/tmp/f o/bar:${PATH}", repr(out) if "jobs" in cap: t1 = time.monotonic() out = m.exec0( "sleep", "10", linux.Background, "echo", "Hello World" ).strip() t2 = time.monotonic() assert re.match(r"\[\d+\] \d+\nHello World", out), repr(out) assert ( t2 - t1 ) < 9.0, ( f"Command took {t2 - t1}s (max 9s). Sleep was not sent to background" ) if "control" in cap: out = m.exec0( "false", linux.AndThen, "echo", "FOO", linux.OrElse, "echo", "BAR" ).strip() assert out == "BAR", repr(out) out = m.exec0( "true", linux.AndThen, "echo", "FOO", linux.OrElse, "echo", "BAR" ).strip() assert out == "FOO", repr(out) tbot.log.message("Testing subshell ...") out = m.env("SUBSHELL_TEST_VAR") assert out == "", repr(out) with m.subshell(): m.exec0("export", "SUBSHELL_TEST_VAR=123") out = m.env("SUBSHELL_TEST_VAR") assert out == "123", repr(out) out = m.env("SUBSHELL_TEST_VAR") assert out == "", repr(out) with m.subshell( "env", "SUBSHELL_TEST_VAR2=1337", "bash", "--norc", shell=linux.shell.Bash ): out = m.env("SUBSHELL_TEST_VAR2") assert out == "1337", repr(out) if isinstance(m, board.UBootMachine): tbot.log.message("Testing env vars ...") m.exec0("setenv", "TBOT_TEST", "Lorem ipsum dolor sit amet") out = m.exec0("printenv", "TBOT_TEST") assert out == "TBOT_TEST=Lorem ipsum dolor sit amet\n", repr(out) out = m.env("TBOT_TEST") assert out == "Lorem ipsum dolor sit amet", repr(out)
def selftest_user(lab: typing.Optional[linux.LabHost] = None, ) -> None: with lab or tbot.acquire_lab() as lh: lh.exec0("echo", linux.Env("USER"))