fix(appium): add missing support for schema-as-object
I had intended to allow extensions to define a schema via `package.json` instead of a path to a file, and apparently forgot to implement this.  So, now it is implemented.
MODIFY
packages/appium/lib/extension-config.js
packages/appium/lib/extension-config.js
@@ -111,9 +111,15 @@ export default class ExtensionConfig {
val: argSchemaPath
});
}
+ } else if (_.isPlainObject(argSchemaPath)) {
+ try {
+ this.readExtensionSchema(extName, extData);
+ } catch (err) {
+ problems.push({err: `Unable to register embedded schema; ${err.message}`, val: argSchemaPath});
+ }
} else {
problems.push({
- err: 'Incorrectly formatted schema field; must be a path to a schema file.',
+ err: 'Incorrectly formatted schema field; must be a path to a schema file or a schema object.',
val: argSchemaPath
});
}
@@ -301,12 +307,17 @@ export default class ExtensionConfig {
`No \`schema\` property found in config for ${extType} ${pkgName} -- why is this function being called?`,
);
}
+ let moduleObject;
+ if (_.isString(argSchemaPath)) {
const schemaPath = resolveFrom(
path.resolve(appiumHome, installPath),
// this path sep is fine because `resolveFrom` uses Node's module resolution
path.normalize(`${pkgName}/${argSchemaPath}`),
);
- const moduleObject = require(schemaPath);
+ moduleObject = require(schemaPath);
+ } else {
+ moduleObject = argSchemaPath;
+ }
// this sucks. default exports should be destroyed
const schema = moduleObject.__esModule
? moduleObject.default
@@ -348,7 +359,7 @@ export {
/**
* Extension data (pulled from config YAML)
* @typedef {Object} ExtData
- * @property {string} [schema] - Optional schema path if the ext defined it
+ * @property {string|import('ajv').SchemaObject} [schema] - Optional schema path if the ext defined it
* @property {string} pkgName - Package name
* @property {string} installPath - Actually looks more like a module identifier? Resolved from `APPIUM_HOME`
*/

MODIFY
packages/appium/test/extension-config-specs.js
packages/appium/test/extension-config-specs.js
@@ -143,7 +143,7 @@ describe('ExtensionConfig', function () {
driverConfig
.getSchemaProblems({schema: []}, 'foo')
.should.deep.include({
- err: 'Incorrectly formatted schema field; must be a path to a schema file.',
+ err: 'Incorrectly formatted schema field; must be a path to a schema file or a schema object.',
val: [],
});
});
@@ -325,12 +325,13 @@ describe('ExtensionConfig', function () {
beforeEach(function () {
pluginConfig = PluginConfig.getInstance('/tmp/');
});
- describe('when provided an object with a defined non-string `schema` property', function () {
+
+ describe('when provided an object with a defined `schema` property of unsupported type', function () {
it('should return an array with an associated problem', function () {
pluginConfig
.getSchemaProblems({schema: []}, 'foo')
.should.deep.include({
- err: 'Incorrectly formatted schema field; must be a path to a schema file.',
+ err: 'Incorrectly formatted schema field; must be a path to a schema file or a schema object.',
val: [],
});
});
@@ -378,6 +379,20 @@ describe('ExtensionConfig', function () {
});
});
});
+
+ describe('when provided an object `schema` property', function () {
+ it('should return an empty array', function () {
+ const problems = pluginConfig.getSchemaProblems(
+ {
+ pkgName: 'fixtures', // just corresponds to a directory name relative to `installPath` `(__dirname)`
+ installPath: __dirname,
+ schema: {type: 'object', properties: {foo: {type: 'string'}}},
+ },
+ 'foo',
+ );
+ problems.should.be.empty;
+ });
+ });
});
describe('read()', function () {

fix(appium): add missing support for schema-as-object

I had intended to allow extensions to define a schema via `package.json` instead of a path to a file, and apparently forgot to implement this.  So, now it is implemented.