Skip to content

Commit

Permalink
test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Oct 26, 2024
1 parent b7c5a06 commit 85603d1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 0 additions & 2 deletions tests/fixtures/circular_expressions.staxfile

This file was deleted.

2 changes: 0 additions & 2 deletions tests/fixtures/duplicate_expressions.staxfile

This file was deleted.

1 change: 1 addition & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { afterEach, mock } from 'bun:test'
import Expressions from '~/staxfile/expressions'

afterEach(() => {
// Add manual cleanup of any existing tmp files
mock.restore()
Expressions.clearCache()
})
28 changes: 24 additions & 4 deletions tests/unit/yamler.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { beforeEach, describe, it, expect } from 'bun:test'
import { afterEach, beforeEach, describe, it, expect } from 'bun:test'
import { loadFile, dump } from '~/yamler'
import { dig, resolve } from '~/utils'
import { writeFileSync } from 'fs'
import tmp from 'tmp'

const tempFiles = []

function tempYamlFile(obj) {
const file = tmp.fileSync({ postfix: '.yaml' })
writeFileSync(file.name, dump(obj))
tempFiles.push(file)
return file.name
}

describe('YamlER', () => {
const fixturesDir = resolve(__dirname, '../../tests/fixtures')
Expand All @@ -13,7 +24,11 @@ describe('YamlER', () => {
return '<' + [key].concat(args).join(' ') + '>'
}

beforeEach(async () => yaml = await loadFile(composeYaml, expressionCallback))
beforeEach(async () => {
tempFiles.length = 0
yaml = await loadFile(composeYaml, expressionCallback)
})
afterEach(() => tempFiles.forEach(file => file.removeCallback()))

it('loads and processes a YAML file with imports', () => {
expect(yaml.stax.app).toBe('some_service')
Expand Down Expand Up @@ -54,7 +69,7 @@ describe('YamlER', () => {
})

it('throws an error when expressions have circular references', async () => {
const circularYaml = resolve(fixturesDir, 'circular_expressions.staxfile')
const circularYaml = tempYamlFile({ value1: '${{ get value2 }}', value2: '${{ get value1 }}' })
const promise = loadFile(circularYaml, expressionCallback)

await expect(promise).rejects.toThrow('Maximum expression parsing iterations (100) exceeded. Possible circular reference in expressions.')
Expand All @@ -75,7 +90,7 @@ describe('YamlER', () => {
return `result-${expressionId}`
}

const yamlWithDuplicateExpressions = resolve(fixturesDir, 'duplicate_expressions.staxfile')
const yamlWithDuplicateExpressions = tempYamlFile({ value1: '${{ test arg1 }}', value2: '${{ test arg1 }}' })
const result = await loadFile(yamlWithDuplicateExpressions, cachingCallback)

// Both values should be the same since they use the same expression (key + args)
Expand All @@ -84,4 +99,9 @@ describe('YamlER', () => {
expect(callCount).toBe(1)
expect(seenExpressions.size).toBe(1)
})

it('writes and reads yaml from a temp file', async () => {
const content = await loadFile(tempYamlFile({ test: 'value' }))
expect(content.test).toBe('value')
})
})

0 comments on commit 85603d1

Please sign in to comment.