Skip to content

Commit

Permalink
expressions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Sep 19, 2024
1 parent e223c28 commit 91e7bc3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/staxfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class Staxfile {

constructor(config: StaxConfig) {
this.config = new Config(config)
this.warnings = new Set()
}

get staxfile(): string { return this.config.staxfile }
Expand Down
1 change: 1 addition & 0 deletions tests/Staxfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
stax:
app: test
workspace_volume: ${{ stax.app }}-workspace-volume
workspace: /workspaces/${{ stax.app }}
vars:
Expand Down
68 changes: 23 additions & 45 deletions tests/unit/staxfile/expressions.test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,36 @@
import { describe, it, expect, beforeEach, mock } from 'bun:test'
import { describe, it, expect, beforeEach } from 'bun:test'
import Expressions from '~/staxfile/expressions'
import Staxfile from '~/staxfile'
import os from 'os'
import path from 'path'

describe('Expressions', () => {
let expression
let mockStaxfile
let staxfile

beforeEach(() => {
mockStaxfile = {
warnings: { add: mock(() => {}) },
config: {
hasProperty: mock(() => true),
fetch: mock((key) => `mock_${key}`),
},
location: {
local: true,
readSync: mock(() => 'mock_file_content'),
},
}
expression = new Expressions(mockStaxfile)
staxfile = new Staxfile({ source: './tests', staxfile: './tests/Staxfile', workspace: '/workspaces/tests' })
expression = new Expressions(staxfile)
})

it('evaluates stax config values', () => {
const result = expression.evaluate('stax.some_key', [])
expect(result).toBe('mock_some_key')
expect(mockStaxfile.config.fetch).toHaveBeenCalledWith('some_key')
it('evaluates undefined stax config values', () => {
const result = expression.evaluate('stax.app', [])
expect(result).toBe('tests')
})

it('evaluates read function', () => {
const result = expression.evaluate('read', ['test.txt', 'default'])
expect(result).toBe('mock_file_content')
expect(mockStaxfile.location.readSync).toHaveBeenCalledWith('test.txt')
})

// it('evaluates mount_workspace function', () => {
// mockStaxfile.config.source = '/mock/source'
// mockStaxfile.config.workspace = '/mock/workspace'
// const result = expression.evaluate('mount_workspace', [])
// expect(result).toBe('/mock/source:/mock/workspace')
// it('evaluates read function', () => {
// const result = expression.evaluate('read', [__filename, 'default'])
// expect(result).toContain('import { describe, it, expect, beforeEach } from \'bun:test\'')
// })

it('evaluates mount_workspace function', () => {
const result = expression.evaluate('mount_workspace', [])
expect(result).toBe(`${path.resolve('./tests')}:/workspaces/tests`)
})

it('evaluates mount_ssh_auth_sock function', () => {
const originalPlatform = process.platform
Object.defineProperty(process, 'platform', { value: 'darwin' })
const result = expression.evaluate('mount_ssh_auth_sock', [])
expect(result).toBe('${{ stax.ssh_auth_sock }}:${{ stax.ssh_auth_sock }}')
Object.defineProperty(process, 'platform', { value: originalPlatform })
expect(result).toBe('${{ stax.host_services }}:/run/host-services')
})

it('evaluates path.resolve function', () => {
Expand All @@ -53,11 +39,8 @@ describe('Expressions', () => {
})

it('evaluates user function', () => {
const originalUser = process.env.USER
process.env.USER = 'testuser'
const result = expression.evaluate('user', [])
expect(result).toBe('testuser')
process.env.USER = originalUser
expect(result).toBe(os.userInfo().username)
})

it('evaluates user_id function', () => {
Expand All @@ -70,19 +53,14 @@ describe('Expressions', () => {
expect(result).toBe('test-string')
})

// it('evaluates exists function', () => {
// const result = expression.evaluate('exists', ['/existing/path'])
// expect(result).toBe('true')
// })

it('adds warning for invalid expression', () => {
expression.evaluate('invalid_expression', [])
expect(mockStaxfile.warnings.add).toHaveBeenCalledWith('Invalid template expression: invalid_expression')
expect(staxfile.warnings).toContain('Invalid template expression: invalid_expression')
})

it('adds warning for undefined stax config', () => {
mockStaxfile.config.hasProperty = mock(() => false)
expression.evaluate('stax.undefined_key', [])
expect(mockStaxfile.warnings.add).toHaveBeenCalledWith("Undefined reference to 'stax.undefined_key'")
console.log(staxfile.warnings)
expect(staxfile.warnings).toContain("Undefined reference to 'stax.undefined_key'")
})
})

0 comments on commit 91e7bc3

Please sign in to comment.