Search in sources :

Example 46 with Code

use of com.twosigma.beakerx.kernel.Code in project beakerx by twosigma.

the class ClasspathAddMvnDepsMagicCommandTest method handleClasspathReset.

@Test
public void handleClasspathReset() throws Exception {
    // given
    String allCode = CLASSPATH_ADD_MVN + " com.google.code.gson:gson:2.6.2";
    handleClasspathAddMvnDep(allCode, "gson-2.6.2.jar");
    kernel.clearMessages();
    ClasspathAddMvnMagicCommand mvnMagicCommand = MagicCommandTypesFactory.getClasspathAddMvnMagicCommand(kernel);
    mvnMagicCommand.addRepo("jcenter", "jcenter");
    // when
    String resetCode = CLASSPATH_RESET;
    ClasspathResetMagicCommand resetMagicCommand = MagicCommandTypesFactory.getClasspathResetMagicCommand(kernel);
    MagicCommand command = new MagicCommand(resetMagicCommand, resetCode);
    Code code = Code.createCode(resetCode, singletonList(command), NO_ERRORS, new Message());
    code.execute(kernel, 1);
    // then
    List<Message> stderr = EvaluatorResultTestWatcher.getStdouts(kernel.getPublishedMessages());
    String text = (String) stderr.get(0).getContent().get("text");
    assertThat(text).contains("Reset done");
    boolean cache = Files.exists(Paths.get(mvnMagicCommand.getCommandParams().getPathToCache()));
    Assert.assertFalse(cache);
    boolean jars = Files.exists(Paths.get(mvnMagicCommand.getCommandParams().getPathToNotebookJars()));
    Assert.assertFalse(jars);
    Assert.assertTrue(mvnMagicCommand.getRepos().get().isEmpty());
}
Also used : ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) Message(com.twosigma.beakerx.message.Message) ClasspathResetMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand) ClasspathResetMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand) ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) Code(com.twosigma.beakerx.kernel.Code) EvaluatorTest(com.twosigma.beakerx.evaluator.EvaluatorTest) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Example 47 with Code

use of com.twosigma.beakerx.kernel.Code in project beakerx by twosigma.

the class ClasspathAddMvnDepsMagicCommandTest method wrongCommandFormat.

@Test
public void wrongCommandFormat() {
    // given
    String allCode = CLASSPATH_ADD_MVN + " com.google.code.XXXX gson";
    MagicCommand command = new MagicCommand(new ClasspathAddMvnMagicCommand(kernel.mavenResolverParam, kernel), allCode);
    Code code = Code.createCode(allCode, singletonList(command), NO_ERRORS, new Message());
    // when
    code.execute(kernel, 1);
    // then
    List<Message> stderr = EvaluatorResultTestWatcher.getStderr(kernel.getPublishedMessages());
    String text = (String) stderr.get(0).getContent().get("text");
    assertThat(text).contains(ADD_MVN_FORMAT_ERROR_MESSAGE + "\n");
}
Also used : ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) Message(com.twosigma.beakerx.message.Message) ClasspathResetMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand) ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) Code(com.twosigma.beakerx.kernel.Code) EvaluatorTest(com.twosigma.beakerx.evaluator.EvaluatorTest) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Example 48 with Code

use of com.twosigma.beakerx.kernel.Code in project beakerx by twosigma.

the class ClasspathAddMvnDepsMagicCommandTest method handleClasspathAddMvnDep.

