Skip to content

Commit 92d00c6

Browse files
committed
refactor getCommand logical and add test
1 parent 66d4484 commit 92d00c6

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

src/cli.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,23 @@ export class CLI {
119119
return `${v}\n${cm}\n\n${plugins}`;
120120
}
121121

122-
private static getCommand(command: string): string {
122+
public static getCommandCandidates(command: string): string[] {
123123
let validExecExt = [""];
124-
let options = CLI.getDefaultSpawnOptions();
125124
if (platform() === 'win32') {
126125
validExecExt.push(".bat", ".exe", ".cmd");
127126
}
128-
for (const ext of validExecExt) {
129-
let executable = `${command}${ext}`;
130-
if (!spawnSync(executable, [], options).error) return executable;
127+
return validExecExt.map((ext) => `${command}${ext}`);
128+
}
129+
130+
public static checkSpawnable(command: string): boolean {
131+
const result = spawnSync(command, [], CLI.getDefaultSpawnOptions());
132+
return result.status === 0 && !result.error;
133+
}
134+
135+
private static getCommand(command: string): string {
136+
let possiableCommands = this.getCommandCandidates(command);
137+
for (const possiableCommand of possiableCommands) {
138+
if (this.checkSpawnable(possiableCommand)) return possiableCommand;
131139
}
132140
}
133141

test/cli.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as assert from 'assert';
22
import { CLI } from '../src/cli';
3+
import path = require('path');
4+
5+
let testCommandsPath = path.join(__dirname, '..', '..', 'test', 'commands');
36

47
suite('CLI', () => {
58
test('.isPluginInstalled should tell a gauge plugin is installed or not', () => {
@@ -137,4 +140,40 @@ ruby (1.2.0)`;
137140
assert.ok(!cli.isGaugeVersionGreaterOrEqual('1.3.0'));
138141
done();
139142
});
143+
144+
test('.getCommandCandidates choices all valid', (done) => {
145+
let candidates = CLI.getCommandCandidates('test_command');
146+
const originalPath = process.env.PATH;
147+
process.env.PATH = testCommandsPath;
148+
let invalid_candidates = [];
149+
try {
150+
for (const candidate of candidates) {
151+
if (!CLI.checkSpawnable(candidate)) {
152+
invalid_candidates.push(candidate);
153+
}
154+
}
155+
assert.ok(invalid_candidates.length === 0, `invalid candidates: ${invalid_candidates.join(', ')}, those should be valid`);
156+
} finally {
157+
process.env.PATH = originalPath;
158+
}
159+
done();
160+
});
161+
162+
test('.getCommandCandidates choices are all not valid', (done) => {
163+
let candidates = CLI.getCommandCandidates('test_command_not_found');
164+
const originalPath = process.env.PATH;
165+
process.env.PATH = testCommandsPath;
166+
let valid_candidates = [];
167+
try {
168+
for (const candidate of candidates) {
169+
if (CLI.checkSpawnable(candidate)) {
170+
valid_candidates.push(candidate);
171+
}
172+
}
173+
assert.ok(valid_candidates.length === 0, `valid candidates: ${valid_candidates.join(', ')}, those should not be valid`);
174+
} finally {
175+
process.env.PATH = originalPath;
176+
}
177+
done();
178+
});
140179
});

test/commands/test_command

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo success

test/commands/test_command.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo hello

test/commands/test_command.cmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@echo off
2+
echo success

test/commands/test_command.exe

400 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)