Skip to content

Commit

Permalink
#7780: fix ClasspathAddMvnDepsMagicCommandTest under windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete committed Sep 18, 2018
1 parent 91c974d commit dbc261b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.twosigma.beakerx.kernel.magic.command;

import com.twosigma.beakerx.kernel.magic.command.functionality.FileService;
import org.apache.commons.io.FileUtils;

import java.io.File;

public class FileServiceImpl implements FileService {

@Override
public void delete(File file) {
FileUtils.deleteQuietly(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static ClasspathResetMagicCommand getClasspathResetMagicCommand(KernelFun
}

private static MagicCommandType addClasspathReset(KernelFunctionality kernel) {
return new MagicCommandType(ClasspathResetMagicCommand.CLASSPATH_RESET, "", new ClasspathResetMagicCommand(kernel));
return new MagicCommandType(ClasspathResetMagicCommand.CLASSPATH_RESET, "", new ClasspathResetMagicCommand(kernel, new FileServiceImpl()));
}

private static MagicCommandType addDynamic(KernelFunctionality kernel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public class ClasspathResetMagicCommand implements MagicCommandFunctionality {
public static final String CLASSPATH_PREFIX = "%classpath";
public static final String RESET = "reset";
public static final String CLASSPATH_RESET = CLASSPATH_PREFIX + " " + RESET;
private final FileService fileService;

private KernelFunctionality kernel;

public ClasspathResetMagicCommand(KernelFunctionality kernel) {
public ClasspathResetMagicCommand(KernelFunctionality kernel, FileService fileService) {
this.kernel = kernel;
this.fileService = fileService;
}

@Override
Expand All @@ -50,8 +52,8 @@ public MagicCommandOutcomeItem execute(MagicCommandExecutionParam param) {
ClasspathAddMvnMagicCommand mvnMagicCommand = MagicCommandTypesFactory.getClasspathAddMvnMagicCommand(kernel);
mvnMagicCommand.resetRepo();
try {
FileUtils.deleteQuietly(new File(mvnMagicCommand.getCommandParams().getPathToCache()));
FileUtils.deleteQuietly(new File(mvnMagicCommand.getCommandParams().getPathToNotebookJars()));
fileService.delete(new File(mvnMagicCommand.getCommandParams().getPathToCache()));
fileService.delete(new File(mvnMagicCommand.getCommandParams().getPathToNotebookJars()));
} catch (Exception e) {
return new MagicCommandOutput(Status.ERROR, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.twosigma.beakerx.kernel.magic.command.functionality;

import java.io.File;

public interface FileService {
void delete(File file);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.twosigma.beakerx;

import com.twosigma.beakerx.kernel.magic.command.functionality.FileService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileServiceMock implements FileService {

private List<String> deletedFiles = new ArrayList<>();

@Override
public void delete(File file) {
this.deletedFiles.add(file.getAbsolutePath());
}

public List<String> getDeletedFiles() {
return deletedFiles;
}
}
70 changes: 17 additions & 53 deletions kernel/base/src/test/java/com/twosigma/beakerx/KernelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,17 @@
import com.twosigma.beakerx.handler.Handler;
import com.twosigma.beakerx.inspect.InspectResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.AddImportStatus;
import com.twosigma.beakerx.kernel.ExecutionOptions;
import com.twosigma.beakerx.kernel.GroupName;
import com.twosigma.beakerx.kernel.magic.command.MagicCommandTypesFactory;
import com.twosigma.beakerx.kernel.restserver.BeakerXServer;
import com.twosigma.beakerx.kernel.Classpath;
import com.twosigma.beakerx.kernel.EvaluatorParameters;
import com.twosigma.beakerx.kernel.ImportPath;
import com.twosigma.beakerx.kernel.Imports;
import com.twosigma.beakerx.kernel.KernelFunctionality;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.kernel.NoSuchKernelException;
import com.twosigma.beakerx.kernel.PathToJar;
import com.twosigma.beakerx.kernel.PythonEntryPoint;
import com.twosigma.beakerx.kernel.MagicKernelManager;
import com.twosigma.beakerx.kernel.*;
import com.twosigma.beakerx.kernel.comm.Comm;
import com.twosigma.beakerx.kernel.magic.command.MagicCommandType;
import com.twosigma.beakerx.kernel.magic.command.MagicCommandTypesFactory;
import com.twosigma.beakerx.kernel.magic.command.MagicCommandWhichThrowsException;
import com.twosigma.beakerx.kernel.magic.command.MavenJarResolver;
import com.twosigma.beakerx.kernel.magic.command.functionality.AddImportMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.AddStaticImportMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.BashMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClassPathAddMvnCellMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddDynamicMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddJarMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddMvnMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathAddRepoMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathResetMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.ClasspathShowMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.HtmlAliasMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.HtmlMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.JSMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.JavaScriptMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.LoadMagicMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.LsMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.ClojureMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.GroovyMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.JavaMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.KernelMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.KotlinMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.PythonMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.TimeCellModeMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.TimeItCellModeMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.TimeItLineModeMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.TimeLineModeMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.UnImportMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.ScalaMagicCommand;
import com.twosigma.beakerx.kernel.magic.command.functionality.*;
import com.twosigma.beakerx.kernel.magic.command.functionality.kernelMagic.*;
import com.twosigma.beakerx.kernel.msg.JupyterMessages;
import com.twosigma.beakerx.kernel.msg.MessageCreator;
import com.twosigma.beakerx.kernel.restserver.BeakerXServer;
import com.twosigma.beakerx.kernel.threads.ExecutionResultSender;
import com.twosigma.beakerx.message.Message;
import org.apache.commons.io.FileUtils;
Expand All @@ -80,13 +42,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observer;
import java.util.Set;
import java.util.*;

import static com.twosigma.beakerx.MessageFactorTest.commMsg;
import static com.twosigma.beakerx.kernel.magic.command.ClasspathAddMvnDepsMagicCommandTest.TEST_MVN_CACHE;
Expand All @@ -95,6 +51,7 @@

public class KernelTest implements KernelFunctionality {

private FileServiceMock fileService;
private List<Message> publishedMessages = synchronizedList(new ArrayList<>());
private List<Message> sentMessages = synchronizedList(new ArrayList<>());
private String id;
Expand Down Expand Up @@ -122,19 +79,22 @@ public KernelTest(CommRepository commRepository) {
public KernelTest(String id, CommRepository commRepository) {
this.id = id;
this.commRepository = commRepository;
this.fileService = new FileServiceMock();
initMavenResolverParam();
initMagicCommands();
SimpleEvaluationObject value = new SimpleEvaluationObject("ok");
Message jupyterMessage = commMsg();
value.setJupyterMessage(jupyterMessage);
InternalVariable.setValue(value);
KernelManager.register(this);

}

public KernelTest(String id, Evaluator evaluator) {
this.id = id;
this.evaluator = evaluator;
this.commRepository = new BeakerXCommRepositoryMock();
this.fileService = new FileServiceMock();
initMavenResolverParam();
initMagicCommands();
SimpleEvaluationObject value = new SimpleEvaluationObject("ok");
Expand Down Expand Up @@ -167,7 +127,7 @@ private void initMagicCommands() {
new ClasspathAddMvnMagicCommand(mavenResolverParam, this)),
new MagicCommandType(ClassPathAddMvnCellMagicCommand.CLASSPATH_ADD_MVN_CELL, "<group name version>",
new ClassPathAddMvnCellMagicCommand(mavenResolverParam, this)),
addClasspathReset(this),
addClasspathReset(this, this.fileService),
addDynamic(this),
addMagicCommandWhichThrowsException(),
new MagicCommandType(ClasspathShowMagicCommand.CLASSPATH_SHOW, "", new ClasspathShowMagicCommand(this)),
Expand All @@ -190,8 +150,8 @@ private void initMagicCommands() {
));
}

private static MagicCommandType addClasspathReset(KernelFunctionality kernel) {
return new MagicCommandType(ClasspathResetMagicCommand.CLASSPATH_RESET, "", new ClasspathResetMagicCommand(kernel));
private static MagicCommandType addClasspathReset(KernelFunctionality kernel, FileService fileService) {
return new MagicCommandType(ClasspathResetMagicCommand.CLASSPATH_RESET, "", new ClasspathResetMagicCommand(kernel, fileService));
}

private static MagicCommandType addDynamic(KernelFunctionality kernel) {
Expand Down Expand Up @@ -455,4 +415,8 @@ public void putEvaluationInToBackground() {
public BeakerXServer getBeakerXServer() {
return BeakerXServerMock.create();
}

public FileServiceMock getFileService() {
return fileService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ public void handleClasspathReset() throws Exception {
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);
List<String> deletedFiles = kernel.getFileService().getDeletedFiles();
Assert.assertTrue(deletedFiles.contains(mvnMagicCommand.getCommandParams().getPathToCache()));
Assert.assertTrue(deletedFiles.contains(mvnMagicCommand.getCommandParams().getPathToNotebookJars()));
assertTrue(mvnMagicCommand.getRepos().get().size() == DEFAULT_MAVEN_REPOS.size());
}

Expand Down

0 comments on commit dbc261b

Please sign in to comment.