Skip to content

Commit 5b216f2

Browse files
committed
test(shared-ini-file-loader): add external file cache mock controls
1 parent 73ba037 commit 5b216f2

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

.changeset/light-kiwis-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/shared-ini-file-loader": minor
3+
---
4+
5+
add mock controls to file loader
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
};

packages/shared-ini-file-loader/src/getSSOTokenFromFile.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/
6166
export 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;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
export * from "./getHomeDir";
22
export * from "./getProfileName";
33
export * from "./getSSOTokenFilepath";
4-
export * from "./getSSOTokenFromFile";
4+
export { getSSOTokenFromFile, SSOToken } from "./getSSOTokenFromFile";
55
export * from "./loadSharedConfigFiles";
66
export * from "./loadSsoSessionData";
77
export * from "./parseKnownFiles";
8+
export { externalDataInterceptor } from "./externalDataInterceptor";
89
export * from "./types";

packages/shared-ini-file-loader/src/slurpFile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { promises as fsPromises } from "fs";
33

44
const { 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

89
interface SlurpFileOptions {
910
ignoreCache?: boolean;
1011
}
1112

1213
export 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
}

0 commit comments

Comments
 (0)