-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e80903b
commit 06b3755
Showing
7 changed files
with
220 additions
and
8 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,23 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { getNoteContent } from './create-note.usecases'; | ||
|
||
describe('create-note usecases', () => { | ||
describe('getNoteContent', () => { | ||
test('by default, returns the raw content', async () => { | ||
expect(await getNoteContent({ rawContent: 'content' })).toEqual('content'); | ||
}); | ||
|
||
test('when the user wants to read from stdin, reads from stdin, regardless of the content', async () => { | ||
const readFromStdin = async () => 'stdin content'; | ||
expect(await getNoteContent({ rawContent: 'content', shouldReadFromStdin: true, readFromStdin })).toEqual('stdin content'); | ||
}); | ||
|
||
// Citty, the cli builder does not support single dash as positional argument, so this feature is not supported | ||
// test('to follow the Unix convention, reads from stdin when the raw content is "-"', async () => { | ||
// const readFromStdin = async () => 'stdin content'; | ||
// expect(await getNoteContent({ rawContent: '-', readFromStdin })).toEqual('stdin content'); | ||
// expect(await getNoteContent({ rawContent: '-', readFromStdin, shouldReadFromStdin: true })).toEqual('stdin content'); | ||
// expect(await getNoteContent({ rawContent: '-', readFromStdin, shouldReadFromStdin: false })).toEqual('stdin content'); | ||
// }); | ||
}); | ||
}); |
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,18 @@ | ||
import { readFromStdin as readFromStdinImpl } from '../shared/cli.models'; | ||
|
||
export async function getNoteContent({ | ||
rawContent, | ||
shouldReadFromStdin, | ||
readFromStdin = readFromStdinImpl, | ||
}: { | ||
rawContent: string | undefined; | ||
shouldReadFromStdin?: boolean; | ||
readFromStdin?: () => Promise<string>; | ||
}) { | ||
if (shouldReadFromStdin) { | ||
const content = await readFromStdin(); | ||
return content; | ||
} | ||
|
||
return rawContent; | ||
} |
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,72 @@ | ||
import fs from 'node:fs/promises'; | ||
import os from 'node:os'; | ||
import path from 'node:path'; | ||
import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'vitest'; | ||
import { checkFileExist, checkFilesExist } from './files.services'; | ||
|
||
async function createTempDir() { | ||
const ostmpdir = os.tmpdir(); | ||
const tmpdir = path.join(ostmpdir, 'unit-test-'); | ||
return await fs.mkdtemp(tmpdir); | ||
} | ||
|
||
describe('files services', () => { | ||
let tmpDir: string = ''; | ||
let fileAPath: string = ''; | ||
let fileBPath: string = ''; | ||
let nonExistentFilePath: string = ''; | ||
|
||
beforeAll(async () => { | ||
tmpDir = await createTempDir(); | ||
fileAPath = path.join(tmpDir, 'file-a.txt'); | ||
fileBPath = path.join(tmpDir, 'file-b.txt'); | ||
nonExistentFilePath = path.join(tmpDir, 'non-existent-file.txt'); | ||
|
||
await fs.writeFile(fileAPath, 'file a content'); | ||
await fs.writeFile(fileBPath, 'file b content'); | ||
}); | ||
|
||
afterAll(async () => { | ||
await fs.rm(tmpDir, { recursive: true }); | ||
}); | ||
|
||
describe('checkFileExist', () => { | ||
test('checks if a file exists', async () => { | ||
expect(await checkFileExist({ filePath: fileAPath })).to.eql(true); | ||
expect(await checkFileExist({ filePath: nonExistentFilePath })).to.eql(false); | ||
}); | ||
|
||
test('a directory is not a file', async () => { | ||
expect(await checkFileExist({ filePath: tmpDir })).to.eql(false); | ||
}); | ||
}); | ||
|
||
describe('checkFilesExist', () => { | ||
test('ensures all provided files exist', async () => { | ||
expect( | ||
await checkFilesExist({ filePaths: [fileAPath, fileBPath] }), | ||
).to.eql({ | ||
missingFiles: [], | ||
allFilesExist: true, | ||
}); | ||
}); | ||
|
||
test('the missing files are reported', async () => { | ||
expect( | ||
await checkFilesExist({ filePaths: [fileAPath, fileBPath, nonExistentFilePath, tmpDir] }), | ||
).to.eql({ | ||
missingFiles: [nonExistentFilePath, tmpDir], | ||
allFilesExist: false, | ||
}); | ||
}); | ||
|
||
test('when no files are provided, they are all considered to exist', async () => { | ||
expect( | ||
await checkFilesExist({ filePaths: [] }), | ||
).to.eql({ | ||
missingFiles: [], | ||
allFilesExist: true, | ||
}); | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { fileToNoteAsset } from '@enclosed/lib'; | ||
|
||
export async function checkFileExist({ filePath }: { filePath: string }): Promise<boolean> { | ||
try { | ||
const stats = await fs.stat(filePath); | ||
return stats.isFile(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export async function checkFilesExist({ filePaths }: { filePaths: string[] }) { | ||
const missingFiles = ( | ||
await Promise.all( | ||
filePaths.map(async (filePath) => { | ||
const exists = await checkFileExist({ filePath }); | ||
return exists ? null : filePath; | ||
}), | ||
)).filter(Boolean) as string[]; | ||
|
||
return { missingFiles, allFilesExist: missingFiles.length === 0 }; | ||
} | ||
|
||
export async function buildFileAssets({ filePaths }: { filePaths: string[] }) { | ||
const assets = await Promise.all( | ||
filePaths.map(async (filePath) => { | ||
const content = await fs.readFile(filePath); | ||
const file = new File([content], path.basename(filePath)); | ||
|
||
return fileToNoteAsset({ | ||
file, | ||
}); | ||
}), | ||
); | ||
|
||
return { assets }; | ||
} |
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