Skip to content

Commit

Permalink
test: unfancy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 26, 2023
1 parent ebc1887 commit 7463e92
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
"output": []
},
"test:command-reference": {
"command": "\"./bin/dev.js\" commandreference:generate --erroronwarnings",
"command": "ts-node \"./bin/dev.js\" commandreference:generate --erroronwarnings",
"files": [
"src/**/*.ts",
"messages/**",
Expand All @@ -209,7 +209,7 @@
]
},
"test:deprecation-policy": {
"command": "\"./bin/dev.js\" snapshot:compare",
"command": "ts-node \"./bin/dev.js\" snapshot:compare",
"files": [
"src/**/*.ts"
],
Expand All @@ -219,7 +219,7 @@
]
},
"test:json-schema": {
"command": "\"./bin/dev.js\" schema:compare",
"command": "ts-node \"./bin/dev.js\" schema:compare",
"files": [
"src/**/*.ts",
"schemas"
Expand Down
19 changes: 7 additions & 12 deletions src/hooks/init/load_config_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
*/

import type { Hook, Interfaces } from '@oclif/core';
import { Config, ConfigPropertyMeta, Logger } from '@salesforce/core';
import { Config, ConfigPropertyMeta } from '@salesforce/core';
import { isObject, get } from '@salesforce/ts-types';
import { load } from '@oclif/core/lib/module-loader.js';
import { ModuleLoader } from '@oclif/core';

const log = Logger.childFromRoot('plugin-settings:load_config_meta');
const OCLIF_META_PJSON_KEY = 'configMeta';

async function loadConfigMeta(plugin: Interfaces.Plugin): Promise<ConfigPropertyMeta | undefined> {
Expand All @@ -21,25 +20,21 @@ async function loadConfigMeta(plugin: Interfaces.Plugin): Promise<ConfigProperty
return;
}

const x = (await load(plugin, configMetaPath)) as { default: ConfigPropertyMeta };

log.info(x);

return x.default ?? x;
} catch {
const module = await ModuleLoader.load<{ default?: ConfigPropertyMeta }>(plugin, configMetaPath);
return module.default;
} catch (err) {
return;
}
}

