From 45430c5e7d085885deaf55c2d8f158f2af690735 Mon Sep 17 00:00:00 2001 From: Kien La Date: Sun, 13 Oct 2024 15:11:25 -0400 Subject: [PATCH] test & cache updates --- src/staxfile/index.ts | 8 ++++++-- tests/fixtures/build/Dockerfile | 8 ++++++++ tests/fixtures/compose.staxfile | 10 ---------- tests/fixtures/imports/base.yaml | 15 +++++++++++++++ tests/fixtures/imports/common.yaml | 10 ---------- tests/fixtures/imports/service.yaml | 5 +++-- tests/fixtures/imports/services.yaml | 12 ++++++++++++ tests/fixtures/imports/ubuntu.yaml | 2 -- tests/fixtures/imports/web.yaml | 6 ------ tests/fixtures/some_service.staxfile | 8 ++++++++ tests/unit/staxfile.test.js | 20 ++++++++++++++++++++ tests/unit/staxfile/expressions.test.js | 4 +++- tests/unit/staxfile/yaml.test.js | 10 ++++++---- 13 files changed, 81 insertions(+), 37 deletions(-) create mode 100644 tests/fixtures/build/Dockerfile delete mode 100644 tests/fixtures/compose.staxfile create mode 100644 tests/fixtures/imports/base.yaml delete mode 100644 tests/fixtures/imports/common.yaml create mode 100644 tests/fixtures/imports/services.yaml delete mode 100644 tests/fixtures/imports/ubuntu.yaml delete mode 100644 tests/fixtures/imports/web.yaml create mode 100644 tests/fixtures/some_service.staxfile create mode 100644 tests/unit/staxfile.test.js diff --git a/src/staxfile/index.ts b/src/staxfile/index.ts index 508bbd9..bfa8a1a 100644 --- a/src/staxfile/index.ts +++ b/src/staxfile/index.ts @@ -15,15 +15,18 @@ export default class Staxfile { public config: Config public compose: Record public warnings: Set + public cacheDir: string private buildsCompiled: Record = {} 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) } @@ -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)) @@ -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') }) diff --git a/tests/fixtures/build/Dockerfile b/tests/fixtures/build/Dockerfile new file mode 100644 index 0000000..2fe03aa --- /dev/null +++ b/tests/fixtures/build/Dockerfile @@ -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 diff --git a/tests/fixtures/compose.staxfile b/tests/fixtures/compose.staxfile deleted file mode 100644 index f6448f7..0000000 --- a/tests/fixtures/compose.staxfile +++ /dev/null @@ -1,10 +0,0 @@ -!import imports/common.yaml as common - -stax: !extends common.stax - app: test2 - vars: !extends common.stax.vars - ruby_version: 2.0.1 - -volumes: !extends common.volumes - -!extends common.stax.vars diff --git a/tests/fixtures/imports/base.yaml b/tests/fixtures/imports/base.yaml new file mode 100644 index 0000000..d3c8086 --- /dev/null +++ b/tests/fixtures/imports/base.yaml @@ -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 diff --git a/tests/fixtures/imports/common.yaml b/tests/fixtures/imports/common.yaml deleted file mode 100644 index ab3d3cd..0000000 --- a/tests/fixtures/imports/common.yaml +++ /dev/null @@ -1,10 +0,0 @@ -stax: - app: test - workspace: /workspaces/${{ stax.app }} - vars: - rails_server_port: 3000 - ruby_version: 3.3.3 - relative: ${{ resolve_relative . }} - -volumes: - home: diff --git a/tests/fixtures/imports/service.yaml b/tests/fixtures/imports/service.yaml index 1e4121f..f03f492 100644 --- a/tests/fixtures/imports/service.yaml +++ b/tests/fixtures/imports/service.yaml @@ -1,2 +1,3 @@ -environment: - PATH: /usr/local/bin:/usr/bin:/bin +build: + context: ${{ resolve_relative ../build }} + dockerfile: ${{ resolve_relative ../build/Dockerfile }} diff --git a/tests/fixtures/imports/services.yaml b/tests/fixtures/imports/services.yaml new file mode 100644 index 0000000..7c196c0 --- /dev/null +++ b/tests/fixtures/imports/services.yaml @@ -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 diff --git a/tests/fixtures/imports/ubuntu.yaml b/tests/fixtures/imports/ubuntu.yaml deleted file mode 100644 index 0ce5dd2..0000000 --- a/tests/fixtures/imports/ubuntu.yaml +++ /dev/null @@ -1,2 +0,0 @@ -context: ${{ resolve_relative ./ }} -dockerfile: Dockerfile diff --git a/tests/fixtures/imports/web.yaml b/tests/fixtures/imports/web.yaml deleted file mode 100644 index eed5b89..0000000 --- a/tests/fixtures/imports/web.yaml +++ /dev/null @@ -1,6 +0,0 @@ -!import ./ubuntu.yaml as ubuntu -!import ./service.yaml as service - -!extends service -build: !extends ubuntu -command: bin/rails server diff --git a/tests/fixtures/some_service.staxfile b/tests/fixtures/some_service.staxfile new file mode 100644 index 0000000..d9ac18c --- /dev/null +++ b/tests/fixtures/some_service.staxfile @@ -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 diff --git a/tests/unit/staxfile.test.js b/tests/unit/staxfile.test.js new file mode 100644 index 0000000..17c34e3 --- /dev/null +++ b/tests/unit/staxfile.test.js @@ -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) + }) +}) diff --git a/tests/unit/staxfile/expressions.test.js b/tests/unit/staxfile/expressions.test.js index 7415c99..be918a3 100644 --- a/tests/unit/staxfile/expressions.test.js +++ b/tests/unit/staxfile/expressions.test.js @@ -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) }) diff --git a/tests/unit/staxfile/yaml.test.js b/tests/unit/staxfile/yaml.test.js index f3c2154..cdbe827 100644 --- a/tests/unit/staxfile/yaml.test.js +++ b/tests/unit/staxfile/yaml.test.js @@ -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', () => {