def test_missing_filename(self): """ Executable() fails if filename is None or empty. """ with self.assertRaises(ValueError): Executable(None) with self.assertRaises(ValueError): Executable("")
def test_envvars(self): """ Executable.envvars gives a copy of the originally provided env vars. """ exe = Executable("my-exe", {"SPAM": "eggs"}) exe.envvars["SPAM"] = "ham" self.assertEqual(exe.envvars, {"SPAM": "eggs"})
def test_minimal(self): """ Executable() works with minimal arguments. """ exe = Executable("my-exe") self.assertEqual(exe.filename, "my-exe") self.assertIsNone(exe.envvars)
def test_conversion(self): """ Executable() converts the args to str. """ exe = Executable(u"my-exe", [(u"SPAM", u"eggs"), ("ham", "")]) self.assertEqual(exe.filename, "my-exe") self.assertEqual(exe.envvars, {"SPAM": "eggs"})
def test_full(self): """ Executable() works when provided all arguments. """ exe = Executable("my-exe", {"SPAM": "eggs"}) self.assertEqual(exe.filename, "my-exe") self.assertEqual(exe.envvars, {"SPAM": "eggs"})
def test_resolve_args(self): """ Executable.resolve_args() returns the args list that may be passed to subprocess.*(). """ exe = Executable("my-exe", {"SPAM": "eggs"}) args = exe.resolve_args("x", "-y", "z") self.assertEqual(args, ["my-exe", "x", "-y", "z"])
def test_run_executable_does_not_exist(self): """ Executable.run_out() fails if the executable does not exist. """ filename = self._resolve_executable("does-not-exist") exe = Executable(filename) with self.assertRaises(ExecutableNotFoundError): exe.run_out()
def test_run_out(self): """ Executable.run_out() runs the command and returns stdout. """ filename = self._write_executable("script") self.assertTrue(os.path.exists(filename)) exe = Executable(filename, {"SPAM": "eggs"}) out = exe.run_out("x", "-y", "z") self.assertTrue(out.startswith("x -y z\n")) self.assertIn("SPAM=eggs\n", out)
def test_run(self): """ Executable.run() runs the command and returns nothing. """ filename = self._write_executable("script") exe = Executable(filename, {"SPAM": "eggs"}) with tempfile.NamedTemporaryFile() as outfile: exe.run("x", "-y", "z", stdout=outfile) outfile.seek(0) out = outfile.read() self.assertTrue(out.startswith("x -y z\n")) self.assertIn("SPAM=eggs\n", out)
def test_cli_full(self): """FakeJuju.cli() works correctly when given all args.""" juju = FakeJuju("/fake-juju", "1.25.6", "/x") cli = juju.cli("/y", {"SPAM": "eggs"}) self.assertEqual( cli._exe, Executable( "/fake-juju", { "SPAM": "eggs", "FAKE_JUJU_DATA_DIR": "/x", "FAKE_JUJU_FAILURES": "/x/juju-failures", "FAKE_JUJU_LOGS_DIR": "/x", "JUJU_HOME": "/y", }), )
def test_cli_minimal(self): """FakeJuju.cli() works correctly when given minimal args.""" juju = FakeJuju("/fake-juju", "1.25.6", "/x") cli = juju.cli("/y") self.assertEqual( cli._exe, Executable( "/fake-juju", dict( os.environ, **{ "FAKE_JUJU_DATA_DIR": "/x", "FAKE_JUJU_FAILURES": "/x/juju-failures", "FAKE_JUJU_LOGS_DIR": "/x", "JUJU_HOME": "/y", })), )
def test_relative_filename(self): """ Executable() fails if filename is None or empty. """ with self.assertRaises(ValueError): Executable("x/y/z")