11import * as assert from 'assert' ;
22import { CLI , Command } from '../src/cli' ;
33import path = require( 'path' ) ;
4- import { spawnSync } from "child_process" ;
54
65let testCommandsPath = path . join ( __dirname , '..' , '..' , 'test' , 'commands' ) ;
76
@@ -149,12 +148,11 @@ ruby (1.2.0)`;
149148 } ) ;
150149
151150 test ( '.getCommandCandidates choices all valid by .isSpawnable' , ( done ) => {
152- let candidates = CLI . getCommandCandidates ( 'test_command' ) ;
153151 const originalPath = process . env . PATH ;
154152 process . env . PATH = testCommandsPath ;
155153 let invalid_candidates = [ ] ;
156154 try {
157- for ( const candidate of candidates ) {
155+ for ( const candidate of CLI . getCommandCandidates ( 'test_command' ) ) {
158156 if ( ! CLI . isSpawnable ( candidate ) ) {
159157 invalid_candidates . push ( candidate ) ;
160158 }
@@ -166,13 +164,26 @@ ruby (1.2.0)`;
166164 done ( ) ;
167165 } ) ;
168166
169- test ( '.getCommandCandidates choices can be found as in valid via .isSpawnable' , ( done ) => {
170- let candidates = CLI . getCommandCandidates ( 'test_command_not_found' ) ;
167+ test ( '.getCommandCandidates choices all valid by .isSpawnable when requiring an arg' , ( done ) => {
168+ const originalPath = process . env . PATH ;
169+ process . env . PATH = testCommandsPath ;
170+ try {
171+ for ( const candidate of CLI . getCommandCandidates ( 'test_command_needs_version_arg' ) . filter ( c => c . cmdSuffix !== ".exe" ) ) {
172+ assert . ok ( ! CLI . isSpawnable ( candidate ) , `candidate ${ candidate . command } should not be spawnable without --version arg` ) ;
173+ assert . ok ( CLI . isSpawnable ( candidate , [ "--version" ] ) , `candidate ${ candidate . command } should be spawnable with --version arg` ) ;
174+ }
175+ } finally {
176+ process . env . PATH = originalPath ;
177+ }
178+ done ( ) ;
179+ } ) ;
180+
181+ test ( '.getCommandCandidates choices can be found as invalid via .isSpawnable' , ( done ) => {
171182 const originalPath = process . env . PATH ;
172183 process . env . PATH = testCommandsPath ;
173184 let valid_candidates = [ ] ;
174185 try {
175- for ( const candidate of candidates ) {
186+ for ( const candidate of CLI . getCommandCandidates ( 'test_command_not_found' ) ) {
176187 if ( CLI . isSpawnable ( candidate ) ) {
177188 valid_candidates . push ( candidate ) ;
178189 }
@@ -185,11 +196,10 @@ ruby (1.2.0)`;
185196 } ) ;
186197
187198 test ( '.getCommandCandidates can be spawned with an arg' , ( done ) => {
188- let candidates = CLI . getCommandCandidates ( 'test_command' ) ;
189199 const originalPath = process . env . PATH ;
190200 process . env . PATH = testCommandsPath ;
191201 try {
192- for ( const candidate of candidates . filter ( c => ( c . cmdSuffix !== ".exe" ) ) ) {
202+ for ( const candidate of CLI . getCommandCandidates ( 'test_command' ) . filter ( c => c . cmdSuffix !== ".exe" ) ) {
193203 const result = candidate . spawnSync ( [ "Hello World" ] ) ;
194204 assert . ok ( result . status === 0 && ! result . error , `Command candidate ${ candidate . command } failed to spawn` ) ;
195205 assert . equal ( result . stdout . toString ( ) . trim ( ) , 'Success: "Hello World"' , `Command candidate ${ candidate . command } has wrong output` )
0 commit comments