You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
444 lines
14 KiB
444 lines
14 KiB
from scripttease.lib.commands.base import Command, Content, ItemizedCommand, MultipleCommands, Prompt, Sudo, Template
|
|
from scripttease.lib.commands.python import python_pip
|
|
|
|
|
|
class TestCommand(object):
|
|
|
|
def test_getattr(self):
|
|
c = Command("ls -ls", extra=True)
|
|
assert c.extra is True
|
|
|
|
def test_get_statement(self):
|
|
c = Command(
|
|
"ls -ls",
|
|
comment="kitchen sink",
|
|
condition="$last_command -eq 0",
|
|
cd="/path/to/project",
|
|
prefix="source python/bin/active",
|
|
register="list_success",
|
|
stop=True,
|
|
sudo="deploy"
|
|
)
|
|
statement = c.get_statement(cd=True)
|
|
assert "( cd" in statement
|
|
assert "sudo" in statement
|
|
assert ")" in statement
|
|
assert "# kitchen sink" in statement
|
|
assert "if [[ $last_command" in statement
|
|
assert "list_success=$?" in statement
|
|
assert "if [[ $list_success" in statement
|
|
|
|
c = Command(
|
|
"ls -ls",
|
|
stop=True
|
|
)
|
|
statement = c.get_statement()
|
|
assert "if [[ $?" in statement
|
|
|
|
def test_init(self):
|
|
c = Command("ls -ls", sudo=Sudo(user="deploy"))
|
|
assert isinstance(c.sudo, Sudo)
|
|
assert c.sudo.user == "deploy"
|
|
|
|
c = Command("ls -ls", sudo="deploy")
|
|
assert isinstance(c.sudo, Sudo)
|
|
assert c.sudo.user == "deploy"
|
|
|
|
c = Command("ls -ls", sudo=True)
|
|
assert isinstance(c.sudo, Sudo)
|
|
assert c.sudo.user == "root"
|
|
|
|
c = Command("ls -ls")
|
|
assert isinstance(c.sudo, Sudo)
|
|
assert c.sudo.user == "root"
|
|
assert c.sudo.enabled is False
|
|
|
|
def test_is_itemized(self):
|
|
c = Command("ls -ls")
|
|
assert c.is_itemized is False
|
|
|
|
def test_repr(self):
|
|
c = Command("ls -ls", comment="listing")
|
|
assert repr(c) == "<Command listing>"
|
|
|
|
c = Command("ls -ls")
|
|
assert repr(c) == "<Command>"
|
|
|
|
|
|
class TestContent(object):
|
|
|
|
def test_getattr(self):
|
|
c = Content("testing", extra=True)
|
|
assert c.extra is True
|
|
|
|
def test_get_image_output(self):
|
|
c = Content("screenshot", caption="this is a test", image="static/images/testing.png")
|
|
assert "# this is a test" in c.get_output("bash")
|
|
assert "# static/images/testing.png" in c.get_output("bash")
|
|
|
|
c.css = "img right"
|
|
c.height = 100
|
|
c.width = 150
|
|
assert '<img src="static/images/testing.png"' in c.get_output("html")
|
|
assert 'alt="this is a test"' in c.get_output("html")
|
|
assert 'class="img right"' in c.get_output("html")
|
|
assert 'height="100"' in c.get_output("html")
|
|
assert 'width="150"' in c.get_output("html")
|
|
|
|
assert c.get_output("md") == "![this is a test](static/images/testing.png)\n"
|
|
|
|
assert ".. figure:: static/images/testing.png" in c.get_output("rst")
|
|
assert ":alt: this is a test" in c.get_output("rst")
|
|
assert ":height: 100" in c.get_output("rst")
|
|
assert ":width: 150" in c.get_output("rst")
|
|
|
|
assert "this is a test: static/images/testing.png" in c.get_output("plain")
|
|
c.caption = None
|
|
assert "static/images/testing.png" in c.get_output("plain")
|
|
|
|
def test_get_message_output(self):
|
|
c = Content("explain", message="this is a test")
|
|
assert "# this is a test" in c.get_output("bash")
|
|
assert "<p>this is a test" in c.get_output("html")
|
|
assert "this is a test" in c.get_output("md")
|
|
assert "this is a test" in c.get_output("rst")
|
|
assert "this is a test" in c.get_output("plain")
|
|
|
|
c.heading = "Test"
|
|
assert "# Test: this is a test" in c.get_output("bash")
|
|
assert "<h2>Test</h2>" in c.get_output("html")
|
|
assert "## Test" in c.get_output("md")
|
|
assert "Test" in c.get_output("rst")
|
|
assert "====" in c.get_output("rst")
|
|
assert "***** Test *****" in c.get_output("plain")
|
|
|
|
def test_get_output(self):
|
|
c = Content("testing")
|
|
assert c.get_output("nonexistent") is None
|
|
|
|
def test_is_itemized(self):
|
|
c = Content("testing")
|
|
assert c.is_itemized is False
|
|
|
|
def test_repr(self):
|
|
c = Content("testing")
|
|
assert repr(c) == "<Content testing>"
|
|
|
|
def test_get_statement(self):
|
|
c = Content("screenshot", caption="this is a test", image="static/images/testing.png")
|
|
assert "# this is a test" in c.get_statement()
|
|
assert "# static/images/testing.png" in c.get_statement()
|
|
|
|
|
|
class TestItemizedCommand(object):
|
|
|
|
def test_getattr(self):
|
|
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item", extra=True)
|
|
assert c.extra is True
|
|
|
|
def test_get_commands(self):
|
|
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
|
|
commands = c.get_commands()
|
|
for i in commands:
|
|
assert isinstance(i, Command)
|
|
|
|
def test_get_statement(self):
|
|
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
|
|
statement = c.get_statement()
|
|
assert "Pillow" in statement
|
|
assert "psycopg2-binary" in statement
|
|
assert "django" in statement
|
|
|
|
def test_is_itemized(self):
|
|
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
|
|
assert c.is_itemized is True
|
|
|
|
def test_repr(self):
|
|
c = ItemizedCommand(python_pip, ["Pillow", "psycopg2-binary", "django"], "$item")
|
|
assert repr(c) == "<ItemizedCommand python_pip>"
|
|
|
|
|
|
class TestMultipleCommands(object):
|
|
|
|
def test_getattr(self):
|
|
commands = [
|
|
Command("ls -ls", name="run"),
|
|
Command("pip install django", name="pip"),
|
|
Command("touch /tmp/testing.txt", name="touch")
|
|
]
|
|
multiple = MultipleCommands(commands, extra=True)
|
|
assert multiple.extra is True
|
|
|
|
def test_iter(self):
|
|
commands = [
|
|
Command("ls -ls", name="run"),
|
|
Command("pip install django", name="pip"),
|
|
Command("touch /tmp/testing.txt", name="touch")
|
|
]
|
|
multiple = MultipleCommands(commands)
|
|
|
|
count = 0
|
|
for c in multiple:
|
|
count += 1
|
|
|
|
assert count == 3
|
|
|
|
def test_repr(self):
|
|
commands = [
|
|
Command("ls -ls", name="run"),
|
|
Command("pip install django", name="pip"),
|
|
Command("touch /tmp/testing.txt", name="touch")
|
|
]
|
|
multiple = MultipleCommands(commands)
|
|
assert repr(multiple) == "<MultipleCommands run multiple commands>"
|
|
|
|
def test_get_statement(self):
|
|
commands = [
|
|
Command("ls -ls", name="run"),
|
|
Command("pip install django", name="pip"),
|
|
Command("touch /tmp/testing.txt", name="touch")
|
|
]
|
|
multiple = MultipleCommands(commands)
|
|
statement = multiple.get_statement()
|
|
assert "ls -ls" in statement
|
|
assert "pip install django" in statement
|
|
assert "touch /tmp/testing.txt" in statement
|
|
|
|
|
|
class TestPrompt(object):
|
|
|
|
def test_get_dialog_statement(self):
|
|
prompt = Prompt("testing", choices=["1", "2", "3"], default="1", dialog=True, help_text="This is a test.", label="Test")
|
|
statement = prompt.get_statement()
|
|
assert "--menu" in statement
|
|
assert "This is a test." in statement
|
|
|
|
prompt = Prompt("testing", choices="1,2,3", default="1", dialog=True, help_text="This is a test.", label="Test")
|
|
statement = prompt.get_statement()
|
|
assert "--menu" in statement
|
|
|
|
prompt = Prompt("testing", default="1", dialog=True, help_text="This is a test.", label="Test")
|
|
statement = prompt.get_statement()
|
|
assert "--inputbox" in statement
|
|
assert "This is a test." in statement
|
|
|
|
prompt = Prompt("testing", default="1", dialog=True, label="Test")
|
|
statement = prompt.get_statement()
|
|
assert "--inputbox" in statement
|
|
|
|
def test_get_read_statement(self):
|
|
prompt = Prompt("testing", choices="1,2,3", default="1", help_text="This is a test.", label="Test")
|
|
statement = prompt.get_statement()
|
|
assert "select opt in" in statement
|
|
|
|
prompt = Prompt("testing", default="1", help_text="This is a test.", label="Test")
|
|
statement = prompt.get_statement()
|
|
assert "echo -n" in statement
|
|
|
|
|
|
class TestSudo(object):
|
|
|
|
def test_bool(self):
|
|
s = Sudo()
|
|
assert bool(s) is False
|
|
|
|
s = Sudo(True)
|
|
assert bool(s) is True
|
|
|
|
def test_str(self):
|
|
s = Sudo()
|
|
assert str(s) == ""
|
|
|
|
s = Sudo(True)
|
|
assert str(s) == "sudo -u root"
|
|
|
|
|
|
class TestTemplate(object):
|
|
|
|
def test_getattr(self):
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/simple.txt",
|
|
"tests/tmp/simple.txt",
|
|
backup=False,
|
|
# context=context,
|
|
parser=Template.PARSER_SIMPLE,
|
|
**context
|
|
)
|
|
assert t.testing == "yes"
|
|
assert t.times == 123
|
|
|
|
def test_get_content(self):
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/simple.txt",
|
|
"tests/tmp/simple.txt",
|
|
backup=False,
|
|
# context=context,
|
|
parser=Template.PARSER_SIMPLE,
|
|
**context
|
|
)
|
|
content = t.get_content()
|
|
assert "I am testing? yes" in content
|
|
assert "How many times? 123" in content
|
|
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/simple.sh.txt",
|
|
"tests/tmp/simple.sh",
|
|
backup=False,
|
|
# context=context,
|
|
parser=Template.PARSER_SIMPLE,
|
|
**context
|
|
)
|
|
content = t.get_content()
|
|
assert "I am testing? yes" in content
|
|
assert "How many times? 123" in content
|
|
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/good.j2.txt",
|
|
"tests/tmp/good.txt",
|
|
backup=False,
|
|
# context=context
|
|
**context
|
|
)
|
|
content = t.get_content()
|
|
assert "I am testing? yes" in content
|
|
assert "How many times? 123" in content
|
|
|
|
t = Template("tests/examples/templates/nonexistent.j2.txt", "test/tmp/nonexistent.txt")
|
|
assert t.get_content() is None
|
|
|
|
t = Template("tests/examples/templates/bad.j2.txt", "test/tmp/nonexistent.txt")
|
|
assert t.get_content() is None
|
|
|
|
context = {
|
|
'testing': True,
|
|
'times': 3,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/settings.py",
|
|
"test/tmp/settings.py",
|
|
# context=context,
|
|
parser=Template.PARSER_PYTHON,
|
|
**context
|
|
)
|
|
content = t.get_content()
|
|
assert "TESTING = True" in content
|
|
assert "TOTAL_TIMES = 3" in content
|
|
|
|
def test_get_statement(self):
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/simple.txt",
|
|
"tests/tmp/simple.txt",
|
|
# context=context,
|
|
comment="A simple parser example.",
|
|
parser=Template.PARSER_SIMPLE,
|
|
register="template_created",
|
|
stop=True,
|
|
sudo=Sudo(user="root"),
|
|
**context
|
|
)
|
|
s = t.get_statement()
|
|
assert "I am testing? yes" in s
|
|
assert "How many times? 123" in s
|
|
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/simple.sh.txt",
|
|
"tests/tmp/simple.txt",
|
|
# context=context,
|
|
parser=Template.PARSER_SIMPLE,
|
|
stop=True,
|
|
sudo="root",
|
|
**context
|
|
)
|
|
s = t.get_statement()
|
|
assert "I am testing? yes" in s
|
|
assert "How many times? 123" in s
|
|
|
|
context = {
|
|
'testing': "yes",
|
|
'times': 123,
|
|
}
|
|
t = Template(
|
|
"tests/examples/templates/good.j2.txt",
|
|
"tests/tmp/good.txt",
|
|
# context=context,
|
|
sudo=True,
|
|
**context
|
|
)
|
|
s = t.get_statement()
|
|
assert "I am testing? yes" in s
|
|
assert "How many times? 123" in s
|
|
|
|
t = Template(
|
|
"tests/examples/templates/simple.txt",
|
|
"tests/tmp/simple.txt",
|
|
parser="nonexistent"
|
|
)
|
|
assert "# NO CONTENT AVAILABLE" in t.get_statement()
|
|
|
|
def test_repr(self):
|
|
t = Template("/path/to/template.conf", "/path/to/file.conf")
|
|
assert repr(t) == "<Template /path/to/template.conf>"
|
|
|
|
def test_get_target_language(self):
|
|
t = Template("/path/to/template.conf", "/path/to/file.conf", lang="c++")
|
|
assert t.get_target_language() == "c++"
|
|
|
|
t = Template("/path/to/template.conf", "/path/to/file.conf")
|
|
assert t.get_target_language() == "conf"
|
|
|
|
t = Template("/path/to/template.ini", "/path/to/file.ini")
|
|
assert t.get_target_language() == "ini"
|
|
|
|
t = Template("/path/to/template.php", "/path/to/file.php")
|
|
assert t.get_target_language() == "php"
|
|
|
|
t = Template("/path/to/template.py", "/path/to/file.py")
|
|
assert t.get_target_language() == "python"
|
|
|
|
t = Template("/path/to/template.sh", "/path/to/file.sh")
|
|
assert t.get_target_language() == "bash"
|
|
|
|
t = Template("/path/to/template.sql", "/path/to/file.sql")
|
|
assert t.get_target_language() == "sql"
|
|
|
|
t = Template("/path/to/template.yml", "/path/to/file.yml")
|
|
assert t.get_target_language() == "yaml"
|
|
|
|
t = Template("/path/to/template.txt", "/path/to/file.txt")
|
|
assert t.get_target_language() == "text"
|
|
|
|
def test_get_template(self):
|
|
t = Template(
|
|
"simple.txt",
|
|
"tests/tmp/simple.txt",
|
|
locations=["tests/examples/templates"]
|
|
)
|
|
assert t.get_template() == "tests/examples/templates/simple.txt"
|
|
|
|
def test_is_itemized(self):
|
|
t = Template("/path/to/template.conf", "/path/to/file.conf")
|
|
assert t.is_itemized is False
|
|
|