Skip to content

Commit

Permalink
chore: fix tests
Browse files Browse the repository at this point in the history
The previous regex matches caught all strings, which wasn't ideal
  • Loading branch information
nytamin committed Sep 20, 2023
1 parent 16451e8 commit f93d4ad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
17 changes: 11 additions & 6 deletions tests/internal-tests/src/__mocks__/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ const wndMock = wndMock0 as any as WNDMockType
/* eslint-disable no-console */
const DEBUG_LOG = false

enum fsConstants {
R_OK = 2,
W_OK = 4,
}

const fs: any = jest.createMockFromModule('fs')

type MockAny = MockDirectory | MockFile
Expand Down Expand Up @@ -199,7 +194,11 @@ export function __printAllFiles(): string {
strs.push(`${indent}${name}/`)
strs.push(getPaths(file, indent + ' '))
} else {
strs.push(`${indent}${name}: size: ${file.size}`)
strs.push(
`${indent}${name}: size: ${file.size} (${file.accessRead ? 'read' : ''} ${
file.accessWrite ? 'write' : ''
})`
)
}
}
return strs.join('\n')
Expand Down Expand Up @@ -289,6 +288,12 @@ export function __emitter(): EventEmitter {
}
fs.__emitter = __emitter

export enum constants {
R_OK = 2,
W_OK = 4,
}
fs.constants = constants

export function stat(path: string, callback: (error: any, result?: any) => void): void {
path = fixPath(path)
if (DEBUG_LOG) console.log('fs.stat', path)
Expand Down
43 changes: 29 additions & 14 deletions tests/internal-tests/src/__tests__/issues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fsOrg from 'fs'
import { promisify } from 'util'
// eslint-disable-next-line node/no-extraneous-import
import { ExpectedPackageStatusAPI } from '@sofie-automation/shared-lib/dist/package-manager/package'
import { Expectation, literal } from '@sofie-package-manager/api'
import { Expectation, INNER_ACTION_TIMEOUT, literal } from '@sofie-package-manager/api'
import type * as fsMockType from '../__mocks__/fs'
import { prepareTestEnviromnent, TestEnviromnent } from './lib/setupEnv'
import { waitUntil, waitTime } from './lib/lib'
Expand All @@ -20,16 +20,14 @@ const fsAccess = promisify(fs.access)
const fsStat = promisify(fs.stat)

const fsExists = async (filePath: string) => {
let exists = false
try {
await fsAccess(filePath, undefined)
await fsAccess(filePath, fs.constants.R_OK)
// The file exists
exists = true
return true
} catch (err) {
// Ignore
if (typeof err === 'object' && err && (err as any).code === 'ENOENT') return false
throw err
}

return exists
}

// const fsStat = promisify(fs.stat)
Expand Down Expand Up @@ -76,9 +74,9 @@ describe('Handle unhappy paths', () => {
expect(env.expectationStatuses['copy0']).toMatchObject({
actualVersionHash: null,
statusInfo: {
status: /new|waiting/,
status: expect.stringMatching(/new|waiting/),
statusReason: {
tech: /not able to access file/i,
tech: expect.stringMatching(/not able to access file/i),
},
},
})
Expand Down Expand Up @@ -163,7 +161,6 @@ describe('Handle unhappy paths', () => {
accessRead: true,
accessWrite: false,
})
// fs.__printAllFiles()

addCopyFileExpectation(
env,
Expand All @@ -177,9 +174,9 @@ describe('Handle unhappy paths', () => {
expect(env.expectationStatuses['copy0']).toMatchObject({
actualVersionHash: null,
statusInfo: {
status: /new|waiting/,
status: expect.stringMatching(/new|waiting/),
statusReason: {
tech: /not able to access file/i,
tech: expect.stringMatching(/not able to access file/i),
},
},
})
Expand All @@ -189,13 +186,14 @@ describe('Handle unhappy paths', () => {
fs.__mockSetFile('/sources/source0/file0Source.mp4', 1234)

await waitUntil(() => {
// Expect the Expectation to be waiting:
// Expect the Expectation to be waiting -> new:
expect(env.expectationStatuses['copy0']).toMatchObject({
actualVersionHash: null,
statusInfo: {
status: 'new',
statusReason: {
tech: /not able to access target/i,
// user: expect.stringMatching(/asdf/i),
tech: expect.stringMatching(/Not able to write to container folder.*write access denied/i),
},
},
})
Expand Down Expand Up @@ -266,6 +264,14 @@ describe('Handle unhappy paths', () => {

fs.__emitter().once('copyFile', listenToCopyFile)

env.setLogFilterFunction((level, ...args) => {
const str = args.join(',')
// Suppress some logged warnings:
if (level === 'warn' && str.includes('stalled, restarting')) return false
if (level === 'error' && str.includes('cancelling timed out work')) return false
return true
})

addCopyFileExpectation(
env,
'copy0',
Expand Down Expand Up @@ -326,6 +332,14 @@ describe('Handle unhappy paths', () => {
})
fs.__emitter().once('copyFile', listenToCopyFile)

env.setLogFilterFunction((level, ...args) => {
const str = args.join(',')
// Suppress some logged warnings:
if (level === 'warn' && str.includes('stalled, restarting')) return false
if (level === 'warn' && str.includes('Cancelling job')) return false
return true
})

addCopyFileExpectation(
env,
'copy0',
Expand Down Expand Up @@ -450,6 +464,7 @@ describe('Handle unhappy paths', () => {
expect(env.expectationStatuses['step1'].statusInfo.status).toEqual('fulfilled')
expect(env.expectationStatuses['step2'].statusInfo.status).toEqual('fulfilled')

expect(await fsExists('/targets/target0/myFolder/file0Target.mp4')).toBe(true)
expect(await fsStat('/targets/target0/myFolder/file0Target.mp4')).toMatchObject({
size: 1234,
})
Expand Down

0 comments on commit f93d4ad

Please sign in to comment.