const hook: Hook<'init'> = async ({ config }): Promise<void> => {
const hook: Hook<'init'> = async ({ config, context }): Promise<void> => {
const flattenedConfigMetas = (
await Promise.all(
(config.getPluginsList() || []).flatMap(async (plugin) => {
const configMeta = await loadConfigMeta(plugin);
if (!configMeta) {
log.info(`No config meta found for ${plugin.name}`);
context.debug(`No config meta found for ${plugin.name}`);
}

return configMeta;
})
)
Expand Down
72 changes: 45 additions & 27 deletions test/hooks/load_config_meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,68 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { test, expect } from '@oclif/test';
import { Plugin } from '@oclif/core';
import { expect } from 'chai';
import { Config as OclifConfig, Plugin } from '@oclif/core';
import { Config } from '@salesforce/core';
import { stubMethod } from '@salesforce/ts-sinon';
import sinon from 'sinon';
import { SinonSandbox, SinonStub } from 'sinon';
import tsSrcConfigMetaMock from '../config-meta-mocks/typescript-src/src/config-meta.js';
// @ts-expect-error because it's a js file with no types
import jsLibConfigMetaMock from '../config-meta-mocks/javascript-lib/lib/config-meta.js';

process.env.NODE_ENV = 'development';

describe('hooks', () => {
let sandbox: SinonSandbox;
beforeEach(() => {
let config: OclifConfig;
let addAllowedPropertiesStub: SinonStub;

beforeEach(async () => {
sandbox = sinon.createSandbox();
stubMethod(sandbox, Config, 'addAllowedProperties');
addAllowedPropertiesStub = stubMethod(sandbox, Config, 'addAllowedProperties');
config = await OclifConfig.load(process.cwd());
});

afterEach(() => {
sandbox.restore();
});
test
.stdout()
.loadConfig()
.do((ctx) => {
const mockPluginRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../config-meta-mocks/typescript-src'
);
ctx.config.plugins.set('sfdx-cli-ts-plugin', {

it('should load config metas from a ts src directory', async () => {
const mockPluginRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../config-meta-mocks/typescript-src'
);

sandbox.stub(config, 'plugins').value(
new Map(config.plugins).set('sfdx-cli-ts-plugin', {
root: mockPluginRoot,
hooks: {},
name: 'sf-cli-ts-plugin',
pjson: JSON.parse(readFileSync(path.resolve(mockPluginRoot, 'package.json'), 'utf-8')),
} as Plugin)
);

await config.runHook('init', { argv: [], id: 'test' });
expect(addAllowedPropertiesStub.firstCall.args[0][1]).to.equal(tsSrcConfigMetaMock.default);
});

it('should load config metas from a js lib directory', async () => {
const mockPluginRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
'../config-meta-mocks/javascript-lib'
);

sandbox.stub(config, 'plugins').value(
new Map(config.plugins).set('sfdx-cli-js-plugin', {
root: mockPluginRoot,
hooks: {},
name: 'sf-cli-js-plugin',
pjson: JSON.parse(readFileSync(path.resolve(mockPluginRoot, 'package.json'), 'utf-8')),
} as unknown as Plugin);
})
.hook('init')
.do(() => {
expect(tsSrcConfigMetaMock.default).to.deep.equal([
{
key: 'customKey',
},
]);
// modified since devPlugins now includes plugin-deploy-retrieve to exercise a config-meta that it includes.
// see https://github.com/salesforcecli/plugin-deploy-retrieve/blob/main/src/configMeta.ts
expect((Config.addAllowedProperties as SinonStub).firstCall.args[0][1]).to.equal(tsSrcConfigMetaMock.default);
})
.it('loads config metas from a ts src directory');
} as Plugin)
);

await config.runHook('init', { argv: [], id: 'test' });
expect(addAllowedPropertiesStub.firstCall.args[0][1]).to.equal(jsLibConfigMetaMock.default);
});
});
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.20.7", "@babel/template@^7.22.15":
"@babel/template@^7.22.15":
version "7.22.15"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.22.15.tgz#09576efc3830f0430f4548ef971dde1350ef2f38"
integrity sha512-QPErUVm4uyJa60rkI73qneDacvdvzxshT3kksGqlGWYdOTIUOwJ7RDUL8sGqslY1uXWSL6xMFKEXDS3ox2uF0w==
Expand All @@ -184,7 +184,7 @@
"@babel/parser" "^7.22.15"
"@babel/types" "^7.22.15"

"@babel/traverse@^7.21.0", "@babel/traverse@^7.23.2":
"@babel/traverse@^7.23.2":
version "7.23.2"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.23.2.tgz#329c7a06735e144a506bdb2cad0268b7f46f4ad8"
integrity sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw==
Expand All @@ -200,7 +200,7 @@
debug "^4.1.0"
globals "^11.1.0"

"@babel/types@^7.20.2", "@babel/types@^7.21.0", "@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0":
"@babel/types@^7.22.15", "@babel/types@^7.22.5", "@babel/types@^7.23.0":
version "7.23.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.23.0.tgz#8c1f020c9df0e737e4e247c0619f58c68458aaeb"
integrity sha512-0oIyUfKoI3mSqMvsxBdclDwxXKXAUA8v/apZbc+iSyARYou1o8ZGDxbUYyLFoW2arqS2jDGqJuZvv1d/io1axg==
Expand Down Expand Up @@ -5066,7 +5066,7 @@ json5@^1.0.2:
dependencies:
minimist "^1.2.0"

json5@^2.2.2, json5@^2.2.3:
json5@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==
Expand Down Expand Up @@ -7050,7 +7050,7 @@ redeyed@~2.1.0:
dependencies:
esprima "~4.0.0"

regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.3:
regenerator-runtime@^0.13.3:
version "0.13.11"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==
Expand Down

0 comments on commit 7463e92

Please sign in to comment.