-
Notifications
You must be signed in to change notification settings - Fork 122
ncu-ci: command that shows results of jenkins #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3e614ef
lib: add cache utility
joyeecheung 9c45921
ncu-ci: initial implementation
joyeecheung ee5b43b
ncu-ci: replace --markdown with --copy
joyeecheung 353c812
ncu-ci: support parsing CI links from PR thread
joyeecheung 766fc91
ncu-ci: add docs
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
lib: add cache utility
- Loading branch information
commit 3e614effe5e8ace69debcee9c926a4fe317efeaf
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ tmp | |
| coverage.lcov | ||
| tmp-* | ||
| .eslintcache | ||
| .ncu | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
| const { writeJson, readJson, writeFile, readFile } = require('./file'); | ||
|
|
||
| function isAsync(fn) { | ||
| return fn[Symbol.toStringTag] === 'AsyncFunction'; | ||
| } | ||
|
|
||
| class Cache { | ||
| constructor(dir) { | ||
| this.dir = dir || path.join(__dirname, '..', '.ncu', 'cache'); | ||
| this.originals = {}; | ||
| this.disabled = true; | ||
| } | ||
|
|
||
| disable() { | ||
| this.disabled = true; | ||
| } | ||
|
|
||
| enable() { | ||
| this.disabled = false; | ||
| } | ||
|
|
||
| getFilename(key, ext) { | ||
| return path.join(this.dir, key) + ext; | ||
| } | ||
|
|
||
| has(key, ext) { | ||
| if (this.disabled) { | ||
| return false; | ||
| } | ||
|
|
||
| return fs.existsSync(this.getFilename(key, ext)); | ||
| } | ||
|
|
||
| get(key, ext) { | ||
| if (!this.has(key, ext)) { | ||
| return undefined; | ||
| } | ||
| if (ext === '.json') { | ||
| return readJson(this.getFilename(key, ext)); | ||
| } else { | ||
| return readFile(this.getFilename(key, ext)); | ||
| } | ||
| } | ||
|
|
||
| write(key, ext, content) { | ||
| if (this.disabled) { | ||
| return; | ||
| } | ||
| const filename = this.getFilename(key, ext); | ||
| if (ext === '.json') { | ||
| return writeJson(filename, content); | ||
| } else { | ||
| return writeFile(filename, content); | ||
| } | ||
| } | ||
|
|
||
| wrapAsync(original, identity) { | ||
| const cache = this; | ||
| return async function(...args) { | ||
| const { key, ext } = identity.call(this, ...args); | ||
| const cached = cache.get(key, ext); | ||
| if (cached) { | ||
| return cached; | ||
| } | ||
| const result = await original.call(this, ...args); | ||
| cache.write(key, ext, result); | ||
| return result; | ||
| }; | ||
| } | ||
|
|
||
| wrapNormal(original, identity) { | ||
| const cache = this; | ||
| return function(...args) { | ||
| const { key, ext } = identity.call(this, ...args); | ||
| const cached = cache.get(key, ext); | ||
| if (cached) { | ||
| return cached; | ||
| } | ||
| const result = original.call(this, ...args); | ||
| cache.write(key, ext, result); | ||
| return result; | ||
| }; | ||
| } | ||
|
|
||
| wrap(Class, identities) { | ||
| for (let method of Object.keys(identities)) { | ||
| const original = Class.prototype[method]; | ||
| const identity = identities[method]; | ||
| this.originals[method] = original; | ||
| if (isAsync(original)) { | ||
| Class.prototype[method] = this.wrapAsync(original, identity); | ||
| } else { | ||
| Class.prototype[method] = this.wrapNormal(original, identity); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| module.exports = Cache; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| 'use strict'; | ||
|
|
||
| const path = require('path'); | ||
| const rimraf = require('rimraf'); | ||
| const mkdirp = require('mkdirp'); | ||
| const fs = require('fs'); | ||
|
|
||
| exports.tmpdir = { | ||
| get path() { | ||
| return path.join(__dirname, 'tmp'); | ||
| }, | ||
| refresh() { | ||
| rimraf.sync(this.path); | ||
| mkdirp.sync(this.path); | ||
| } | ||
| }; | ||
|
|
||
| exports.copyShallow = function(src, dest) { | ||
| mkdirp.sync(dest); | ||
| const list = fs.readdirSync(src); | ||
| for (const file of list) { | ||
| fs.copyFileSync(path.join(src, file), path.join(dest, file)); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| 'use strict'; | ||
|
|
||
| const Cache = require('../../lib/cache'); | ||
| const { tmpdir } = require('../common'); | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
| const assert = require('assert'); | ||
|
|
||
| describe('Cache', () => { | ||
| const syncResult = 'content in sync'; | ||
| const asyncResult = { | ||
| results: 'content in async.json' | ||
| }; | ||
|
|
||
| class CachedClass { | ||
| constructor(foo) { | ||
| this.foo = foo; | ||
| this.sync = 0; | ||
| this.async = 0; | ||
| } | ||
|
|
||
| cachedSyncMethod(...args) { | ||
| this.sync++; | ||
| return syncResult; | ||
| } | ||
|
|
||
| async cachedAsyncMethod(...args) { | ||
| this.async++; | ||
| const p = Promise.resolve(asyncResult); | ||
| const result = await p; // make sure it's async | ||
| return result; | ||
| } | ||
|
|
||
| getCacheKey(prefix, ...args) { | ||
| return `${prefix}-${args.join('-')}-${this.foo}`; | ||
| } | ||
| } | ||
|
|
||
| tmpdir.refresh(); | ||
| const cache = new Cache(tmpdir.path); | ||
| cache.wrap(CachedClass, { | ||
| cachedSyncMethod(...args) { | ||
| return { key: this.getCacheKey('sync', ...args), ext: '.txt' }; | ||
| }, | ||
| cachedAsyncMethod(...args) { | ||
| return { key: this.getCacheKey('async', ...args), ext: '.json' }; | ||
| } | ||
| }); | ||
|
|
||
| it('should cache sync results', () => { | ||
| tmpdir.refresh(); | ||
| cache.enable(); | ||
| const expected = syncResult; | ||
| const instance = new CachedClass('foo'); | ||
| let actual = instance.cachedSyncMethod('test'); | ||
| assert.strictEqual(instance.sync, 1); | ||
| assert.strictEqual(actual, expected); | ||
|
|
||
| let syncCache = path.join(tmpdir.path, 'sync-test-foo.txt'); | ||
| let cached = fs.readFileSync(syncCache, 'utf8'); | ||
| assert.strictEqual(cached, expected); | ||
|
|
||
| // Call it again | ||
| actual = instance.cachedSyncMethod('test'); | ||
| assert.strictEqual(instance.sync, 1); | ||
| assert.strictEqual(actual, expected); | ||
|
|
||
| syncCache = path.join(tmpdir.path, 'sync-test-foo.txt'); | ||
| cached = fs.readFileSync(syncCache, 'utf8'); | ||
| assert.strictEqual(cached, expected); | ||
| }); | ||
|
|
||
| it('should cache async results', async() => { | ||
| tmpdir.refresh(); | ||
| cache.enable(); | ||
| const expected = Object.assign({}, asyncResult); | ||
| const instance = new CachedClass('foo'); | ||
| let actual = await instance.cachedAsyncMethod('test'); | ||
| assert.strictEqual(instance.async, 1); | ||
| assert.deepStrictEqual(actual, expected); | ||
|
|
||
| let asyncCache = path.join(tmpdir.path, 'async-test-foo.json'); | ||
| let cached = JSON.parse(fs.readFileSync(asyncCache, 'utf8')); | ||
| assert.deepStrictEqual(cached, expected); | ||
|
|
||
| // Call it again | ||
| actual = await instance.cachedAsyncMethod('test'); | ||
| assert.strictEqual(instance.async, 1); | ||
| assert.deepStrictEqual(actual, expected); | ||
|
|
||
| asyncCache = path.join(tmpdir.path, 'async-test-foo.json'); | ||
| cached = JSON.parse(fs.readFileSync(asyncCache, 'utf8')); | ||
| assert.deepStrictEqual(cached, expected); | ||
| }); | ||
|
|
||
| it('should not cache if disabled', async() => { | ||
| tmpdir.refresh(); | ||
| cache.disable(); | ||
| const expected = Object.assign({}, asyncResult); | ||
| const instance = new CachedClass('foo'); | ||
| let actual = await instance.cachedAsyncMethod('test'); | ||
| assert.strictEqual(instance.async, 1); | ||
| assert.deepStrictEqual(actual, expected); | ||
|
|
||
| let list = fs.readdirSync(tmpdir.path); | ||
| assert.deepStrictEqual(list, []); | ||
|
|
||
| // Call it again | ||
| actual = await instance.cachedAsyncMethod('test'); | ||
| assert.strictEqual(instance.async, 2); | ||
| assert.deepStrictEqual(actual, expected); | ||
|
|
||
| list = fs.readdirSync(tmpdir.path); | ||
| assert.deepStrictEqual(list, []); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd would have prefer this to be a
Mapinstead of plain object, for readability.