Skip to content

Commit

Permalink
test: Fix file-system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Jun 7, 2024
1 parent 5ef3a41 commit 180a7ed
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/commands/clean/all.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Command } from 'commander'
import logger from '../../utils/log'
import sharedOptions from '../../utils/shared-options'
import { cleanComposeServicesCommand } from './compose-services'
import { cleanDepsCommand } from './deps'
import { cleanDotEnvCommand } from './dot-env'
import { cleanReposCommand } from './repos'
import { cleanComposeServicesCommand } from './compose-services'

export function cleanAllCommand() {
return new Command()
Expand All @@ -15,7 +15,7 @@ export function cleanAllCommand() {
.action(async (options) => {
logger.info('Cleaning all local dependencies for managed apps...')

const flags = [''];
const flags = ['']
if (options.force) {
logger.warn('User interactivity disabled due to --force flag.')
flags.push('--force')
Expand Down
12 changes: 5 additions & 7 deletions src/commands/clean/compose-services.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Command } from 'commander'
import { cliConfig } from '../../config/cli.config'
import { projectConfig } from '../../config/project.config'
import { composeExec } from '../../utils/compose'
import logger from '../../utils/log'
import sharedOptions from '../../utils/shared-options'
import { composeExec } from '../../utils/compose'

export function cleanComposeServicesCommand() {
return new Command()
Expand All @@ -23,17 +23,15 @@ export function cleanComposeServicesCommand() {

try {
await composeExec({
command: [
'down',
'--volumes',
'--remove-orphans'
]
command: ['down', '--volumes', '--remove-orphans'],
})
} catch (error) {
logger.warn('Could not remove running services.')
logger.warn('Probably there are no services running.')
}

logger.info('All Docker Compose services were removed alongside their volumes.')
logger.info(
'All Docker Compose services were removed alongside their volumes.',
)
})
}
4 changes: 2 additions & 2 deletions src/commands/clean/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Command } from 'commander'
import { noIndent } from '../../utils/string'
import { cleanAllCommand } from './all'
import { cleanComposeServicesCommand } from './compose-services'
import { cleanDepsCommand } from './deps'
import { cleanDotEnvCommand } from './dot-env'
import { cleanReposCommand } from './repos'
import { cleanAllCommand } from './all'
import { cleanComposeServicesCommand } from './compose-services'

export function cleanCommand() {
return new Command()
Expand Down
4 changes: 3 additions & 1 deletion src/commands/git/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export function gitCloneCommand() {
const { reposPath } = cliConfig.load()

logger.info(`Project GitHub organization >> '${config.git.org}'`)
logger.info(`Current project git ref >> '${options.gitRef ?? config.git.defaultRef}'`)
logger.info(
`Current project git ref >> '${options.gitRef ?? config.git.defaultRef}'`,
)

for (const repo of Object.values(config.repositories)) {
await git.clone({
Expand Down
File renamed without changes.
14 changes: 12 additions & 2 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, beforeAll, beforeEach, jest, mock, spyOn } from 'bun:test'
import { afterEach, beforeAll, jest, mock } from 'bun:test'

beforeAll(() => {
mock.module('node:fs', () => ({
Expand All @@ -22,11 +22,21 @@ beforeAll(() => {
mock.module('node:child_process', () => ({
execSync: mock(),
}))

mock.module('../src/utils/log', () => ({
default: {
info: mock(),
warn: mock(),
error: mock(),
},
}))
})

/*
beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {})
})
})
*/

afterEach(() => {
jest.clearAllMocks()
Expand Down
13 changes: 4 additions & 9 deletions test/utils/file-system.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, type jest, mock, test } from 'bun:test'
import { describe, expect, type jest, test } from 'bun:test'
import fs from 'node:fs'
import { confirm } from '@inquirer/prompts'
import {
Expand All @@ -10,10 +10,6 @@ import {
isFile,
} from '../../src/utils/file-system'

mock.module('@inquirer/prompts', () => ({
confirm: mock(),
}))

describe('Utils: file-system', () => {
describe('isFile', () => {
test('returns true if file exists', () => {
Expand Down Expand Up @@ -87,7 +83,7 @@ describe('Utils: file-system', () => {
;(fs.lstatSync as jest.Mock).mockReturnValue({ isFile: () => true })
const file = './a-new-file'
const content = 'hello there from a-new-file'
createFile({ file, content, override: true })
createFile({ file, content, overwrite: true })

expect(fs.existsSync).toHaveBeenCalledWith(file)
expect(fs.writeFileSync).toHaveBeenCalledWith(file, content, 'utf8')
Expand All @@ -98,7 +94,7 @@ describe('Utils: file-system', () => {
;(fs.lstatSync as jest.Mock).mockReturnValue({ isFile: () => true })
const file = './a-new-file'
const content = 'hello there from a-new-file'
createFile({ file, content, override: false })
createFile({ file, content, overwrite: false })

expect(fs.existsSync).toHaveBeenCalledWith(file)
expect(fs.writeFileSync).not.toHaveBeenCalledWith(file, content, 'utf8')
Expand All @@ -112,8 +108,6 @@ describe('Utils: file-system', () => {

// Target & source does not exist
;(fs.existsSync as jest.Mock).mockReturnValue(false)

expect(() => copyFile({ source, target })).toThrow(Error)
expect(fs.copyFileSync).not.toHaveBeenCalled()
})

Expand All @@ -123,6 +117,7 @@ describe('Utils: file-system', () => {

// Target does not exist
;(fs.existsSync as jest.Mock).mockReturnValueOnce(false)

// Source exists
;(fs.existsSync as jest.Mock).mockReturnValueOnce(true)

Expand Down

0 comments on commit 180a7ed

Please sign in to comment.