Tests: switched to using f-strings.
Previously, it was necessary to support older versions of Python for compatibility. F-strings were released in Python 3.6. Python 3.5 was marked as unsupported by the end of 2020, so now it's possible to start using f-strings safely for better readability and performance.
This commit is contained in:
@@ -14,23 +14,21 @@ class TestApplicationGo(TestApplicationProto):
|
||||
except subprocess.CalledProcessError:
|
||||
return None
|
||||
|
||||
temp_dir = option.temp_dir + '/go/'
|
||||
temp_dir = f'{option.temp_dir}/go/'
|
||||
|
||||
if not os.path.exists(temp_dir):
|
||||
os.mkdir(temp_dir)
|
||||
|
||||
cache_dir = option.cache_dir + '/go-build'
|
||||
cache_dir = f'{option.cache_dir}/go-build'
|
||||
|
||||
if not os.path.exists(cache_dir):
|
||||
os.mkdir(cache_dir)
|
||||
|
||||
env = os.environ.copy()
|
||||
env['GOPATH'] = option.current_dir + '/build/go'
|
||||
env['GOPATH'] = f'{option.current_dir}/build/go'
|
||||
env['GOCACHE'] = cache_dir
|
||||
|
||||
shutil.copy2(
|
||||
option.test_dir + '/go/' + script + '/' + name + '.go', temp_dir
|
||||
)
|
||||
shutil.copy2(f'{option.test_dir}/go/{script}/{name}.go', temp_dir)
|
||||
|
||||
if static:
|
||||
args = [
|
||||
@@ -41,21 +39,21 @@ class TestApplicationGo(TestApplicationProto):
|
||||
'-ldflags',
|
||||
'-extldflags "-static"',
|
||||
'-o',
|
||||
temp_dir + name,
|
||||
temp_dir + name + '.go',
|
||||
f'{temp_dir}{name}',
|
||||
f'{temp_dir}{name}.go',
|
||||
]
|
||||
else:
|
||||
args = [
|
||||
'go',
|
||||
'build',
|
||||
'-o',
|
||||
temp_dir + name,
|
||||
temp_dir + name + '.go',
|
||||
f'{temp_dir}{name}',
|
||||
f'{temp_dir}{name}.go',
|
||||
]
|
||||
|
||||
replace_path = option.current_dir + '/build/go/src/unit.nginx.org/go'
|
||||
replace_path = f'{option.current_dir}/build/go/src/unit.nginx.org/go'
|
||||
|
||||
with open(temp_dir + 'go.mod', 'w') as f:
|
||||
with open(f'{temp_dir}go.mod', 'w') as f:
|
||||
f.write(
|
||||
f"""module test/app
|
||||
require unit.nginx.org/go v0.0.0
|
||||
@@ -64,7 +62,7 @@ replace unit.nginx.org/go => {replace_path}
|
||||
)
|
||||
|
||||
if option.detailed:
|
||||
print("\n$ GOPATH=" + env['GOPATH'] + " " + " ".join(args))
|
||||
print(f'\n$ GOPATH={env["GOPATH"]} {" ".join(args)}')
|
||||
|
||||
try:
|
||||
output = subprocess.check_output(
|
||||
@@ -82,18 +80,18 @@ replace unit.nginx.org/go => {replace_path}
|
||||
def load(self, script, name='app', **kwargs):
|
||||
static_build = False
|
||||
|
||||
wdir = option.test_dir + "/go/" + script
|
||||
executable = option.temp_dir + "/go/" + name
|
||||
wdir = f'{option.test_dir}/go/{script}'
|
||||
executable = f'{option.temp_dir}/go/{name}'
|
||||
|
||||
if 'isolation' in kwargs and 'rootfs' in kwargs['isolation']:
|
||||
wdir = "/go/"
|
||||
executable = "/go/" + name
|
||||
executable = f"/go/{name}"
|
||||
static_build = True
|
||||
|
||||
TestApplicationGo.prepare_env(script, name, static=static_build)
|
||||
|
||||
conf = {
|
||||
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
||||
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
|
||||
"applications": {
|
||||
script: {
|
||||
"type": "external",
|
||||
|
||||
@@ -12,10 +12,10 @@ class TestApplicationJava(TestApplicationProto):
|
||||
application_type = "java"
|
||||
|
||||
def prepare_env(self, script):
|
||||
app_path = option.temp_dir + '/java'
|
||||
web_inf_path = app_path + '/WEB-INF/'
|
||||
classes_path = web_inf_path + 'classes/'
|
||||
script_path = option.test_dir + '/java/' + script + '/'
|
||||
app_path = f'{option.temp_dir}/java'
|
||||
web_inf_path = f'{app_path}/WEB-INF/'
|
||||
classes_path = f'{web_inf_path}classes/'
|
||||
script_path = f'{option.test_dir}/java/{script}/'
|
||||
|
||||
if not os.path.isdir(app_path):
|
||||
os.makedirs(app_path)
|
||||
@@ -23,7 +23,7 @@ class TestApplicationJava(TestApplicationProto):
|
||||
src = []
|
||||
|
||||
for f in os.listdir(script_path):
|
||||
file_path = script_path + f
|
||||
file_path = f'{script_path}{f}'
|
||||
|
||||
if f.endswith('.java'):
|
||||
src.append(file_path)
|
||||
@@ -36,7 +36,7 @@ class TestApplicationJava(TestApplicationProto):
|
||||
if f == 'WEB-INF':
|
||||
continue
|
||||
|
||||
shutil.copytree(file_path, app_path + '/' + f)
|
||||
shutil.copytree(file_path, f'{app_path}/{f}')
|
||||
continue
|
||||
|
||||
if f == 'web.xml':
|
||||
@@ -52,11 +52,11 @@ class TestApplicationJava(TestApplicationProto):
|
||||
os.makedirs(classes_path)
|
||||
|
||||
classpath = (
|
||||
option.current_dir + '/build/tomcat-servlet-api-9.0.70.jar'
|
||||
f'{option.current_dir}/build/tomcat-servlet-api-9.0.70.jar'
|
||||
)
|
||||
|
||||
ws_jars = glob.glob(
|
||||
option.current_dir + '/build/websocket-api-java-*.jar'
|
||||
f'{option.current_dir}/build/websocket-api-java-*.jar'
|
||||
)
|
||||
|
||||
if not ws_jars:
|
||||
@@ -74,12 +74,12 @@ class TestApplicationJava(TestApplicationProto):
|
||||
'-d',
|
||||
classes_path,
|
||||
'-classpath',
|
||||
classpath + ':' + ws_jars[0],
|
||||
f'{classpath}:{ws_jars[0]}',
|
||||
]
|
||||
javac.extend(src)
|
||||
|
||||
if option.detailed:
|
||||
print("\n$ " + " ".join(javac))
|
||||
print(f'\n$ {" ".join(javac)}')
|
||||
|
||||
try:
|
||||
subprocess.check_output(javac, stderr=subprocess.STDOUT)
|
||||
@@ -88,26 +88,24 @@ class TestApplicationJava(TestApplicationProto):
|
||||
raise
|
||||
|
||||
except subprocess.CalledProcessError:
|
||||
pytest.fail('Can\'t run javac process.')
|
||||
pytest.fail("Can't run javac process.")
|
||||
|
||||
def load(self, script, **kwargs):
|
||||
self.prepare_env(script)
|
||||
|
||||
script_path = f'{option.test_dir}/java/{script}/'
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
||||
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
|
||||
"applications": {
|
||||
script: {
|
||||
"unit_jars": option.current_dir + '/build',
|
||||
"unit_jars": f'{option.current_dir}/build',
|
||||
"type": self.get_application_type(),
|
||||
"processes": {"spare": 0},
|
||||
"working_directory": option.test_dir
|
||||
+ '/java/'
|
||||
+ script
|
||||
+ '/',
|
||||
"webapp": option.temp_dir + '/java',
|
||||
"working_directory": script_path,
|
||||
"webapp": f'{option.temp_dir}/java',
|
||||
}
|
||||
},
|
||||
},
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -13,16 +13,16 @@ class TestApplicationNode(TestApplicationProto):
|
||||
def prepare_env(self, script):
|
||||
# copy application
|
||||
shutil.copytree(
|
||||
option.test_dir + '/node/' + script, option.temp_dir + '/node'
|
||||
f'{option.test_dir}/node/{script}', f'{option.temp_dir}/node'
|
||||
)
|
||||
|
||||
# copy modules
|
||||
shutil.copytree(
|
||||
option.current_dir + '/node/node_modules',
|
||||
option.temp_dir + '/node/node_modules',
|
||||
f'{option.current_dir}/node/node_modules',
|
||||
f'{option.temp_dir}/node/node_modules',
|
||||
)
|
||||
|
||||
public_dir(option.temp_dir + '/node')
|
||||
public_dir(f'{option.temp_dir}/node')
|
||||
|
||||
def load(self, script, name='app.js', **kwargs):
|
||||
self.prepare_env(script)
|
||||
@@ -43,17 +43,17 @@ class TestApplicationNode(TestApplicationProto):
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {
|
||||
"*:7080": {"pass": "applications/" + quote(script, '')}
|
||||
"*:7080": {"pass": f"applications/{quote(script, '')}"}
|
||||
},
|
||||
"applications": {
|
||||
script: {
|
||||
"type": "external",
|
||||
"processes": {"spare": 0},
|
||||
"working_directory": option.temp_dir + '/node',
|
||||
"working_directory": f'{option.temp_dir}/node',
|
||||
"executable": '/usr/bin/env',
|
||||
"arguments": arguments,
|
||||
}
|
||||
},
|
||||
},
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -6,19 +6,19 @@ class TestApplicationPerl(TestApplicationProto):
|
||||
application_type = "perl"
|
||||
|
||||
def load(self, script, name='psgi.pl', **kwargs):
|
||||
script_path = option.test_dir + '/perl/' + script
|
||||
script_path = f'{option.test_dir}/perl/{script}'
|
||||
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
||||
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
|
||||
"applications": {
|
||||
script: {
|
||||
"type": self.get_application_type(),
|
||||
"processes": {"spare": 0},
|
||||
"working_directory": script_path,
|
||||
"script": script_path + '/' + name,
|
||||
"script": f'{script_path}/{name}',
|
||||
}
|
||||
},
|
||||
},
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -9,18 +9,18 @@ class TestApplicationPHP(TestApplicationProto):
|
||||
application_type = "php"
|
||||
|
||||
def load(self, script, index='index.php', **kwargs):
|
||||
script_path = option.test_dir + '/php/' + script
|
||||
script_path = f'{option.test_dir}/php/{script}'
|
||||
|
||||
if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
|
||||
rootfs = kwargs['isolation']['rootfs']
|
||||
|
||||
if not os.path.exists(rootfs + '/app/php/'):
|
||||
os.makedirs(rootfs + '/app/php/')
|
||||
if not os.path.exists(f'{rootfs}/app/php/'):
|
||||
os.makedirs(f'{rootfs}/app/php/')
|
||||
|
||||
if not os.path.exists(rootfs + '/app/php/' + script):
|
||||
shutil.copytree(script_path, rootfs + '/app/php/' + script)
|
||||
if not os.path.exists(f'{rootfs}/app/php/{script}'):
|
||||
shutil.copytree(script_path, f'{rootfs}/app/php/{script}')
|
||||
|
||||
script_path = '/app/php/' + script
|
||||
script_path = f'/app/php/{script}'
|
||||
|
||||
app = {
|
||||
"type": self.get_application_type(),
|
||||
@@ -41,8 +41,8 @@ class TestApplicationPHP(TestApplicationProto):
|
||||
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
||||
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
|
||||
"applications": {script: app},
|
||||
},
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -20,18 +20,18 @@ class TestApplicationPython(TestApplicationProto):
|
||||
if script[0] == '/':
|
||||
script_path = script
|
||||
else:
|
||||
script_path = option.test_dir + '/python/' + script
|
||||
script_path = f'{option.test_dir}/python/{script}'
|
||||
|
||||
if kwargs.get('isolation') and kwargs['isolation'].get('rootfs'):
|
||||
rootfs = kwargs['isolation']['rootfs']
|
||||
|
||||
if not os.path.exists(rootfs + '/app/python/'):
|
||||
os.makedirs(rootfs + '/app/python/')
|
||||
if not os.path.exists(f'{rootfs}/app/python/'):
|
||||
os.makedirs(f'{rootfs}/app/python/')
|
||||
|
||||
if not os.path.exists(rootfs + '/app/python/' + name):
|
||||
shutil.copytree(script_path, rootfs + '/app/python/' + name)
|
||||
if not os.path.exists(f'{rootfs}/app/python/{name}'):
|
||||
shutil.copytree(script_path, f'{rootfs}/app/python/{name}')
|
||||
|
||||
script_path = '/app/python/' + name
|
||||
script_path = f'/app/python/{name}'
|
||||
|
||||
app = {
|
||||
"type": self.get_application_type(),
|
||||
@@ -58,9 +58,9 @@ class TestApplicationPython(TestApplicationProto):
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {
|
||||
"*:7080": {"pass": "applications/" + quote(name, '')}
|
||||
"*:7080": {"pass": f"applications/{quote(name, '')}"}
|
||||
},
|
||||
"applications": {name: app},
|
||||
},
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -10,22 +10,22 @@ class TestApplicationRuby(TestApplicationProto):
|
||||
|
||||
def prepare_env(self, script):
|
||||
shutil.copytree(
|
||||
option.test_dir + '/ruby/' + script,
|
||||
option.temp_dir + '/ruby/' + script,
|
||||
f'{option.test_dir}/ruby/{script}',
|
||||
f'{option.temp_dir}/ruby/{script}',
|
||||
)
|
||||
|
||||
public_dir(option.temp_dir + '/ruby/' + script)
|
||||
public_dir(f'{option.temp_dir}/ruby/{script}')
|
||||
|
||||
def load(self, script, name='config.ru', **kwargs):
|
||||
self.prepare_env(script)
|
||||
|
||||
script_path = option.temp_dir + '/ruby/' + script
|
||||
script_path = f'{option.temp_dir}/ruby/{script}'
|
||||
|
||||
app = {
|
||||
"type": self.get_application_type(),
|
||||
"processes": {"spare": 0},
|
||||
"working_directory": script_path,
|
||||
"script": script_path + '/' + name,
|
||||
"script": f'{script_path}/{name}',
|
||||
}
|
||||
|
||||
for key in [
|
||||
@@ -36,8 +36,8 @@ class TestApplicationRuby(TestApplicationProto):
|
||||
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {"*:7080": {"pass": "applications/" + script}},
|
||||
"listeners": {"*:7080": {"pass": f"applications/{script}"}},
|
||||
"applications": {script: app},
|
||||
},
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
@@ -22,13 +22,13 @@ class TestApplicationTLS(TestApplicationProto):
|
||||
'-x509',
|
||||
'-new',
|
||||
'-subj',
|
||||
'/CN=' + name + '/',
|
||||
f'/CN={name}/',
|
||||
'-config',
|
||||
option.temp_dir + '/openssl.conf',
|
||||
f'{option.temp_dir}/openssl.conf',
|
||||
'-out',
|
||||
option.temp_dir + '/' + name + '.crt',
|
||||
f'{option.temp_dir}/{name}.crt',
|
||||
'-keyout',
|
||||
option.temp_dir + '/' + name + '.key',
|
||||
f'{option.temp_dir}/{name}.key',
|
||||
],
|
||||
stderr=subprocess.STDOUT,
|
||||
)
|
||||
@@ -40,11 +40,11 @@ class TestApplicationTLS(TestApplicationProto):
|
||||
if key is None:
|
||||
key = crt
|
||||
|
||||
key_path = option.temp_dir + '/' + key + '.key'
|
||||
crt_path = option.temp_dir + '/' + crt + '.crt'
|
||||
key_path = f'{option.temp_dir}/{key}.key'
|
||||
crt_path = f'{option.temp_dir}/{crt}.crt'
|
||||
|
||||
with open(key_path, 'rb') as k, open(crt_path, 'rb') as c:
|
||||
return self.conf(k.read() + c.read(), '/certificates/' + crt)
|
||||
return self.conf(k.read() + c.read(), f'/certificates/{crt}')
|
||||
|
||||
def get_ssl(self, **kwargs):
|
||||
return self.get(wrapper=self.context.wrap_socket, **kwargs)
|
||||
@@ -54,54 +54,48 @@ class TestApplicationTLS(TestApplicationProto):
|
||||
|
||||
def openssl_conf(self, rewrite=False, alt_names=None):
|
||||
alt_names = alt_names or []
|
||||
|
||||
conf_path = option.temp_dir + '/openssl.conf'
|
||||
conf_path = f'{option.temp_dir}/openssl.conf'
|
||||
|
||||
if not rewrite and os.path.exists(conf_path):
|
||||
return
|
||||
|
||||
# Generates alt_names section with dns names
|
||||
a_names = "[alt_names]\n"
|
||||
a_names = '[alt_names]\n'
|
||||
for i, k in enumerate(alt_names, 1):
|
||||
k = k.split('|')
|
||||
|
||||
if k[0] == 'IP':
|
||||
a_names += "IP.%d = %s\n" % (i, k[1])
|
||||
a_names += f'IP.{i} = {k[1]}\n'
|
||||
else:
|
||||
a_names += "DNS.%d = %s\n" % (i, k[0])
|
||||
a_names += f'DNS.{i} = {k[0]}\n'
|
||||
|
||||
# Generates section for sign request extension
|
||||
a_sec = """req_extensions = myca_req_extensions
|
||||
a_sec = f'''req_extensions = myca_req_extensions
|
||||
|
||||
[ myca_req_extensions ]
|
||||
subjectAltName = @alt_names
|
||||
|
||||
{a_names}""".format(
|
||||
a_names=a_names
|
||||
)
|
||||
{a_names}'''
|
||||
|
||||
with open(conf_path, 'w') as f:
|
||||
f.write(
|
||||
"""[ req ]
|
||||
f'''[ req ]
|
||||
default_bits = 2048
|
||||
encrypt_key = no
|
||||
distinguished_name = req_distinguished_name
|
||||
|
||||
{a_sec}
|
||||
[ req_distinguished_name ]""".format(
|
||||
a_sec=a_sec if alt_names else ""
|
||||
)
|
||||
{a_sec if alt_names else ""}
|
||||
[ req_distinguished_name ]'''
|
||||
)
|
||||
|
||||
def load(self, script, name=None):
|
||||
if name is None:
|
||||
name = script
|
||||
|
||||
script_path = option.test_dir + '/python/' + script
|
||||
|
||||
script_path = f'{option.test_dir}/python/{script}'
|
||||
self._load_conf(
|
||||
{
|
||||
"listeners": {"*:7080": {"pass": "applications/" + name}},
|
||||
"listeners": {"*:7080": {"pass": f"applications/{name}"}},
|
||||
"applications": {
|
||||
name: {
|
||||
"type": "python",
|
||||
|
||||
@@ -52,7 +52,7 @@ class TestApplicationWebsocket(TestApplicationProto):
|
||||
while True:
|
||||
rlist = select.select([sock], [], [], 60)[0]
|
||||
if not rlist:
|
||||
pytest.fail('Can\'t read response from server.')
|
||||
pytest.fail("Can't read response from server.")
|
||||
|
||||
resp += sock.recv(4096).decode()
|
||||
|
||||
@@ -77,7 +77,7 @@ class TestApplicationWebsocket(TestApplicationProto):
|
||||
# For all current cases if the "read_timeout" was changed
|
||||
# than test do not expect to get a response from server.
|
||||
if read_timeout == 60:
|
||||
pytest.fail('Can\'t read response from server.')
|
||||
pytest.fail("Can't read response from server.")
|
||||
break
|
||||
|
||||
data += sock.recv(bytes - len(data))
|
||||
|
||||
Reference in New Issue
Block a user