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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
# -*- coding: utf-8 -*-
#
import os
import platform
import subprocess
import sys
from os import getenv, path
from sys import stderr
PATH = getenv("PATH", "")
PP = getenv('PYTHONPATH', "")
here_dir = path.abspath(path.dirname(__file__))
ENV_VARS = {"PATH": PATH, "PYTHONPATH": ':'.join((here_dir, PP))}
PH = getenv('PYTHONHOME', "")
if PH:
ENV_VARS['PYTHONHOME'] = PH
VE = getenv('VIRTUAL_ENV', "")
if VE:
ENV_VARS['VIRTUAL_ENV'] = VE
virtual_bin = path.join(VE, "bin")
ENV_VARS['PATH'] = ':'.join((virtual_bin, PATH))
abs_resources_dir = path.join(here_dir, 'resources')
cmdline_files_dir = path.join(abs_resources_dir, 'cmdline_tests')
check_resources = path.join(path.abspath(os.getcwd()), 'resources')
in_test_dir = False
if path.exists(check_resources) and path.isdir(check_resources):
in_test_dir = True
else:
in_test_dir = False
if in_test_dir:
lib_dir = os.path.abspath(os.path.join(here_dir, os.pardir))
ENV_VARS["PYTHONPATH"] = ':'.join((lib_dir, PP))
it = ENV_VARS["PYTHONPATH"].split(":")
scr_dir = "scripts-{}.{}".format(sys.version_info[0], sys.version_info[1])
if in_test_dir:
scr_dir = path.join('..', scr_dir)
check_scrdir = path.join(path.abspath(os.getcwd()), scr_dir)
if path.exists(check_scrdir) and path.isdir(check_scrdir):
has_scripts_dir = True
else:
has_scripts_dir = False
bin_dir = "bin"
if in_test_dir:
bin_dir = path.join('..', bin_dir)
check_bindir = path.join(path.abspath(os.getcwd()), bin_dir)
if path.exists(check_bindir) and path.isdir(check_bindir):
has_bin_dir = True
else:
has_bin_dir = False
cli_script = "pyshacl/cli.py"
if in_test_dir:
cli_script = path.join('..', cli_script)
check_cli_script = path.join(path.abspath(os.getcwd()), cli_script)
if path.exists(check_cli_script) and path.isfile(check_cli_script):
has_cli_script = True
else:
has_cli_script = False
if has_scripts_dir:
pyshacl_command = ["{}/pyshacl".format(scr_dir)]
elif has_bin_dir:
pyshacl_command = ["{}/pyshacl".format(bin_dir)]
elif has_cli_script:
pyshacl_command = ["python3", cli_script]
else:
pyshacl_command = ["pyshacl"]
def test_cmdline():
if not hasattr(subprocess, 'run'):
print("Subprocess.run() not available, skip this test")
assert True
return True
if platform.system() == "Windows":
print("Commandline tests cannot run on Windows.")
assert True
return True
if os.environ.get("PYBUILD_NAME", None) is not None:
print("We don't have access to scripts dir during pybuild process.")
assert True
return True
graph_file = path.join(cmdline_files_dir, 'd1.ttl')
shacl_file = path.join(cmdline_files_dir, 's1.ttl')
ont_file = path.join(cmdline_files_dir, 'o1.ttl')
cmd = pyshacl_command
args = [graph_file, '-s', shacl_file, '-i', 'rdfs', '-e', ont_file]
res = subprocess.run(cmd + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV_VARS)
print("result = {}".format(res.returncode))
print(res.stdout.decode('utf-8'))
print(res.stderr.decode('utf-8'))
assert res.returncode == 0
def test_cmdline_fail():
if not hasattr(subprocess, 'run'):
print("Subprocess.run() not available, skip this test")
assert True
return True
if platform.system() == "Windows":
print("Commandline tests cannot run on Windows.")
assert True
return True
if os.environ.get("PYBUILD_NAME", None) is not None:
print("We don't have access to scripts dir during pybuild process.")
assert True
return True
graph_file = path.join(cmdline_files_dir, 'd2.ttl')
shacl_file = path.join(cmdline_files_dir, 's1.ttl')
ont_file = path.join(cmdline_files_dir, 'o1.ttl')
cmd = pyshacl_command
args = [graph_file, '-s', shacl_file, '-i', 'rdfs', '-e', ont_file]
res = subprocess.run(cmd + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV_VARS)
print("result = {}".format(res.returncode))
print(res.stdout.decode('utf-8'))
print(res.stderr.decode('utf-8'))
assert res.returncode == 1
def test_cmdline_table():
graph_file = path.join(cmdline_files_dir, 'd1.ttl')
shacl_file = path.join(cmdline_files_dir, 's1.ttl')
args = [graph_file, '-s', shacl_file, '-f', 'table']
res = subprocess.run(pyshacl_command + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV_VARS)
output_table = res.stdout.decode('utf-8')
assert (
"+-----+-----------+---------------------------+---------------------------+"
"---------------------------+--------------------------+---------------------------+"
"---------------------------+" in output_table
)
assert "| 1 | Violation | http://example.com/ex#Hum | http://example.com/exOnt# " in output_table
def test_cmdline_web():
if not hasattr(subprocess, 'run'):
print("Subprocess.run() not available, skip this test")
assert True
return
if platform.system() == "Windows":
print("Commandline tests cannot run on Windows.")
assert True
return
if os.environ.get("PYBUILD_NAME", None) is not None:
print("We don't have access to scripts dir during pybuild process.")
assert True
return
DEB_BUILD_ARCH = os.environ.get('DEB_BUILD_ARCH', None)
DEB_HOST_ARCH = os.environ.get('DEB_HOST_ARCH', None)
if DEB_BUILD_ARCH is not None or DEB_HOST_ARCH is not None:
print("Cannot run web requests in debhelper tests.")
assert True
return
graph_file = path.join(cmdline_files_dir, 'd1.ttl')
shacl_file = "https://raw.githubusercontent.com/RDFLib/pySHACL/master/test/resources/cmdline_tests/s1.ttl"
ont_file = "https://raw.githubusercontent.com/RDFLib/pySHACL/master/test/resources/cmdline_tests/o1.ttl"
cmd = pyshacl_command
args = [graph_file, '-s', shacl_file, '-i', 'rdfs', '-e', ont_file]
res = subprocess.run(cmd + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV_VARS)
print("result = {}".format(res.returncode))
print(res.stdout.decode('utf-8'))
print(res.stderr.decode('utf-8'))
assert res.returncode == 0
def test_cmdline_jsonld():
if not hasattr(subprocess, 'run'):
print("Subprocess.run() not available, skip this test")
assert True
return
if platform.system() == "Windows":
print("Commandline tests cannot run on Windows.")
assert True
return
if os.environ.get("PYBUILD_NAME", None) is not None:
print("We don't have access to scripts dir during pybuild process.")
assert True
return
DEB_BUILD_ARCH = os.environ.get('DEB_BUILD_ARCH', None)
DEB_HOST_ARCH = os.environ.get('DEB_HOST_ARCH', None)
if DEB_BUILD_ARCH is not None or DEB_HOST_ARCH is not None:
print("Cannot run web requests in debhelper tests.")
assert True
return
graph_file = path.join(cmdline_files_dir, 'd1.jsonld')
shacl_file = "https://raw.githubusercontent.com/RDFLib/pySHACL/master/test/resources/cmdline_tests/s1.ttl"
ont_file = "https://raw.githubusercontent.com/RDFLib/pySHACL/master/test/resources/cmdline_tests/o1.ttl"
cmd = pyshacl_command
args = [graph_file, '-df', 'json-ld', '-s', shacl_file, '-i', 'rdfs', '-e', ont_file]
res = subprocess.run(cmd + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=ENV_VARS)
print("result = {}".format(res.returncode))
print(res.stdout.decode('utf-8'))
print(res.stderr.decode('utf-8'))
assert res.returncode == 0
if __name__ == "__main__":
test_cmdline()
test_cmdline_fail()
test_cmdline_table()
test_cmdline_web()
test_cmdline_jsonld()
|