File tree Expand file tree Collapse file tree 6 files changed +67
-2
lines changed
packages/shared-ini-file-loader/src Expand file tree Collapse file tree 6 files changed +67
-2
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @smithy/shared-ini-file-loader " : minor
3+ ---
4+
5+ add mock controls to file loader
Original file line number Diff line number Diff line change 1+ import { describe , expect , test as it } from "vitest" ;
2+
3+ import { externalDataInterceptor } from "./externalDataInterceptor" ;
4+ import { getSSOTokenFromFile } from "./getSSOTokenFromFile" ;
5+ import { slurpFile } from "./slurpFile" ;
6+
7+ describe ( "fileMockController" , ( ) => {
8+ it ( "intercepts slurpFile" , async ( ) => {
9+ externalDataInterceptor . interceptFile ( "abcd" , "contents" ) ;
10+
11+ expect ( await slurpFile ( "abcd" ) ) . toEqual ( "contents" ) ;
12+ expect ( externalDataInterceptor . getFileRecord ( ) ) . toEqual ( {
13+ abcd : Promise . resolve ( "contents" ) ,
14+ } ) ;
15+ expect ( await externalDataInterceptor . getFileRecord ( ) . abcd ) . toEqual ( "contents" ) ;
16+ } ) ;
17+
18+ it ( "intercepts getSSOTokenFromFile" , async ( ) => {
19+ externalDataInterceptor . interceptToken ( "TOKEN" , "token-contents" ) ;
20+
21+ expect ( await getSSOTokenFromFile ( "TOKEN" ) ) . toEqual ( "token-contents" ) ;
22+
23+ expect ( externalDataInterceptor . getTokenRecord ( ) ) . toEqual ( {
24+ TOKEN : "token-contents" ,
25+ } ) ;
26+ } ) ;
27+ } ) ;
Original file line number Diff line number Diff line change 1+ import { tokenIntercept } from "./getSSOTokenFromFile" ;
2+ import { fileIntercept } from "./slurpFile" ;
3+
4+ /**
5+ * @internal
6+ */
7+ export const externalDataInterceptor = {
8+ getFileRecord ( ) {
9+ return fileIntercept ;
10+ } ,
11+ interceptFile ( path : string , contents : string ) {
12+ fileIntercept [ path ] = Promise . resolve ( contents ) ;
13+ } ,
14+ getTokenRecord ( ) {
15+ return tokenIntercept ;
16+ } ,
17+ interceptToken ( id : string , contents : any ) {
18+ tokenIntercept [ id ] = contents ;
19+ } ,
20+ } ;
Original file line number Diff line number Diff line change @@ -53,12 +53,20 @@ export interface SSOToken {
5353 startUrl ?: string ;
5454}
5555
56+ /**
57+ * @internal
58+ */
59+ export const tokenIntercept = { } as Record < string , any > ;
60+
5661/**
5762 * @internal
5863 * @param id - can be either a start URL or the SSO session name.
5964 * Returns the SSO token from the file system.
6065 */
6166export const getSSOTokenFromFile = async ( id : string ) => {
67+ if ( tokenIntercept [ id ] ) {
68+ return tokenIntercept [ id ] ;
69+ }
6270 const ssoTokenFilepath = getSSOTokenFilepath ( id ) ;
6371 const ssoTokenText = await readFile ( ssoTokenFilepath , "utf8" ) ;
6472 return JSON . parse ( ssoTokenText ) as SSOToken ;
Original file line number Diff line number Diff line change 11export * from "./getHomeDir" ;
22export * from "./getProfileName" ;
33export * from "./getSSOTokenFilepath" ;
4- export * from "./getSSOTokenFromFile" ;
4+ export { getSSOTokenFromFile , SSOToken } from "./getSSOTokenFromFile" ;
55export * from "./loadSharedConfigFiles" ;
66export * from "./loadSsoSessionData" ;
77export * from "./parseKnownFiles" ;
8+ export { externalDataInterceptor } from "./externalDataInterceptor" ;
89export * from "./types" ;
Original file line number Diff line number Diff line change @@ -3,13 +3,17 @@ import { promises as fsPromises } from "fs";
33
44const { readFile } = fsPromises ;
55
6- const filePromisesHash : Record < string , Promise < string > > = { } ;
6+ export const filePromisesHash : Record < string , Promise < string > > = { } ;
7+ export const fileIntercept : Record < string , Promise < string > > = { } ;
78
89interface SlurpFileOptions {
910 ignoreCache ?: boolean ;
1011}
1112
1213export const slurpFile = ( path : string , options ?: SlurpFileOptions ) => {
14+ if ( fileIntercept [ path ] !== undefined ) {
15+ return fileIntercept [ path ] ;
16+ }
1317 if ( ! filePromisesHash [ path ] || options ?. ignoreCache ) {
1418 filePromisesHash [ path ] = readFile ( path , "utf8" ) ;
1519 }
You can’t perform that action at this time.
0 commit comments