Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infrastructure for testing inter project imports and exports #6840

Merged
merged 6 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: Logical_Import_Violated_Test
license: APLv2
enso-version: default
version: "0.0.1"
author: "Enso Team <[email protected]>"
maintainer: "Enso Team <[email protected]>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from Test.Logical_Export import Api

main =
element = Api.Element.Element.create
element.describe
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,47 @@ class ImportExportTest
}
}

"Import resolution from another library honor Main" should {
"resolve Api from Main" in {
val mainIr = """
|from Test.Logical_Export import Api
|
|main =
| element = Api.Element.Element.create
| element.describe
|""".stripMargin
.createModule(packageQualifiedName.createChild("Main"))
.getIr

mainIr.imports.size shouldEqual 1
val in = mainIr.imports.head
.asInstanceOf[IR.Module.Scope.Import.Module]

in.name.name.toString() should include("Test.Logical_Export.Main")
in.onlyNames.get.map(_.name.toString()) shouldEqual List("Api")

val errors = mainIr.preorder.filter(x => x.isInstanceOf[IR.Error])
errors.size shouldEqual 0
}

"don't expose Impl from Main" in {
val mainIr = """
|from Test.Logical_Export import Impl
|
|main = Impl
|""".stripMargin
.createModule(packageQualifiedName.createChild("Main"))
.getIr

mainIr.imports.head
.asInstanceOf[IR.Error.ImportExport]
.reason
.message should include(
"The symbol Impl (module or type) does not exist in module Test.Logical_Export.Main."
)
}
}

"Import resolution for three modules" should {

"not resolve symbol that is not explicitly exported" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ package org.enso.interpreter.test
import org.graalvm.polyglot.PolyglotException

case class InterpreterException(
@transient polyglotException: PolyglotException
@transient polyglotException: PolyglotException,
@transient output: Option[Any]
) extends Throwable
with Serializable {
override def getMessage: String = polyglotException.getMessage
override def getMessage: String = {
val msg = polyglotException.getMessage
output.map(msg + "\n" + _.toString()).getOrElse(msg)
}
override def getLocalizedMessage: String = {
polyglotException.getMessage
}
override def getStackTrace: Array[StackTraceElement] =
polyglotException.getStackTrace
override def fillInStackTrace(): Throwable = this
}

object InterpreterException {
def rethrowPolyglot[T](compute: => T): T =
def rethrowPolyglot[T](compute: => T, output: Option[Any] = None): T =
try {
compute
} catch { case e: PolyglotException => throw InterpreterException(e) }
} catch {
case e: PolyglotException => throw InterpreterException(e, output)
}

implicit def toPolyglotException(
interpreterException: InterpreterException
): PolyglotException = interpreterException.polyglotException

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ trait PackageTest extends AnyFlatSpec with Matchers with ValueEquality {
.build()
context.initialize(LanguageInfo.ID)
val executionContext = new PolyglotContext(context)
InterpreterException.rethrowPolyglot {
val topScope = executionContext.getTopScope
val mainModuleScope = topScope.getModule(mainModule.toString)
val assocCons = mainModuleScope.getAssociatedType
val mainFun = mainModuleScope.getMethod(assocCons, "main").get
mainFun.execute()
}
InterpreterException.rethrowPolyglot(
{
val topScope = executionContext.getTopScope
val mainModuleScope = topScope.getModule(mainModule.toString)
val assocCons = mainModuleScope.getAssociatedType
val mainFun = mainModuleScope.getMethod(assocCons, "main").get
mainFun.execute()
},
output = Some(output)
)
}

def consumeOut: List[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package org.enso.interpreter.test.semantic
import org.enso.interpreter.test.{InterpreterException, PackageTest}

class ImportsTest extends PackageTest {
implicit def messagingNatureOInterpreterException
: org.scalatest.enablers.Messaging[InterpreterException] =
new org.scalatest.enablers.Messaging[InterpreterException] {
def messageOf(exception: InterpreterException): String =
exception.getLocalizedMessage
}

"Atoms and methods" should "be available for import" in {
evalTestProject("TestSimpleImports") shouldEqual 20
}
Expand Down Expand Up @@ -81,6 +88,14 @@ class ImportsTest extends PackageTest {
consumeOut should contain("Export statements form a cycle:")
}

"Exports system" should "honor logical export" in {
val compilationResult = evalTestProject(
"Logical_Import_Violated_Test"
)
compilationResult shouldEqual "Element with Internal"
consumeOut shouldEqual List()
}

"Import statements" should "allow for importing submodules" in {
evalTestProject("TestSubmodules") shouldEqual 42
val outLines = consumeOut
Expand Down
3 changes: 3 additions & 0 deletions test/micro-distribution/editions/0.0.0-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ libraries:
- name: Standard.Base
repository: main
version: 0.0.0-dev
- name: Test.Logical_Export
repository: main
version: 0.0.0-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
archives:
- main.tgz
dependencies: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Logical_Export
namespace: Test
version: 0.0.0-dev
license: APLv2
authors:
- name: Enso Team
email: [email protected]
maintainers:
- name: Enso Team
email: [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import project.Impl.Internal.Internal

type Element
Value impl:Internal

create = Element.Value (Internal.Impl)

describe self = "Element with " + self.impl.describe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Internal
Impl

describe self = "Internal"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import project.Api
export project.Api