def test_armor_raw(): raw_encoded = sh('python -m saltpack armor --raw', input=inputstr).read() print(raw_encoded) raw_decoded = sh('python -m saltpack dearmor --raw', input=raw_encoded).read() print(raw_decoded) assert inputstr == raw_decoded
def test_local_path_doesnt_match_PATH(): echo_path = Path('echo') assert not echo_path.exists(), 'This path is supposed to be nonexistent.' with raises(PROGRAM_NOT_FOUND_ERROR): cmd(echo_path).run() with raises(duct.StatusError): sh(echo_path).run()
def test_encryption(): encrypted = sh("python -m saltpack encrypt") \ .input(message) \ .read() print(encrypted) decrypted = sh("python -m saltpack decrypt --debug") \ .input(encrypted) \ .read() assert message == decrypted, repr(message) + " != " + repr(decrypted)
def test_start(): handle1 = sh('echo one').stdout_capture().start() handle2 = sh('echo two').stdout_capture().start() result1 = handle1.wait() result2 = handle2.wait() assert b"one" + NEWLINE == result1.stdout assert b"" == result1.stderr assert 0 == result1.status assert b"two" + NEWLINE == result2.stdout assert b"" == result2.stderr assert 0 == result2.status
def test_stderr(): # with a file path temp = mktemp() sh('echo hi').stdout_to_stderr().stderr(temp).run() with open(temp) as f: assert 'hi\n' == f.read() # with a Path path if has_pathlib: temp = mktemp() sh('echo hi').stdout_to_stderr().stderr(Path(temp)).run() with open(temp) as f: assert 'hi\n' == f.read() # with an open file temp = mktemp() with open(temp, 'w') as f: sh('echo hi').stdout_to_stderr().stderr_file(f).run() with open(temp) as f: assert 'hi\n' == f.read() # to /dev/null out = sh('echo hi').stdout_to_stderr().stderr_null().read() assert '' == out # to stdout result = (sh('echo hi') .stdout_to_stderr() .stderr_to_stdout() .stdout_capture() .stderr_capture() .run()) assert b'hi' + NEWLINE == result.stdout assert b'' == result.stderr
def test_stderr(): # with a file path temp = mktemp() sh('echo hi').stdout_to_stderr().stderr(temp).run() with open(temp) as f: assert 'hi\n' == f.read() # with a Path path if has_pathlib: temp = mktemp() sh('echo hi').stdout_to_stderr().stderr(Path(temp)).run() with open(temp) as f: assert 'hi\n' == f.read() # with an open file temp = mktemp() with open(temp, 'w') as f: sh('echo hi').stdout_to_stderr().stderr_file(f).run() with open(temp) as f: assert 'hi\n' == f.read() # to /dev/null out = sh('echo hi').stdout_to_stderr().stderr_null().read() assert '' == out # to stdout result = (sh('echo hi').stdout_to_stderr().stderr_to_stdout(). stdout_capture().stderr_capture().run()) assert b'hi' + NEWLINE == result.stdout assert b'' == result.stderr
def test_run_local_path(): '''Trying to execute 'test.sh' without the leading dot fails in bash and subprocess.py. But it needs to succeed with Path('test.sh'), because there's no difference between that and Path('./test.sh').''' if os.name == 'nt': extension = '.bat' code = textwrap.dedent(u'''\ @echo off echo foo ''') else: extension = '.sh' code = textwrap.dedent(u'''\ #! /bin/sh echo foo ''') # Use a random name just in case. random_letters = binascii.hexlify(os.urandom(4)).decode() local_script = 'test_' + random_letters + extension script_path = Path(local_script) try: with script_path.open('w') as f: f.write(code) script_path.chmod(0o755) assert 'foo' == cmd(script_path).read() assert 'foo' == sh(script_path).read() finally: script_path.unlink()
def test_unchecked(): assert 1 == false().unchecked().run().status with raises(StatusError) as e: false().run() assert e.value.result.status == 1 # Make sure unchecked errors don't short-circuit a `then`. output = false().unchecked().then(sh("echo hi")).read() assert output == "hi"
def test_sign_detached(): detached = sh("python -m saltpack sign --binary --detached").read( input=message, decode=False) print(detached) _, temp = tempfile.mkstemp() with open(temp, 'wb') as f: f.write(detached) command = ["python", "-m", "saltpack", "verify", "--signature", temp, "--binary", "--debug"] cmd(*command).read(input=message)
def test_sign_detached(): detached = sh("python -m saltpack sign --binary --detached").read( input=message, decode=False) print(detached) _, temp = tempfile.mkstemp() with open(temp, 'wb') as f: f.write(detached) command = [ "python", "-m", "saltpack", "verify", "--signature", temp, "--binary", "--debug" ] cmd(*command).read(input=message)
def test_commands_can_be_paths(): tempdir = tempfile.mkdtemp() path = Path(tempdir, "script.bat") # Note that Path.open() rejects Python 2 non-unicode strings. with open(str(path), 'w') as f: if os.name == 'nt': f.write('@echo off\n') else: f.write('#! /bin/sh\n') f.write('echo some stuff\n') path.chmod(0o755) assert 'some stuff' == cmd(path).read() assert 'some stuff' == sh(path).read()
def main(): os.chdir(str(here)) status = sh("git status --porcelain").read() if status: print("repo isn't clean") return 1 cmd("git", "push", "origin", "master").run() cmd( "ssh", "*****@*****.**", "cd /srv/jacko.io && git pull --ff-only && peru sync --no-cache", ).run()
def test_sign_detached(): detached = sh("python -m saltpack sign --binary --detached") \ .input(message) \ .stdout_capture() \ .run() \ .stdout print(detached) _, temp = tempfile.mkstemp() with open(temp, 'wb') as f: f.write(detached) command = [ "python", "-m", "saltpack", "verify", "--signature", temp, "--binary", "--debug" ] cmd(*command).input(message).read()
def test_block(): blocked = sh('python -m saltpack block', input=inputstr).read() print(blocked) unblocked = sh('python -m saltpack unblock', input=blocked).read() print(unblocked) assert inputstr == unblocked
def test_sign_attached(): signed = sh("python -m saltpack sign").read(input=message) print(signed) verified = sh("python -m saltpack verify --debug").read(input=signed) assert message == verified, repr(message) + " != " + repr(verified)
def test_encryption(): encrypted = sh("python -m saltpack encrypt") \ .read(input=message, decode=False) print(encrypted) decrypted = sh("python -m saltpack decrypt --debug").read(input=encrypted) assert message == decrypted, repr(message) + " != " + repr(decrypted)
def test_efficient(): efficient = sh('python -m saltpack efficient 64').read() print(efficient) assert re.search('3 bytes.*4 chars', efficient)
def test_swap_and_redirect_at_same_time(): '''We need to make sure that doing e.g. stderr_to_stdout while also doing stdout_capture means that stderr joins the redirected stdout, rather than joining what stdout used to be.''' err_out = sh('echo hi>&2').stderr_to_stdout().read() assert err_out == 'hi'
def test_nesting(): innermost = true().then(replace('i', 'o')) middle = true().then(innermost) out = sh('echo hi').pipe(middle).read() assert 'ho' == out
def test_result(): result = sh('echo more stuff').stdout_capture().run() assert b"more stuff" + NEWLINE == result.stdout assert b"" == result.stderr assert 0 == result.status
def test_hello_world(): out = sh('echo hello world').read() assert "hello world" == out
def test_string_mode_returns_unicode(): '''In Python 2, reading a file in text mode still returns a raw string, instead of a unicode string. Make sure we convert.''' out = sh('echo hi').read() assert isinstance(out, type(u''))
def test_armor(): encoded = sh('python -m saltpack armor', input=inputstr).read() print(encoded) decoded = sh('python -m saltpack dearmor', input=encoded).read() print(decoded) assert inputstr == decoded