-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hurry.py
96 lines (65 loc) · 2.89 KB
/
test_hurry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import sys
import json
import subprocess
from collections import OrderedDict
import pytest
import hurry
@pytest.fixture()
def create_hurry_json(tmpdir):
path_to_file = tmpdir.join('hurry.json')
os.chdir(os.path.dirname(path_to_file))
def creator(dictionary):
with open(path_to_file, "w+") as hurry_json:
json.dump(dictionary, hurry_json)
return creator
def test_generate_exec_str():
conf = {
'one': 'one command',
'two words': 'two words command',
'start <name>': 'docker run <name>',
'end <arg1> <arg2>': 'arg1=<arg1> arg2=<arg2>',
}
sys.argv = ['hurry', 'one']
assert hurry.generate_exec_str(conf) == 'one command'
sys.argv = ['hurry', 'two', 'words']
assert hurry.generate_exec_str(conf) == 'two words command'
sys.argv = ['hurry', 'start', 'service']
assert hurry.generate_exec_str(conf) == 'docker run service'
sys.argv = ['hurry', 'end', 'test', 'prod']
assert hurry.generate_exec_str(conf) == 'arg1=test arg2=prod'
sys.argv = ['hurry', 'one', '--', 'dynamic', 'arg']
assert hurry.generate_exec_str(conf) == 'one command dynamic arg'
sys.argv = ['hurry', 'one', 'unknown']
assert hurry.generate_exec_str(conf) is None
def test_run_hurry_e2e(create_hurry_json, tmpdir):
create_hurry_json({
'write <file_path>': 'echo "this is e2e test" > <file_path>',
'read <file_path>': 'cat <file_path>',
})
test_file = tmpdir.join("test.txt")
stdout = execute('hurry write ' + str(test_file))
assert stdout.decode().strip() == 'Execute: echo "this is e2e test" > ' + str(test_file)
assert test_file.read().strip() == 'this is e2e test'
stdout = execute('hurry read some/path')
assert stdout.decode().strip() == 'Execute: cat some/path'
stdout = execute('hurry unknown')
help_msg = 'Usage:\n hurry write <file_path>\n hurry read <file_path>'
assert stdout.decode().strip() == help_msg
stdout = execute('hurry read file -- hello')
assert stdout.decode().strip() == 'Execute: cat file hello'
def execute(command):
src = os.path.abspath(os.path.dirname(__file__))
return subprocess.check_output("PYTHONPATH={src} python {src}/bin/{cmd}".format(src=src, cmd=command), shell=True)
def test_run_hurry_without_hurry_json(capsys, tmpdir):
os.chdir(tmpdir)
hurry.main()
stdout, _stderr = capsys.readouterr()
assert stdout.strip() == "Can't find hurry.json in the current folder"
@pytest.mark.parametrize("content", ("\"-p wow! first with dash!\"", "\"first without dash\""))
def test_run_hurry_with_many_args(create_hurry_json, tmpdir, content):
create_hurry_json({"write <file_path> <content>": "echo \"<content>\" > <file_path>"})
test_file = tmpdir.join("test.txt")
sys.argv[1:] = ["write", str(test_file), content]
hurry.main()
assert test_file.read().strip() == content.strip("\"")