Skip to content

Commit 832fa27

Browse files
committed
Add tests for virtualenv
1 parent 401a77c commit 832fa27

File tree

12 files changed

+78
-15
lines changed

12 files changed

+78
-15
lines changed

.github/workflows/ci.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ jobs:
3131
run: sbt +test
3232
shell: bash
3333
- name: Integration tests again latest ScalaPy stable
34-
run: .github/scripts/integration_test.sh python3 0.5.0
34+
run: scripts/integration_test.sh python3 0.5.0
3535
shell: bash
36+
- name: Test virtualenv
37+
run: scripts/virtualenv_test.sh 0.5.0+8-7c7a6042

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ target/
55
.bsp/
66
.bloop/
77

8+
__pycache__/
9+
.ipynb_checkpoints/
10+
811
.DS_Store
912
*.worksheet.sc

build.sbt

+11-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ lazy val scala212 = "2.12.14"
2020
lazy val scala213 = "2.13.6"
2121
lazy val scala3 = "3.0.0"
2222

23-
lazy val enableScripted = Option(sys.props("plugin.ci")).map(_.trim.nonEmpty).getOrElse(false)
23+
lazy val scalapyVersion = getProp("plugin.scalapy.version").getOrElse("0.5.0")
24+
25+
lazy val enableScripted = getProp("plugin.ci").isDefined
2426

2527
ThisBuild / scalaVersion := (if (enableScripted) scala212 else scala213)
2628

@@ -32,25 +34,28 @@ def warnUnusedImports(scalaVersion: String) =
3234
case _ => Nil
3335
}
3436

37+
def getProp(p: String) = Option(sys.props(p)).map(_.trim).filter(_.nonEmpty)
38+
39+
def getProps(prop: String*) =
40+
prop
41+
.map(p => p -> getProp(p))
42+
.collect { case (k, Some(v)) => s"""-D$k=$v""" }
43+
3544
def scriptedPlugin = if (enableScripted) Seq(ScriptedPlugin) else Nil
3645

3746
def scriptedSettings = if (enableScripted) {
3847
Seq(
3948
scriptedLaunchOpts := {
4049
scriptedLaunchOpts.value ++ {
4150
Seq(s"-Dplugin.scalapy.version=$scalapyVersion") ++
42-
Option(sys.props("plugin.python.executable"))
43-
.map("-Dplugin.python.executable=" + _)
44-
.toSeq ++
51+
getProps("plugin.python.executable", "plugin.virtualenv") ++
4552
Seq("-Xmx1024M", "-Dplugin.version=" + version.value)
4653
}
4754
},
4855
scriptedBufferLog := false
4956
)
5057
} else Nil
5158

52-
lazy val scalapyVersion = Option(sys.props("plugin.scalapy.version")).map(_.trim).getOrElse("0.5.0")
53-
5459
lazy val root = (project in file("."))
5560
.enablePlugins(scriptedPlugin: _*)
5661
.settings(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '0.1.0'

examples/python-package/setup.cfg

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[metadata]
2+
name = dummy
3+
version = attr: dummy.__version__
4+
5+
[options]
6+
packages = find:

examples/python-package/setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import setuptools
2+
setuptools.setup()
File renamed without changes.

scripts/virtualenv_test.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
VENV_DIR=$(mktemp -d)
4+
5+
echo "Virtualenv created at ${VENV_DIR}"
6+
7+
python3 -m venv "${VENV_DIR}"
8+
"${VENV_DIR}"/bin/python -m pip install examples/python-package
9+
10+
sbt +publishLocal
11+
12+
sbt \
13+
-Dplugin.ci=true \
14+
-Dplugin.virtualenv=true \
15+
-Dplugin.python.executable="${VENV_DIR}/bin/python" \
16+
-Dplugin.scalapy.version="$1" \
17+
scripted
18+
19+
echo "Delete virtualenv ${VENV_DIR}"
20+
rm -r "${VENV_DIR}"

src/sbt-test/integration/scalapy/build.sbt

+18-7
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,38 @@ lazy val scala3 = "3.0.0"
66

77
ThisBuild / scalaVersion := scala213
88

9-
lazy val scalapyVersion = sys.props("plugin.scalapy.version")
9+
def getProp(p: String) = Option(sys.props(p)).map(_.trim).filter(_.nonEmpty)
1010

11-
lazy val python = Python(
12-
Option(sys.props("plugin.python.executable")).filter(_.trim.nonEmpty)
13-
)
11+
def getProps(prop: String*) =
12+
prop
13+
.map(p => p -> getProp(p))
14+
.collect { case (k, Some(v)) => s"""-D$k=$v""" }
15+
16+
lazy val scalapyVersion = getProp("plugin.scalapy.version").get
17+
18+
lazy val python = Python(getProp("plugin.python.executable"))
1419

1520
lazy val pythonLdFlags = python.ldflags.get
1621

1722
lazy val javaOpts = python.scalapyProperties.get.map { case (k, v) =>
1823
s"""-D$k=$v"""
1924
}.toSeq
2025

26+
val checkModule = taskKey[Unit]("Check loading a dummy module")
27+
2128
lazy val root = crossProject(JVMPlatform, NativePlatform)
2229
.in(file("."))
2330
.settings(
24-
crossScalaVersions := Seq(scala212, scala213)
31+
crossScalaVersions := Seq(scala212, scala213),
32+
Compile / mainClass := Some("project.Main")
2533
)
2634
.jvmSettings(
2735
fork := true,
28-
javaOptions ++= javaOpts,
29-
libraryDependencies += "me.shadaj" %% "scalapy-core" % scalapyVersion
36+
javaOptions ++= javaOpts ++ getProps("plugin.virtualenv"),
37+
libraryDependencies += "me.shadaj" %% "scalapy-core" % scalapyVersion,
38+
checkModule := {
39+
(Compile / runMain).toTask(" project.Module").value
40+
}
3041
)
3142
.nativeSettings(
3243
libraryDependencies += "me.shadaj" %%% "scalapy-core" % scalapyVersion,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package project
2+
3+
import me.shadaj.scalapy.py
4+
import me.shadaj.scalapy.py.{PyQuote, SeqConverters}
5+
6+
object Module {
7+
def main(args: Array[String]): Unit = {
8+
Option(sys.props("plugin.virtualenv")).map(_.trim).filter(_.nonEmpty).foreach { _ =>
9+
println(s"Successfully load ${py.module("dummy")}")
10+
}
11+
}
12+
}

src/sbt-test/integration/scalapy/shared/src/main/scala/project/Main.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import me.shadaj.scalapy.py.{PyQuote, SeqConverters}
55

66
object Main {
77
def main(args: Array[String]): Unit = {
8-
println(py.Dynamic.global.list(Seq(1, 2, 3).toPythonProxy))
8+
println(py.Dynamic.global.list(Seq(1, 2, 3).toPythonCopy))
99
println(py"'Hello from ScalaPy!'")
1010
}
1111
}

src/sbt-test/integration/scalapy/test

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
> +rootJVM/run
22
> +rootNative/run
3+
> +rootJVM/checkModule

0 commit comments

Comments
 (0)