Skip to content

Commit

Permalink
Merge pull request #403 from SockDrawer/fix-github-checkout-reference
Browse files Browse the repository at this point in the history
Fix GitHub checkout reference
  • Loading branch information
AccaliaDeElementia authored Dec 21, 2016
2 parents 5a0269a + 04d86f7 commit 5cf5553
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
version.js export-subst
*.js eol=lf

package.json ident
124 changes: 124 additions & 0 deletions docs/api/lib/app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
## Functions

<dl>
<dt><a href="#getVersion">getVersion()</a> ⇒ <code>string</code></dt>
<dd><p>Get current version information, using latest commit sha1 as a fallback if detected version is semantic release
placeholder.</p>
</dd>
<dt><a href="#getUserAgent">getUserAgent(cfg, provider)</a> ⇒ <code>string</code></dt>
<dd><p>Construct a useragent for sockbot to use</p>
</dd>
<dt><a href="#_buildMessage">_buildMessage(args)</a> ⇒ <code>string</code></dt>
<dd><p>Construct a stringified message to log</p>
</dd>
<dt><a href="#log">log(...message)</a></dt>
<dd><p>Log a message to stdout</p>
</dd>
<dt><a href="#error">error(...message)</a></dt>
<dd><p>Log a message to stderr</p>
</dd>
<dt><a href="#relativeRequire">relativeRequire(relativePath, module, requireIt)</a> ⇒ <code>object</code> | <code>function</code></dt>
<dd><p>Load a module relative to a local path, or relative to loaded config file</p>
</dd>
<dt><a href="#loadPlugins">loadPlugins(forumInstance, botConfig)</a> ⇒ <code>Promise</code></dt>
<dd><p>Load plugins for forum instance</p>
</dd>
<dt><a href="#activateConfig">activateConfig(botConfig)</a> ⇒ <code>Promise</code></dt>
<dd><p>Activate a loaded configuration.</p>
</dd>
</dl>

<a name="getVersion"></a>

## getVersion() ⇒ <code>string</code>
Get current version information, using latest commit sha1 as a fallback if detected version is semantic release
placeholder.

**Kind**: global function
**Returns**: <code>string</code> - Version information
<a name="getUserAgent"></a>

## getUserAgent(cfg, provider) ⇒ <code>string</code>
Construct a useragent for sockbot to use

**Kind**: global function
**Returns**: <code>string</code> - User-Agent to use for a forum instance

| Param | Type | Description |
| --- | --- | --- |
| cfg | <code>object</code> | Instance Configuration to construct User Agent for |
| provider | <code>Forum</code> | Forum Provider class to construct User Agent for |

<a name="_buildMessage"></a>

## _buildMessage(args) ⇒ <code>string</code>
Construct a stringified message to log

**Kind**: global function
**Returns**: <code>string</code> - stringified message

| Param | Type | Description |
| --- | --- | --- |
| args | <code>Array.&lt;\*&gt;</code> | Item to stringify and log |

<a name="log"></a>

## log(...message)
Log a message to stdout

**Kind**: global function

| Param | Type | Description |
| --- | --- | --- |
| ...message | <code>\*</code> | Message to log to stdout |

<a name="error"></a>

## error(...message)
Log a message to stderr

**Kind**: global function

| Param | Type | Description |
| --- | --- | --- |
| ...message | <code>\*</code> | Message to log to stderr |

<a name="relativeRequire"></a>

## relativeRequire(relativePath, module, requireIt) ⇒ <code>object</code> &#124; <code>function</code>
Load a module relative to a local path, or relative to loaded config file

**Kind**: global function
**Returns**: <code>object</code> &#124; <code>function</code> - Loaded module

| Param | Type | Description |
| --- | --- | --- |
| relativePath | <code>string</code> | Local path to use |
| module | <code>string</code> | Module to load |
| requireIt | <code>function</code> | Function to use to load module |

<a name="loadPlugins"></a>

## loadPlugins(forumInstance, botConfig) ⇒ <code>Promise</code>
Load plugins for forum instance

**Kind**: global function
**Returns**: <code>Promise</code> - Resolves when plugins have been loaded

| Param | Type | Description |
| --- | --- | --- |
| forumInstance | <code>Provider</code> | Provider instance to load plugins into |
| botConfig | <code>object</code> | Bot configuration to load plugins with |

<a name="activateConfig"></a>

## activateConfig(botConfig) ⇒ <code>Promise</code>
Activate a loaded configuration.

**Kind**: global function
**Returns**: <code>Promise</code> - Resolves when configuration is fully activated

| Param | Type | Description |
| --- | --- | --- |
| botConfig | <code>object</code> | Configuration to activate |

