-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move filesystem operations to new file (#1482)
## Description This PR moves a helper function to a dedicated file (filename open to discussion). This change was planned as part of #1402, we can bring it in here to reduce the amount of changes we consider at any given time. ## Related Issue Relates to #1397 ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [x] Other (security config, docs update, etc) ## Checklist before merging - [x] Unit, [Journey](https://github.com/defenseunicorns/pepr/tree/main/journey), [E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples), [docs](https://github.com/defenseunicorns/pepr/tree/main/docs), [adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or updated as needed - [x] [Contributor Guide Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request) followed
- Loading branch information
Showing
6 changed files
with
74 additions
and
65 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
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
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,54 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { describe, jest, expect, it } from "@jest/globals"; | ||
import { createDirectoryIfNotExists } from "./filesystemService"; | ||
import { promises as fs } from "fs"; | ||
|
||
jest.mock("fs", () => { | ||
return { | ||
promises: { | ||
access: jest.fn(), | ||
mkdir: jest.fn(), | ||
}, | ||
}; | ||
}); | ||
|
||
describe("createDirectoryIfNotExists function", () => { | ||
it("should create a directory if it doesn't exist", async () => { | ||
(fs.access as jest.Mock).mockRejectedValue({ code: "ENOENT" } as never); | ||
(fs.mkdir as jest.Mock).mockResolvedValue(undefined as never); | ||
|
||
const directoryPath = "/pepr/pepr-test-module/asdf"; | ||
|
||
await createDirectoryIfNotExists(directoryPath); | ||
|
||
expect(fs.access).toHaveBeenCalledWith(directoryPath); | ||
expect(fs.mkdir).toHaveBeenCalledWith(directoryPath, { recursive: true }); | ||
}); | ||
|
||
it("should not create a directory if it already exists", async () => { | ||
jest.resetAllMocks(); | ||
(fs.access as jest.Mock).mockResolvedValue(undefined as never); | ||
|
||
const directoryPath = "/pepr/pepr-test-module/asdf"; | ||
|
||
await createDirectoryIfNotExists(directoryPath); | ||
|
||
expect(fs.access).toHaveBeenCalledWith(directoryPath); | ||
expect(fs.mkdir).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should throw an error for other fs.access errors", async () => { | ||
jest.resetAllMocks(); | ||
(fs.access as jest.Mock).mockRejectedValue({ code: "ERROR" } as never); | ||
|
||
const directoryPath = "/pepr/pepr-test-module/asdf"; | ||
|
||
try { | ||
await createDirectoryIfNotExists(directoryPath); | ||
} catch (error) { | ||
expect(error.code).toEqual("ERROR"); | ||
} | ||
}); | ||
}); |
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,16 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023-Present The Pepr Authors | ||
|
||
import { promises } from "fs"; | ||
|
||
export async function createDirectoryIfNotExists(path: string) { | ||
try { | ||
await promises.access(path); | ||
} catch (error) { | ||
if (error.code === "ENOENT") { | ||
await promises.mkdir(path, { recursive: true }); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} |
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
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