test: build

This commit is contained in:
cha0s 2024-02-04 19:00:46 -06:00
parent b0dbc59f6d
commit 742b8d0f24
8 changed files with 85 additions and 14 deletions

View File

@ -44,7 +44,12 @@ module.exports = class Build extends Flecks {
.filter((e) => e);
}
static async buildRuntime(originalConfig, platforms, flecks = {}) {
static async buildRuntime({
flecks = {},
originalConfig,
platforms,
root,
}) {
const cleanConfig = JSON.parse(JSON.stringify(originalConfig));
// Dealias the config keys.
const dealiasedConfig = Object.fromEntries(
@ -57,7 +62,7 @@ module.exports = class Build extends Flecks {
];
}),
);
const resolver = new Resolver({root: FLECKS_CORE_ROOT});
const resolver = new Resolver({root});
const {paths, roots} = await explicate({
paths: Object.keys(originalConfig),
platforms,
@ -129,6 +134,7 @@ module.exports = class Build extends Flecks {
config: configParameter,
flecks: configFlecks,
platforms = ['server'],
root = FLECKS_CORE_ROOT,
} = {},
) {
// Load or use parameterized configuration.
@ -136,7 +142,7 @@ module.exports = class Build extends Flecks {
let configType = 'parameter';
if (!configParameter) {
// eslint-disable-next-line no-param-reassign
[configType, originalConfig] = await loadConfig();
[configType, originalConfig] = await loadConfig(root);
}
else {
originalConfig = JSON.parse(JSON.stringify(configParameter));
@ -147,9 +153,15 @@ module.exports = class Build extends Flecks {
resolver,
roots,
runtime,
} = await this.buildRuntime(originalConfig, platforms, configFlecks);
} = await this.buildRuntime({
flecks: configFlecks,
originalConfig,
platforms,
root,
});
const flecks = await super.from(runtime);
flecks.platforms = platforms;
flecks.root = root;
flecks.roots = roots;
flecks.resolver = resolver;
flecks.loadBuildFiles();
@ -181,7 +193,7 @@ module.exports = class Build extends Flecks {
if (!fleck) {
throw new Error(`Unknown build config: '${config}'`);
}
const rootConfig = await this.resolver.resolve(join(FLECKS_CORE_ROOT, 'build', config));
const rootConfig = await this.resolver.resolve(join(this.root, 'build', config));
if (rootConfig) {
return rootConfig;
}

View File

@ -5,14 +5,10 @@ const D = require('@flecks/core/build/debug');
const debug = D('@flecks/build/build/load-config');
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
module.exports = async function loadConfig() {
module.exports = async function loadConfig(root) {
try {
const {load} = require('js-yaml');
const filename = join(FLECKS_CORE_ROOT, 'build', 'flecks.yml');
const filename = join(root, 'build', 'flecks.yml');
const buffer = await readFile(filename, 'utf8');
debug('parsing configuration from YML...');
return ['YML', load(buffer, {filename})];
@ -21,7 +17,7 @@ module.exports = async function loadConfig() {
if ('ENOENT' !== error.code) {
throw error;
}
const {name} = require(join(FLECKS_CORE_ROOT, 'package.json'));
const {name} = require(join(root, 'package.json'));
const barebones = {
'@flecks/build': {},
'@flecks/core': {},
@ -30,7 +26,7 @@ module.exports = async function loadConfig() {
if (barebones[name]) {
delete barebones[name];
}
barebones[`${name}:${FLECKS_CORE_ROOT}`] = {};
barebones[`${name}:${root}`] = {};
return ['barebones', barebones];
}
};

View File

@ -11,7 +11,7 @@
"clean": "rm -rf dist node_modules yarn.lock",
"lint": "eslint --config ./build/eslint.config.js .",
"postversion": "npm run build",
"test": "webpack --config ../core/build/build.test.webpack.config.js --mode production && mocha --colors ./dist/test.js --timeout 10000",
"test": "webpack --config ./build/build.test.webpack.config.js --mode production && mocha --colors ./dist/test.js --timeout 10000",
"test:watch": "webpack watch --config ./build/build.test.webpack.config.js --mode development & mocha --parallel --watch --watch-files ./dist/test.js --colors ./dist/test.js --timeout 10000"
},
"bin": {

View File

@ -0,0 +1,56 @@
import {join} from 'path';
import {expect} from 'chai';
const {
FLECKS_CORE_ROOT = process.cwd(),
} = process.env;
const Build = __non_webpack_require__(join(FLECKS_CORE_ROOT, 'build', 'build'));
const loadConfig = __non_webpack_require__(join(FLECKS_CORE_ROOT, 'build', 'load-config'));
const buildRoot = join(FLECKS_CORE_ROOT, 'test', 'server', 'build', 'root');
it('defaults config', async () => {
expect(await loadConfig(FLECKS_CORE_ROOT))
.to.deep.equal([
'barebones',
{
[`@flecks/build:${FLECKS_CORE_ROOT}`]: {},
'@flecks/core': {},
'@flecks/fleck': {},
},
]);
});
it('loads config', async () => {
expect(await loadConfig(buildRoot))
.to.deep.equal(['YML', {one: {}, 'two:./two': {}}]);
});
it('configures from environment', async () => {
const {env} = process;
env.FLECKS_ENV__one__foo = '{"boo": 2}';
env.FLECKS_ENV__two__bar = 'yo';
expect(
Build.environmentConfiguration({
one: {foo: {boo: 1}},
two: {bar: 'hi'},
}),
)
.to.deep.equal({
one: {foo: {boo: 2}},
two: {bar: 'yo'},
});
});
it('dealiases config', async () => {
expect(await Build.buildRuntime({
originalConfig: {'two:./two': {foo: 2}},
platforms: [],
root: buildRoot,
}))
.to.nested.include({
'runtime.config.two.foo': 2,
});
});

View File

@ -0,0 +1,2 @@
one: {}
'two:./two': {}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,4 @@
a: {}
bar: {}
foo: {}
two: {}