@@ -34,6 +34,7 @@ import { SettingsService } from "../lib/common/test/unit-tests/stubs";
3434import StaticConfigLib = require( "../lib/config" ) ;
3535import * as path from "path" ;
3636import * as temp from "temp" ;
37+ import { PLUGINS_BUILD_DATA_FILENAME } from '../lib/constants' ;
3738temp . track ( ) ;
3839
3940let isErrorThrown = false ;
@@ -119,6 +120,10 @@ function createTestInjector() {
119120 testInjector . register ( "androidResourcesMigrationService" , stubs . AndroidResourcesMigrationServiceStub ) ;
120121
121122 testInjector . register ( "platformEnvironmentRequirements" , { } ) ;
123+ testInjector . register ( "filesHashService" , {
124+ hasChangesInShasums : ( oldPluginNativeHashes : IStringDictionary , currentPluginNativeHashes : IStringDictionary ) => true ,
125+ generateHashes : async ( files : string [ ] ) : Promise < IStringDictionary > => ( { } )
126+ } ) ;
122127 return testInjector ;
123128}
124129
@@ -541,4 +546,90 @@ describe("Plugins service", () => {
541546 await pluginsService . prepare ( pluginJsonData , "android" , projectData , { } ) ;
542547 } ) ;
543548 } ) ;
549+
550+ describe ( "preparePluginNativeCode" , ( ) => {
551+ const setupTest = ( opts : { hasChangesInShasums ?: boolean , newPluginHashes ?: IStringDictionary , buildDataFileExists ?: boolean , hasPluginPlatformsDir ?: boolean } ) : any => {
552+ const testData : any = {
553+ pluginsService : null ,
554+ isPreparePluginNativeCodeCalled : false ,
555+ dataPassedToWriteJson : null
556+ } ;
557+
558+ const unitTestsInjector = new Yok ( ) ;
559+ unitTestsInjector . register ( "platformsData" , {
560+ getPlatformData : ( platform : string , projectData : IProjectData ) => ( {
561+ projectRoot : "projectRoot" ,
562+ platformProjectService : {
563+ preparePluginNativeCode : async ( pluginData : IPluginData , projData : IProjectData ) => {
564+ testData . isPreparePluginNativeCodeCalled = true ;
565+ }
566+ }
567+ } )
568+ } ) ;
569+
570+ const pluginHashes = opts . newPluginHashes || { "file1" : "hash1" } ;
571+ const pluginData : IPluginData = < any > {
572+ fullPath : "plugin_full_path" ,
573+ name : "plugin_name"
574+ } ;
575+
576+ unitTestsInjector . register ( "filesHashService" , {
577+ hasChangesInShasums : ( oldPluginNativeHashes : IStringDictionary , currentPluginNativeHashes : IStringDictionary ) => ! ! opts . hasChangesInShasums ,
578+ generateHashes : async ( files : string [ ] ) : Promise < IStringDictionary > => pluginHashes
579+ } ) ;
580+
581+ unitTestsInjector . register ( "fs" , {
582+ exists : ( file : string ) => {
583+ if ( file . indexOf ( PLUGINS_BUILD_DATA_FILENAME ) !== - 1 ) {
584+ return ! ! opts . buildDataFileExists ;
585+ }
586+
587+ if ( file . indexOf ( "platforms" ) !== - 1 ) {
588+ return ! ! opts . hasPluginPlatformsDir ;
589+ }
590+
591+ return true ;
592+ } ,
593+ readJson : ( file : string ) => ( {
594+ [ pluginData . name ] : pluginHashes
595+ } ) ,
596+ writeJson : ( file : string , json : any ) => { testData . dataPassedToWriteJson = json ; } ,
597+ enumerateFilesInDirectorySync : ( ) : string [ ] => [ "some_file" ]
598+ } ) ;
599+
600+ unitTestsInjector . register ( "npm" , { } ) ;
601+ unitTestsInjector . register ( "options" , { } ) ;
602+ unitTestsInjector . register ( "logger" , { } ) ;
603+ unitTestsInjector . register ( "errors" , { } ) ;
604+ unitTestsInjector . register ( "injector" , unitTestsInjector ) ;
605+
606+ const pluginsService : PluginsService = unitTestsInjector . resolve ( PluginsService ) ;
607+ testData . pluginsService = pluginsService ;
608+ testData . pluginData = pluginData ;
609+ return testData ;
610+ } ;
611+
612+ const platform = "platform" ;
613+ const projectData : IProjectData = < any > { } ;
614+
615+ it ( "does not prepare the files when plugin does not have platforms dir" , async ( ) => {
616+ const testData = setupTest ( { hasPluginPlatformsDir : false } ) ;
617+ await testData . pluginsService . preparePluginNativeCode ( testData . pluginData , platform , projectData ) ;
618+ assert . isFalse ( testData . isPreparePluginNativeCodeCalled ) ;
619+ } ) ;
620+
621+ it ( "prepares the files when plugin has platforms dir and has not been built before" , async ( ) => {
622+ const newPluginHashes = { "file" : "hash" } ;
623+ const testData = setupTest ( { newPluginHashes, hasPluginPlatformsDir : true } ) ;
624+ await testData . pluginsService . preparePluginNativeCode ( testData . pluginData , platform , projectData ) ;
625+ assert . isTrue ( testData . isPreparePluginNativeCodeCalled ) ;
626+ assert . deepEqual ( testData . dataPassedToWriteJson , { [ testData . pluginData . name ] : newPluginHashes } ) ;
627+ } ) ;
628+
629+ it ( "does not prepare the files when plugin has platforms dir and has files has not changed since then" , async ( ) => {
630+ const testData = setupTest ( { hasChangesInShasums : false , buildDataFileExists : true , hasPluginPlatformsDir : true } ) ;
631+ await testData . pluginsService . preparePluginNativeCode ( testData . pluginData , platform , projectData ) ;
632+ assert . isFalse ( testData . isPreparePluginNativeCodeCalled ) ;
633+ } ) ;
634+ } ) ;
544635} ) ;
0 commit comments