Skip to content

Commit

Permalink
fix: circular dependency issues
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Jan 3, 2024
1 parent ed4e5f9 commit 0cc025c
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ import {
TRANSLATION_EXTENSION,
TRANSLATION_TYPE,
} from '../../../../src/utils/metadataConstants'
import {
writeFile,
scanExtension,
isSubDir,
readFile,
} from '../../../../src/utils/fsHelper'
import { writeFile, scanExtension } from '../../../../src/utils/fsHelper'
import { isSubDir, readFile } from '../../../../src/utils/fsUtils'
import { MetadataRepository } from '../../../../src/types/metadata'
import { Work } from '../../../../src/types/work'

jest.mock('fs-extra')
jest.mock('../../../../src/utils/fsHelper')
jest.mock('../../../../src/utils/fsUtils')

const mockedScanExtension = jest.mocked(scanExtension)
const mockedParseXmlFileToJson = jest.mocked(parseXmlFileToJson)
Expand Down
9 changes: 3 additions & 6 deletions __tests__/unit/lib/utils/cliHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import CLIHelper from '../../../../src/utils/cliHelper'
import { getLatestSupportedVersion } from '../../../../src/metadata/metadataManager'
import messages from '../../../../src/locales/en'
import { Work } from '../../../../src/types/work'
import {
readFile,
dirExists,
fileExists,
isGit,
} from '../../../../src/utils/fsHelper'
import { isGit } from '../../../../src/utils/fsHelper'
import { readFile, dirExists, fileExists } from '../../../../src/utils/fsUtils'
import { format } from 'util'

jest.mock('../../../../src/utils/childProcessUtils', () => {
Expand All @@ -39,6 +35,7 @@ jest.mock('../../../../src/utils/repoSetup', () => {
})

jest.mock('../../../../src/utils/fsHelper')
jest.mock('../../../../src/utils/fsUtils')

const mockedReadFile = jest.mocked(readFile)
const mockedDirExists = jest.mocked(dirExists)
Expand Down
174 changes: 1 addition & 173 deletions __tests__/unit/lib/utils/fsHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ import { expect, jest, describe, it } from '@jest/globals'
import { getWork } from '../../../__utils__/globalTestHelper'
import {
copyFiles,
dirExists,
fileExists,
gitPathSeparatorNormalizer,
isGit,
isSubDir,
pathExists,
readDir,
readFile,
readPathFromGit,
scan,
scanExtension,
Expand Down Expand Up @@ -309,7 +305,7 @@ describe('readDir', () => {
it('should throw', async () => {
// Act
try {
await readFile('path')
await readDir('path', work.config)
} catch (err) {
// Assert
expect(err).toBeTruthy()
Expand All @@ -318,41 +314,7 @@ describe('readDir', () => {
})
})
})
/*
describe('readFile', () => {
describe('when readfile succeed', () => {
beforeEach(() => {
// Arrange
fs.promises.readFile.mockImplementationOnce(() => Promise.resolve({}))
})
it('should return the file', async () => {
// Act
const file = await readFile('path')

// Assert
expect(file).toBeTruthy()
expect(fs.promises.readFile).toHaveBeenCalled()
})
})
describe('when readfile throw', () => {
beforeEach(() => {
// Arrange
fs.promises.readFile.mockImplementationOnce(() => Promise.reject('Error'))
})
it('should throw', async () => {
// Act
try {
await readFile('path')
} catch (err) {
// Assert
expect(err).toBeTruthy()
expect(fs.promises.readFile).toHaveBeenCalled()
}
})
})
})
*/
describe('scan', () => {
describe('when getSpawnContent throw', () => {
beforeEach(() => {
Expand Down Expand Up @@ -485,56 +447,6 @@ describe('scanExtension', () => {
})
})

describe('isSubDir', () => {
describe('when parent contains dir', () => {
it('returns true', async () => {
// Arrange

// Act
const result = isSubDir('parent', 'parent/dir')

// Assert
expect(result).toBe(true)
})
})

describe('when parent does not contains dir', () => {
it('returns false', async () => {
// Arrange

// Act
const result = isSubDir('parent', 'dir/child')

// Assert
expect(result).toBe(false)
})
})
it.each([
['/foo', '/foo', false],
['/foo', '/bar', false],
['/foo', '/foobar', false],
['/foo', '/foo/bar', true],
['/foo', '/foo/../bar', false],
['/foo', '/foo/./bar', true],
['/bar/../foo', '/foo/bar', true],
['/foo', './bar', false],
['C:\\Foo', 'C:\\Foo\\Bar', false],
['C:\\Foo', 'C:\\Bar', false],
['C:\\Foo', 'D:\\Foo\\Bar', false],
])(
`should verify %s expect %s to be a subDir: %s`,
(parent, child, expected) => {
// Arrange

// Act
const actual = isSubDir(parent, child)

// Assert
expect(actual).toBe(expected)
}
)
})

describe('pathExists', () => {
it('returns true when path is folder', async () => {
// Arrange
Expand Down Expand Up @@ -638,90 +550,6 @@ describe('writeFile', () => {
})
})

describe('dirExists', () => {
it('returns true when dir exist', async () => {
// Arrange
mockedStat.mockImplementation((() =>
Promise.resolve({
isDirectory: () => true,
} as unknown as Stats)) as unknown as typeof stat)

// Act
const exist = await dirExists('test')

// Assert
expect(exist).toBe(true)
})

it('returns false when dir does not exist', async () => {
// Arrange
mockedStat.mockImplementation((() =>
Promise.resolve({
isDirectory: () => false,
} as unknown as Stats)) as unknown as typeof stat)

// Act
const exist = await dirExists('test')

// Assert
expect(exist).toBe(false)
})

it('returns false when an exception occurs', async () => {
// Arrange
mockedStat.mockImplementation((() =>
Promise.reject(new Error('test'))) as unknown as typeof stat)

// Act
const exist = await dirExists('test')

// Assert
expect(exist).toBe(false)
})
})

describe('fileExists', () => {
it('returns true when file exist', async () => {
// Arrange
mockedStat.mockImplementation((() =>
Promise.resolve({
isFile: () => true,
} as unknown as Stats)) as unknown as typeof stat)

// Act
const exist = await fileExists('test')

// Assert
expect(exist).toBe(true)
})

it('returns false when file does not exist', async () => {
// Arrange
mockedStat.mockImplementation((() =>
Promise.resolve({
isFile: () => false,
} as unknown as Stats)) as unknown as typeof stat)

// Act
const exist = await fileExists('test')

// Assert
expect(exist).toBe(false)
})

it('returns false when an exception occurs', async () => {
// Arrange
mockedStat.mockImplementation((() =>
Promise.reject(new Error('test'))) as unknown as typeof stat)

// Act
const exist = await fileExists('test')

// Assert
expect(exist).toBe(false)
})
})

describe('isGit', () => {
it('returns true when it is a git file', async () => {
// Arrange
Expand Down
Loading

0 comments on commit 0cc025c

Please sign in to comment.