From dbc261be51a787ddde51d0cef69f35c1f8e5bcb4 Mon Sep 17 00:00:00 2001 From: jaroslawmalekcodete Date: Tue, 18 Sep 2018 14:16:07 +0200 Subject: [PATCH] #7780: fix ClasspathAddMvnDepsMagicCommandTest under windows --- .../kernel/magic/command/FileServiceImpl.java | 14 ++++ .../command/MagicCommandTypesFactory.java | 2 +- .../ClasspathResetMagicCommand.java | 8 ++- .../command/functionality/FileService.java | 7 ++ .../com/twosigma/beakerx/FileServiceMock.java | 21 ++++++ .../java/com/twosigma/beakerx/KernelTest.java | 70 +++++-------------- .../ClasspathAddMvnDepsMagicCommandTest.java | 7 +- 7 files changed, 68 insertions(+), 61 deletions(-) create mode 100644 kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/FileServiceImpl.java create mode 100644 kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/FileService.java create mode 100644 kernel/base/src/test/java/com/twosigma/beakerx/FileServiceMock.java diff --git a/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/FileServiceImpl.java b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/FileServiceImpl.java new file mode 100644 index 0000000000..d41e410799 --- /dev/null +++ b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/FileServiceImpl.java @@ -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); + } +} diff --git a/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/MagicCommandTypesFactory.java b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/MagicCommandTypesFactory.java index d5f8733671..bc6fb0c9e1 100644 --- a/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/MagicCommandTypesFactory.java +++ b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/MagicCommandTypesFactory.java @@ -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) { diff --git a/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/ClasspathResetMagicCommand.java b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/ClasspathResetMagicCommand.java index bf756cae82..3a72555997 100644 --- a/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/ClasspathResetMagicCommand.java +++ b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/ClasspathResetMagicCommand.java @@ -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 @@ -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()); } diff --git a/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/FileService.java b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/FileService.java new file mode 100644 index 0000000000..4ea5e82ba6 --- /dev/null +++ b/kernel/base/src/main/java/com/twosigma/beakerx/kernel/magic/command/functionality/FileService.java @@ -0,0 +1,7 @@ +package com.twosigma.beakerx.kernel.magic.command.functionality; + +import java.io.File; + +public interface FileService { + void delete(File file); +} diff --git a/kernel/base/src/test/java/com/twosigma/beakerx/FileServiceMock.java b/kernel/base/src/test/java/com/twosigma/beakerx/FileServiceMock.java new file mode 100644 index 0000000000..5d4a00b21e --- /dev/null +++ b/kernel/base/src/test/java/com/twosigma/beakerx/FileServiceMock.java @@ -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 deletedFiles = new ArrayList<>(); + + @Override + public void delete(File file) { + this.deletedFiles.add(file.getAbsolutePath()); + } + + public List getDeletedFiles() { + return deletedFiles; + } +} diff --git a/kernel/base/src/test/java/com/twosigma/beakerx/KernelTest.java b/kernel/base/src/test/java/com/twosigma/beakerx/KernelTest.java index 71afb1f9fb..6363aacf4b 100644 --- a/kernel/base/src/test/java/com/twosigma/beakerx/KernelTest.java +++ b/kernel/base/src/test/java/com/twosigma/beakerx/KernelTest.java @@ -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; @@ -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; @@ -95,6 +51,7 @@ public class KernelTest implements KernelFunctionality { + private FileServiceMock fileService; private List publishedMessages = synchronizedList(new ArrayList<>()); private List sentMessages = synchronizedList(new ArrayList<>()); private String id; @@ -122,6 +79,7 @@ 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"); @@ -129,12 +87,14 @@ public KernelTest(String id, CommRepository commRepository) { 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"); @@ -167,7 +127,7 @@ private void initMagicCommands() { new ClasspathAddMvnMagicCommand(mavenResolverParam, this)), new MagicCommandType(ClassPathAddMvnCellMagicCommand.CLASSPATH_ADD_MVN_CELL, "", new ClassPathAddMvnCellMagicCommand(mavenResolverParam, this)), - addClasspathReset(this), + addClasspathReset(this, this.fileService), addDynamic(this), addMagicCommandWhichThrowsException(), new MagicCommandType(ClasspathShowMagicCommand.CLASSPATH_SHOW, "", new ClasspathShowMagicCommand(this)), @@ -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) { @@ -455,4 +415,8 @@ public void putEvaluationInToBackground() { public BeakerXServer getBeakerXServer() { return BeakerXServerMock.create(); } + + public FileServiceMock getFileService() { + return fileService; + } } diff --git a/kernel/base/src/test/java/com/twosigma/beakerx/kernel/magic/command/ClasspathAddMvnDepsMagicCommandTest.java b/kernel/base/src/test/java/com/twosigma/beakerx/kernel/magic/command/ClasspathAddMvnDepsMagicCommandTest.java index 4f7794fb5b..9779535852 100644 --- a/kernel/base/src/test/java/com/twosigma/beakerx/kernel/magic/command/ClasspathAddMvnDepsMagicCommandTest.java +++ b/kernel/base/src/test/java/com/twosigma/beakerx/kernel/magic/command/ClasspathAddMvnDepsMagicCommandTest.java @@ -124,10 +124,9 @@ public void handleClasspathReset() throws Exception { List 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 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()); }