|
1 | | -describe("loadSharedConfigFiles", () => { |
2 | | - let ENV_CONFIG_PATH; |
3 | | - let ENV_CREDENTIALS_PATH; |
4 | | - let loadSharedConfigFiles; |
| 1 | +import { join } from "path"; |
| 2 | + |
| 3 | +import { getHomeDir } from "./getHomeDir"; |
| 4 | +import { ENV_CONFIG_PATH, ENV_CREDENTIALS_PATH, loadSharedConfigFiles } from "./loadSharedConfigFiles"; |
| 5 | +import { normalizeConfigFile } from "./normalizeConfigFile"; |
| 6 | +import { parseIni } from "./parseIni"; |
| 7 | +import { slurpFile } from "./slurpFile"; |
| 8 | + |
| 9 | +jest.mock("path"); |
| 10 | +jest.mock("./getHomeDir"); |
| 11 | +jest.mock("./normalizeConfigFile"); |
| 12 | +jest.mock("./parseIni"); |
| 13 | +jest.mock("./slurpFile"); |
5 | 14 |
|
| 15 | +describe("loadSharedConfigFiles", () => { |
6 | 16 | const mockSeparator = "/"; |
7 | 17 | const mockHomeDir = "/mock/home/dir"; |
| 18 | + |
8 | 19 | const mockConfigFilepath = "/mock/file/path/config"; |
9 | 20 | const mockCredsFilepath = "/mock/file/path/credentials"; |
| 21 | + const mockCustomSharedConfigFiles = { |
| 22 | + configFile: mockConfigFilepath, |
| 23 | + credentialsFile: mockCredsFilepath, |
| 24 | + }; |
| 25 | + |
| 26 | + const defaultConfigFilepath = `${mockHomeDir}/.aws/config`; |
| 27 | + const defaultCredsFilepath = `${mockHomeDir}/.aws/credentials`; |
| 28 | + const mockDefaultSharedConfigFiles = { |
| 29 | + configFile: defaultConfigFilepath, |
| 30 | + credentialsFile: defaultCredsFilepath, |
| 31 | + }; |
10 | 32 |
|
11 | 33 | beforeEach(() => { |
12 | | - jest.mock("./getHomeDir", () => ({ |
13 | | - getHomeDir: jest.fn().mockReturnValue(mockHomeDir), |
14 | | - })); |
15 | | - jest.mock("path", () => ({ |
16 | | - join: jest.fn().mockImplementation((...args) => args.join(mockSeparator)), |
17 | | - })); |
| 34 | + (join as jest.Mock).mockImplementation((...args) => args.join(mockSeparator)); |
| 35 | + (getHomeDir as jest.Mock).mockReturnValue(mockHomeDir); |
| 36 | + (parseIni as jest.Mock).mockImplementation((args) => args); |
| 37 | + (normalizeConfigFile as jest.Mock).mockImplementation((args) => args); |
| 38 | + (slurpFile as jest.Mock).mockImplementation((path) => Promise.resolve(path)); |
18 | 39 | }); |
19 | 40 |
|
20 | 41 | afterEach(() => { |
21 | 42 | jest.clearAllMocks(); |
22 | 43 | jest.resetModules(); |
23 | 44 | }); |
24 | 45 |
|
25 | | - describe("no error is thrown", () => { |
26 | | - let mockReadFile; |
27 | | - const defaultConfigFilepath = `${mockHomeDir}/.aws/config`; |
28 | | - const defaultCredsFilepath = `${mockHomeDir}/.aws/credentials`; |
| 46 | + it("returns configFile and credentialsFile from default locations", async () => { |
| 47 | + const sharedConfigFiles = await loadSharedConfigFiles(); |
| 48 | + expect(sharedConfigFiles).toStrictEqual(mockDefaultSharedConfigFiles); |
| 49 | + }); |
29 | 50 |
|
30 | | - const mockDefaultSharedConfigFiles = { |
31 | | - configFile: { path: defaultConfigFilepath, options: "utf-8" }, |
32 | | - credentialsFile: { path: defaultCredsFilepath, options: "utf-8" }, |
33 | | - }; |
34 | | - const mockCustomSharedConfigFiles = { |
35 | | - configFile: { path: mockConfigFilepath, options: "utf-8" }, |
36 | | - credentialsFile: { path: mockCredsFilepath, options: "utf-8" }, |
| 51 | + it("returns configFile and credentialsFile from locations defined in environment", async () => { |
| 52 | + const OLD_ENV = process.env; |
| 53 | + process.env = { |
| 54 | + ...OLD_ENV, |
| 55 | + [ENV_CONFIG_PATH]: mockConfigFilepath, |
| 56 | + [ENV_CREDENTIALS_PATH]: mockCredsFilepath, |
37 | 57 | }; |
| 58 | + const sharedConfigFiles = await loadSharedConfigFiles({}); |
| 59 | + expect(sharedConfigFiles).toStrictEqual(mockCustomSharedConfigFiles); |
| 60 | + process.env = OLD_ENV; |
| 61 | + }); |
38 | 62 |
|
39 | | - beforeEach(() => { |
40 | | - mockReadFile = jest.fn().mockImplementation((path, options) => Promise.resolve({ path, options })); |
41 | | - jest.mock("fs", () => ({ promises: { readFile: mockReadFile } })); |
42 | | - |
43 | | - jest.mock("./parseIni", () => ({ |
44 | | - parseIni: jest.fn().mockImplementation((args) => args), |
45 | | - })); |
46 | | - |
47 | | - jest.mock("./normalizeConfigFile", () => ({ |
48 | | - normalizeConfigFile: jest.fn().mockImplementation((args) => args), |
49 | | - })); |
50 | | - |
51 | | - // Using require as loadSharedConfigFiles contains local state. |
52 | | - const loadSharedConfigFilesRequire = require("./loadSharedConfigFiles"); |
53 | | - ENV_CONFIG_PATH = loadSharedConfigFilesRequire.ENV_CONFIG_PATH; |
54 | | - ENV_CREDENTIALS_PATH = loadSharedConfigFilesRequire.ENV_CREDENTIALS_PATH; |
55 | | - loadSharedConfigFiles = loadSharedConfigFilesRequire.loadSharedConfigFiles; |
56 | | - }); |
57 | | - |
58 | | - it("returns configFile and credentialsFile from default locations", async () => { |
59 | | - const sharedConfigFiles = await loadSharedConfigFiles(); |
60 | | - expect(sharedConfigFiles).toStrictEqual(mockDefaultSharedConfigFiles); |
61 | | - }); |
62 | | - |
63 | | - it("returns configFile and credentialsFile from locations defined in environment", async () => { |
64 | | - const OLD_ENV = process.env; |
65 | | - process.env = { |
66 | | - ...OLD_ENV, |
67 | | - [ENV_CONFIG_PATH]: mockConfigFilepath, |
68 | | - [ENV_CREDENTIALS_PATH]: mockCredsFilepath, |
69 | | - }; |
70 | | - const sharedConfigFiles = await loadSharedConfigFiles({}); |
71 | | - expect(sharedConfigFiles).toStrictEqual(mockCustomSharedConfigFiles); |
72 | | - process.env = OLD_ENV; |
73 | | - }); |
74 | | - |
75 | | - it("returns configFile and credentialsFile from init if defined", async () => { |
76 | | - const sharedConfigFiles = await loadSharedConfigFiles({ |
77 | | - filepath: mockCredsFilepath, |
78 | | - configFilepath: mockConfigFilepath, |
79 | | - }); |
80 | | - expect(sharedConfigFiles).toStrictEqual(mockCustomSharedConfigFiles); |
81 | | - }); |
82 | | - |
83 | | - it("makes just two readFile calls with multiple parallel or series calls for same filepaths", async () => { |
84 | | - const sharedConfigFilesArr = await Promise.all([loadSharedConfigFiles(), loadSharedConfigFiles()]); |
85 | | - expect(sharedConfigFilesArr).toEqual([mockDefaultSharedConfigFiles, mockDefaultSharedConfigFiles]); |
86 | | - |
87 | | - // There are two readFile calls even through loadSharedConfigFiles is called in parallel twice. |
88 | | - expect(mockReadFile).toHaveBeenCalledTimes(2); |
89 | | - expect(mockReadFile).toHaveBeenNthCalledWith(1, defaultConfigFilepath, "utf-8"); |
90 | | - expect(mockReadFile).toHaveBeenNthCalledWith(2, defaultCredsFilepath, "utf-8"); |
91 | | - |
92 | | - const sharedConfigFiles = await loadSharedConfigFiles(); |
93 | | - expect(sharedConfigFiles).toStrictEqual(mockDefaultSharedConfigFiles); |
94 | | - // There are two readFile calls even through loadSharedConfigFiles is called for the third time. |
95 | | - expect(mockReadFile).toHaveBeenCalledTimes(2); |
96 | | - }); |
97 | | - |
98 | | - describe("makes readFile calls based on filepaths", () => { |
99 | | - afterEach(() => { |
100 | | - expect(mockReadFile).toHaveBeenCalledTimes(4); |
101 | | - expect(mockReadFile).toHaveBeenNthCalledWith(1, defaultConfigFilepath, "utf-8"); |
102 | | - expect(mockReadFile).toHaveBeenNthCalledWith(2, defaultCredsFilepath, "utf-8"); |
103 | | - expect(mockReadFile).toHaveBeenNthCalledWith(3, mockConfigFilepath, "utf-8"); |
104 | | - expect(mockReadFile).toHaveBeenNthCalledWith(4, mockCredsFilepath, "utf-8"); |
105 | | - }); |
106 | | - |
107 | | - it("in parallel", async () => { |
108 | | - const sharedConfigFilesArr = await Promise.all([ |
109 | | - loadSharedConfigFiles(), |
110 | | - loadSharedConfigFiles({ |
111 | | - filepath: mockCredsFilepath, |
112 | | - configFilepath: mockConfigFilepath, |
113 | | - }), |
114 | | - ]); |
115 | | - expect(sharedConfigFilesArr).toEqual([mockDefaultSharedConfigFiles, mockCustomSharedConfigFiles]); |
116 | | - }); |
117 | | - |
118 | | - it("in series", async () => { |
119 | | - const sharedConfigFilesDefault = await loadSharedConfigFiles(); |
120 | | - expect(sharedConfigFilesDefault).toStrictEqual(mockDefaultSharedConfigFiles); |
121 | | - const sharedConfigFilesCustom = await loadSharedConfigFiles({ |
122 | | - filepath: mockCredsFilepath, |
123 | | - configFilepath: mockConfigFilepath, |
124 | | - }); |
125 | | - expect(sharedConfigFilesCustom).toStrictEqual(mockCustomSharedConfigFiles); |
126 | | - }); |
| 63 | + it("returns configFile and credentialsFile from init if defined", async () => { |
| 64 | + const sharedConfigFiles = await loadSharedConfigFiles({ |
| 65 | + filepath: mockCredsFilepath, |
| 66 | + configFilepath: mockConfigFilepath, |
127 | 67 | }); |
| 68 | + expect(sharedConfigFiles).toStrictEqual(mockCustomSharedConfigFiles); |
128 | 69 | }); |
129 | 70 |
|
130 | 71 | describe("swallows error and returns empty configuration", () => { |
131 | 72 | it("when readFile throws error", async () => { |
132 | | - jest.mock("fs", () => ({ |
133 | | - promises: { |
134 | | - readFile: jest.fn().mockRejectedValue("error"), |
135 | | - }, |
136 | | - })); |
137 | | - // Using require as loadSharedConfigFiles contains local state. |
138 | | - const loadSharedConfigFilesRequire = require("./loadSharedConfigFiles"); |
139 | | - loadSharedConfigFiles = loadSharedConfigFilesRequire.loadSharedConfigFiles; |
140 | | - |
| 73 | + (slurpFile as jest.Mock).mockRejectedValue("error"); |
141 | 74 | const sharedConfigFiles = await loadSharedConfigFiles(); |
142 | 75 | expect(sharedConfigFiles).toStrictEqual({ configFile: {}, credentialsFile: {} }); |
143 | 76 | }); |
144 | 77 |
|
145 | 78 | it("when parseIni throws error", async () => { |
146 | | - jest.mock("fs", () => ({ |
147 | | - promises: { |
148 | | - readFile: jest.fn().mockImplementation((path, options) => Promise.resolve({ path, options })), |
149 | | - }, |
150 | | - })); |
151 | | - |
152 | | - jest.mock("./parseIni", () => ({ |
153 | | - parseIni: jest.fn().mockRejectedValue("error"), |
154 | | - })); |
155 | | - |
156 | | - // Using require as loadSharedConfigFiles contains local state. |
157 | | - const loadSharedConfigFilesRequire = require("./loadSharedConfigFiles"); |
158 | | - loadSharedConfigFiles = loadSharedConfigFilesRequire.loadSharedConfigFiles; |
159 | | - |
| 79 | + (parseIni as jest.Mock).mockRejectedValue("error"); |
160 | 80 | const sharedConfigFiles = await loadSharedConfigFiles(); |
161 | 81 | expect(sharedConfigFiles).toStrictEqual({ configFile: {}, credentialsFile: {} }); |
162 | 82 | }); |
163 | 83 |
|
164 | 84 | it("when normalizeConfigFile throws error", async () => { |
165 | | - const defaultCredsFilepath = `${mockHomeDir}/.aws/credentials`; |
166 | | - jest.mock("fs", () => ({ |
167 | | - promises: { |
168 | | - readFile: jest.fn().mockImplementation((path, options) => Promise.resolve({ path, options })), |
169 | | - }, |
170 | | - })); |
171 | | - |
172 | | - jest.mock("./parseIni", () => ({ |
173 | | - parseIni: jest.fn().mockImplementation((args) => args), |
174 | | - })); |
175 | | - |
176 | | - jest.mock("./normalizeConfigFile", () => ({ |
177 | | - normalizeConfigFile: jest.fn().mockRejectedValue("error"), |
178 | | - })); |
179 | | - |
180 | | - // Using require as loadSharedConfigFiles contains local state. |
181 | | - const loadSharedConfigFilesRequire = require("./loadSharedConfigFiles"); |
182 | | - loadSharedConfigFiles = loadSharedConfigFilesRequire.loadSharedConfigFiles; |
183 | | - |
| 85 | + (normalizeConfigFile as jest.Mock).mockRejectedValue("error"); |
184 | 86 | const sharedConfigFiles = await loadSharedConfigFiles(); |
185 | 87 | expect(sharedConfigFiles).toStrictEqual({ |
186 | 88 | configFile: {}, |
187 | | - credentialsFile: { path: defaultCredsFilepath, options: "utf-8" }, |
| 89 | + credentialsFile: defaultCredsFilepath, |
188 | 90 | }); |
189 | 91 | }); |
190 | 92 | }); |
|
0 commit comments