@@ -11,6 +11,7 @@ import { tildify, untildify } from '../utils/functions.js';
1111import { ArrayStatefulParameter , StatefulParameter } from '../stateful-parameter/stateful-parameter.js' ;
1212import { Plan } from '../plan/plan.js' ;
1313import os from 'node:os' ;
14+ import { z } from 'zod' ;
1415
1516describe ( 'Resource tests' , ( ) => {
1617
@@ -952,4 +953,129 @@ describe('Resource tests', () => {
952953
953954 process . env = oldProcessEnv ;
954955 } )
956+
957+ it ( 'Can import and return all of the imported parameters (zod schema)' , async ( ) => {
958+ const schema = z . object ( {
959+ path : z
960+ . string ( )
961+ . describe (
962+ 'A list of paths to add to the PATH environment variable'
963+ ) ,
964+ paths : z
965+ . array ( z . string ( ) )
966+ . describe (
967+ 'A list of paths to add to the PATH environment variable'
968+ ) ,
969+ prepend : z
970+ . boolean ( )
971+ . describe (
972+ 'Whether to prepend the paths to the PATH environment variable'
973+ ) ,
974+ declarationsOnly : z
975+ . boolean ( )
976+ . describe (
977+ 'Whether to only declare the paths in the PATH environment variable'
978+ ) ,
979+ } )
980+
981+ const resource = new class extends TestResource {
982+ getSettings ( ) : ResourceSettings < any > {
983+ return {
984+ id : 'path' ,
985+ schema,
986+ operatingSystems : [ OS . Darwin ] ,
987+ parameterSettings : {
988+ path : { type : 'directory' } ,
989+ paths : { canModify : true , type : 'array' , itemType : 'directory' } ,
990+ prepend : { default : false , setting : true } ,
991+ declarationsOnly : { default : false , setting : true } ,
992+ } ,
993+ importAndDestroy : {
994+ refreshMapper : ( input , context ) => {
995+ if ( Object . keys ( input ) . length === 0 ) {
996+ return { paths : [ ] , declarationsOnly : true } ;
997+ }
998+
999+ return input ;
1000+ }
1001+ } ,
1002+ allowMultiple : {
1003+ matcher : ( desired , current ) => {
1004+ if ( desired . path ) {
1005+ return desired . path === current . path ;
1006+ }
1007+
1008+ const currentPaths = new Set ( current . paths )
1009+ return desired . paths ?. some ( ( p ) => currentPaths . has ( p ) ) ;
1010+ }
1011+ }
1012+ }
1013+ }
1014+
1015+ async refresh ( parameters : Partial < TestConfig > ) : Promise < Partial < TestConfig > | null > {
1016+ return {
1017+ paths : [
1018+ `${ os . homedir ( ) } /.pyenv/bin` ,
1019+ `${ os . homedir ( ) } /.bun/bin` ,
1020+ `${ os . homedir ( ) } /.deno/bin` ,
1021+ `${ os . homedir ( ) } /.jenv/bin` ,
1022+ `${ os . homedir ( ) } /a/random/path` ,
1023+ `${ os . homedir ( ) } /.nvm/.bin/2` ,
1024+ `${ os . homedir ( ) } /.nvm/.bin/3`
1025+ ]
1026+ }
1027+ }
1028+ }
1029+
1030+ const oldProcessEnv = structuredClone ( process . env ) ;
1031+
1032+ process . env [ 'PYENV_ROOT' ] = `${ os . homedir ( ) } /.pyenv`
1033+ process . env [ 'BUN_INSTALL' ] = `${ os . homedir ( ) } /.bun`
1034+ process . env [ 'DENO_INSTALL' ] = `${ os . homedir ( ) } /.deno`
1035+ process . env [ 'JENV' ] = `${ os . homedir ( ) } /.jenv`
1036+ process . env [ 'NVM_DIR' ] = `${ os . homedir ( ) } /.nvm`
1037+
1038+ const controller = new ResourceController ( resource ) ;
1039+ const importResult1 = await controller . import ( { type : 'path' } , { } ) ;
1040+ expect ( importResult1 ) . toMatchObject ( [
1041+ {
1042+ 'core' : {
1043+ 'type' : 'path'
1044+ } ,
1045+ 'parameters' : {
1046+ 'paths' : [
1047+ '$PYENV_ROOT/bin' ,
1048+ '$BUN_INSTALL/bin' ,
1049+ '$DENO_INSTALL/bin' ,
1050+ '$JENV/bin' ,
1051+ '~/a/random/path' ,
1052+ '$NVM_DIR/.bin/2' ,
1053+ '$NVM_DIR/.bin/3'
1054+ ]
1055+ }
1056+ }
1057+ ] )
1058+
1059+ const importResult2 = await controller . import ( { type : 'path' } , { paths : [ '$PYENV_ROOT/bin' , '$BUN_INSTALL/bin' ] } ) ;
1060+ expect ( importResult2 ) . toMatchObject ( [
1061+ {
1062+ 'core' : {
1063+ 'type' : 'path'
1064+ } ,
1065+ 'parameters' : {
1066+ 'paths' : [
1067+ '$PYENV_ROOT/bin' ,
1068+ '$BUN_INSTALL/bin' ,
1069+ '$DENO_INSTALL/bin' ,
1070+ '$JENV/bin' ,
1071+ '~/a/random/path' ,
1072+ '$NVM_DIR/.bin/2' ,
1073+ '$NVM_DIR/.bin/3'
1074+ ]
1075+ }
1076+ }
1077+ ] )
1078+
1079+ process . env = oldProcessEnv ;
1080+ } )
9551081} ) ;
0 commit comments