Skip to content

add a read() function #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-spies-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"using-temporary-files": minor
---

added a read() function
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ await usingTemporaryFiles(async ({ path, add, addDirectory, remove }) => {
path("."); // full path to the temporary directory
path("file.txt"); // full path to a particular file
await add("file.txt", "content"); // add a file
const text = await read("file.txt" /*, encoding (optional) */); // read the contents of a file
await addDirectory("dir"); // add a directory
await remove("file.txt"); // remove a file
});
Expand Down
10 changes: 9 additions & 1 deletion src/using-temporary-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ function createRemoveFunction(basePath) {
};
}

function createReadFunction(basePath) {
return async function read(filePath, encoding = "utf8") {
const fullPath = nodePath.join(basePath, filePath);

return fs.readFile(fullPath, encoding);
};
}

export async function usingTemporaryFiles(...callbacks) {
const baseDirectory = DEBUG
? nodePath.resolve(process.cwd(), "./")
Expand All @@ -56,7 +64,7 @@ export async function usingTemporaryFiles(...callbacks) {
add: createAddFunction(temporaryDirectory),
remove: createRemoveFunction(temporaryDirectory),
addDirectory: createAddDirectoryFunction(temporaryDirectory),

read: createReadFunction(temporaryDirectory),
path(...relativePaths) {
return nodePath.join(temporaryDirectory, ...relativePaths);
},
Expand Down
11 changes: 11 additions & 0 deletions test/using-temporary-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ describe("usingTemporaryFiles", () => {
expect(timesCallbackCalled).toBe(1);
});

it("read a file", async () => {
let timesCallbackCalled = 0;
await usingTemporaryFiles(async ({ add, read }) => {
timesCallbackCalled++;
await add("file.txt", "Hello, world!");
expect(await read("file.txt")).toBe("Hello, world!");
});

expect(timesCallbackCalled).toBe(1);
});

it("add a directory", async () => {
let timesCallbackCalled = 0;
await usingTemporaryFiles(async ({ path, addDirectory }) => {
Expand Down