From c0dcea5ae5ddced2d97730f1f39374cf3692b478 Mon Sep 17 00:00:00 2001 From: Shawn Davis Date: Fri, 1 Jan 2021 18:26:30 -0500 Subject: [PATCH] Improved handling of sudo. --- scripttease/library/commands/base.py | 22 ++++++- tests/test_parsers_utils.py | 93 +++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 4 deletions(-) diff --git a/scripttease/library/commands/base.py b/scripttease/library/commands/base.py index 54ccd17..5af1c4f 100644 --- a/scripttease/library/commands/base.py +++ b/scripttease/library/commands/base.py @@ -106,7 +106,7 @@ class Command(object): a.append("%s &&" % self.prefix) if self.sudo: - statement = "sudo -u %s %s" % (self.sudo.user, self._get_statement()) + statement = "%s %s" % (self.sudo, self._get_statement()) else: statement = self._get_statement() @@ -147,6 +147,11 @@ class Command(object): """ return name in self._attributes + @property + def is_itemized(self): + """Always returns ``False``.""" + return False + def set_attribute(self, name, value): """Set the value of a dynamic attribute. @@ -218,7 +223,7 @@ class ItemizedCommand(object): return a - def get_statement(self, cd=False): + def get_statement(self, cd=False, suppress_comment=False): """Override to get multiple commands.""" kwargs = self.kwargs.copy() comment = kwargs.pop("comment", "execute multiple commands") @@ -228,7 +233,7 @@ class ItemizedCommand(object): commands = self.get_commands() for c in commands: - a.append(c.get_statement(cd=cd)) + a.append(c.get_statement(cd=cd, suppress_comment=suppress_comment)) a.append("") # for item in self.items: @@ -253,6 +258,11 @@ class ItemizedCommand(object): """ return name in self.kwargs + @property + def is_itemized(self): + """Always returns ``True``.""" + return True + def set_attribute(self, name, value): """Set the value of a dynamic attribute. @@ -286,3 +296,9 @@ class Sudo(object): def __bool__(self): return self.enabled + + def __str__(self): + if self.enabled: + return "sudo -u %s" % self.user + + return "" diff --git a/tests/test_parsers_utils.py b/tests/test_parsers_utils.py index d099a65..8317e15 100644 --- a/tests/test_parsers_utils.py +++ b/tests/test_parsers_utils.py @@ -1,6 +1,7 @@ +import os import pytest from scripttease.library.commands import Command, ItemizedCommand -from scripttease.parsers import filter_commands, load_commands +from scripttease.parsers.utils import * def test_filter_commands(): @@ -40,3 +41,93 @@ def test_load_commands(): } ) assert len(commands) == 2 + + +def test_load_variables(): + assert len(load_variables("nonexistent.ini")) == 0 + + assert len(load_variables(os.path.join("tests", "examples", "templates", "simple.txt"))) == 0 + + variables = load_variables(os.path.join("tests", "examples", "variables.ini")) + assert len(variables) == 5 + + variables = load_variables(os.path.join("tests", "examples", "variables.ini"), environment="testing") + assert len(variables) == 4 + + +class TestContext(object): + + def test_add(self): + c = Context() + c.add("testing", True) + assert len(c.variables) == 1 + + c.add("also_testing", False) + assert len(c.variables) == 2 + + assert isinstance(c.add("still_testing", True), Variable) + + with pytest.raises(RuntimeError): + c.add("testing", True) + + def test_get(self): + c = Context(testing=True) + assert c.get("testing") is True + assert c.get("nonexistent") is None + assert c.get("nonexistent", default=True) is True + + def test_getattr(self): + c = Context(testing=True) + assert c.testing is True + assert c.nonexistent is None + + def test_has(self): + c = Context(testing=True) + assert c.has("testing") is True + assert c.has("nonexistent") is False + + def test_init(self): + c = Context(testing=True, also_testing=123) + assert len(c.variables) == 2 + + def test_join(self): + c = Context(testing=True) + + variables = [ + Variable("testing", True), + Variable("also_testing", True), + ] + c.join(variables) + assert len(c.variables) == 2 + + def test_mapping(self): + c = Context(testing=True, also_testing=False, still_testing=True) + assert type(c.mapping()) is dict + assert len(c.mapping()) == 3 + + def test_merge(self): + c1 = Context(testing=True, also_testing=False) + c2 = Context(still_testing=True) + c1.merge(c2) + assert len(c1.variables) == 3 + + def test_repr(self): + c = Context(testing=True, also_testing=False, still_testing=True) + assert repr(c) == "" + + +class TestVariable(object): + + def test_eq(self): + var = Variable("testing", True) + assert var == True + + def test_getattr(self): + var = Variable("testing", True, one="a", two="b") + assert var.one == "a" + assert var.two == "b" + assert var.three is None + + def test_repr(self): + var = Variable("testing", True) + assert repr(var) == ""