Skip to content

Commit

Permalink
fix: add more validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sztadii committed Jul 28, 2022
1 parent 71a18d8 commit bb58a83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 15 additions & 3 deletions src/file-naming-enforcer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,29 @@ describe('fileNamingEnforcer function', () => {
})

it('when a folder is empty then we display a error message and kill a process', async () => {
const folderName = 'mocks-5'
await fileService.createFolder(folderName)
await fileNamingEnforcer.validate(`type=kebabCase folder=./${folderName}`)

expect(processService.killProcess).toHaveBeenCalledTimes(1)
expect(logger.log).toHaveBeenCalledTimes(1)
expect(logger.log).toHaveBeenCalledWith(
`Uuu, folder ./${folderName} is empty, please take a look on that`
)
})

it('when a folder does not exist we display a error message and kill a process', async () => {
await fileNamingEnforcer.validate('type=kebabCase folder=./xxx')

expect(processService.killProcess).toHaveBeenCalledTimes(1)
expect(logger.log).toHaveBeenCalledTimes(1)
expect(logger.log).toHaveBeenCalledWith(
'Uuu, folder ./xxx is empty, please take a look on that'
'Uuu, folder ./xxx does not exist, please take a look on that'
)
})

it('when we could not find any file with provided extension', async () => {
const folderName = 'mocks-5'
const folderName = 'mocks-6'
await fileService.createFile(folderName, 'SIMPLE-READ.md')
await fileService.createFile(folderName, 'simple-js-file.js')

Expand All @@ -123,7 +135,7 @@ describe('fileNamingEnforcer function', () => {
})

it('when searched files are correct then we display a success message', async () => {
const folderName = 'mocks-6'
const folderName = 'mocks-7'
await fileService.createFile(folderName, 'simple-styles.sass')
await fileService.createFile(folderName, 'other-styles.sass')

Expand Down
14 changes: 10 additions & 4 deletions src/file-naming-enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ export default class FileNamingEnforcer {
)
}

let files: string[] = []
const hasFolder = this.fileService.hasFolder(folder)

try {
files = await this.fileService.getFiles(folder, ext)
} catch (e) {
if (!hasFolder) {
throw new Error(
`Uuu, folder ${folder} does not exist, please take a look on that`
)
}

const files = await this.fileService.getFiles(folder, ext)

if (!files.length && ext === '*') {
throw new Error(
`Uuu, folder ${folder} is empty, please take a look on that`
)
Expand Down

0 comments on commit bb58a83

Please sign in to comment.