-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
4 changed files
with
82 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
import { vi, expect, test } from 'vitest' | ||
import * as file from '../../src/main/file' | ||
import { app } from 'electron' | ||
import path from 'path' | ||
import fs from 'fs' | ||
import os from 'os' | ||
|
||
vi.mock('electron', async() => { | ||
return { | ||
app: { | ||
getPath: () => os.tmpdir() | ||
}, | ||
} | ||
}) | ||
|
||
test('Find program', async () => { | ||
expect(file.findProgram(null, 'sh')).toBe('/bin/sh') | ||
expect(file.findProgram(null, 'sh2')).toBeNull() | ||
}) | ||
|
||
test('Get file contents', async () => { | ||
const contents = file.getFileContents(app, './tests/fixtures/sample.txt') | ||
const text = Buffer.from(contents.contents, 'base64').toString() | ||
expect(contents).toStrictEqual({ | ||
url: 'file://./tests/fixtures/sample.txt', | ||
contents: 'SGVsbG8gZnJvbSBURVhU', | ||
}) | ||
expect(text).toContain('Hello from TEXT') | ||
}) | ||
|
||
test('Delete file', async () => { | ||
const tempFile = path.join(os.tmpdir(), 'vitest') | ||
fs.writeFileSync(tempFile, 'Hello') | ||
expect(fs.existsSync(tempFile)).toBeTruthy() | ||
file.deleteFile(null, tempFile) | ||
expect(fs.existsSync(tempFile)).toBeFalsy() | ||
}) | ||
|
||
test('Write file contents', async () => { | ||
const tempFile = path.join(os.tmpdir(), 'vitest') | ||
const fileURL = file.writeFileContents(app, { | ||
contents: 'SGVsbG8gZnJvbSBURVhU', | ||
properties: { | ||
filename: 'vitest' | ||
} | ||
}) | ||
expect(fileURL).toBe(tempFile) | ||
expect(fs.existsSync(tempFile)).toBeTruthy() | ||
expect(fs.readFileSync(tempFile, 'utf8')).toBe('Hello from TEXT') | ||
file.deleteFile(null, `file://${tempFile}`) | ||
expect(fs.existsSync(tempFile)).toBeFalsy() | ||
}) | ||
|
||
test('Download local file', async () => { | ||
const tempFile = path.join(os.tmpdir(), 'vitest') | ||
const fileURL = await file.downloadFile(app, { | ||
url: `file://./tests/fixtures/sample.txt`, | ||
properties: { | ||
filename: 'vitest', | ||
prompt: false, | ||
} | ||
}) | ||
expect(fileURL).toBe(tempFile) | ||
expect(fs.existsSync(tempFile)).toBeTruthy() | ||
expect(fs.readFileSync(tempFile, 'utf8')).toBe('Hello from TEXT') | ||
file.deleteFile(null, `file://${tempFile}`) | ||
expect(fs.existsSync(tempFile)).toBeFalsy() | ||
}) |
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,11 @@ | ||
|
||
import { expect, test } from 'vitest' | ||
import { mimeTypeToExtension } from '../../src/main/mimetype' | ||
|
||
test('MIME type to extension', async () => { | ||
expect(mimeTypeToExtension('application/vnd.openxmlformats-officedocument.wordprocessingml.document')).toBe('docx') | ||
expect(mimeTypeToExtension('application/vnd.openxmlformats-officedocument.presentationml.presentation')).toBe('pptx') | ||
expect(mimeTypeToExtension('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')).toBe('xlsx') | ||
expect(mimeTypeToExtension('text/plain')).toBe('txt') | ||
expect(mimeTypeToExtension('application/pdf')).toBe('pdf') | ||
}) |
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