Skip to content
Permalink
Browse files

update test for yaml

  • Loading branch information...
sdpython committed Aug 18, 2016
1 parent bdf8220 commit ff0813e3ecf75ea107551ed3643867803914a841
File renamed without changes.
@@ -38,10 +38,10 @@ def test_jenkins_job_verif(self):

this = os.path.abspath(os.path.dirname(__file__))
yml = os.path.abspath(os.path.join(
this, "..", "..", ".local_jenkins.win.yml"))
this, "..", "..", ".local.jenkins.win.yml"))
if not os.path.exists(yml):
raise FileNotFoundError(yml)
context = dict(Python34=None, Python35=sys.executable,
context = dict(Python34=None, Python35=os.path.dirname(sys.executable),
Python27=None, Anaconda3=None, Anaconda2=None,
WinPython35=None, project_name="pyquickhelper",
root_path="ROOT")
@@ -82,10 +82,10 @@ def test_jenkins_job_multiplication(self):

this = os.path.abspath(os.path.dirname(__file__))
yml = os.path.abspath(os.path.join(
this, "..", "..", ".local_jenkins.win.yml"))
this, "..", "..", ".local.jenkins.win.yml"))
if not os.path.exists(yml):
raise FileNotFoundError(yml)
context = dict(Python34="fake", Python35=sys.executable,
context = dict(Python34="fake", Python35=os.path.dirname(sys.executable),
Python27=None, Anaconda3=None, Anaconda2=None,
WinPython35=None, project_name="pyquickhelper",
root_path="ROOT")
@@ -120,7 +120,7 @@ def test_jconvert_sequence_into_batch_file(self):
def a_test_jconvert_sequence_into_batch_file(self, platform):
this = os.path.abspath(os.path.dirname(__file__))
yml = os.path.abspath(os.path.join(
this, "..", "..", ".local_jenkins.win.yml"))
this, "..", "..", ".local.jenkins.win.yml"))
if not os.path.exists(yml):
raise FileNotFoundError(yml)
context = dict(Python34="fake", Python35="C:\\Python35_x64",
@@ -141,7 +141,7 @@ def a_test_jconvert_sequence_into_batch_file(self, platform):
@echo CREATE VIRTUAL ENVIRONMENT in ROOT\\_virtualenv\\pyquickhelper
if not exist "ROOT\\_virtualenv\\pyquickhelper" mkdir "ROOT\\_virtualenv\\pyquickhelper"
"C:\\Python35_x64\\Scripts\\virtualenv.exe" --system-site-packages "ROOT\\_virtualenv\\pyquickhelper"
"C:\\Python35_x64\\Scripts\\virtualenv" --system-site-packages "ROOT\\_virtualenv\\pyquickhelper"
if %errorlevel% neq 0 exit /b %errorlevel%
@echo INSTALL
@@ -162,6 +162,10 @@ def a_test_jconvert_sequence_into_batch_file(self, platform):
val = conv.strip("\n \t\r")
if expected != val:
mes = "EXP:\n{0}\n###########\nGOT:\n{1}".format(expected, val)
for a, b in zip(expected.split("\n"), val.split("\n")):
if a != b:
raise Exception(
"error on line:\nEXP:\n{0}\nGOT:\n{1}\n#######\n{2}".format(a, b, mes))
raise Exception(mes)


@@ -0,0 +1,108 @@
"""
@brief test log(time=2s)
"""

import sys
import os
import unittest
import re


try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src

from src.pyquickhelper.loghelper import fLOG, run_cmd
from src.pyquickhelper.pycode import get_temp_folder
from src.pyquickhelper.jenkinshelper.yaml_helper import load_yaml, enumerate_convert_yaml_into_instructions, evaluate_condition, convert_sequence_into_batch_file

if sys.version_info[0] == 2:
FileNotFoundError = Exception


class TestYamlExe(unittest.TestCase):

def test_bug_exe(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

this = os.path.abspath(os.path.dirname(__file__))
command = "dir" if sys.platform.startswith("win32") else "ls"
yml = """
language: python
python:
- {{Python35}}
before:
- %s
after_script:
- %s {{PLATFORM}}
script:
- %s
""" % (command, command, command)
context = dict(Python34="fake", Python35=os.path.dirname(sys.executable),
Python27=None, Anaconda3=None, Anaconda2=None,
WinPython35=None, project_name="pyquickhelper",
root_path="ROOT", PLATFORM="win")
obj = load_yaml(yml, context=context)
try:
res = list(enumerate_convert_yaml_into_instructions(
obj, variables=context))
assert False
except ValueError as e:
assert "'before'" in str(e)

def test_exe(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")

temp = get_temp_folder(__file__, "temp_exe")
this = os.path.abspath(os.path.dirname(__file__))
command = "dir" if sys.platform.startswith("win32") else "ls"
yml = """
language: python
python:
- {{Python35}}
before_script:
- %s
after_script:
- %s {{PLATFORM}}
script:
- %s
""" % (command, command, command)
context = dict(Python34="fake", Python35=os.path.dirname(sys.executable),
Python27=None, Anaconda3=None, Anaconda2=None,
WinPython35=None, project_name="pyquickhelper",
root_path="ROOT", PLATFORM="win")
obj = load_yaml(yml, context=context)
res = list(enumerate_convert_yaml_into_instructions(
obj, variables=context))
for r in res:
conv = convert_sequence_into_batch_file(r)
assert "dir win" in conv
fLOG("####", conv)
name = os.path.join(temp, "yml.bat")
with open(name, "w") as f:
f.write(conv)
out, err = run_cmd(name, wait=True)
fLOG("###")
fLOG(out)
assert "BEFORE_SCRIPT" in out
assert "SCRIPT" in out
assert "AFTER_SCRIPT" in out


if __name__ == "__main__":
unittest.main()
@@ -50,7 +50,7 @@ def evaluate_condition(cond, variables=None):
Example of a condition::
[ ${PYTHON} == "C:\\Python35_x64\\pythonw.exe" ]
[ ${PYTHON} == "C:\\Python35_x64" ]
"""
if variables is not None:
for k, v in variables.items():
@@ -76,7 +76,7 @@ def interpret_instruction(inst, variables=None):
Example of a statement::
if [ ${PYTHON} == "C:\\\\Python35_x64\\\\pythonw.exe" ] then python setup.py build_sphinx fi
if [ ${PYTHON} == "C:\\\\Python35_x64" ] then python setup.py build_sphinx fi
"""
if isinstance(inst, list):
res = [interpret_instruction(_, variables) for _ in inst]
@@ -100,12 +100,13 @@ def interpret_instruction(inst, variables=None):
return inst


def enumerate_convert_yaml_into_instructions(obj):
def enumerate_convert_yaml_into_instructions(obj, variables=None):
"""
convert a yaml file into sequences of instructions
@param obj yaml objects (@see fn load_yaml)
@return list of instructions
@param obj yaml objects (@see fn load_yaml)
@param variables additional variables to be used
@return list of instructions
The function expects the following list
of steps in this order:
@@ -122,11 +123,16 @@ def enumerate_convert_yaml_into_instructions(obj):
Each step *multiplies jobs* creates a sequence of jobs
and a Jenkins job.
"""
if variables is None:
def_variables = {}
else:
def_variables = variables.copy()
sequences = []
count = {}
for key in ["language", "python", "virtualenv", "install",
"before_script", "script", "after_script",
"documentation"]:
steps = ["language", "python", "virtualenv", "install",
"before_script", "script", "after_script",
"documentation"]
for key in steps:
value = obj.get(key, None)
if key == "language":
if value != "python":
@@ -138,14 +144,19 @@ def enumerate_convert_yaml_into_instructions(obj):
count[key] = len(value)
sequences.append((key, value))

for k in obj:
if k not in steps:
raise ValueError(
"Unexpected key '{0}' found in yaml file".format(k))

# multiplications
i_python = 0
i_script = 0
notstop = True
while notstop:
seq = []
add = True
variables = {}
variables = def_variables.copy()
for key, value in sequences:
if key == "python":
value = value[i_python]
@@ -218,7 +229,7 @@ def convert_sequence_into_batch_file(seq, platform=None):
conda = None
rows.append("@echo off")

def add_path_win(rows):
def add_path_win(rows, interpreter, pip):
path_inter = ospathdirname(interpreter)
rows.append("set PATH={0};%PATH%".format(path_inter))
path_pip = ospathdirname(pip)
@@ -231,20 +242,20 @@ def add_path_win(rows):
if value.startswith("conda|"):
anaconda = True
interpreter = ospathjoin(
value[6:], "python.exe", platform=platform)
value[6:], "python", platform=platform)
pip = ospathjoin(value[6:], "Scripts",
"pip.exe", platform=platform)
"pip", platform=platform)
venv = ospathjoin(
value[6:], "Scripts", "virtualenv.exe", platform=platform)
value[6:], "Scripts", "virtualenv", platform=platform)
conda = ospathjoin(
value[6:], "Scripts", "conda.exe", platform=platform)
value[6:], "Scripts", "conda", platform=platform)
else:
interpreter = ospathjoin(
value, "python.exe", platform=platform)
value, "python", platform=platform)
pip = ospathjoin(value, "Scripts",
"pip.exe", platform=platform)
"pip", platform=platform)
venv = ospathjoin(value, "Scripts",
"virtualenv.exe", platform=platform)
"virtualenv", platform=platform)

elif key == "virtualenv":
if isinstance(value, list):
@@ -260,23 +271,23 @@ def add_path_win(rows):
rows.append(
'"{0}" create -p "{1}" --clone "{2}" --offline'.format(conda, p, pinter))
interpreter = ospathjoin(
p, "python.exe", platform=platform)
pip = ospathjoin(p, "Scripts", "pip.exe",
p, "python", platform=platform)
pip = ospathjoin(p, "Scripts", "pip",
platform=platform)
else:
rows.append(
'"{0}" --system-site-packages "{1}"'.format(venv, p))
interpreter = ospathjoin(
p, "Scripts", "python.exe", platform=platform)
pip = ospathjoin(p, "Scripts", "pip.exe",
p, "Scripts", "python", platform=platform)
pip = ospathjoin(p, "Scripts", "pip",
platform=platform)
rows.append(error_level)

elif key in {"install", "before_script", "script", "after_script", "documentation"}:
if value is not None:
rows.append("")
rows.append("@echo " + key.upper())
add_path_win(rows)
add_path_win(rows, interpreter, pip)
if not isinstance(value, list):
value = [value]
rows.extend(value)

0 comments on commit ff0813e

Please sign in to comment.
You can’t perform that action at this time.