Skip to content

Commit

Permalink
test & cache updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Oct 13, 2024
1 parent 22c5dde commit 45430c5
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 37 deletions.
8 changes: 6 additions & 2 deletions src/staxfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ export default class Staxfile {
public config: Config
public compose: Record<string, any>
public warnings: Set<string>
public cacheDir: string
private buildsCompiled: Record<string, string> = {}
private expressions: Expressions

constructor(config: StaxConfig) {
constructor(config: StaxConfig, options: { cacheDir?: string } = {}) {
let source = config.source

if (source.endsWith("/Staxfile"))
source = source.slice(0, -9)

this.config = new Config({ ...config, source: source })
this.cacheDir = options.cacheDir || this.systemCacheDir
this.warnings = new Set()
this.expressions = new Expressions(this)
}
Expand All @@ -35,7 +38,7 @@ export default class Staxfile {
get location(): Location { return this.config.location }
get baseDir(): string { return path.dirname(resolve(this.staxfile))}

private get cacheDir(): string {
private get systemCacheDir(): string {
const cacheDir = _cacheDir(this.context, this.app)

if (!existsSync(cacheDir))
Expand Down Expand Up @@ -99,6 +102,7 @@ export default class Staxfile {

// need to re-render after updating services since template expressions may have been added
this.compose = await this.renderCompose(this.compose)
// console.log(this.compose.stax);process.exit(0)

if (this.generatedWarnings.length > 0)
return exit(1, { message: this.generatedWarnings.join('\n') })
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# syntax=docker/dockerfile:1.7-labs
# $stax.section args
FROM ubuntu:${UBUNTU_VERSION} AS base

# $stax.section stages

FROM base AS final
# $stax.section final
10 changes: 0 additions & 10 deletions tests/fixtures/compose.staxfile

This file was deleted.

15 changes: 15 additions & 0 deletions tests/fixtures/imports/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
!import ./services.yaml as services

stax:
workspace_volume: ${{ stax.app }}-workspace
workspace: /workspaces/${{ stax.app }}
vars:
rails_server_port: 3000
ubuntu_version: 24.04

volumes:
shared-home:
${{ stax.workspace_volume }}:

services:
web: !extends services.web
10 changes: 0 additions & 10 deletions tests/fixtures/imports/common.yaml

This file was deleted.

5 changes: 3 additions & 2 deletions tests/fixtures/imports/service.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
environment:
PATH: /usr/local/bin:/usr/bin:/bin
build:
context: ${{ resolve_relative ../build }}
dockerfile: ${{ resolve_relative ../build/Dockerfile }}
12 changes: 12 additions & 0 deletions tests/fixtures/imports/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
!import ./service.yaml as service

web: !extends service
command: bin/rails server --port ${{ stax.vars.rails_server_port }} --binding 0.0.0.0
expose:
- ${{ stax.vars.rails_server_port }}
labels:
caddy: "${{ dasherize stax.app }}.d3v.localhost"
caddy.reverse_proxy: "{{ upstreams ${{ stax.vars.rails_server_port }} }}"

sidekiq: !extends service
command: /usr/local/bin/launch bundle exec sidekiq
2 changes: 0 additions & 2 deletions tests/fixtures/imports/ubuntu.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions tests/fixtures/imports/web.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions tests/fixtures/some_service.staxfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
!import imports/base.yaml as base

!extends base

stax: !extends base.stax
app: some_service
vars: !extends base.stax.vars
ruby_version: 2.0.1
20 changes: 20 additions & 0 deletions tests/unit/staxfile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect, beforeEach } from 'bun:test'
import { mkdirSync,rmSync } from 'fs'
import Staxfile from '~/staxfile'
import path from 'path'

describe('Staxfile', () => {
let staxfile
const cacheDir = path.join(__dirname, '../../tmp/tests-staxfile-cache')

beforeEach(() => {
rmSync(cacheDir, { recursive: true, force: true })
mkdirSync(cacheDir, { recursive: true })
staxfile = new Staxfile({ context: 'tests', source: './tests/fixtures', staxfile: './tests/fixtures/some_service.staxfile' }, { cacheDir })
})

it('compiles', async () => {
staxfile.compile({ force: true })
console.log(staxfile.config)
})
})
4 changes: 3 additions & 1 deletion tests/unit/staxfile/expressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { resolve } from '~/utils'
import Expressions from '~/staxfile/expressions'
import Staxfile from '~/staxfile'
import os from 'os'
import path from 'path'

describe('Expressions', () => {
const cacheDir = path.join(__dirname, '../../tmp/tests-staxfile-cache')
let expression
let staxfile

beforeEach(() => {
staxfile = new Staxfile({ source: './tests/fixtures', staxfile: './tests/fixtures/Staxfile', workspace: '/workspaces/tests' })
staxfile = new Staxfile({ source: './tests/fixtures', staxfile: './tests/fixtures/some_service.staxfile', workspace: '/workspaces/tests' }, { cacheDir })
expression = new Expressions(staxfile)
})

Expand Down
10 changes: 6 additions & 4 deletions tests/unit/staxfile/yaml.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { beforeEach, describe, it, expect } from 'bun:test'
import { loadFile, dump } from '~/staxfile/yaml'
import { resolve } from '~/utils'

describe('loadFile', () => {
const fixturesDir = resolve(__dirname, '../../../tests/fixtures')
const composeYaml = resolve(fixturesDir, 'compose.staxfile')
const composeYaml = resolve(fixturesDir, 'some_service.staxfile')
let yaml

beforeEach(() => yaml = loadFile(composeYaml))

it('loads and processes a YAML file with imports', () => {
expect(yaml.stax.app).toBe('test2')
expect(yaml.stax.app).toBe('some_service')
expect(yaml.stax.vars.ruby_version).toBe('2.0.1')
expect(yaml.stax.vars.rails_server_port).toBe(3000)
expect(yaml.volumes.home).toBeNull()
expect(Object.keys(yaml.volumes).length).toBe(2)
})

it('parses resolve_relative', () => {
expect(yaml.stax.vars.relative).toBe(resolve(fixturesDir, 'imports'))
expect(yaml.services.web.build.context).toBe(resolve(fixturesDir, 'build'))
expect(yaml.services.web.build.dockerfile).toBe(resolve(fixturesDir, 'build/Dockerfile'))
})

it('strips _stax_import_ anchors', () => {
Expand Down

0 comments on commit 45430c5

Please sign in to comment.