|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var fs = require('fs'); |
| 4 | +var path = require('path'); |
| 5 | +var assert = require('assert'); |
| 6 | +var sinon = require('sinon'); |
| 7 | +var rimraf = require('rimraf'); |
| 8 | +var mock = require('../mock'); |
| 9 | +var Environment = require('../../dist/environment'); |
| 10 | + |
| 11 | +describe('#environment', function () { |
| 12 | + var warnings = []; |
| 13 | + var logger; |
| 14 | + var env; |
| 15 | + |
| 16 | + beforeEach(function () { |
| 17 | + logger = new mock.Logger(true); |
| 18 | + env = new Environment(logger, false); |
| 19 | + warnings = logger.output; |
| 20 | + }); |
| 21 | + |
| 22 | + /** |
| 23 | + * Passed in config file. |
| 24 | + */ |
| 25 | + describe('#config', function () { |
| 26 | + var configPath = path.join(__dirname, '../fixture/config.json'); |
| 27 | + var configFile = require(configPath); |
| 28 | + |
| 29 | + beforeEach(function () { |
| 30 | + env.load(configPath); |
| 31 | + env.postProcess(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should properly process a passed in config file', function () { |
| 35 | + assert.ok(path.basename(env.file) === 'config.json'); |
| 36 | + assert.deepEqual(env.display, configFile.display); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + /** |
| 41 | + * Passed in wrong type config file. |
| 42 | + */ |
| 43 | + describe('#config-fail', function () { |
| 44 | + var spy = sinon.spy(); |
| 45 | + |
| 46 | + beforeEach(function () { |
| 47 | + env.on('error', spy); |
| 48 | + env.load(123); // @TOO [] pass |
| 49 | + env.postProcess(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should error if config is of a wrong format', function () { |
| 53 | + assert.ok(spy.called); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + /** |
| 58 | + * Passed in undefined config file. |
| 59 | + */ |
| 60 | + describe('#config-fail', function () { |
| 61 | + beforeEach(function () { |
| 62 | + env.load('fail.json'); |
| 63 | + env.postProcess(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should warn if config file is not found', function () { |
| 67 | + assert.ok(path.basename(env.file) === '.sassdocrc'); |
| 68 | + assert.ok(warnings[0].includes('Config file `fail.json` not found')); |
| 69 | + assert.ok(warnings[1].includes('Falling back to `.sassdocrc')); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + /** |
| 74 | + * Default .sassdocrc process. |
| 75 | + */ |
| 76 | + describe('#sassdocrc', function () { |
| 77 | + var sdrc; |
| 78 | + |
| 79 | + var config = { |
| 80 | + display: { |
| 81 | + access: ['public', 'private'], |
| 82 | + alias: true, |
| 83 | + watermark: true |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + beforeEach(function () { |
| 88 | + sdrc = new mock.SassDocRc(config); |
| 89 | + |
| 90 | + return sdrc.dump().then(function () { |
| 91 | + env.load(); |
| 92 | + env.postProcess(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should default to .sassdocrc', function () { |
| 97 | + assert.ok(warnings.length === 0); |
| 98 | + assert.ok(path.basename(env.file) === '.sassdocrc'); |
| 99 | + assert.deepEqual(env.display, config.display); |
| 100 | + }); |
| 101 | + |
| 102 | + after(function () { |
| 103 | + return sdrc.clean(); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + /** |
| 108 | + * A config.package is passed but fails. |
| 109 | + */ |
| 110 | + describe('#package-fail', function () { |
| 111 | + var spy = sinon.spy(); |
| 112 | + |
| 113 | + beforeEach(function () { |
| 114 | + env.on('warning', spy); |
| 115 | + env.load({ package: 'should/fail.json' }); |
| 116 | + env.postProcess(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should warn if package file is not found and load CWD package.json', function () { |
| 120 | + assert.ok(spy.called); |
| 121 | + assert.ok(env.package.name === 'sassdoc'); |
| 122 | + assert.ok(warnings[0].includes('should/fail.json` not found')); |
| 123 | + assert.ok(warnings[1].includes('Falling back to `package.json`')); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + /** |
| 128 | + * Render default theme. |
| 129 | + */ |
| 130 | + describe('#theme-default', function () { |
| 131 | + beforeEach(function () { |
| 132 | + env.load(); |
| 133 | + env.postProcess(); |
| 134 | + env.data = []; |
| 135 | + return env.theme('.sassdoc', env); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should render the default theme', function () { |
| 139 | + assert.ok(env.themeName === 'default'); |
| 140 | + assert.ok(fs.existsSync('.sassdoc/index.html')); |
| 141 | + assert.ok(fs.existsSync('.sassdoc/assets')); |
| 142 | + }); |
| 143 | + |
| 144 | + after(function (done) { |
| 145 | + rimraf('.sassdoc', done); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + /** |
| 150 | + * A config.theme is passed but fails. |
| 151 | + */ |
| 152 | + describe('#theme-fail', function () { |
| 153 | + beforeEach(function () { |
| 154 | + env.load({ theme: 'fail' }); |
| 155 | + env.postProcess(); |
| 156 | + env.data = []; |
| 157 | + return env.theme('.sassdoc', env); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should warn and render the default theme', function () { |
| 161 | + assert.ok(warnings[0].includes('Theme `fail` not found')); |
| 162 | + assert.ok(warnings[1].includes('Falling back to default theme')); |
| 163 | + // assert.ok(env.themeName === 'default'); // @TODO ?? |
| 164 | + assert.ok(fs.existsSync('.sassdoc/index.html')); |
| 165 | + assert.ok(fs.existsSync('.sassdoc/assets')); |
| 166 | + }); |
| 167 | + |
| 168 | + after(function (done) { |
| 169 | + rimraf('.sassdoc', done); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | +}); |
0 commit comments