11import { DoctorService } from "../../lib/services/doctor-service" ;
22import { Yok } from "../../lib/common/yok" ;
3- import { LoggerStub } from "../stubs" ;
3+ import { LoggerStub , FileSystemStub } from "../stubs" ;
44import { assert } from "chai" ;
55import * as path from "path" ;
6+ import * as sinon from "sinon" ;
7+ const nativescriptDoctor = require ( "nativescript-doctor" ) ;
68
79class DoctorServiceInheritor extends DoctorService {
810 constructor ( $analyticsService : IAnalyticsService ,
@@ -32,8 +34,15 @@ describe("doctorService", () => {
3234 testInjector . register ( "logger" , LoggerStub ) ;
3335 testInjector . register ( "childProcess" , { } ) ;
3436 testInjector . register ( "projectDataService" , { } ) ;
35- testInjector . register ( "fs" , { } ) ;
36- testInjector . register ( "terminalSpinnerService" , { } ) ;
37+ testInjector . register ( "fs" , FileSystemStub ) ;
38+ testInjector . register ( "terminalSpinnerService" , {
39+ execute : ( spinnerOptions : ITerminalSpinnerOptions , action : ( ) => Promise < any > ) : Promise < any > => action ( ) ,
40+ createSpinner : ( spinnerOptions ?: ITerminalSpinnerOptions ) : ITerminalSpinner => ( < any > {
41+ text : '' ,
42+ succeed : ( ) : any => undefined ,
43+ fail : ( ) : any => undefined
44+ } )
45+ } ) ;
3746 testInjector . register ( "versionsService" , { } ) ;
3847 testInjector . register ( "settingsService" , {
3948 getProfileDir : ( ) : string => ""
@@ -42,6 +51,9 @@ describe("doctorService", () => {
4251 getSettingValue : async ( settingName : string , cacheOpts ?: ICacheTimeoutOpts ) : Promise < any > => undefined ,
4352 saveSetting : async ( key : string , value : any , cacheOpts ?: IUseCacheOpts ) : Promise < void > => undefined
4453 } ) ;
54+ testInjector . register ( "platformEnvironmentRequirements" , {
55+ checkEnvironmentRequirements : async ( input : ICheckEnvironmentRequirementsInput ) : Promise < ICheckEnvironmentRequirementsOutput > => ( < any > { } )
56+ } ) ;
4557
4658 return testInjector ;
4759 } ;
@@ -262,4 +274,136 @@ const Observable = require("tns-core-modules-widgets/data/observable").Observabl
262274 } ) ;
263275 } ) ;
264276 } ) ;
277+
278+ describe ( "printWarnings" , ( ) => {
279+ let sandbox : sinon . SinonSandbox ;
280+
281+ beforeEach ( ( ) => {
282+ sandbox = sinon . sandbox . create ( ) ;
283+ } ) ;
284+
285+ afterEach ( ( ) => {
286+ sandbox . restore ( ) ;
287+ } ) ;
288+ const successGetInfosResult = [ {
289+ message :
290+ 'Your ANDROID_HOME environment variable is set and points to correct directory.' ,
291+ platforms : [ 'Android' ] ,
292+ type : 'info'
293+ } ,
294+ {
295+ message : 'Xcode is installed and is configured properly.' ,
296+ platforms : [ 'iOS' ] ,
297+ type : 'info'
298+ } ] ;
299+
300+ const failedGetInfosResult = [ {
301+ message :
302+ 'The ANDROID_HOME environment variable is not set or it points to a non-existent directory. You will not be able to perform any build-related operations for Android.' ,
303+ additionalInformation :
304+ 'To be able to perform Android build-related operations, set the `ANDROID_HOME` variable to point to the root of your Android SDK installation directory.' ,
305+ platforms : [ 'Android' ] ,
306+ type : 'warning'
307+ } ,
308+ {
309+ message :
310+ 'WARNING: adb from the Android SDK is not installed or is not configured properly. ' ,
311+ additionalInformation :
312+ 'For Android-related operations, the NativeScript CLI will use a built-in version of adb.\nTo avoid possible issues with the native Android emulator, Genymotion or connected\nAndroid devices, verify that you have installed the latest Android SDK and\nits dependencies as described in http://developer.android.com/sdk/index.html#Requirements' ,
313+ platforms : [ 'Android' ] ,
314+ type : 'warning'
315+ } ,
316+ {
317+ message : 'Xcode is installed and is configured properly.' ,
318+ platforms : [ 'iOS' ] ,
319+ type : 'info'
320+ } ] ;
321+
322+ it ( "prints correct message when no issues are detected" , async ( ) => {
323+ const nsDoctorStub = sandbox . stub ( nativescriptDoctor . doctor , "getInfos" ) ;
324+ nsDoctorStub . returns ( successGetInfosResult ) ;
325+ const testInjector = createTestInjector ( ) ;
326+ const doctorService = testInjector . resolve < IDoctorService > ( "doctorService" ) ;
327+ const logger = testInjector . resolve < LoggerStub > ( "logger" ) ;
328+ await doctorService . printWarnings ( ) ;
329+ assert . isTrue ( logger . output . indexOf ( "No issues were detected." ) !== - 1 ) ;
330+ } ) ;
331+
332+ it ( "prints correct message when issues are detected" , async ( ) => {
333+ const nsDoctorStub = sandbox . stub ( nativescriptDoctor . doctor , "getInfos" ) ;
334+ nsDoctorStub . returns ( failedGetInfosResult ) ;
335+ const testInjector = createTestInjector ( ) ;
336+ const doctorService = testInjector . resolve < IDoctorService > ( "doctorService" ) ;
337+ const logger = testInjector . resolve < LoggerStub > ( "logger" ) ;
338+ await doctorService . printWarnings ( ) ;
339+ assert . isTrue ( logger . output . indexOf ( "There seem to be issues with your configuration." ) !== - 1 ) ;
340+ } ) ;
341+
342+ it ( "returns result from cached file when they exist and the forceCheck is not passed" , async ( ) => {
343+ const nsDoctorStub = sandbox . stub ( nativescriptDoctor . doctor , "getInfos" ) ;
344+ nsDoctorStub . throws ( new Error ( "We should not call nativescript-doctor package when we have results in the file." ) ) ;
345+
346+ const testInjector = createTestInjector ( ) ;
347+ const doctorService = testInjector . resolve < IDoctorService > ( "doctorService" ) ;
348+ const jsonFileSettingsService = testInjector . resolve < IJsonFileSettingsService > ( "jsonFileSettingsService" ) ;
349+ jsonFileSettingsService . getSettingValue = async ( settingName : string , cacheOpts ?: ICacheTimeoutOpts ) : Promise < any > => successGetInfosResult ;
350+ let saveSettingValue : any = null ;
351+ jsonFileSettingsService . saveSetting = async ( key : string , value : any , cacheOpts ?: IUseCacheOpts ) : Promise < void > => saveSettingValue = value ;
352+ const logger = testInjector . resolve < LoggerStub > ( "logger" ) ;
353+ await doctorService . printWarnings ( ) ;
354+ assert . isTrue ( logger . output . indexOf ( "No issues were detected." ) !== - 1 ) ;
355+ assert . deepEqual ( saveSettingValue , successGetInfosResult ) ;
356+ } ) ;
357+
358+ it ( "saves results in cache when there are no warnings" , async ( ) => {
359+ const nsDoctorStub = sandbox . stub ( nativescriptDoctor . doctor , "getInfos" ) ;
360+ nsDoctorStub . returns ( successGetInfosResult ) ;
361+
362+ const testInjector = createTestInjector ( ) ;
363+ const doctorService = testInjector . resolve < IDoctorService > ( "doctorService" ) ;
364+ const jsonFileSettingsService = testInjector . resolve < IJsonFileSettingsService > ( "jsonFileSettingsService" ) ;
365+ let saveSettingValue : any = null ;
366+ jsonFileSettingsService . saveSetting = async ( key : string , value : any , cacheOpts ?: IUseCacheOpts ) : Promise < void > => saveSettingValue = value ;
367+ const logger = testInjector . resolve < LoggerStub > ( "logger" ) ;
368+ await doctorService . printWarnings ( ) ;
369+ assert . isTrue ( logger . output . indexOf ( "No issues were detected." ) !== - 1 ) ;
370+ assert . deepEqual ( saveSettingValue , successGetInfosResult ) ;
371+ } ) ;
372+
373+ it ( "returns result from nativescript-doctor and saves them in cache when the forceCheck is passed" , async ( ) => {
374+ const nsDoctorStub = sandbox . stub ( nativescriptDoctor . doctor , "getInfos" ) ;
375+ nsDoctorStub . returns ( successGetInfosResult ) ;
376+
377+ const testInjector = createTestInjector ( ) ;
378+ const doctorService = testInjector . resolve < IDoctorService > ( "doctorService" ) ;
379+ const jsonFileSettingsService = testInjector . resolve < IJsonFileSettingsService > ( "jsonFileSettingsService" ) ;
380+ let saveSettingValue : any = null ;
381+ let isGetSettingValueCalled = false ;
382+ jsonFileSettingsService . getSettingValue = async ( settingName : string , cacheOpts ?: ICacheTimeoutOpts ) : Promise < any > => {
383+ isGetSettingValueCalled = true ;
384+ return null ;
385+ } ;
386+ jsonFileSettingsService . saveSetting = async ( key : string , value : any , cacheOpts ?: IUseCacheOpts ) : Promise < void > => saveSettingValue = value ;
387+ const logger = testInjector . resolve < LoggerStub > ( "logger" ) ;
388+ await doctorService . printWarnings ( { forceCheck : true } ) ;
389+ assert . isTrue ( logger . output . indexOf ( "No issues were detected." ) !== - 1 ) ;
390+ assert . deepEqual ( saveSettingValue , successGetInfosResult ) ;
391+ assert . isTrue ( nsDoctorStub . calledOnce ) ;
392+ assert . isFalse ( isGetSettingValueCalled , "When forceCheck is passed, we should not read the cache file." ) ;
393+ } ) ;
394+
395+ it ( "deletes the cache file when issues are detected" , async ( ) => {
396+ const nsDoctorStub = sandbox . stub ( nativescriptDoctor . doctor , "getInfos" ) ;
397+ nsDoctorStub . returns ( failedGetInfosResult ) ;
398+ const testInjector = createTestInjector ( ) ;
399+ const doctorService = testInjector . resolve < IDoctorService > ( "doctorService" ) ;
400+ const fs = testInjector . resolve < IFileSystem > ( "fs" ) ;
401+ let deletedPath = "" ;
402+ fs . deleteFile = ( filePath : string ) : void => { deletedPath = filePath ; } ;
403+ const logger = testInjector . resolve < LoggerStub > ( "logger" ) ;
404+ await doctorService . printWarnings ( ) ;
405+ assert . isTrue ( logger . output . indexOf ( "There seem to be issues with your configuration." ) !== - 1 ) ;
406+ assert . isTrue ( deletedPath . indexOf ( "doctor-cache.json" ) !== - 1 ) ;
407+ } ) ;
408+ } ) ;
265409} ) ;
0 commit comments