fix: check path is string in pathToFsPath
There's a chance this function can be called with a path that is not a string. To catch that, we check if path is of a different type and throw an error if it is. This also adds a couple tests for this function.
This commit is contained in:
@ -453,3 +453,44 @@ describe("escapeHtml", () => {
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("pathToFsPath", () => {
|
||||
it("should convert a path to a file system path", () => {
|
||||
expect(util.pathToFsPath("/foo/bar/baz")).toBe("/foo/bar/baz")
|
||||
})
|
||||
it("should lowercase drive letter casing by default", () => {
|
||||
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:/far/boo")
|
||||
})
|
||||
it("should keep drive letter casing when set to true", () => {
|
||||
expect(util.pathToFsPath("/C:/far/bo", true)).toBe("C:/far/bo")
|
||||
})
|
||||
it("should throw an error if a non-string is passed in for path", () => {
|
||||
expect(() =>
|
||||
util
|
||||
// @ts-expect-error We need to check other types
|
||||
.pathToFsPath({}),
|
||||
).toThrow(
|
||||
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type undefined.`,
|
||||
)
|
||||
})
|
||||
it("should not throw an error for a string array", () => {
|
||||
// @ts-expect-error We need to check other types
|
||||
expect(() => util.pathToFsPath(["/hello/foo", "/hello/bar"]).not.toThrow())
|
||||
})
|
||||
it("should use the first string in a string array", () => {
|
||||
expect(util.pathToFsPath(["/hello/foo", "/hello/bar"])).toBe("/hello/foo")
|
||||
})
|
||||
it("should replace / with \\ on Windows", () => {
|
||||
let ORIGINAL_PLATFORM = process.platform
|
||||
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: "win32",
|
||||
})
|
||||
|
||||
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:\\far\\boo")
|
||||
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: ORIGINAL_PLATFORM,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user