Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shared-ini-file-loader): read config files from paths relative to homedir #1315

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading