Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixes file check failing with folder name #90

Merged
merged 2 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ function extractTrailingNumber (fileName) {
return match ? +match[1] : null
}

function extractFileName (fileName) {
return fileName.split(/(\\|\/)/g).pop()
}

async function isMatchingTime (filePath, time) {
const { birthtimeMs } = await stat(filePath)
return birthtimeMs >= time
Expand All @@ -139,7 +143,7 @@ async function checkSymlink (fileName, linkPath) {
const stats = await lstat(linkPath).then(stats => stats, () => null)
if (stats?.isSymbolicLink()) {
const existingTarget = await readlink(linkPath)
if (existingTarget === fileName) {
if (extractFileName(existingTarget) === extractFileName(fileName)) {
return false
}
await unlink(linkPath)
Expand All @@ -148,12 +152,12 @@ async function checkSymlink (fileName, linkPath) {
}

async function createSymlink (fileVal) {
const fileName = getFileName(fileVal)
const linkPath = join(dirname(fileName), 'current.log')
const shouldCreateSymlink = await checkSymlink(fileName, linkPath)
const linkPath = join(dirname(fileVal), 'current.log')
const shouldCreateSymlink = await checkSymlink(fileVal, linkPath)
if (shouldCreateSymlink) {
await symlink(fileName.split(/(\\|\/)/g).pop(), linkPath)
await symlink(extractFileName(fileVal), linkPath)
}
return false
}

module.exports = {
Expand All @@ -162,6 +166,7 @@ module.exports = {
checkSymlink,
createSymlink,
detectLastNumber,
extractFileName,
parseFrequency,
getNext,
parseSize,
Expand Down
33 changes: 30 additions & 3 deletions test/lib/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
buildFileName,
checkSymlink,
createSymlink,
extractFileName,
getFileSize,
detectLastNumber,
getNext,
Expand Down Expand Up @@ -152,7 +153,11 @@ test('validateLimitOptions()', async ({ doesNotThrow, throws }) => {

test('checkSymlink()', async ({ test, beforeEach }) => {
const folder = join('logs', 'utils')
beforeEach(() => cleanAndCreateFolder(folder))
const other = join(folder, 'other')
beforeEach(async () => {
await cleanAndCreateFolder(folder)
await cleanAndCreateFolder(other)
})

test('given a new symlink (should return true)', async ({ equal }) => {
const fileName = join(folder, 'file.log')
Expand All @@ -172,18 +177,40 @@ test('checkSymlink()', async ({ test, beforeEach }) => {
const linkTarget = await readlink(linkPath)
equal(linkTarget, fileName, 'symlink remains unchanged')
})

test('given a new symlink pointing to a different folder (should return true)', async ({ equal }) => {
const linkPath = join(folder, 'current.log')
const fileName = join(other, 'file.log')
await writeFile(fileName, 'test content')
const result = await checkSymlink(fileName, linkPath)
equal(result, true, 'returns true when symlink does not exist')
})

test('given a symlink pointing to a different folder (should return false)', async ({ equal }) => {
const linkPath = join(folder, 'current.log')
const fileName = join(other, 'file.log')
await writeFile(fileName, 'test content')
await symlink(fileName, linkPath)
const result = await checkSymlink(fileName, linkPath)
equal(result, false, 'returns false when symlink points to a different folder')
})
})

test('createSymlink()', async ({ beforeEach }) => {
const folder = join('logs', 'utils')
beforeEach(() => cleanAndCreateFolder(folder))

test('given a new symlink (should create symlink)', async ({ equal }) => {
const fileName = join(folder, 'file.log')
const fileName = join(folder, 'file1.log')
const linkPath = join(folder, 'current.log')
await writeFile(fileName, 'test content')
await createSymlink(fileName)
const linkTarget = await readlink(linkPath)
equal(linkTarget, fileName, 'creates correct symlink')
equal(linkTarget, extractFileName(fileName), 'creates correct symlink')
})

test('given there is already a symlink (should not create symlink)', async ({ equal }) => {
const fileName = join(folder, 'file1.log')
equal(false, await createSymlink(fileName), 'returns false when symlink already exists')
})
})