feat(): Implement base test fns for pm
script.
This commit is contained in:
parent
4f15c61019
commit
d5de0bfe54
10
log
10
log
|
@ -15,6 +15,10 @@ class Log extends This {
|
||||||
console.log(str);
|
console.log(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
success(str) {
|
||||||
|
console.log(this._echoInGreen(str));
|
||||||
|
}
|
||||||
|
|
||||||
info(str) {
|
info(str) {
|
||||||
console.log(`[INFO] ${str}`);
|
console.log(`[INFO] ${str}`);
|
||||||
}
|
}
|
||||||
|
@ -34,6 +38,12 @@ class Log extends This {
|
||||||
this.echo(message);
|
this.echo(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.program.command('success')
|
||||||
|
.argument('<message>')
|
||||||
|
.action(message => {
|
||||||
|
this.success(message);
|
||||||
|
});
|
||||||
|
|
||||||
this.program.command('info')
|
this.program.command('info')
|
||||||
.argument('<message>')
|
.argument('<message>')
|
||||||
.action(message => {
|
.action(message => {
|
||||||
|
|
91
pm
91
pm
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const This = require('./this');
|
const This = require('./this');
|
||||||
const { Command } = require('commander');
|
|
||||||
|
|
||||||
// Example of subcommands which are implemented as stand-alone executable files.
|
// Example of subcommands which are implemented as stand-alone executable files.
|
||||||
//
|
//
|
||||||
|
@ -14,9 +13,25 @@ class Pm extends This {
|
||||||
super();
|
super();
|
||||||
this.version = '0.0.1';
|
this.version = '0.0.1';
|
||||||
this.description = 'Fake package manager';
|
this.description = 'Fake package manager';
|
||||||
|
super.init(); // initialize commander with overridden version and description
|
||||||
|
}
|
||||||
|
|
||||||
// implement commander abilities
|
install() {
|
||||||
this.program = new Command().name(this.scriptName).version(this.version).description(this.description);
|
this.program.command('install [name]', 'install one or more packages').alias('i');
|
||||||
|
}
|
||||||
|
|
||||||
|
search() {
|
||||||
|
this.program.command('search [query]', 'search with optional query').alias('s');
|
||||||
|
}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
this.program.command('update', 'update installed packages', {
|
||||||
|
executableFile: 'myUpdateSubCommand',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
list() {
|
||||||
|
this.program.command('list', 'list packages installed', { isDefault: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
// override This.discovery()
|
// override This.discovery()
|
||||||
|
@ -25,12 +40,75 @@ class Pm extends This {
|
||||||
return methods;
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Basic test methods
|
||||||
|
_testListMethod() {
|
||||||
|
const pm = new Pm();
|
||||||
|
const pmMethods = pm.listMethods();
|
||||||
|
|
||||||
|
const testMethods =
|
||||||
|
pmMethods.includes('install') &&
|
||||||
|
pmMethods.includes('search') &&
|
||||||
|
pmMethods.includes('update') &&
|
||||||
|
pmMethods.includes('list') &&
|
||||||
|
pmMethods.includes('discovery') &&
|
||||||
|
pmMethods.includes('start');
|
||||||
|
const testInternalMethods = !pmMethods.includes('_testListMethod');
|
||||||
|
|
||||||
|
console.assert(testMethods, 'Pm.listMethod() failed');
|
||||||
|
console.assert(
|
||||||
|
testInternalMethods,
|
||||||
|
'Pm.listMethod() for internal methods failed. Classes with _ should be ignored'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (testMethods && testInternalMethods) {
|
||||||
|
this.execCmd(`${this.workingDir}/log success 'testListMethod passed'`);
|
||||||
|
} else {
|
||||||
|
this.execCmd(`${this.workingDir}/log error 'testListMethod failed'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_testListProperties() {
|
||||||
|
const pm = new Pm();
|
||||||
|
const pmProperties = pm.listProperties();
|
||||||
|
|
||||||
|
const testProperties =
|
||||||
|
pmProperties.includes('version') && pmProperties.includes('description') && pmProperties.includes('program');
|
||||||
|
console.assert(testProperties, 'Pm.listProperties() failed');
|
||||||
|
|
||||||
|
if (testProperties) {
|
||||||
|
this.execCmd(`${this.workingDir}/log success 'testListProperties passed'`);
|
||||||
|
} else {
|
||||||
|
this.execCmd(`${this.workingDir}/log error 'testListProperties failed'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_testGetClassName() {
|
||||||
|
const pm = new Pm();
|
||||||
|
const pmClassName = pm.getClassName();
|
||||||
|
|
||||||
|
const testClassName = pmClassName === 'Pm';
|
||||||
|
console.assert(testClassName, 'Pm.getClassName() failed');
|
||||||
|
|
||||||
|
if (testClassName) {
|
||||||
|
this.execCmd(`${this.workingDir}/log success 'testGetClassName passed'`);
|
||||||
|
} else {
|
||||||
|
this.execCmd(`${this.workingDir}/log error 'testGetClassName failed'`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
selfTest() {
|
||||||
|
// Run tests
|
||||||
|
this._testListMethod();
|
||||||
|
this._testListProperties();
|
||||||
|
this._testGetClassName();
|
||||||
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
// Usage
|
// Usage
|
||||||
const pm = new Pm();
|
const pm = new Pm();
|
||||||
// Output: Methods of ExampleClass: [ 'methodOne', 'methodTwo' ]
|
// Output: Methods of Pm: [ 'methodOne', 'methodTwo' ]
|
||||||
// Properties of ExampleClass: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
|
// Properties of Pm: [ 'propertyOne', 'propertyTwo', 'version' ] and Version: 1.1.0
|
||||||
// pm.discovery();
|
//pm.discovery();
|
||||||
|
|
||||||
// this.program
|
// this.program
|
||||||
// .name(this.scriptName)
|
// .name(this.scriptName)
|
||||||
|
@ -70,3 +148,4 @@ const pm = new Pm();
|
||||||
// logDecoratedDiscovery();
|
// logDecoratedDiscovery();
|
||||||
|
|
||||||
pm.start();
|
pm.start();
|
||||||
|
pm.selfTest();
|
||||||
|
|
|
@ -29,8 +29,8 @@ class PmInstall extends This {
|
||||||
// Debugging commander options and arguments
|
// Debugging commander options and arguments
|
||||||
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true });
|
const opts = util.inspect(this.program.opts(), { depth: null, colors: true, showHidden: true });
|
||||||
const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
|
const args = util.inspect(this.program.args, { depth: null, colors: true, showHidden: true });
|
||||||
this.execCmd(`./log echo 'Options: ${opts}'`);
|
this.execCmd(`${this.workingDir}/log echo 'Options: ${opts}'`);
|
||||||
this.execCmd(`./log echo 'Remaining arguments: ${args}'`);
|
this.execCmd(`${this.workingDir}/log echo 'Remaining arguments: ${args}'`);
|
||||||
} else {
|
} else {
|
||||||
this.program.outputHelp();
|
this.program.outputHelp();
|
||||||
}
|
}
|
||||||
|
|
6
this
6
this
|
@ -9,6 +9,7 @@ class This {
|
||||||
this.version = '0.0.1'; // Default version, can be overridden in subclasses
|
this.version = '0.0.1'; // Default version, can be overridden in subclasses
|
||||||
this.description = 'This is the parent class all other scripts should extend.'
|
this.description = 'This is the parent class all other scripts should extend.'
|
||||||
this.scriptName = path.parse(process.argv[1]).base;
|
this.scriptName = path.parse(process.argv[1]).base;
|
||||||
|
this.workingDir = path.parse(process.argv[1]).dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
@ -76,6 +77,11 @@ class This {
|
||||||
return `\x1b[33m${str}\x1b[0m`;
|
return `\x1b[33m${str}\x1b[0m`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_echoInGreen(str) {
|
||||||
|
// Add ANSI escape codes to display text in green.
|
||||||
|
return `\x1b[32m${str}\x1b[0m`;
|
||||||
|
}
|
||||||
|
|
||||||
_getCurrentFunctionName() {
|
_getCurrentFunctionName() {
|
||||||
// Create an Error object (but don't throw it)
|
// Create an Error object (but don't throw it)
|
||||||
const err = new Error();
|
const err = new Error();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user