Skip to content

Commit

Permalink
feat(shared-ini-file-loader): refactor loadSharedConfigFiles into mod…
Browse files Browse the repository at this point in the history
…ular components (#3285)
  • Loading branch information
trivikr authored Feb 10, 2022
1 parent e2b7b25 commit 2613f66
Show file tree
Hide file tree
Showing 13 changed files with 520 additions and 683 deletions.
62 changes: 62 additions & 0 deletions packages/shared-ini-file-loader/src/getHomeDir.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { homedir } from "os";
import { sep } from "path";

import { getHomeDir } from "./getHomeDir";

jest.mock("os");

describe(getHomeDir.name, () => {
const mockHOME = "mockHOME";
const mockUSERPROFILE = "mockUSERPROFILE";
const mockHOMEPATH = "mockHOMEPATH";
const mockHOMEDRIVE = "mockHOMEDRIVE";
const mockHomeDir = "mockHomeDir";

const OLD_ENV = process.env;

beforeEach(() => {
(homedir as jest.Mock).mockReturnValue(mockHomeDir);
process.env = {
...OLD_ENV,
HOME: mockHOME,
USERPROFILE: mockUSERPROFILE,
HOMEPATH: mockHOMEPATH,
HOMEDRIVE: mockHOMEDRIVE,
};
});

afterEach(() => {
process.env = OLD_ENV;
jest.clearAllMocks();
jest.resetModules();
});

it("returns value in process.env.HOME first", () => {
expect(getHomeDir()).toEqual(mockHOME);
});

it("returns value in process.env.USERPROFILE second", () => {
process.env = { ...process.env, HOME: undefined };
expect(getHomeDir()).toEqual(mockUSERPROFILE);
});

describe("returns value in HOMEPATH third", () => {
beforeEach(() => {
process.env = { ...process.env, HOME: undefined, USERPROFILE: undefined };
});

it("uses value in process.env.HOMEDRIVE if it's set", () => {
expect(getHomeDir()).toEqual(`${mockHOMEDRIVE}${mockHOMEPATH}`);
});

it("uses default if process.env.HOMEDRIVE is not set", () => {
process.env = { ...process.env, HOMEDRIVE: undefined };
expect(getHomeDir()).toEqual(`C:${sep}${mockHOMEPATH}`);
});
});

it("returns value from homedir fourth", () => {
process.env = { ...process.env, HOME: undefined, USERPROFILE: undefined, HOMEPATH: undefined };
expect(getHomeDir()).toEqual(mockHomeDir);
});
});
17 changes: 17 additions & 0 deletions packages/shared-ini-file-loader/src/getHomeDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { homedir } from "os";
import { sep } from "path";

/**
* Get the HOME directory for the current runtime.
*
* @internal
*/
export const getHomeDir = (): string => {
const { HOME, USERPROFILE, HOMEPATH, HOMEDRIVE = `C:${sep}` } = process.env;

if (HOME) return HOME;
if (USERPROFILE) return USERPROFILE;
if (HOMEPATH) return `${HOMEDRIVE}${HOMEPATH}`;

return homedir();
};
Loading

0 comments on commit 2613f66

Please sign in to comment.