Skip to content

Commit

Permalink
Merge branch 'v-next' of github.com:NomicFoundation/hardhat into esmi…
Browse files Browse the repository at this point in the history
…fy-compilation-against-vnext
  • Loading branch information
ChristopherDedominici committed May 13, 2024
2 parents 721a8d8 + 7433ddf commit 5f6bec8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions v-next/hardhat-utils/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export async function isDirectory(absolutePath: string): Promise<boolean> {
* @returns The parsed JSON object.
* @throws FileNotFoundError if the file doesn't exist.
* @throws InvalidFileFormatError if the file is not a valid JSON file.
* @throws IsDirectoryError if the path is a directory instead of a file.
* @throws FileSystemAccessError for any other error.
*/
export async function readJsonFile<T>(absolutePathToFile: string): Promise<T> {
Expand Down Expand Up @@ -177,6 +178,7 @@ export async function writeJsonFile<T>(
* @param absolutePathToFile The path to the file.
* @returns The content of the file as a string.
* @throws FileNotFoundError if the file doesn't exist.
* @throws IsDirectoryError if the path is a directory instead of a file.
* @throws FileSystemAccessError for any other error.
*/
export async function readUtf8File(
Expand All @@ -186,10 +188,15 @@ export async function readUtf8File(
return await fsPromises.readFile(absolutePathToFile, { encoding: "utf8" });
} catch (e) {
ensureError<NodeJS.ErrnoException>(e);

if (e.code === "ENOENT") {
throw new FileNotFoundError(absolutePathToFile, e);
}

if (e.code === "EISDIR") {
throw new IsDirectoryError(absolutePathToFile, e);
}

throw new FileSystemAccessError(e.message, e);
}
}
Expand Down
20 changes: 20 additions & 0 deletions v-next/hardhat-utils/test/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,16 @@ describe("File system utils", () => {
expectTypeOf(await readUtf8File(filePath)).toMatchTypeOf<string>();
});

it("Should throw IsDirectoryError if the path is a dir and not a file", async () => {
const dirPath = path.join(getTmpDir(), "dir-name");
await mkdir(dirPath);

await assert.rejects(readUtf8File(dirPath), {
name: "IsDirectoryError",
message: `Path ${dirPath} is a directory`,
});
});

it("Should throw FileNotFoundError if the file doesn't exist", async () => {
const filePath = path.join(getTmpDir(), "not-exists.txt");

Expand All @@ -478,6 +488,16 @@ describe("File system utils", () => {
});
});

it("Should throw IsDirectoryError if the path is a dir and not a file", async () => {
const dirPath = path.join(getTmpDir(), "dir-name");
await mkdir(dirPath);

await assert.rejects(readUtf8File(dirPath), {
name: "IsDirectoryError",
message: `Path ${dirPath} is a directory`,
});
});

it("Should throw FileSystemAccessError if a different error is thrown", async () => {
const invalidPath = "\0";

Expand Down

0 comments on commit 5f6bec8

Please sign in to comment.