Skip to content

Commit 4b12975

Browse files
authored
Fix/test executable path (#76)
* README.md: Update test executable settings description for clarity adapter.ts: Use test path from settings for working directory in execution options * Add tests for workingDirectory option in ExecutableRunner and testExecutablePath resolution in SettingsProvider * The expression testPath || undefined is redundant. If testPath is falsy (empty string, null, undefined), it will already be falsy and can be used directly. Simply use workingDirectory: testPath instead.
1 parent ca1d15f commit 4b12975

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ To let this plugin know where your tests are set the ```cpputestTestAdapter.test
1717
"cpputestTestAdapter.testExecutablePath": "${workspaceFolder}/test"
1818
}
1919
```
20-
They will be executed in the ```cpputestTestAdapter.testExecutablePath``` path.
20+
21+
Both settings support the ```${workspaceFolder}``` variable. The ```testExecutablePath``` determines the working directory:
22+
- If set, all test executables will be run from this directory
23+
- If not set, each test executable will be run from its own directory (the directory containing the executable)
2124

2225
To arrange for a task to be run prior to running tests or refreshing the test list, set ```cpputestTestAdapter.preLaunchTask``` to the name of a task from tasks.json. This can be used to rebuild the test executable, for example.
2326

src/adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ export class CppUTestAdapter implements TestAdapter {
145145
}
146146

147147
private GetExecutionOptions(): ExecutableRunnerOptions | undefined {
148+
const testPath = this.settingsProvider.GetTestPath();
148149
return {
149150
objDumpExecutable: this.settingsProvider.GetObjDumpPath(),
150-
workingDirectory: undefined // this is calculated inside the ExecutableRunner, not really good style
151+
workingDirectory: testPath
151152
}
152153
}
153154

tests/ExecutableRunner.spec.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,70 @@ describe("ExecutableRunner should", () => {
164164
runner.KillProcess();
165165
verify(mockedExecuter.KillProcess()).called();
166166
})
167+
168+
describe("workingDirectory option", () => {
169+
it("should use provided workingDirectory option", async () => {
170+
const log = mock<Log>();
171+
const mockedExecuter = mock<ProcessExecuter>();
172+
const customWorkingDir = "/custom/working/directory";
173+
let capturedOptions: any;
174+
175+
when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything()))
176+
.thenCall((cmd: string, args: any, options: any, callback: Function) => {
177+
capturedOptions = options;
178+
callback(undefined, "output", "");
179+
});
180+
181+
const executer = instance(mockedExecuter);
182+
const runner = new ExecutableRunner(executer, "/path/to/test.exe", log,
183+
{ workingDirectory: customWorkingDir });
184+
185+
await runner.GetTestList(TestLocationFetchMode.Disabled);
186+
187+
expect(capturedOptions.cwd).to.equal(customWorkingDir);
188+
});
189+
190+
it("should use executable directory when workingDirectory not provided", async () => {
191+
const log = mock<Log>();
192+
const mockedExecuter = mock<ProcessExecuter>();
193+
const executablePath = "/path/to/test.exe";
194+
let capturedOptions: any;
195+
196+
when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything()))
197+
.thenCall((cmd: string, args: any, options: any, callback: Function) => {
198+
capturedOptions = options;
199+
callback(undefined, "output", "");
200+
});
201+
202+
const executer = instance(mockedExecuter);
203+
const runner = new ExecutableRunner(executer, executablePath, log);
204+
205+
await runner.GetTestList(TestLocationFetchMode.Disabled);
206+
207+
expect(capturedOptions.cwd).to.equal("/path/to");
208+
});
209+
210+
it("should use workingDirectory for test execution", async () => {
211+
const log = mock<Log>();
212+
const mockedExecuter = mock<ProcessExecuter>();
213+
const customWorkingDir = "/custom/test/directory";
214+
let capturedOptions: any;
215+
216+
when(mockedExecuter.ExecFile(anything(), anything(), anything(), anything()))
217+
.thenCall((cmd: string, args: any, options: any, callback: Function) => {
218+
capturedOptions = options;
219+
callback(undefined, ".", "");
220+
});
221+
222+
const executer = instance(mockedExecuter);
223+
const runner = new ExecutableRunner(executer, "/path/to/test.exe", log,
224+
{ workingDirectory: customWorkingDir });
225+
226+
await runner.RunTest("TestGroup", "TestName");
227+
228+
expect(capturedOptions.cwd).to.equal(customWorkingDir);
229+
});
230+
});
167231
})
168232

169233
function setupMockCalls(error: ExecException | undefined, returnValue: string, errorValue: string) {

tests/SettingsProvider.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,37 @@ describe("SettingsProvider should", () => {
135135
const actualPath = settingsProvider.GetTestRunners()
136136
expect(actualPath).to.have.members(expectedPath);
137137
})
138+
139+
describe("testExecutablePath variable resolution", () => {
140+
it("should resolve ${workspaceFolder} in testExecutablePath", () => {
141+
const logger = mock<Log>();
142+
config.testExecutablePath = "${workspaceFolder}/test";
143+
const expectedPath = "myFolder/test";
144+
145+
const settingsProvider = new TestSettingsProvider(logger, config, filesToFind);
146+
147+
const actualPath = settingsProvider.GetTestPath();
148+
expect(actualPath).to.equal(expectedPath);
149+
});
150+
151+
it("should return empty string when testExecutablePath is empty", () => {
152+
const logger = mock<Log>();
153+
config.testExecutablePath = "";
154+
155+
const settingsProvider = new TestSettingsProvider(logger, config, filesToFind);
156+
157+
const actualPath = settingsProvider.GetTestPath();
158+
expect(actualPath).to.equal("");
159+
});
160+
161+
it("should return string as-is when no variables present", () => {
162+
const logger = mock<Log>();
163+
config.testExecutablePath = "/absolute/path/test";
164+
165+
const settingsProvider = new TestSettingsProvider(logger, config, filesToFind);
166+
167+
const actualPath = settingsProvider.GetTestPath();
168+
expect(actualPath).to.equal("/absolute/path/test");
169+
});
170+
});
138171
});

0 commit comments

Comments
 (0)