Skip to content

Commit 783c556

Browse files
authored
Merge pull request #40 from scalvert/dynamic-bin-path
2 parents 51407b3 + 696b329 commit 783c556

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/create-bin-tester.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface BinTesterOptions<TProject> {
44
/**
55
* The absolute path to the bin to invoke
66
*/
7-
binPath: string;
7+
binPath: string | (<TProject extends BinTesterProject>(project: TProject) => string);
88
/**
99
* An array of static arguments that will be used every time when running the bin
1010
*/
@@ -113,10 +113,14 @@ export function createBinTester<TProject extends BinTesterProject>(
113113
*/
114114
function runBin(...args: RunBinArgs): execa.ExecaChildProcess<string> {
115115
const mergedRunOptions = parseArgs(args);
116+
const binPath =
117+
typeof mergedOptions.binPath === 'function'
118+
? mergedOptions.binPath(project)
119+
: mergedOptions.binPath;
116120

117121
return execa(
118122
process.execPath,
119-
[mergedOptions.binPath, ...mergedOptions.staticArgs, ...mergedRunOptions.args],
123+
[binPath, ...mergedOptions.staticArgs, ...mergedRunOptions.args],
120124
{
121125
reject: false,
122126
cwd: project.baseDir,

tests/index-test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ describe('createBinTester', () => {
7878
expect(existsSync(project.baseDir)).toEqual(false);
7979
});
8080

81+
test('runBin can run the configured bin script dynamically', async () => {
82+
const { setupProject, teardownProject, runBin } = createBinTester({
83+
binPath: (p) => {
84+
expect(p).toEqual(project);
85+
return fileURLToPath(new URL('fixtures/fake-bin.js', import.meta.url));
86+
},
87+
});
88+
89+
const project = await setupProject();
90+
91+
const result = await runBin();
92+
93+
expect(result.stdout).toMatchInlineSnapshot('"I am a bin who takes args []"');
94+
95+
teardownProject();
96+
97+
expect(existsSync(project.baseDir)).toEqual(false);
98+
});
99+
81100
test('runBin can run the configured bin script with static arguments', async () => {
82101
const { setupProject, teardownProject, runBin } = createBinTester({
83102
binPath: fileURLToPath(new URL('fixtures/fake-bin.js', import.meta.url)),

0 commit comments

Comments
 (0)