Skip to content

Commit

Permalink
test: fixed hanging tests and permission errors
Browse files Browse the repository at this point in the history
  • Loading branch information
aarontravass committed May 31, 2023
1 parent a6e2fac commit 6454359
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
21 changes: 12 additions & 9 deletions src/filelogger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { accessSync, constants as fsConstants, writeFileSync, createWriteStream, WriteStream, mkdirSync } from 'fs'
import { accessSync, writeFileSync, createWriteStream, WriteStream, mkdirSync } from 'fs'
import { dirname as directoryname } from 'path'

export class FileLogger {
Expand All @@ -10,9 +10,10 @@ export class FileLogger {
this.#filename = filename
this.#_stat()
this.#_createWritableStream()
this.#_endStream()
}

#fsAccess(filename: string, mode: number) {
#fsAccess(filename: string, mode?: number) {
try {
accessSync(filename, mode)
return true
Expand All @@ -23,11 +24,13 @@ export class FileLogger {

#_stat() {
//check if file exists
if (!this.#fsAccess(this.#filename, fsConstants.W_OK)) {
if (!this.#fsAccess(this.#filename)) {
// check if directory exists
if (!this.#fsAccess(this.#dirname, fsConstants.W_OK)) {
mkdirSync(this.#dirname, { recursive: true, mode: fsConstants.W_OK })
if (!this.#fsAccess(this.#dirname)) {
// create the directory
mkdirSync(this.#dirname, { recursive: true })
}
// create the file and write an empty string to it
writeFileSync(this.#filename, '')
return
}
Expand All @@ -42,21 +45,21 @@ export class FileLogger {
}

#_endStream() {
process.on('exit', (code) => {
process.on('exit', () => {
this.writableStream.close()
})

process.on('SIGTERM', (signal) => {
process.on('SIGTERM', () => {
this.writableStream.close()
process.exit(0)
})

process.on('SIGINT', (signal) => {
process.on('SIGINT', () => {
this.writableStream.close()
process.exit(0)
})

process.on('uncaughtException', (err) => {
process.on('uncaughtException', () => {
this.writableStream.close()
process.exit(1)
})
Expand Down
7 changes: 4 additions & 3 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { App } from '@tinyhttp/app'
import expect from 'expect'
import * as assert from 'uvu/assert'
import { promises, constants as fsConstants, readFileSync, unlinkSync, existsSync } from 'fs'
import { resolve } from 'path'

function describe(name: string, fn: (it: uvu.Test<Context>) => void) {
const s = suite(name)
Expand All @@ -15,7 +16,7 @@ function describe(name: string, fn: (it: uvu.Test<Context>) => void) {

function checkFileExists(file) {
return promises
.access(file, fsConstants.F_OK)
.access(file)
.then(() => true)
.catch(() => false)
}
Expand Down Expand Up @@ -98,7 +99,7 @@ describe('Logger tests', (it) => {
})
describe('Log file tests', (it) => {
it('should check if log file and directory is created', async (test) => {
const filename = './log/tiny.log'
const filename = './tests/tiny.log'

const app = new App()
app.use(
Expand All @@ -120,7 +121,7 @@ describe('Logger tests', (it) => {
.finally(() => server.close())
})
it('should read log file and check if logs are written', async (test) => {
const filename = './log/tiny.log'
const filename = './logs/test1/tiny.log'
const level = LogLevel.warn
const app = new App()
app.use(
Expand Down

0 comments on commit 6454359

Please sign in to comment.