Skip to content

Commit

Permalink
fix(shared-ini-file-loader): read config files from paths relative to…
Browse files Browse the repository at this point in the history
… homedir
  • Loading branch information
Flackus committed Jul 9, 2024
1 parent 978b724 commit a18ec40
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-beans-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/shared-ini-file-loader": patch
---

read config files from paths relative to homedir
18 changes: 18 additions & 0 deletions packages/shared-ini-file-loader/src/loadSharedConfigFiles.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getConfigData } from "./getConfigData";
import { getConfigFilepath } from "./getConfigFilepath";
import { getCredentialsFilepath } from "./getCredentialsFilepath";
import { getHomeDir } from "./getHomeDir";
import { loadSharedConfigFiles } from "./loadSharedConfigFiles";
import { parseIni } from "./parseIni";
import { slurpFile } from "./slurpFile";
Expand All @@ -10,6 +11,7 @@ jest.mock("./getConfigFilepath");
jest.mock("./getCredentialsFilepath");
jest.mock("./parseIni");
jest.mock("./slurpFile");
jest.mock("./getHomeDir");

describe("loadSharedConfigFiles", () => {
const mockConfigFilepath = "/mock/file/path/config";
Expand All @@ -18,13 +20,15 @@ describe("loadSharedConfigFiles", () => {
configFile: mockConfigFilepath,
credentialsFile: mockCredsFilepath,
};
const mockHomeDir = "/users/alias";

beforeEach(() => {
(getConfigFilepath as jest.Mock).mockReturnValue(mockConfigFilepath);
(getCredentialsFilepath as jest.Mock).mockReturnValue(mockCredsFilepath);
(parseIni as jest.Mock).mockImplementation((args) => args);
(getConfigData as jest.Mock).mockImplementation((args) => args);
(slurpFile as jest.Mock).mockImplementation((path) => Promise.resolve(path));
(getHomeDir as jest.Mock).mockReturnValue(mockHomeDir);
});

afterEach(() => {
Expand All @@ -49,6 +53,20 @@ describe("loadSharedConfigFiles", () => {
expect(getCredentialsFilepath).not.toHaveBeenCalled();
});

it("expands homedir in configFile and credentialsFile from init if defined", async () => {
const sharedConfigFiles = await loadSharedConfigFiles({
filepath: "~/path/credentials",
configFilepath: "~/path/config",
});
expect(sharedConfigFiles).toStrictEqual({
configFile: "/users/alias/path/config",
credentialsFile: "/users/alias/path/credentials",
});
expect(getHomeDir).toHaveBeenCalled();
expect(getConfigFilepath).not.toHaveBeenCalled();
expect(getCredentialsFilepath).not.toHaveBeenCalled();
});

describe("swallows error and returns empty configuration", () => {
it("when readFile throws error", async () => {
(slurpFile as jest.Mock).mockRejectedValue("error");
Expand Down
18 changes: 16 additions & 2 deletions packages/shared-ini-file-loader/src/loadSharedConfigFiles.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Logger, SharedConfigFiles } from "@smithy/types";
import { join } from "path";

import { getConfigData } from "./getConfigData";
import { getConfigFilepath } from "./getConfigFilepath";
import { getCredentialsFilepath } from "./getCredentialsFilepath";
import { getHomeDir } from "./getHomeDir";
import { parseIni } from "./parseIni";
import { slurpFile } from "./slurpFile";

Expand Down Expand Up @@ -39,15 +41,27 @@ export const CONFIG_PREFIX_SEPARATOR = ".";

export const loadSharedConfigFiles = async (init: SharedConfigInit = {}): Promise<SharedConfigFiles> => {
const { filepath = getCredentialsFilepath(), configFilepath = getConfigFilepath() } = init;
const homeDir = getHomeDir();
const relativeHomeDirPrefix = "~/";

let resolvedFilepath = filepath;
if (filepath.startsWith(relativeHomeDirPrefix)) {
resolvedFilepath = join(homeDir, filepath.slice(2));
}

let resolvedConfigFilepath = configFilepath;
if (configFilepath.startsWith(relativeHomeDirPrefix)) {
resolvedConfigFilepath = join(homeDir, configFilepath.slice(2));
}

const parsedFiles = await Promise.all([
slurpFile(configFilepath, {
slurpFile(resolvedConfigFilepath, {
ignoreCache: init.ignoreCache,
})
.then(parseIni)
.then(getConfigData)
.catch(swallowError),
slurpFile(filepath, {
slurpFile(resolvedFilepath, {
ignoreCache: init.ignoreCache,
})
.then(parseIni)
Expand Down

0 comments on commit a18ec40

Please sign in to comment.