Skip to content

Commit

Permalink
Merge pull request #45 from matlab-actions/sdfix
Browse files Browse the repository at this point in the history
Sdfix
  • Loading branch information
sameagen-MW authored Dec 15, 2023
2 parents 536a275 + 0e4d45e commit 07310e1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/bat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ jobs:
uses: ./
with:
command: assert(onetyone==11, 'the variable `onetyone` was not set as expected by startup.m')

- run: |
mkdir subdir
echo 'onetyonetyone = 111' > subdir/startup.m
shell: bash
- name: MATLAB sd startup option is not overwritten
uses: ./
with:
command: >
assert(onetyonetyone==111);
[~, f] = fileparts(pwd);
assert(strcmp(f, 'subdir'));
startup-options: -sd subdir
9 changes: 6 additions & 3 deletions src/matlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ export async function generateScript(workspaceDir: string, command: string): Pro
const temporaryDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "run_matlab_command-"));

const scriptPath = path.join(temporaryDirectory, scriptName + ".m");
await fs.writeFile(scriptPath, script.cdAndCall(workspaceDir, command), {
await fs.writeFile(scriptPath, script.cdAndCall(command), {
encoding: "utf8",
});

return { dir: temporaryDirectory, command: scriptName };
return {
dir: temporaryDirectory,
command: scriptName
};
}

/**
Expand All @@ -54,7 +57,7 @@ export async function runCommand(hs: HelperScript, platform: string, architectur
const rmcPath = getRunMATLABCommandScriptPath(platform, architecture);
await fs.chmod(rmcPath, 0o777);

const rmcArg = script.cdAndCall(hs.dir, hs.command);
const rmcArg = `setenv('MW_ORIG_WORKING_FOLDER', cd('${script.pathToCharVec(hs.dir)}'));${hs.command}`;

let execArgs = [rmcArg];

Expand Down
6 changes: 3 additions & 3 deletions src/script.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The MathWorks, Inc.
// Copyright 2020-2023 The MathWorks, Inc.

/**
* Generate MATLAB command for changing directories and calling a command in it.
Expand All @@ -7,8 +7,8 @@
* @param command Command to run in directory.
* @returns MATLAB command.
*/
export function cdAndCall(dir: string, command: string): string {
return `cd('${pathToCharVec(dir)}');${command}`;
export function cdAndCall(command: string): string {
return `cd(getenv('MW_ORIG_WORKING_FOLDER'));${command}`;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/script.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright 2020 The MathWorks, Inc.
// Copyright 2020-2023 The MathWorks, Inc.

import * as script from "./script";

describe("call generation", () => {
it("ideally works", () => {
// I know what your thinking
const testDir = String.raw`C:\Users\you\You're Documents`;
const testCommand = "disp('hello world')";
const expectedString = String.raw`cd('C:\Users\you\You''re Documents');${testCommand}`;
const expectedString = `cd(getenv('MW_ORIG_WORKING_FOLDER'));${testCommand}`;

expect(script.cdAndCall(testDir, testCommand)).toMatch(expectedString);
expect(script.cdAndCall(testCommand)).toMatch(expectedString);
});
});

Expand Down

0 comments on commit 07310e1

Please sign in to comment.