private void handleClasspathAddMvnDep(String allCode, String expected) throws IOException {
    MagicCommand command = new MagicCommand(new ClasspathAddMvnMagicCommand(kernel.mavenResolverParam, kernel), allCode);
    Code code = Code.createCode(allCode, singletonList(command), NO_ERRORS, new Message());
    // when
    code.execute(kernel, 1);
    // then
    List<Message> stderr = EvaluatorResultTestWatcher.getStdouts(kernel.getPublishedMessages());
    String text = (String) stderr.get(0).getContent().get("text");
    assertThat(text).contains("Added jar");
    assertThat(text).contains(expected);
    String mvnDir = kernel.getTempFolder().toString() + MavenJarResolver.MVN_DIR;
    Stream<Path> paths = Files.walk(Paths.get(mvnDir));
    Optional<Path> dep = paths.filter(file -> (file.getFileName().toFile().getName().contains("gson") || file.getFileName().toFile().getName().contains("slf4j"))).findFirst();
    assertThat(dep).isPresent();
    assertThat(kernel.getClasspath().get(0)).contains(mvnDir);
    dep.ifPresent(path -> {
        try {
            FileUtils.forceDelete(path.toFile());
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}
Also used : ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) Path(java.nio.file.Path) CLASSPATH_RESET(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand.CLASSPATH_RESET) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) EvaluatorTest(com.twosigma.beakerx.evaluator.EvaluatorTest) After(org.junit.After) ZipFile(java.util.zip.ZipFile) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) ClasspathResetMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand) Before(org.junit.Before) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) IOException(java.io.IOException) KernelTest(com.twosigma.beakerx.KernelTest) Message(com.twosigma.beakerx.message.Message) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) CLASSPATH_ADD_MVN(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand.CLASSPATH_ADD_MVN) ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) EvaluatorResultTestWatcher(com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher) Optional(java.util.Optional) ADD_MVN_FORMAT_ERROR_MESSAGE(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand.ADD_MVN_FORMAT_ERROR_MESSAGE) Assert(org.junit.Assert) Code(com.twosigma.beakerx.kernel.Code) MagicCommandOutcomeItem(com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem) InputStream(java.io.InputStream) Message(com.twosigma.beakerx.message.Message) ClasspathResetMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand) ClasspathAddMvnMagicCommand(com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand) IOException(java.io.IOException) Code(com.twosigma.beakerx.kernel.Code)

Example 49 with Code

use of com.twosigma.beakerx.kernel.Code in project beakerx by twosigma.

the class CodeFactoryTest method shouldBuildCodeWithMagicCommandInsideIfStatement.

@Test
public void shouldBuildCodeWithMagicCommandInsideIfStatement() {
    // given
    String codeAsString = "" + "println(1)\n" + "if(true){\n" + "    %time println(221)\n" + "}\n" + "println(2)\n" + "  %time 1+2";
    // when
    Code code = CodeFactory.create(codeAsString, new Message(), kernel);
    // then
    assertThat(code.getCodeFrames().size()).isEqualTo(4);
}
Also used : Message(com.twosigma.beakerx.message.Message) Code(com.twosigma.beakerx.kernel.Code) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Example 50 with Code

use of com.twosigma.beakerx.kernel.Code in project beakerx by twosigma.

the class CodeFactoryTest method shouldBuildCodeWithIfStatement.

@Test
public void shouldBuildCodeWithIfStatement() {
    // given
    String codeAsString = "" + "println(1)\n" + "%time 1+1\n" + "if(true){\n" + "  println(2)\n" + "}\n" + "println(3)\n" + "%time 1+2\n" + "println(4)\n";
    // when
    Code code = CodeFactory.create(codeAsString, new Message(), kernel);
    // then
    assertThat(code.getCodeFrames().size()).isEqualTo(5);
}
Also used : Message(com.twosigma.beakerx.message.Message) Code(com.twosigma.beakerx.kernel.Code) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Aggregations

Code (com.twosigma.beakerx.kernel.Code)55 Message (com.twosigma.beakerx.message.Message)54 Test (org.junit.Test)47 KernelTest (com.twosigma.beakerx.KernelTest)33 EvaluatorTest (com.twosigma.beakerx.evaluator.EvaluatorTest)29 MessageFactoryTest.getExecuteRequestMessage (com.twosigma.beakerx.MessageFactoryTest.getExecuteRequestMessage)16 EvaluatorResultTestWatcher.waitForIdleMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForIdleMessage)16 PlainCode (com.twosigma.beakerx.kernel.PlainCode)14 MessageAssertions.verifyExecuteReplyMessage (com.twosigma.MessageAssertions.verifyExecuteReplyMessage)9 EvaluatorResultTestWatcher.waitForErrorMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForErrorMessage)9 EvaluatorResultTestWatcher.waitForSentMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForSentMessage)9 KernelExecutionTest (com.twosigma.beakerx.KernelExecutionTest)7 EvaluatorResultTestWatcher.waitForUpdateMessage (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher.waitForUpdateMessage)7 ClasspathAddMvnMagicCommand (com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand)4 ClasspathResetMagicCommand (com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand)4 ImportPath (com.twosigma.beakerx.kernel.ImportPath)3 PathToJar (com.twosigma.beakerx.kernel.PathToJar)2 MagicCommand (com.twosigma.beakerx.kernel.magic.command.MagicCommand)2 ClassPathAddMvnCellMagicCommand (com.twosigma.beakerx.kernel.magic.command.functionality.ClassPathAddMvnCellMagicCommand)2 EvaluatorResultTestWatcher (com.twosigma.beakerx.evaluator.EvaluatorResultTestWatcher)1