Skip to content

Commit

Permalink
Merge pull request #33 from mathworks/sdfix
Browse files Browse the repository at this point in the history
Fix issue with sd flag
  • Loading branch information
sameagen-MW authored Mar 12, 2024
2 parents da4e355 + 0072a0a commit c2a0f6d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,8 +53,9 @@ private List<String> generateCommandArgs(BuildRunnerContext runnerContext, Strin
List<String> commandArgs = new ArrayList<String>();

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());
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public void verifyScriptCreated() throws IOException, NoSuchMethodException, Ill
String command = "disp(\"Hello world\")";

List<String> expectedCommand = new ArrayList<String>();
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);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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}};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,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);");
}

0 comments on commit c2a0f6d

Please sign in to comment.