Skip to content

Commit

Permalink
withPage function, dependencies update, travis-ci releasing to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
todvora committed Feb 2, 2016
1 parent ae2a702 commit 1bb1330
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
21 changes: 14 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "0.12"
- "4.1"
- "4.0"
- '0.10'
- '0.12'
- '4.1'
- '4.0'
before_script:
- gitbook install

after_script: "npm run coverage"
- gitbook install
after_script: npm run coverage
deploy:
provider: npm
email: todvora@gmail.com
api_key:
secure: njIibQFEKnpUqSoxI1QLVAi+MH4mvkWVJqhho4DdMM506I/rz/NqHH7e0DF4KKLugtLUUlnAU+SAI5Gns6qQcXYsyS+5miUMwOupQ1e6fXJ6RWS1qXdi78nyDPnIpI8XQKJb8CesiGpxbBeQ2hrh3QbMmhBh2PVS8I9tFrpTKvukgIHCLztUKfkowNGorpOsLxl8Dcqw2vLL0vNdmOYyTkj3NRTDFjTyutjS3+BT/N7BtxqHl39IjPOILhZjXPRoR2lOu348P3we0VooYwR1T34wvVtV2w7fkTBpBK56B/lRGnWkA+ypzomLMF5qZX/tLKrvHtzqAlphxBOyh0396VyihouYv1Yu2hZCtQnWi36xnlRAgXIsd/WTgR6R7NJW1Spt5YEJUTxwn0s1UTMk7vnHncNX5IG0KsZb9IJI4yPK7An4i6DLFxri/AAiuSyADhnRUBweVL2MOGcHWIQBoNbAtbabDNvhvYgo09UXCv8DcMRPsoV8B3wdCWSdKY65Gy0vz47PsmPvxE+P1396svhCLcjHK8sF+OHiOfcnHHjuDfoam1xkuLL4mV9iLkIo3cST5yE655cDuX4LrkBrj8bmFyx+YCetSkVJ3/E75csIkBxOFhxHWjHv4owcgE13FTyEU0h9ME7zVG6hvS3hxa4fyJKGgnFMq2vXaplznnE=
on:
tags: true
repo: todvora/gitbook-tester
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Gitbook integration tests framework

