Skip to content

Commit

Permalink
allow settings in !import lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Nov 26, 2024
1 parent b99402e commit f151766
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ function validateName(name: string) {
}

const settings = {
all: function() { return load() },
all: load,

read: function(name: string, defaultValue: any | undefined = undefined) {
isValidName: (name: string) => VALID_NAMES.includes(name),

read: (name: string, defaultValue: any | undefined = undefined) => {
validateName(name)
const settings = load()
return settings[name] || defaultValue
},

write: function(name: string, value): any {
write: (name: string, value: any) => {
validateName(name)

if (name === 'services_home') verifyDirectory(value = resolve(value))
Expand Down
6 changes: 6 additions & 0 deletions src/yamler/yamler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as fs from 'fs'
import * as path from 'path'
import yaml from 'js-yaml'
import icons from '~/icons'
import settings from '~/settings'
import Import from './import'

export async function loadFile(filePath: string, expressionCallback?: Function | undefined): Promise<Record<string, any>> {
Expand Down Expand Up @@ -89,9 +90,14 @@ export default class YamlER {
}
}

private subsituteSettings(filePath: string): string {
return filePath.replace(/\$(\w+)/g, (match, name) => settings.isValidName(name) ? settings.read(name) : match)
}

private parseImports() {
this.imports = {}
this.content = this.content.replace(importRegex, (match, filePath, name) => {
filePath = this.subsituteSettings(filePath)
const yamlImport = new Import({ name, match, filePath, parentFile: this.filePath })
this.imports[yamlImport.name] = yamlImport

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/imports/service.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
!import ./environment/test/environment.yaml as environment
!import ./environment/$environment/environment.yaml as environment

environment: !extends environment
HELLO: world
Expand Down
14 changes: 12 additions & 2 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { afterEach, mock } from 'bun:test'
import { beforeEach, afterEach, mock } from 'bun:test'
import { evaluatorCache } from '~/staxfile/evaluator'
import { mkdirSync, rmSync } from 'fs'
import { join } from 'path'
import settings from '~/settings'

process.env.STAX_HOME = join(__dirname, '../tmp/.stax-tests')

beforeEach(() => {
mkdirSync(process.env.STAX_HOME)

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3

Check failure on line 10 in tests/setup.js

View workflow job for this annotation

GitHub Actions / tests

ENOENT: No such file or directory

at /home/runner/work/stax/stax/tests/setup.js:10:3
settings.write('environment', 'test')
})

afterEach(() => {
// Add manual cleanup of any existing tmp files
mock.restore()
evaluatorCache.clear()
rmSync(process.env.STAX_HOME, { recursive: true, force: true })
})
5 changes: 5 additions & 0 deletions tests/unit/yamler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ describe('YamlER', () => {
it('strips _stax_import_ anchors', () => {
expect(dump(yaml)).not.toContain('_stax_import_')
})

it('imports correct env', () => {
expect(yaml.services.web.environment.HELLO).toBe('world')
expect(yaml.services.web.environment.RAILS_ENV).toBe('test')
})
})

it('handles expressions that reference a value from an attribute set by another expression', async () => {
Expand Down

0 comments on commit f151766

Please sign in to comment.