From 662b1a0b7158635760480ef66baf75ef9c096c9e Mon Sep 17 00:00:00 2001
From: Kirill Smelkov <kirr@nexedi.com>
Date: Thu, 15 Oct 2020 07:07:10 +0300
Subject: [PATCH] pygolang: Add way to run tests

- Add pygolang/buildout-dev.cfg that overrides [pygolang] to use the
  software from git checkout.

- Add pygolang/test.cfg that is software-release to create a test
  instance to be run under testnode. This software release uses
  just-added stack/nxdtest.cfg to help itself on this task, so the code
  in pygolang is minimal.

A new section [python-interpreter] is added, because python interpreters
that zc.recipe.egg generates don't process `-m args` correctly and
handle subprocess well. I had to workaround that with code from
gpython.pymain to be able to run `python -m pytest --<pytestarg>` and to
spawn children processes with preserving sys.path. Comments around and
inside [python-interpreter] has more details on this topic.
---
 component/pygolang/buildout-dev.cfg | 24 ++++++++++++
 component/pygolang/buildout.cfg     | 25 ++++++++++++
 component/pygolang/test.cfg         | 60 +++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 component/pygolang/buildout-dev.cfg
 create mode 100644 component/pygolang/test.cfg

diff --git a/component/pygolang/buildout-dev.cfg b/component/pygolang/buildout-dev.cfg
new file mode 100644
index 000000000..9e9d8e0bf
--- /dev/null
+++ b/component/pygolang/buildout-dev.cfg
@@ -0,0 +1,24 @@
+# SlapOS component for pygolang development.
+
+[buildout]
+extends =
+    buildout.cfg
+    ../git/buildout.cfg
+
+# override pygolang to install it from latest git version
+[pygolang]
+recipe  = zc.recipe.egg:develop
+setup   = ${pygolang-repository:location}
+
+[pygolang-repository]
+recipe  = slapos.recipe.build:gitclone
+repository = https://lab.nexedi.com/nexedi/pygolang.git
+branch  = master
+location = ${buildout:parts-directory}/pygolang-dev
+git-executable = ${git:location}/bin/git
+
+
+# unpin pygolang from versions, so that buildout does not fallback to
+# installing non-dev egg if dev one has changed its version.
+[versions]
+pygolang =
diff --git a/component/pygolang/buildout.cfg b/component/pygolang/buildout.cfg
index 376fa03e8..8db4898b3 100644
--- a/component/pygolang/buildout.cfg
+++ b/component/pygolang/buildout.cfg
@@ -27,6 +27,31 @@ scripts = gpython
 # convenience for gpython users
 exe     = ${buildout:bin-directory}/gpython
 
+# python-interpreter provides python interpreter with all specified eggs.
+# eggs default to pygolang, but can be overwritten or changed in inherited section.
+# if eggs are changes, they must still have pygolang.
+[python-interpreter]
+recipe = zc.recipe.egg:scripts
+eggs   = ${pygolang:egg}
+interpreter = python
+# interpreter code that buildout generates cannot process `-m pytest --<pytest-option>`
+# -> use pymain from gpython to workaround that.
+initialization =
+  # set sys.executable to self, so that subprocess and friends go through us
+  # and this way spawn children with correct sys.path where all eggs that
+  # parent have are present. TODO consider migrating this into pymain
+  sys.executable = sys.argv[0]
+  # tail to pymain
+  from gpython import pymain
+  pymain(sys.argv[1:])
+  sys.exit(0)
+# don't install scripts from listed eggs (avoid conflict with other sections
+# that use zc.recipe.egg with eggs overlapping with ${:eggs} - ex neoppod)
+# (we cannot use zc.recipe.egg:eggs because interpreter does not work there)
+# NOTE with scripts=酶 interpreter is not handled, so we use `scripts=python` as
+# a workaround.
+scripts = ${:interpreter}
+
 
 [versions]
 pygolang = 0.0.7.post1
diff --git a/component/pygolang/test.cfg b/component/pygolang/test.cfg
new file mode 100644
index 000000000..8f73b8188
--- /dev/null
+++ b/component/pygolang/test.cfg
@@ -0,0 +1,60 @@
+# SlapOS software release to test pygolang on Nexedi testing infrastructure.
+
+[buildout]
+extends =
+    ../../stack/nxdtest.cfg
+    ../pytest/buildout.cfg
+    buildout-dev.cfg
+
+parts =
+    pygolang
+    gpython
+    pygolang-python
+
+#   for instance
+    slapos-cookbook
+    instance.cfg
+
+
+# tune pygolang to install with all and for-tests extras.
+[pygolang]
+egg = pygolang[all_test]
+
+# bin/python is preinstalled with sys.path to pygolang & friends.
+[pygolang-python]
+<= python-interpreter
+eggs = ${pygolang:egg}
+
+# env.sh for pygolang's python/gpython to be on $PATH.
+[pygolang-env.sh]
+recipe   = slapos.recipe.template:jinja2
+rendered = ${buildout:directory}/${:_buildout_section_name_}
+template = inline:
+  export PS1="(pygolang-env) $PS1"
+  export PATH=${buildout:bin-directory}:$PATH
+
+# instance to run nxdtest.
+[instance.cfg]
+<= jinja2-template
+template = inline:
+  [buildout]
+  extends = ${nxdtest-instance.cfg:rendered}
+
+  [runTestSuite]
+  env.sh  = ${pygolang-env.sh:rendered}
+  workdir = ${pygolang-repository:location}
+
+
+[versions]
+numpy   = 1.16.6
+ipython = 5.10.0
+ipython-genutils = 0.2.0
+
+traitlets = 4.3.3
+simplegeneric = 0.8.1
+Pygments = 2.5.2
+prompt-toolkit = 1.0.18
+pickleshare = 0.7.5
+pexpect = 4.8.0
+backports.shutil-get-terminal-size = 1.0.0
+ptyprocess = 0.6.0
-- 
2.30.9