[![Build Status](https://travis-ci.org/todvora/gitbook-tester.svg?branch=master)](https://travis-ci.org/todvora/gitbook-tester)
[![Coverage Status](https://coveralls.io/repos/github/todvora/gitbook-tester/badge.svg?branch=master)](https://coveralls.io/github/todvora/gitbook-tester?branch=master)
[![npm version](https://badge.fury.io/js/gitbook-tester.svg)](https://badge.fury.io/js/gitbook-tester)
[![Dependencies Status](https://david-dm.org/todvora/gitbook-tester/status.svg)](https://david-dm.org/todvora/gitbook-tester/)
[![DevDependencies Status](https://david-dm.org/todvora/gitbook-tester/dev-status.svg)](https://david-dm.org/todvora/gitbook-tester/#info=devDependencies)

No more mocking of gitbook build! Verify your gitbook-plugin against real, up-to-date
version of gitbook. This integration framework creates temporary book, attaches your local gitbook plugin, runs gitbook build and returns parsed pages content.
Expand Down Expand Up @@ -38,8 +41,29 @@ tester.builder()
On the builder following methods can be called:

### .withContent(markdownString)
Put some **Markdown** content to the generated book. Currently only single
page book supported (contains only README.md).
Put some **Markdown** content to the generated books README.md (initial/intro page).

### .withPage(pageName, pageContent)
Add another book page. Usage like
```js
.withPage('second', 'Second page content')
```
There is no need of specifying extension, ```.md``` will be automatically added.
The rendered page can be accessed later in tests like
```js
it('should add second book page', function(testDone) {
tester.builder()
.withContent('First page content')
.withPage('second', 'Second page content')
.create()
.then(function(result) {
expect(result.get('second.html').content).toEqual('<p>Second page content</p>');
})
.fin(testDone)
.done();
});
```


### .withBookJson(jsObject)
Put your own ```book.json``` content as a JS object. May contain plugins,
Expand Down Expand Up @@ -86,10 +110,10 @@ plugins, attaches provided local modules. Returns ```promise```.
console.log(index);
})
```
should output JavaScript object like
should output JavaScript object like
```js
{ path: 'index.html',
'$': [cheerio representation of the page]
'$': [cheerio representation of the page]
content: '<h1 id="test-me">test me</h1>' }

```
Expand Down
29 changes: 24 additions & 5 deletions lib/tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ function endsWith(str, suffix) {
var createBook = function(content, children) {
return Q.nfcall(temp.mkdir, 'gitbook-tester')
.then(function(dirPath) {

var summaryPages = children.map(function(page){
return '* ['+page.name+']('+page.name+'.md)';
});

var pagesPromises = children.map(function(page) {
return Q.nfcall(fs.writeFile, path.join(dirPath, page.name + '.md'), page.content);
});

var summaryContent = '# Summary\n\n* [Introduction](README.md)' +'\n'+ summaryPages.join('\n');

var summary = Q.nfcall(fs.writeFile, path.join(dirPath, 'SUMMARY.md'), summaryContent);
var readme = Q.nfcall(fs.writeFile, path.join(dirPath, 'README.md'), content);
var summary = Q.nfcall(fs.writeFile, path.join(dirPath, 'SUMMARY.md'), '# Summary\n\n* [Introduction](README.md)');
return Q.all([readme, summary])

return Q.all([readme, summary].concat(pagesPromises))
.then(function() {
return dirPath;
});
Expand Down Expand Up @@ -196,14 +208,14 @@ var processFiles = function(bookPath, files) {
};

// main entry point - generate book, install plugins, attach local modules, read and transform html pages
var execute = function(htmlContent, bookJson, children, localModules, files) {
var execute = function(htmlContent, bookJson, localModules, files, pages) {
winston.level = process.env.DEBUG ? 'debug' : 'warn';

var that = this;

var modules = preprocessLocalModules(localModules);

return createBook(htmlContent, bookJson, children)
return createBook(htmlContent, pages)
.then(function(bookPath) {
return attachLocalPlugins(bookPath, modules)
.then(installBookJson.bind(that, bookPath, bookJson, modules, false))
Expand All @@ -219,6 +231,7 @@ var execute = function(htmlContent, bookJson, children, localModules, files) {
function Builder() {
this._modules = [];
this._files = [];
this._pages = [];
}

// attach Markdown content to book (currently only to README.md - single page book)
Expand All @@ -227,6 +240,12 @@ Builder.prototype.withContent = function(content) {
return this;
};

// attach Markdown content to book (currently only to README.md - single page book)
Builder.prototype.withPage = function(name, content) {
this._pages.push({name:name, content:content});
return this;
};

// attach book.json. Expects JS object
Builder.prototype.withBookJson = function(bookJson) {
this._bookJson = bookJson;
Expand All @@ -246,7 +265,7 @@ Builder.prototype.withFile = function(path, content) {

// start build, return promise with processed html content of pages
Builder.prototype.create = function() {
return execute(this._content, this._bookJson, [], this._modules, this._files);
return execute(this._content, this._bookJson, this._modules, this._files, this._pages);
};

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
"license": "Apache 2",
"dependencies": {
"cheerio": "0.19.0",
"child-process-promise": "~1.0.2",
"child-process-promise": "~1.1.0",
"findit": "2.0.0",
"gitbook-cli": "1.0.1",
"lodash": "^3.10.1",
"lodash": "^4.2.0",
"mkdirp": "^0.5.1",
"q": "~1.2.0",
"q": "~1.4.1",
"temp": "^0.8.3",
"winston": "^2.1.1"
},
Expand Down
13 changes: 13 additions & 0 deletions spec/tester_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ describe(__filename, function() {
.done();
});

it('should add external second book page', function(testDone) {
tester.builder()
.withContent('First page content')
.withPage('second', 'Second page content')
.create()
.then(function(result) {
expect(result.get('index.html').content).toEqual('<p>First page content</p>');
expect(result.get('second.html').content).toEqual('<p>Second page content</p>');
})
.fin(testDone)
.done();
});



});

0 comments on commit 1bb1330

Please sign in to comment.