Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
Fix withInterpreter issue (#594)
Browse files Browse the repository at this point in the history
* directly execute the app if absolute path of the executable is provided

* add tests for withInterpreter function

* remove unused imports and variables

* simplify the value extraction from object
  • Loading branch information
priyamsahoo authored Jul 25, 2023
1 parent eb5f31f commit f0f8e8d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ export function withInterpreter(

const pathEntry = path.join(virtualEnv, "bin");
if (path.isAbsolute(executable)) {
// if both interpreter path and absolute command path are provided, we can
// bolster the chances of success by letting the interpreter execute the
// command
command = `${interpreterPath} ${executable} ${args}`;
// if the user provided a path to the executable, we directly execute the app.
command = `${executable} ${args}`;
}

// emulating virtual environment activation script
Expand Down
76 changes: 76 additions & 0 deletions test/utils/withInterpreter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { expect } from "chai";
import { withInterpreter } from "../../src/utils/misc";

describe("withInterpreter", () => {
const tests = [
{
scenario: "when activation script is provided",
executable: "ansible-lint",
args: "playbook.yml",
interpreterPath: "",
activationScript: "/path/to/venv/bin/activate",
expectedCommand:
"bash -c 'source /path/to/venv/bin/activate && ansible-lint playbook.yml'",
expectedEnv: undefined,
},
{
scenario: "when no activation script is provided",
executable: "ansible-lint",
args: "playbook.yml",
interpreterPath: "",
activationScript: "",
expectedCommand: "ansible-lint playbook.yml",
},
{
scenario: "when absolute path of executable is provided",
executable: "/absolute/path/to/ansible-lint",
args: "playbook.yml",
interpreterPath: "",
activationScript: "",
expectedCommand: "/absolute/path/to/ansible-lint playbook.yml",
},
{
scenario: "when absolute path of interpreter is provided",
executable: "/absolute/path/to/ansible-lint",
args: "playbook.yml",
interpreterPath: "/path/to/venv/bin/python",
activationScript: "",
expectedCommand: "/absolute/path/to/ansible-lint playbook.yml",
expectedEnv: {
VIRTUAL_ENV: "/path/to/venv",
PATH: "/path/to/venv/bin",
},
},
];

tests.forEach(
({
scenario,
executable,
args,
interpreterPath,
activationScript,
expectedCommand,
expectedEnv,
}) => {
it(`should provide command ${scenario}`, () => {
const actualCommand = withInterpreter(
executable,
args,
interpreterPath,
activationScript,
);
expect(actualCommand[0]).to.equal(expectedCommand);

if (expectedEnv) {
const expectedKeys = Object.keys(expectedEnv);

expectedKeys.forEach((key) => {
expect(actualCommand[1]).to.haveOwnProperty(key);
expect(actualCommand[1][key]).to.include(expectedEnv[key]);
});
}
});
},
);
});

0 comments on commit f0f8e8d

Please sign in to comment.