27 changes: 25 additions & 2 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ const packageInfo = require('../package.json'),

const debug = require('debug')('sockbot');

/**
* Get current version information, using latest commit sha1 as a fallback if detected version is semantic release
* placeholder.
*
* @returns {string} Version information
*/
exports.getVersion = function getVersion() {
if (packageInfo.version !== '0.0.0-semantic-release') {
return packageInfo.version;
}
const parser = /\$Id: (\S+) \$/;
if (packageInfo.latestCommit) {
const parsed = parser.exec(packageInfo.latestCommit);
if (parsed && parsed[1]) {
return parsed[1];
}
if (packageInfo.latestCommit !== '$Id$') {
return packageInfo.latestCommit;
}
}
return '[Unknown Version]';
};

/**
* Construct a useragent for sockbot to use
*
Expand All @@ -18,7 +41,7 @@ const debug = require('debug')('sockbot');
* @returns {string} User-Agent to use for a forum instance
*/
exports.getUserAgent = function getUserAgent(cfg, provider) {
const ua = `${packageInfo.name}/${packageInfo.version} ` +
const ua = `${packageInfo.name}/${exports.getVersion()} ` +
`(${process.platform} ${process.arch}) ` +
`(nodejs v${process.versions.node}) ` +
`(v8 v${process.versions.v8}) ` +
Expand Down Expand Up @@ -168,7 +191,7 @@ if (require.main === module) {
.usage('Usage: $0 <cfgFile>')
.demand(1, 1, 'A valid configuration file must be provided')
.argv;
exports.log(`Starting Sockbot ${packageInfo.version} - ${packageInfo.releaseName}`);
exports.log(`Starting Sockbot ${exports.getVersion()}`);
config.load(argv._[0])
.then((cfg) => {
exports.log(`Loaded configuration file: ${argv._[0]}`);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "sockbot",
"latestCommit": "$Id$",
"version": "0.0.0-semantic-release",
"description": "A sockpuppet bot to use on http://what.thedailywtf.com.",
"repository": {
Expand Down
38 changes: 36 additions & 2 deletions test/lib/appTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,13 @@ describe('lib/app', () => {
let username = null,
owner = null,
ua = null,
provider = null;
provider = null,
sandbox = null,
version = null;
beforeEach(() => {
version = `ver$${Math.random()}$`;
sandbox = sinon.sandbox.create();
sandbox.stub(testModule, 'getVersion').returns(version);
username = `username${Math.random()}`;
owner = `owner${Math.random()}`;
provider = {};
Expand All @@ -433,6 +438,7 @@ describe('lib/app', () => {
}
}, provider);
});
afterEach(() => sandbox.restore());
it('should return a string', () => {
testModule.getUserAgent({
core: {}
Expand All @@ -454,7 +460,7 @@ describe('lib/app', () => {
}).should.endWith(expected);
});
it('should startWith package name/version', () => {
ua.indexOf(`${packageInfo.name}/${packageInfo.version}`).should.equal(0);
ua.indexOf(`${packageInfo.name}/${version}`).should.equal(0);
});
it('should contain OS platform/architecture', () => {
ua.indexOf(`${process.platform} ${process.arch}`).should.be.greaterThan(0);
Expand Down Expand Up @@ -502,4 +508,32 @@ describe('lib/app', () => {
lines[lines.length - 1].should.endWith(message);
});
});
describe('getVersion()', () => {
const sha = '33d24d7c8f7f8cf320abb39425375d74d280f3e3';
beforeEach(() => {
packageInfo.version = '0.0.0-semantic-release'; // Restore placeholder version
packageInfo.latestCommit = `$Id: ${sha} $`;
});
it('should provide version when version is not semantic-release placeholder', () => {
const version = `${Math.random() * 20}.${Math.random() * 20}`;
packageInfo.version = version;
testModule.getVersion().should.equal(version);
});
it('should provide latestCommit sha when version is placeholder and format is correct', () => {
testModule.getVersion().should.equal(sha);
});
it('should provide latestCommit when version is place holder and format is weird', () => {
const version = `I LIKE ${Math.ceil(Math.random() * 1000)}FISH`;
packageInfo.latestCommit = version;
testModule.getVersion().should.equal(version);
});
it('should provide unknown version when version is placeholder and latestCommit is placeholder', () => {
packageInfo.latestCommit = '$Id$';
testModule.getVersion().should.equal('[Unknown Version]');
});
it('should provide unknown version when version is placeholder and latestCommit is missing', () => {
packageInfo.latestCommit = undefined;
testModule.getVersion().should.equal('[Unknown Version]');
});
});
});

0 comments on commit 5cf5553

Please sign in to comment.