A collection of classes and commands for automated command line scripting using Python.
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.
 
 

263 lines
6.8 KiB

import pytest
from scripttease.lib.commands.posix import *
def test_append():
c = append("/path/to/file.txt", content="testing = yes")
assert 'echo "testing = yes" >> /path/to/file.txt' in c.get_statement()
def test_archive():
c = archive(
"/path/to/target",
absolute=True,
exclude="*.log",
strip=1,
view=True
)
s = c.get_statement()
print(s)
# tar -czPv --exclude *.log --strip-components 1 -f ./archive.tgz /path/to/target
assert "tar -czPv --exclude *.log --strip-components 1" in s
assert "-f ./archive.tgz /path/to/target" in s
def test_certbot():
with pytest.raises(ValueError):
c = certbot("example.com")
c = certbot("example.com", email="webmaster@example.com")
s = c.get_statement()
assert "certbot certonly --agree-tos --email webmaster@example.com -n" in s
assert "--webroot -w /var/www/domains/example_com/www -d example.com" in s
def test_copy():
c = copy("/path/to/file.txt", "/path/to/new-file.txt")
s = c.get_statement()
assert "cp" in s
assert "-n" in s
assert "/path/to/file.txt /path/to/new-file.txt" in s
c = copy("/path/to/dir", "/path/to/newdir", recursive=True)
s = c.get_statement()
assert "cp" in s
assert "-R" in s
assert "/path/to/dir /path/to/newdir" in s
def test_directory():
c = directory("/path/to/dir", group="www-data", mode=755, owner="deploy", recursive=True)
s = c.get_statement()
assert "mkdir" in s
assert "-m 755" in s
assert "-p" in s
assert "/path/to/dir" in s
c = directory("/path/to/dir", mode="755")
s = c.get_statement()
assert "-m 755" in s
c = directory("/path/to/dir", group="www-data", recursive=True)
s = c.get_statement()
assert "chgrp -R www-data" in s
c = directory("/path/to/dir", group="www-data")
s = c.get_statement()
assert "chgrp www-data" in s
c = directory("/path/to/dir", owner="deploy", recursive=True)
s = c.get_statement()
assert "chown -R deploy" in s
c = directory("/path/to/dir", owner="deploy")
s = c.get_statement()
assert "chown deploy" in s
def test_extract():
c = extract(
"/path/to/archive.tgz",
absolute=True,
exclude="*.log",
strip=1,
view=True
)
s = c.get_statement()
assert "tar -xzPv --exclude *.log --strip-components 1" in s
assert "-f /path/to/archive.tgz ./" in s
def test_link():
c = link("/var/www/domains", force=True)
s = c.get_statement()
assert "ln -s" in s
assert "-f" in s
assert "/var/www/domains" in s
def test_move():
c = move("/path/to/file.txt", "/path/to/file.txt.b")
assert "mv /path/to/file.txt /path/to/file.txt.b" in c.get_statement()
def test_perms():
c = perms("/path/to/dir", group="www-data", mode=755, owner="deploy", recursive=True)
s = c.get_statement()
assert "chgrp -R www-data /path/to/dir" in s
assert "chown -R deploy /path/to/dir" in s
assert "chmod -R 755 /path/to/dir" in s
c = perms("/path/to/dir", mode=755)
s = c.get_statement()
assert "chmod 755 /path/to/dir" in s
def test_prompt():
# This is just a placeholder to test the prompt() interface. See TestPrompt.
c = prompt("testing")
assert isinstance(c, Prompt)
def test_remove():
c = remove("/path/to/dir", force=True, recursive=True)
s = c.get_statement()
assert "rm" in s
assert "-f" in s
assert "-r" in s
assert "/path/to/dir" in s
def test_rsync():
c = rsync(
"/path/to/local/",
"/path/to/remote",
links=True,
delete=True,
exclude="deploy/exclude.txt",
recursive=True,
host="example.com",
key_file="~/.ssh/deploy",
user="deploy"
)
s = c.get_statement()
assert "rsync" in s
assert "--checksum" in s
assert "--compress" in s
assert "--copy-links" in s
assert "--delete" in s
assert "--exclude-from=deploy/exclude.txt" in s
assert "-P" in s
assert "--recursive /path/to/local/" in s
assert '-e "ssh -i ~/.ssh/deploy -p 22"' in s
assert "deploy@example.com:/path/to/remote" in s
c = rsync(
"/path/to/local/",
"/path/to/remote",
links=True,
delete=True,
exclude="deploy/exclude.txt",
recursive=True,
)
s = c.get_statement()
assert "rsync" in s
assert "--checksum" in s
assert "--compress" in s
assert "--copy-links" in s
assert "--delete" in s
assert "--exclude-from=deploy/exclude.txt" in s
assert "-P" in s
assert "--recursive" in s
assert "/path/to/local/" in s
assert "/path/to/remote" in s
def test_run():
c = run("ls -ls")
s = c.get_statement()
assert "ls -ls" in s
def test_scopy():
with pytest.raises(ValueError):
c = scopy("/path/to/local/file.txt", "/path/to/remote/file.txt")
c = scopy(
"/path/to/local/file.txt",
"/path/to/remote/file.txt",
key_file="~/.ssh/deploy",
host="example.com",
user="deploy"
)
s = c.get_statement()
assert "scp -i ~/.ssh/deploy" in s
assert "-P 22" in s
assert "/path/to/local/file.txt" in s
assert "deploy@example.com:/path/to/remote/file.txt" in s
c = scopy(
"/path/to/local/file.txt",
"/path/to/remote/file.txt",
host="example.com",
)
s = c.get_statement()
assert "scp -P 22" in s
assert "/path/to/local/file.txt" in s
assert "example.com:/path/to/remote/file.txt" in s
def test_sed():
c = replace("/path/to/file.txt", find="testing", sub="123")
s = c.get_statement()
assert "sed -i .b" in s
assert "s/testing/123/g" in s
assert "/path/to/file.txt" in s
def test_sync():
c = sync(
"/path/to/local/",
"/path/to/remote",
links=True,
delete=True,
exclude="~/exclude.txt",
recursive=True,
)
s = c.get_statement()
assert "rsync" in s
assert "--checksum" in s
assert "--compress" in s
assert "--copy-links" in s
assert "--delete" in s
assert "--exclude-from=~/exclude.txt" in s
assert "-P" in s
assert "--recursive /path/to/local/" in s
def test_touch():
c = touch("/path/to/file.txt")
assert "touch /path/to/file.txt" in c.get_statement()
def test_wait():
c = wait(5)
s = c.get_statement()
assert 'sleep 5' in s
def test_write():
c = write("/path/to/file.txt", content="testing 123")
assert 'echo "testing 123" > /path/to/file.txt' in c.get_statement()
content = [
"I am testing",
"I am testing",
"I am testing",
"testing 123",
]
c = write("/path/to/file.txt", content="\n".join(content))
s = c.get_statement()
assert "cat > /path/to/file.txt << EOF" in s
assert "I am testing" in s
assert "testing 123" in s