generated from JoshuaKGoldberg/create-typescript-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: switch file mode option to executable
- Loading branch information
1 parent
f7f1dac
commit df00504
Showing
13 changed files
with
154 additions
and
51 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,65 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { createWritingFileSystem } from "./createWritingFileSystem.js"; | ||
|
||
const mockMkdir = vi.fn(); | ||
const mockWriteFile = vi.fn(); | ||
|
||
vi.mock("node:fs/promises", () => ({ | ||
get mkdir() { | ||
return mockMkdir; | ||
}, | ||
get writeFile() { | ||
return mockWriteFile; | ||
}, | ||
})); | ||
|
||
const contents = "abc123"; | ||
const directoryPath = "path/to/file"; | ||
const filePath = "path/to/file"; | ||
|
||
describe("createWritingFileSystem", () => { | ||
describe("writeDirectory", () => { | ||
it("writes with recursive: true", async () => { | ||
const system = createWritingFileSystem(); | ||
|
||
await system.writeDirectory(directoryPath); | ||
|
||
expect(mockMkdir).toHaveBeenCalledWith(directoryPath, { | ||
recursive: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("writeFile", () => { | ||
it("writes with mode 0x644 when options does not exist", async () => { | ||
const system = createWritingFileSystem(); | ||
|
||
await system.writeFile(filePath, contents); | ||
|
||
expect(mockWriteFile).toHaveBeenCalledWith(filePath, contents, { | ||
mode: 0x644, | ||
}); | ||
}); | ||
|
||
it("writes with mode 0x644 when options.executable is false", async () => { | ||
const system = createWritingFileSystem(); | ||
|
||
await system.writeFile(filePath, contents, { executable: false }); | ||
|
||
expect(mockWriteFile).toHaveBeenCalledWith(filePath, contents, { | ||
mode: 0x644, | ||
}); | ||
}); | ||
|
||
it("writes with mode 0x755 when options.executable is true", async () => { | ||
const system = createWritingFileSystem(); | ||
|
||
await system.writeFile(filePath, contents, { executable: true }); | ||
|
||
expect(mockWriteFile).toHaveBeenCalledWith(filePath, contents, { | ||
mode: 0x755, | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import * as fs from "node:fs/promises"; | ||
|
||
import { createReadingFileSystem } from "./createReadingFileSystem.js"; | ||
import { CreatedFileOptions } from "./types/files.js"; | ||
|
||
export function createWritingFileSystem() { | ||
return { | ||
...createReadingFileSystem(), | ||
writeDirectory: async (directoryPath: string) => | ||
void (await fs.mkdir(directoryPath, { recursive: true })), | ||
writeFile: async (filePath: string, contents: string) => { | ||
await fs.writeFile(filePath, contents); | ||
writeFile: async ( | ||
filePath: string, | ||
contents: string, | ||
options?: CreatedFileOptions, | ||
) => { | ||
await fs.writeFile(filePath, contents, { | ||
mode: options?.executable ? 0x755 : 0x644, | ||
}); | ||
}, | ||
}; | ||
} |
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,13 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { isModeExecutable } from "./isModeExecutable.js"; | ||
|
||
describe("isModeExecutable", () => { | ||
test.each([ | ||
["0x755", true], | ||
["0x777", true], | ||
["0x644", false], | ||
])("%s is %j", (mode, expected) => { | ||
expect(isModeExecutable(parseInt(mode, 16))).toBe(expected); | ||
}); | ||
}); |
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,3 @@ | ||
export function isModeExecutable(mode: number) { | ||
return (mode & 0o1) !== 0; | ||
} |
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
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
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