# HG changeset patch # User Quentin Raynaud <quentin@qraynaud.eu> # Date 1588669820 -7200 # Tue May 05 11:10:20 2020 +0200 # Node ID 2a242e31590274bcd0aa29a33dfa36376d41f16f # Parent 0e0dc277816f829e1e2bd3f8bc6b1d4e9cd1b1da test: add tests for the save option diff --git a/src/environment.mjs b/src/environment.mjs --- a/src/environment.mjs +++ b/src/environment.mjs @@ -85,11 +85,9 @@ return writeFileSync case 'async': return writeFile - case 'console': + case 'logger': return (path, content) => console.debug(`[DEBUG:config.io] should have saved configuration ${this.config.ns ? this.config.ns + ' ' : ''}to ${path} with\n--\n${content}--\n`) - default: - throw new Error(`Unknown save method: ${this.config.options.save}`) } })() diff --git a/src/schemas.mjs b/src/schemas.mjs --- a/src/schemas.mjs +++ b/src/schemas.mjs @@ -13,7 +13,7 @@ const autoSave = Joi.boolean() .default(true) const save = Joi.string() - .valid('sync', 'async', 'console') + .valid('sync', 'async', 'logger') .default('sync') const throwIfNotReady = Joi.boolean() .default(true) diff --git a/test/load/options/save/1-default-value.spec.yaml b/test/load/options/save/1-default-value.spec.yaml new file mode 100644 --- /dev/null +++ b/test/load/options/save/1-default-value.spec.yaml @@ -0,0 +1,11 @@ +it: should write file synchronously by default +start: + autoload: + - base + readOnly: false +run: load/options/save/write-value-sync.js +mode: save +save: + local: base.local.yaml +res: | + value: test diff --git a/test/load/options/save/2-sync.spec.yaml b/test/load/options/save/2-sync.spec.yaml new file mode 100644 --- /dev/null +++ b/test/load/options/save/2-sync.spec.yaml @@ -0,0 +1,12 @@ +it: should write file synchronously if save is sync +start: + autoload: + - base + readOnly: false + save: sync +run: load/options/save/write-value-sync.js +mode: save +save: + local: base.local.yaml +res: | + value: test diff --git a/test/load/options/save/3-async.spec.yaml b/test/load/options/save/3-async.spec.yaml new file mode 100644 --- /dev/null +++ b/test/load/options/save/3-async.spec.yaml @@ -0,0 +1,12 @@ +it: should support async value for save +start: + autoload: + - base + readOnly: false + save: async +run: load/options/save/write-value-async.js +mode: save +save: + local: base.local.yaml +res: | + value: test diff --git a/test/load/options/save/4-logger.spec.yaml b/test/load/options/save/4-logger.spec.yaml new file mode 100644 --- /dev/null +++ b/test/load/options/save/4-logger.spec.yaml @@ -0,0 +1,18 @@ +it: should send the file in the logger instead of saving it if save is "logger" +start: + autoload: + - base + readOnly: false + save: logger +run: load/options/save/write-value-logger.js +save: + local: base.local.yaml +property: spy.logged +res: |+ + [DEBUG:config.io] Trying to save <%=baseDir%>/<%=configDir%>/base.local.yaml… + [DEBUG:config.io] should have saved configuration base to <%=baseDir%>/<%=configDir%>/base.local.yaml with + -- + value: test + -- + +# keep the empty line above, there: are 2 \n at the end of the expected output diff --git a/test/load/options/save/base.yaml b/test/load/options/save/base.yaml new file mode 100644 --- /dev/null +++ b/test/load/options/save/base.yaml @@ -0,0 +1,1 @@ +value: test diff --git a/test/load/options/save/spy.yaml b/test/load/options/save/spy.yaml new file mode 100644 --- /dev/null +++ b/test/load/options/save/spy.yaml @@ -0,0 +1,1 @@ +logged: '' diff --git a/test/load/options/auto-save/write-value-async.js b/test/load/options/save/write-value-async.js rename from test/load/options/auto-save/write-value-async.js rename to test/load/options/save/write-value-async.js --- a/test/load/options/auto-save/write-value-async.js +++ b/test/load/options/save/write-value-async.js @@ -1,9 +1,17 @@ -const assert = require('assert') const fs = require('fs') const path = require('path') -module.exports.run = async (configIO, loadModule) => { - configIO.value = 'test' +module.exports.run = configIO => { + const localFile = path.resolve(__dirname, 'base.local.yaml') + + if (fs.existsSync(localFile)) { + throw new Error('local file exists before writing!') + } - assert.ok(!fs.existsSync(path.resolve(__dirname, 'local.yaml'))) + configIO.base.value = 'test' + + if (fs.existsSync(localFile)) { + fs.unlinkSync(localFile) + throw new Error('local file was written synchronously') + } } diff --git a/test/load/options/save/write-value-logger.js b/test/load/options/save/write-value-logger.js new file mode 100644 --- /dev/null +++ b/test/load/options/save/write-value-logger.js @@ -0,0 +1,24 @@ +const fs = require('fs') +const { inspect } = require('util') +const path = require('path') +const sinon = require('sinon') + +module.exports.run = configIO => { + const spy = configIO('spy', { autoSave: false, readOnly: false }) + const localFile = path.resolve(__dirname, 'base.local.yaml') + + if (fs.existsSync(localFile)) { + throw new Error('local file exists before writing!') + } + + const debugStub = sinon.stub(console, 'debug').callsFake((...args) => { + spy.logged += args.map(arg => typeof arg === 'string' ? arg : inspect(arg)).join(' ') + '\n' + }) + configIO.base.value = 'test' + debugStub.restore() + + if (fs.existsSync(localFile)) { + fs.unlinkSync(localFile) + throw new Error('local file was written synchronously') + } +} diff --git a/test/load/options/save/write-value-sync.js b/test/load/options/save/write-value-sync.js new file mode 100644 --- /dev/null +++ b/test/load/options/save/write-value-sync.js @@ -0,0 +1,16 @@ +const fs = require('fs') +const path = require('path') + +module.exports.run = configIO => { + const localFile = path.resolve(__dirname, 'base.local.yaml') + + if (fs.existsSync(localFile)) { + throw new Error('local file exists before writing!') + } + + configIO.base.value = 'test' + + if (!fs.existsSync(localFile)) { + throw new Error('local file was not written synchronously') + } +} diff --git a/test/load/options/write-value.js b/test/load/options/write-value.js --- a/test/load/options/write-value.js +++ b/test/load/options/write-value.js @@ -1,3 +1,3 @@ -module.exports.run = async (configIO, loadModule) => { +module.exports.run = configIO => { configIO.value = 'test' } diff --git a/test/worker/run-test.js b/test/worker/run-test.js --- a/test/worker/run-test.js +++ b/test/worker/run-test.js @@ -23,6 +23,13 @@ let data = configIO + const saveOpts = workerData.save || {} + const localFile = resolve(__dirname, '..', process.env.CONFIGIO_DIR || '.', saveOpts.local || 'local.yaml') + + if (workerData.mode === 'save' && !saveOpts.keepLocal) { + await unlink(localFile).then(null, () => {}) + } + if (workerData.run) { await require(resolve(__dirname, '..', workerData.run)).run(configIO, loadModule) } @@ -35,9 +42,6 @@ } const save = async () => { - const saveOpts = workerData.save || {} - const localFile = resolve(__dirname, '..', process.env.CONFIGIO_DIR, saveOpts.local || 'local.yaml') - if ((workerData.start || {}).autoSave === 'async') { await new Promise(resolve => setTimeout(resolve, 100)) }