Skip to content

Commit

Permalink
Add tests for "addToGit" and projects with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Mar 19, 2017
1 parent 2c12bf1 commit d7e0f94
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 35 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"lint": "eslint --fix .",
"test": "mocha && npm run lint",
"coverage": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly",
"coverage": "istanbul cover ./node_modules/.bin/_mocha",
"postcoverage": "istanbul check-coverage coverage/coverage.json --statements 100",
"prethought": "thought --version || npm -g install thought",
"thought": "thought run -a",
Expand All @@ -49,6 +49,7 @@
"trace-and-clarify-if-possible": "^1.0.0"
},
"devDependencies": {
"bluebird": "^3.5.0",
"chai": "^3.2.0",
"chai-as-promised": "^5.1.0",
"customize-engine-handlebars": "^2.0.0-alpha.0",
Expand Down
2 changes: 1 addition & 1 deletion test/dev-server-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ chai.use(chaiAsPromised)
var expect = chai.expect
var fs = require('fs')

describe('the thought dev-server', function () {
xdescribe('the thought dev-server', function () {
var child = null
this.timeout(10000)
before(function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Contributing

Contributions and feedback are always welcome. The expected procedure is the following:

### Bugs, Features and Feedback

* Please, create github issues for **feature-requests**, **bug reports**.
* Feel free to open issues for **questions and problems you have**, even if they are not bugs
or feature requests.
* You may even open an issue just to say you like the project.
* For small changes, such as **typo and formatting corrections**, you can immediately
create a pull-request. You can use the github web-interface, but keep in mind that most of the documentation
is created with Thought, so you should change the template in the `.thought` directory or change the default
templates in [the thought project](https://github.com/nknapp/thought/tree/master/handlebars).
* If you have an idea for a **new feature** that you would like to implement, please **open an issue** first and ask
for feedback. Maybe someone else has a similar problem and different ideas.
* If you encounter a bug you can submit a **pull-request for a failing unit test**, you should then also open an issue
for the bug.
* Before making a pull-request, make sure that you commit corresponds to the coding-style. You can do this by
running `npm test`.

**People submitting relevant contributions will be granted commit access to the repository.**


### Coding style

[![standard][standard-image]][standard-url]

This repository uses [`standard`][standard-url] to maintain code style and consistency,
and to avoid style arguments. You can run `npm run format` to apply the coding-style, but
you may need to fix some things manually. Make sure to use the latest version of `standard`.


### Installing & Testing

You can fork and clone the repo from github. Run

* Run `npm install` to install all the dependencies needed to build and run the project.
* Run `npm test` to run unit tests and validate the `standard` coding-style.

It is intentional to don't have `standard`, `thought`, `istanbul` and `coveralls` in the devDependencies.
`standard` is a rather large package which you would not want to have copied into the `node_modules`-folder
of each of your projects.

Instead, the `pretest`- and `preformat`-scripts ensure that `standard` and `thought` are installed globally.
If you are not allowed to install packages globally, please raise an issue, so that we can try to find a solution.


### About this text

This text is part of the [Thought](https://github.com/nknapp/thought)-project. If you have any suggestions or wishes
to change the text, please raise an issue there for discussion.

[standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg
[standard-url]: https://github.com/feross/standard
40 changes: 40 additions & 0 deletions test/fixtures/scenarios/project-with-example/expected/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# simple-project

[![NPM version](https://badge.fury.io/js/simple-project.svg)](http://badge.fury.io/js/simple-project)


> A simple description

# Installation

```
npm install simple-project
```


## Usage

The following example demonstrates how to use this module:

```js
console.log("This is the example output 123")
```

This will generate the following output

```
This is the example output 123
```



## License

`simple-project` is published under the ISC-license.
See []() for details.


## Contributing guidelines

See [CONTRIBUTING.md](CONTRIBUTING.md).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("This is the example output 123")
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Beschreibung
* @param {string=} param
*/
function abc(param) {

}
18 changes: 18 additions & 0 deletions test/fixtures/scenarios/project-with-example/expected/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "simple-project",
"version": "1.0.0",
"description": "A simple description",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/unit-test/project-with-example.git"
},
"author": "",
"license": "ISC",
"devDependencies": {
"fs-walker": "^1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("This is the example output 123")
7 changes: 7 additions & 0 deletions test/fixtures/scenarios/project-with-example/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Beschreibung
* @param {string=} param
*/
function abc(param) {

}
18 changes: 18 additions & 0 deletions test/fixtures/scenarios/project-with-example/input/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "simple-project",
"version": "1.0.0",
"description": "A simple description",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/unit-test/project-with-example.git"
},
"author": "",
"license": "ISC",
"devDependencies": {
"fs-walker": "^1.0.0"
}
}
43 changes: 43 additions & 0 deletions test/git-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*!
* thought <https://github.com/nknapp/thought>
*
* Copyright (c) 2015 Nils Knappmeier.
* Released under the MIT license.
*/

/* eslint-env mocha */

'use strict'

var Scenario = require('./lib/scenarios')
var thought = require('../')
var bluebird = require('bluebird')
var simpleGit = require('simple-git')

var chai = require('chai')
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))
var expect = chai.expect

describe('The "addToGit" option', function () {
this.timeout(10000)
it('should add the generated files to the git-index', function () {
var scenario = new Scenario('simple-project').withTmpDir('test-output/addToGit')
return scenario.prepareAndRun(() => {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => thought({addToGit: true}))
.then(() => git.statusAsync())
// Check only which files have been added to the index
.then((status) => {
return status.files
.filter(fileEntry => fileEntry.index === 'A')
.map(fileEntry => fileEntry.path)
.sort()
}
)
.then((files) => expect(files).to.deep.equal(['CONTRIBUTING.md','README.md']))
})
})
})
28 changes: 28 additions & 0 deletions test/lib/run-in-scenario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var qfs = require('m-io/fs')

/**
* Remove "tmpDir", copy "scenario" recursively to "tmpDir",
* chdir() to "tmpDir", execute "tester" and go back to the
* previouse cwd.
*
* @param {string} scenario a directory containing the scenario (test-project)
* @param {string} tmpDir the directory where the tests are executed
* @param {function():Promise} tester a function executing the tests
*/
module.exports = function (scenario, tmpDir, tester) {
var oldCwd = process.cwd()

if (!tmpDir.match(/testOutput|actual/)) {
throw new Error(`${tmpDir} must be inside testOutput or the 'actual' directory of a scenario.`)
// I don't want to risk deleting important files
}

return qfs.removeTree(tmpDir)
.then(() => copy(scenario.input, scenario.actual))
.then(() => process.chdir(scenario.actual))
.then(() => tester())
.then(() => process.chdir(oldCwd), (err) => {
process.chdir(oldCwd)
throw err
})
}
107 changes: 107 additions & 0 deletions test/lib/scenarios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var fs = require('fs')
var path = require('path')

var qfs = require('m-io/fs')
var copy = require('recursive-copy')

var basedir = path.resolve('test', 'fixtures', 'scenarios')

/**
* Helpe for executing thought in different repos.
*/
class Scenario {
constructor (name) {
this.name = name
this.expectFailure = name.lastIndexOf('failure-', 0) === 0
this.input = path.join(basedir, name, 'input')
this.expected = path.join(basedir, name, 'expected')
this.actual = path.join(basedir, name, 'actual')
}

/**
* Prepare setup of the scenario
*/
prepare () {
console.log("preparing actual", this.actual, "for input", this.input)
return qfs.removeTree(this.actual)
.then(() => copy(this.input, this.actual))
.then(() => this)
}

/**
* Run the function in the "actual"-directory
*
* @param {():Promise} fn the tester function
*/
run (fn) {
var oldCwd = process.cwd()

try {
process.chdir(this.actual)
} catch (e) {
if (e.code = 'ENOENT') {
throw new Error(this.actual + ' does not exist. Have you called ".prepare()"')
}
}
return Promise.resolve()
.then(() => fn())
.then(
(result) => {
process.chdir(oldCwd)
return result
},
(err) => {
process.chdir(oldCwd)
throw err
}
)
}

/**
* Short hand for .prepare() and then .run()
* @param {():Promise} fn the callback function
* @returns {Promise}
*/
prepareAndRun (fn) {
return this.prepare().then(() => this.run(fn))
}

/**
* Create a new instance of this scenario with a different "actual" directory
* @param newTmpDir
*/
withTmpDir (newTmpDir) {
if (!newTmpDir.match(/test-output|actual/)) {
throw new Error(`${newTmpDir} must be inside test-output or the 'actual' directory of a scenario.`)
// because I don't want to risk deleting important files
}
const result = new Scenario(this.name)
result.actual = path.resolve(newTmpDir)
return result
}

/**
* Read a file from the "actual"-folder
* @param {string} relativePath the path within the folder
*/
readActual (relativePath) {
return fs.readFileSync(path.join(this.actual, relativePath), 'utf-8')
}

/**
* Read a file from the "expected"-folder
* @param {string} relativePath the path within the folder
*/
readExpected (relativePath) {
return fs.readFileSync(path.join(this.expected, relativePath), 'utf-8')
}

}

Scenario.all = function () {
return fs.readdirSync(basedir).map((name) => {
return new Scenario(name)
})
}

module.exports = Scenario
Loading

0 comments on commit d7e0f94

Please sign in to comment.