From 0072a0aed276c6509f7030924a343ddce3178568 Mon Sep 17 00:00:00 2001 From: sameagen Date: Thu, 29 Feb 2024 12:02:58 -0500 Subject: [PATCH] Fix issue with sd flag --- .../com/mathworks/ci/MatlabCommandRunner.java | 22 ++++++++++++++++--- .../mathworks/ci/RunMatlabTestsService.java | 21 ++++++------------ .../mathworks/ci/MatlabCommandRunnerTest.java | 6 +++-- .../com/mathworks/ci/RunMatlabTestsTest.java | 4 ++-- .../com/mathworks/ci/MatlabConstants.java | 9 ++------ 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/matlab-plugin-agent/src/main/java/com/mathworks/ci/MatlabCommandRunner.java b/matlab-plugin-agent/src/main/java/com/mathworks/ci/MatlabCommandRunner.java index cd67d3d..120f643 100644 --- a/matlab-plugin-agent/src/main/java/com/mathworks/ci/MatlabCommandRunner.java +++ b/matlab-plugin-agent/src/main/java/com/mathworks/ci/MatlabCommandRunner.java @@ -19,6 +19,7 @@ import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.io.FileUtils; import org.jetbrains.annotations.NotNull; +import net.lingala.zip4j.ZipFile; public class MatlabCommandRunner { private File tempDirectory; @@ -52,8 +53,9 @@ private List generateCommandArgs(BuildRunnerContext runnerContext, Strin List commandArgs = new ArrayList(); final File uniqueCommandFile = new File(this.tempDirectory, "matlab_" + this.tempDirectory.getName()); - String commandToExecute = "addpath('" + this.tempDirectory.getPath().replaceAll("'", ";;") + "');" + uniqueCommandFile.getName(); - + String commandToExecute = "setenv('MW_ORIG_WORKING_FOLDER', cd('" + + this.tempDirectory.getAbsolutePath() + + "'));" + uniqueCommandFile.getName(); // Create script createMatlabScriptByName(runnerContext, command, uniqueCommandFile.getName()); @@ -74,7 +76,7 @@ private void createMatlabScriptByName(BuildRunnerContext runnerContext, String c final File matlabCommandFile = new File(this.tempDirectory, uniqueScriptName + ".m"); final String matlabCommandFileContent = - "cd '" + runnerContext.getWorkingDirectory().getAbsolutePath().replaceAll("'", "''") + "';\n" + command; + "cd(getenv('MW_ORIG_WORKING_FOLDER'));\n" + command; FileUtils.writeStringToFile(matlabCommandFile, matlabCommandFileContent); } @@ -105,10 +107,24 @@ private String copyExecutable(BuildRunnerContext runnerContext) throws IOExcepti return executable; } + public void unzipToTempDir(String zipName) throws IOException { + // Copy zip to tempDirectory + File zipFileLocation = new File(tempDirectory, zipName); + copyFileToWorkspace(zipName, zipFileLocation); + + // Unzip + ZipFile zipFile = new ZipFile(zipFileLocation); + zipFile.extractAll(tempDirectory.toString()); + } + public void copyFileToWorkspace(String sourceFile, File targetFile) throws IOException { InputStream is = MatlabCommandRunner.class.getClassLoader().getResourceAsStream(sourceFile); java.nio.file.Files.copy(is, targetFile.toPath(), REPLACE_EXISTING); + + targetFile.setReadable(true, true); + targetFile.setWritable(true); targetFile.setExecutable(true); + IOUtils.closeQuietly(is); } diff --git a/matlab-plugin-agent/src/main/java/com/mathworks/ci/RunMatlabTestsService.java b/matlab-plugin-agent/src/main/java/com/mathworks/ci/RunMatlabTestsService.java index 74f2160..d5f1c10 100644 --- a/matlab-plugin-agent/src/main/java/com/mathworks/ci/RunMatlabTestsService.java +++ b/matlab-plugin-agent/src/main/java/com/mathworks/ci/RunMatlabTestsService.java @@ -43,16 +43,14 @@ public ProgramCommandLine makeProgramCommandLine() throws RunBuildException { // Set up runner - can't be done at construction time since we don't have access to the context this.runner.createUniqueFolder(getContext()); - // Move genscript to temp directory - File genscriptLocation = new File(this.runner.getTempDirectory(), MatlabConstants.MATLAB_SCRIPT_GENERATOR); - // Prepare command - String runnerScript = getRunnerScript(MatlabConstants.TEST_RUNNER_SCRIPT, getGenScriptParametersForTests(this.runner.getTempDirectory().getName())); - runnerScript = replaceZipPlaceholder(runnerScript, genscriptLocation.getPath()); + String runnerScript = getRunnerScript(MatlabConstants.TEST_RUNNER_SCRIPT, + getGenScriptParametersForTests(this.runner.getTempDirectory().getName()), + this.runner.getTempDirectory().getAbsolutePath()); ProgramCommandLine value; try { - this.runner.copyFileToWorkspace(MatlabConstants.MATLAB_SCRIPT_GENERATOR, genscriptLocation); + this.runner.unzipToTempDir(MatlabConstants.MATLAB_SCRIPT_GENERATOR); value = this.runner.createCommand(getContext(), runnerScript); } catch (Exception e) { throw new RunBuildException(e); @@ -62,14 +60,9 @@ public ProgramCommandLine makeProgramCommandLine() throws RunBuildException { return value; } - //This method replaces the placeholder with genscript's zip file location URL in temp folder - private String replaceZipPlaceholder(String script, String url) { - script = script.replace("${ZIP_FILE}", url.replaceAll("'", "''")); - return script; - } - - //To get therunner script - private String getRunnerScript(String script, String params) { + //To get the runner script + private String getRunnerScript(String script, String params, String tempFolder) { + script = script.replace("${TEMPFOLDER}", tempFolder); script = script.replace("${PARAMS}", params); return script; } diff --git a/matlab-plugin-agent/src/test/java/com/mathworks/ci/MatlabCommandRunnerTest.java b/matlab-plugin-agent/src/test/java/com/mathworks/ci/MatlabCommandRunnerTest.java index 906b91a..4b3eb0a 100644 --- a/matlab-plugin-agent/src/test/java/com/mathworks/ci/MatlabCommandRunnerTest.java +++ b/matlab-plugin-agent/src/test/java/com/mathworks/ci/MatlabCommandRunnerTest.java @@ -149,7 +149,9 @@ public void verifyScriptCreated() throws IOException, NoSuchMethodException, Ill String command = "disp(\"Hello world\")"; List expectedCommand = new ArrayList(); - expectedCommand.add("addpath('" + currDir.getPath().replaceAll("'", ";;") + "');" + "matlab_" + currDir.getName()); + expectedCommand.add("setenv('MW_ORIG_WORKING_FOLDER', cd('" + + currDir.getPath().replaceAll("'", ";;") + + "'));" + "matlab_" + currDir.getName()); Method generateCommandArgs = getAccessibleMethod("generateCommandArgs", BuildRunnerContext.class, String.class); @@ -160,7 +162,7 @@ public void verifyScriptCreated() throws IOException, NoSuchMethodException, Ill Assert.assertTrue(expectedFile.exists()); String contents = FileUtils.readFileToString(expectedFile); - Assert.assertEquals(contents, "cd '" + currDir.getPath() + "';\n" + command); + Assert.assertEquals(contents, "cd(getenv('MW_ORIG_WORKING_FOLDER'));\n" + command); } // startup options test diff --git a/matlab-plugin-agent/src/test/java/com/mathworks/ci/RunMatlabTestsTest.java b/matlab-plugin-agent/src/test/java/com/mathworks/ci/RunMatlabTestsTest.java index fdbcdc1..06884ca 100644 --- a/matlab-plugin-agent/src/test/java/com/mathworks/ci/RunMatlabTestsTest.java +++ b/matlab-plugin-agent/src/test/java/com/mathworks/ci/RunMatlabTestsTest.java @@ -96,7 +96,7 @@ public void checkBaseCase() throws IOException, RunBuildException { Mockito.verify(runner).createCommand(isNull(), matlabCommand.capture()); String expected = MatlabConstants.TEST_RUNNER_SCRIPT.replace("${PARAMS}", genscriptArgs) - .replace("${ZIP_FILE}", genscriptZipLocation); + .replace("${TEMPFOLDER}", tempDir.getAbsolutePath()); Assert.assertEquals(expected, matlabCommand.getValue()); } @@ -125,7 +125,7 @@ public static Object[][] dataWithAllInput(){ "'HTMLCodeCoverage','.matlab/tempDir/htmlCoverage'"; String genscriptZipLocation = new File(tempDir, "matlab-script-generator.zip").getAbsolutePath(); - String expectedGeneratedScript = MatlabConstants.TEST_RUNNER_SCRIPT.replace("${ZIP_FILE}", genscriptZipLocation) + String expectedGeneratedScript = MatlabConstants.TEST_RUNNER_SCRIPT.replace("${TEMPFOLDER}", tempDir.getAbsolutePath()) .replace("${PARAMS}", expectedGenscriptArgs); return new Object[][] {{envMapsWithAllUserInputs, expectedGeneratedScript}}; } diff --git a/matlab-plugin-common/src/main/java/com/mathworks/ci/MatlabConstants.java b/matlab-plugin-common/src/main/java/com/mathworks/ci/MatlabConstants.java index 833ec43..19fc647 100644 --- a/matlab-plugin-common/src/main/java/com/mathworks/ci/MatlabConstants.java +++ b/matlab-plugin-common/src/main/java/com/mathworks/ci/MatlabConstants.java @@ -40,16 +40,11 @@ public interface MatlabConstants { //MATLAB Runner Script static final String TEST_RUNNER_SCRIPT = String.join(NEW_LINE, - "tmpDir=tempname;", - "mkdir(tmpDir);", - "addpath(tmpDir);", - "zipURL='${ZIP_FILE}';", - "unzip(zipURL,tmpDir);", + "addpath('${TEMPFOLDER}');", "testScript = genscript(${PARAMS});", "disp('Running MATLAB script with content:');", "disp(testScript.Contents);", - "testScript.writeToFile(fullfile(tmpDir,'runnerScript.m'));", "fprintf('___________________________________\\n\\n');", - "runnerScript()"); + "run(testScript);"); }