-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared-ini-file-loader): refactor loadSharedConfigFiles into mod…
…ular components (#3285)
- Loading branch information
Showing
13 changed files
with
520 additions
and
683 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
Oops, something went wrong.