58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import {test, describe, afterEach, beforeEach, expect} from 'bun:test'
|
|
import {type FileResult, fileSync, dirSync, type DirResult} from 'tmp'
|
|
import * as fs from 'node:fs/promises'
|
|
|
|
import * as env from '../src/env'
|
|
|
|
let file: FileResult
|
|
beforeEach(() => (file = fileSync()))
|
|
afterEach(() => file.removeCallback())
|
|
|
|
describe('File IO', () => {
|
|
test('Can read file content', async () => {
|
|
const message = 'coucou'
|
|
await fs.writeFile(file.name, message)
|
|
const res = await env.get_file_content(file.name)
|
|
expect(res).toEqual({ok: true, data: message})
|
|
})
|
|
test("Fails safe if file doesn't exist", async () => {
|
|
const res = await env.get_file_content('tralalero tralala')
|
|
expect(res.ok).not.toBeTrue()
|
|
})
|
|
test('Reading a file trims whitespaces', async () => {
|
|
const message = 'coucou\r\n\n\r\n'
|
|
await fs.writeFile(file.name, message)
|
|
const res = await env.from_file(file.name)
|
|
expect(res).toEqual({ok: true, data: 'coucou'})
|
|
})
|
|
test.todo('Fails safe if no access to read')
|
|
})
|
|
describe('Folder', () => {
|
|
let folder: DirResult
|
|
beforeEach(() => (folder = dirSync()))
|
|
afterEach(() => folder.removeCallback())
|
|
test('Fails safe if path points to a folder', async () => {
|
|
const res = await env.get_file_content(folder.name)
|
|
expect(res.ok).not.toBeTrue()
|
|
})
|
|
})
|
|
describe('read_env', () => {
|
|
test('Can get value from env', async () => {
|
|
const data = 'coucou'
|
|
const key = 'DB_PORT'
|
|
process.env[key] = data
|
|
|
|
const res = await env.read_env(key)
|
|
expect(res).toEqual({ok: true, data})
|
|
})
|
|
test('Can get value from file', async () => {
|
|
const data = 'coucou'
|
|
const key = 'DB_PORT'
|
|
process.env[`${key}__FILE`] = file.name
|
|
await fs.writeFile(file.name, data)
|
|
|
|
const res = await env.read_env(key)
|
|
expect(res).toEqual({ok: true, data})
|
|
})
|
|
})
|