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 {
53
53
startUrl ?: string ;
54
54
}
55
55
56
+ /**
57
+ * @internal
58
+ */
59
+ export const tokenIntercept = { } as Record < string , any > ;
60
+
56
61
/**
57
62
* @internal
58
63
* @param id - can be either a start URL or the SSO session name.
59
64
* Returns the SSO token from the file system.
60
65
*/
61
66
export const getSSOTokenFromFile = async ( id : string ) => {
67
+ if ( tokenIntercept [ id ] ) {
68
+ return tokenIntercept [ id ] ;
69
+ }
62
70
const ssoTokenFilepath = getSSOTokenFilepath ( id ) ;
63
71
const ssoTokenText = await readFile ( ssoTokenFilepath , "utf8" ) ;
64
72
return JSON . parse ( ssoTokenText ) as SSOToken ;
Original file line number Diff line number Diff line change 1
1
export * from "./getHomeDir" ;
2
2
export * from "./getProfileName" ;
3
3
export * from "./getSSOTokenFilepath" ;
4
- export * from "./getSSOTokenFromFile" ;
4
+ export { getSSOTokenFromFile , SSOToken } from "./getSSOTokenFromFile" ;
5
5
export * from "./loadSharedConfigFiles" ;
6
6
export * from "./loadSsoSessionData" ;
7
7
export * from "./parseKnownFiles" ;
8
+ export { externalDataInterceptor } from "./externalDataInterceptor" ;
8
9
export * from "./types" ;
Original file line number Diff line number Diff line change @@ -3,13 +3,17 @@ import { promises as fsPromises } from "fs";
3
3
4
4
const { readFile } = fsPromises ;
5
5
6
- const filePromisesHash : Record < string , Promise < string > > = { } ;
6
+ export const filePromisesHash : Record < string , Promise < string > > = { } ;
7
+ export const fileIntercept : Record < string , Promise < string > > = { } ;
7
8
8
9
interface SlurpFileOptions {
9
10
ignoreCache ?: boolean ;
10
11
}
11
12
12
13
export const slurpFile = ( path : string , options ?: SlurpFileOptions ) => {
14
+ if ( fileIntercept [ path ] !== undefined ) {
15
+ return fileIntercept [ path ] ;
16
+ }
13
17
if ( ! filePromisesHash [ path ] || options ?. ignoreCache ) {
14
18
filePromisesHash [ path ] = readFile ( path , "utf8" ) ;
15
19
}
You can’t perform that action at this time.
0 commit comments